#include "reg51.h"
typedef unsigned char BYTE;typedef unsigned int WORD;
//-----------------------------------------------
/* define constants */#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) //1ms
timer calculation method in 12T mode
/* define SFR */sbit TEST_LED = P1^0; //work LED, flash once per secondsbit pwm1 = P1^1;sbit pwm2 = P1^2;sbit pwm3 = P1^3;sbit pwm4 = P1^4;sbit k1=P1^5;sbit k2=P1^6;sbit k3=P1^7;sbit k4=P2^0;/* define variables */WORD count; //1000 times counterWORD a,b,c;WORD time;WORD p1,p2,p3,p4;//-----------------------------------------------
/* Timer0 interrupt routine */void tm0_isr() interrupt 1 using 1{ TL0 = T1MS; //reload timer0 low byte TH0 = T1MS >> 8; //reload timer0 high byte if (count++ == 100) //1ms * 1000 -> 1s { count = 0; //reset counter TEST_LED = ! TEST_LED; //work LED flash } if (count<=p1) { pwm1=0; } else{pwm1=1;} if (count<=p2) { pwm2=0; } else{pwm2=1;} if (count<=p3) { pwm3=0; } else{pwm3=1;} if (count<=p4) { pwm4=0; } else{pwm4=1;}//-----------------------------------------------
/* main program */void main(){ while (1){ if (k1==0) { for(a=0;a++;a<10000) { if(a<5000) { if(k1==1) { for(b=0;b++;b<10000) { if(k1==0) { p1=80; p2=60; p3=90; p4=80; 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 count = 0; //initial counter b=10000; a=10000; } else { a=10000; b=10000; } } } } if(a>4999) { if(k3==0&&k1==0) { p1=100; p2=60; p3=90; p4=80; 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 count = 0; //initial counter } else { if(k1==0) { p1=100; p2=80; p3=80; p4=100; 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 count = 0; //initial counter } } }
} if (k3==0) { p1=100; p2=0; p3=0; p4=100; 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 count = 0; //initial counter } else { p1=100; p2=0; p3=0; p4=0;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 count = 0; //initial counter } } else { if (k2==0) { p1=50; p2=100; p3=0; p4=0; 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 count = 0; //initial counter } else { if (k3==0) { p1=0; p1=40; p1=100; p1=0; 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 count = 0; //initial counter } else { if (k4==0) { p1=0; p2=30; p3=0; p4=100; 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 count = 0; //initial counter } } } } } } //loop}
一周热门 更多>