// Program Description:
//
// This program presents an example of use of the Timer0 of the C8051F0xx's in
// 16-bit counter/timer mode. It uses the 'F005DK as HW platform.
// In this example the LED is toggled at a rate defined by the
// LED_TOGGLE_RATE constant.(in milliseconds)
//
// The timer is set with this values and counts until an overflow when it
// generates an interrupt. This interrupt reloads the timer values and toggles
// the LED.
//
// Pinout:
//
// P1.6 -> LED (pin 3 on the 'F005 TB)
//
#include <C8051F000.h> // SFR declarations
#define SYSCLK 11059200 // SYSCLK in Hz (16 MHz internal
// oscillator / 8)
// the internal oscillator has a
// tolerance of +/- 20%
#define TIMER_PRESCALER 12 // Based on Timer CKCON settings
#define LED_TOGGLE_RATE 40 // LED toggle rate in milliseconds 500
// if LED_TOGGLE_RATE = 1, the LED will
// be on for 1 millisecond and off for
// 1 millisecond
// There are SYSCLK/TIMER_PRESCALER timer ticks per second, so
// SYSCLK/TIMER_PRESCALER/1000 timer ticks per millisecond.
#define TIMER_TICKS_PER_MS SYSCLK/TIMER_PRESCALER/1000
// Note: LED_TOGGLE_RATE*TIMER_TICKS_PER_MS should not exceed 65535 (0xFFFF)
// for the 16-bit timer
#define AUX1 TIMER_TICKS_PER_MS*LED_TOGGLE_RATE
#define AUX2 -AUX1
#define AUX3 AUX2&0x00FF
#define AUX4 ((AUX2&0xFF00)>>8)
#define TIMER0_RELOAD_HIGH AUX4 // Reload value for Timer0 high byte
#define TIMER0_RELOAD_LOW AUX3 // Reload value for Timer0 low byte
sbit LED = P0^3; // LED='1' means ON
void Port_Init (void); // Port initialization routine
void Timer0_Init (void); // Timer0 initialization routine
#ifdef CLK11M
void delay(unsigned int time)
{
CKCON |= 0x10; //定时器1使用系统时钟(不分频)
TH0 = 0xd4; //[TH1:TL1] = 65536 - SYSCLK/定时器1溢出率
// = 65536 - SYSCLK/1000
// = 65536 - 16000000/1000
// = 65536 - 16000
// = 49536
TL0 = 0xcc;
ET0 = 0; //禁止定时器1中断
TR0 = 1; //启动定时器1
while(time--)
{
TF0 = 0;
while(!TF0); //延时1ms
TH0 = 0xd4;
TL0 = 0xcc;
}
TR0 = 0; //停止定时器1
}
#else
void delay(unsigned int time)
{
CKCON |= 0x10; //定时器1使用系统时钟(不分频)
TH0 = 0xc1; //[TH1:TL1] = 65536 - SYSCLK/定时器1溢出率
// = 65536 - SYSCLK/1000
// = 65536 - 16000000/1000
// = 65536 - 16000
// = 49536
TL0 = 0x80;
ET0 = 0; //禁止定时器1中断
TR0 = 1; //启动定时器1
while(time--)
{
TF0 = 0;
while(!TF0); //延时1ms
TH0 = 0xc1;
TL0 = 0x80;
}
TR0 = 0; //停止定时器1
}
#endif
void main (void)
{
int i = 0;
WDTCN = 0xDE; // Disable watchdog timer
WDTCN = 0xAD;
#ifdef CLK11M
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
OSCICN = 0x08;//External Clock:11059200Hz
#else
OSCICN = 0x07;//16M 10-14ms,40-56
#endif
Port_Init (); // Init Ports
EA = 1; // Enable global interrupts
while (1){
delay(40); //10-15,40-60
LED=0;
delay(40);
LED=1;
}
}
void Port_Init (void)
{
// Enable crossbar
XBR2 = 0x40;
PRT1CF = 0x40; // Set P1.6(LED) to push-pull
PRT0CF = 0xFF;
}
//-----------------------------------------------------------------------------
// Timer0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the Timer0 as a 16-bit timer, interrupt enabled.
// Using the SYSCLK at 16MHz/8 and reloading the T0 register with
// TIMER0_RELOAD_xxxx.
//
// Note: The Timer0 uses a 1:12 prescaler. If this setting changes, the
// TIMER_PRESCALER constant must also be changed.
//-----------------------------------------------------------------------------
void Timer0_Init(void)
{
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timer0 High register
ET0 = 1; // Timer0 interrupt enabled
TMOD = 0x01; // 16-bit Mode Timer0
TCON = 0x10; // Timer0 ON
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer0_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer0 interrupt and toggle the LED
//
//-----------------------------------------------------------------------------
/*
void Timer0_ISR (void) interrupt 1
{
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timer0 register
TL0 = TIMER0_RELOAD_LOW;
LED = ~LED; // Toggle the LED
} //*/
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
//
// This program presents an example of use of the Timer0 of the C8051F0xx's in
// 16-bit counter/timer mode. It uses the 'F005DK as HW platform.
// In this example the LED is toggled at a rate defined by the
// LED_TOGGLE_RATE constant.(in milliseconds)
//
// The timer is set with this values and counts until an overflow when it
// generates an interrupt. This interrupt reloads the timer values and toggles
// the LED.
//
// Pinout:
//
// P1.6 -> LED (pin 3 on the 'F005 TB)
//
#include <C8051F000.h> // SFR declarations
#define SYSCLK 11059200 // SYSCLK in Hz (16 MHz internal
// oscillator / 8)
// the internal oscillator has a
// tolerance of +/- 20%
#define TIMER_PRESCALER 12 // Based on Timer CKCON settings
#define LED_TOGGLE_RATE 40 // LED toggle rate in milliseconds 500
// if LED_TOGGLE_RATE = 1, the LED will
// be on for 1 millisecond and off for
// 1 millisecond
// There are SYSCLK/TIMER_PRESCALER timer ticks per second, so
// SYSCLK/TIMER_PRESCALER/1000 timer ticks per millisecond.
#define TIMER_TICKS_PER_MS SYSCLK/TIMER_PRESCALER/1000
// Note: LED_TOGGLE_RATE*TIMER_TICKS_PER_MS should not exceed 65535 (0xFFFF)
// for the 16-bit timer
#define AUX1 TIMER_TICKS_PER_MS*LED_TOGGLE_RATE
#define AUX2 -AUX1
#define AUX3 AUX2&0x00FF
#define AUX4 ((AUX2&0xFF00)>>8)
#define TIMER0_RELOAD_HIGH AUX4 // Reload value for Timer0 high byte
#define TIMER0_RELOAD_LOW AUX3 // Reload value for Timer0 low byte
sbit LED = P0^3; // LED='1' means ON
void Port_Init (void); // Port initialization routine
void Timer0_Init (void); // Timer0 initialization routine
#ifdef CLK11M
void delay(unsigned int time)
{
CKCON |= 0x10; //定时器1使用系统时钟(不分频)
TH0 = 0xd4; //[TH1:TL1] = 65536 - SYSCLK/定时器1溢出率
// = 65536 - SYSCLK/1000
// = 65536 - 16000000/1000
// = 65536 - 16000
// = 49536
TL0 = 0xcc;
ET0 = 0; //禁止定时器1中断
TR0 = 1; //启动定时器1
while(time--)
{
TF0 = 0;
while(!TF0); //延时1ms
TH0 = 0xd4;
TL0 = 0xcc;
}
TR0 = 0; //停止定时器1
}
#else
void delay(unsigned int time)
{
CKCON |= 0x10; //定时器1使用系统时钟(不分频)
TH0 = 0xc1; //[TH1:TL1] = 65536 - SYSCLK/定时器1溢出率
// = 65536 - SYSCLK/1000
// = 65536 - 16000000/1000
// = 65536 - 16000
// = 49536
TL0 = 0x80;
ET0 = 0; //禁止定时器1中断
TR0 = 1; //启动定时器1
while(time--)
{
TF0 = 0;
while(!TF0); //延时1ms
TH0 = 0xc1;
TL0 = 0x80;
}
TR0 = 0; //停止定时器1
}
#endif
void main (void)
{
int i = 0;
WDTCN = 0xDE; // Disable watchdog timer
WDTCN = 0xAD;
#ifdef CLK11M
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
OSCICN = 0x08;//External Clock:11059200Hz
#else
OSCICN = 0x07;//16M 10-14ms,40-56
#endif
Port_Init (); // Init Ports
EA = 1; // Enable global interrupts
while (1){
delay(40); //10-15,40-60
LED=0;
delay(40);
LED=1;
}
}
void Port_Init (void)
{
// Enable crossbar
XBR2 = 0x40;
PRT1CF = 0x40; // Set P1.6(LED) to push-pull
PRT0CF = 0xFF;
}
//-----------------------------------------------------------------------------
// Timer0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the Timer0 as a 16-bit timer, interrupt enabled.
// Using the SYSCLK at 16MHz/8 and reloading the T0 register with
// TIMER0_RELOAD_xxxx.
//
// Note: The Timer0 uses a 1:12 prescaler. If this setting changes, the
// TIMER_PRESCALER constant must also be changed.
//-----------------------------------------------------------------------------
void Timer0_Init(void)
{
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timer0 High register
ET0 = 1; // Timer0 interrupt enabled
TMOD = 0x01; // 16-bit Mode Timer0
TCON = 0x10; // Timer0 ON
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer0_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer0 interrupt and toggle the LED
//
//-----------------------------------------------------------------------------
/*
void Timer0_ISR (void) interrupt 1
{
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timer0 register
TL0 = TIMER0_RELOAD_LOW;
LED = ~LED; // Toggle the LED
} //*/
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------