#include "ML51.h"
/* if define TIMER0_FSYS_DIV12, timer = (0x1FFF-0x1000)*12/24MHz = 4.08ms */
/* if define TIMER0_FSYS, timer = (0x1FFF-0x0010)/24MHz = 340us */
#define TH0_INIT 0x10
#define TL0_INIT 0x00
#define pwm_num 1000
sbit LED = P1^4;
/************************************************************************************************************
* TIMER 0 interrupt subroutine
************************************************************************************************************/
void Timer0_ISR (void) interrupt 1 //interrupt address is 0x000B
{
static unsigned char counter = 0;
_push_(SFRS);
TH0 = TH0_INIT;
TL0 = TL0_INIT;
TF0 = 0 ;
P32 = ~P32; // GPIO toggle when interrupt
counter++;
if(counter==1500)
counter=0;
LED = (counter<=pwm_num)?0:1;
_pop_(SFRS);
}
/************************************************************************************************************
* Main function
************************************************************************************************************/
void main (void)
{
/* As defaut all multi function define as GPIO */
ALL_GPIO_QUASI_MODE;
MFP_P32_GPIO;
P32_PUSHPULL_MODE;
MFP_P14_GPIO;
P14_PUSHPULL_MODE;
ENABLE_TIMER1_MODE0; //Timer 0 mode configuration
TIMER0_FSYS_DIV12;
TH0 = TH0_INIT;
TL0 = TL0_INIT;
ENABLE_TIMER0_INTERRUPT; //enable Timer0 interrupt
ENABLE_GLOBAL_INTERRUPT; //enable interrupts
set_TCON_TR0; //Timer0 run
while(1);
} |