#include <REGX51.H>
#include <math.H>
#define FOSC 12000000L
#define T1MS (65536-FOSC/12/1000*10) //10ms timer calculation method in 12T mode
#define PI 3.1415926
#define On 0
#define Off 1
void Timer0_Init(void);
void LEDBlink(void);
unsigned int KeyScan(void);
#define LEDPORT P1
#define dx (PI*2/100)
#define PWM_PERIOD 100
#define LEDNUM 8
sbit LED0 = P1^0;
sbit LED1 = P1^1;
sbit LED2 = P1^2;
sbit LED3 = P1^3;
sbit LED4 = P1^4;
sbit LED5 = P1^5;
sbit LED6 = P1^6;
sbit LED7 = P1^7;
sbit KEY1 = P2^0;
sbit KEY2 = P2^1;
unsigned char led_tab[8] = {
0XFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F};
unsigned int TimeTikTok = 0;
unsigned int TimeBase = 0;
void main(void)
{
unsigned int i = 0;
unsigned int keycount = 0;
double x = 0.0;
Timer0_Init();
while(1)
{
if (TimeBase) 只有为1的时候会继续
{
TimeBase = 0;
TimeTikTok ++;if(TimeTikTok > 60000) TimeTikTok = 0; //当TimeBase=1时,1%10=0.1
if(! (TimeTikTok % 10)) //100ms 这是什么意思 如果0.1!=1成立?
{
//LEDBlink();
}
if(! (TimeTikTok % 20)) //200ms
{
if(KeyScan() == 1)
LED0 ^=1 ;
if(KeyScan() == 2)
LED1 ^=1 ;
}
}
}
} //时间表现在哪里,过了多久
void Timer0_Init(void)
{
TMOD = 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1; //timer0 start running
ET0 = 1; //enable timer0 interrupt
EA = 1; //open global interrupt switch
}
//LEDPORT |= (0X01<<LEDX);
//unsigned int LEDX,unsigned int delay
void LEDBlink(void)
{
static int statue = 0;
switch (statue)
{
case 0: LEDPORT = 0xFE; statue = 1; break;
case 1: LEDPORT = 0XFD; statue = 2; break;
case 2: LEDPORT = 0XFB; statue = 3; break;
case 3: LEDPORT = 0XF7; statue = 4; break;
case 4: LEDPORT = 0XEF; statue = 5; break;
case 5: LEDPORT = 0XDF; statue = 6; break;
case 6: LEDPORT = 0XBF; statue = 7; break;
case 7: LEDPORT = 0X7F; statue = 0; break;
}
}
unsigned int KeyScan(void)
{
static int statue = 0;
unsigned int keyVal = 0;
switch (statue)
{
case 0: statue = 1; break;
case 1:
{
if(!KEY1) 等价于KEY1=0 keyVal = 1;
if(!KEY2) keyVal = 2;
if((!KEY1) && (!KEY2)) keyVal = 3;
statue = 2; break;
}
case 2: statue = 0; break;
}
return keyVal;
}
/* Timer0 interrupt routine */
//unsigned int PWM_CHANGE_cnt = 0;
void TM0_isr() interrupt 1
{
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8; //reload timer0 high byte
TimeBase = 1;
}
|