[C] 纯文本查看 复制代码
#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit pwm=P2^0;uint pwm_val;void delayms(uint xms); //软件延时函数void timer_set(uint value);void main(){ TMOD=0X01; //定时器初始化 TH0=(65536-1000)/256; TL0=(65536-1000)%256; EA=1; ET0=1; TR0=1; while(1) { pwm_val=500; delayms(1000); pwm_val=1500; delayms(1000); }}void timer0() interrupt 1{ static uchar i=1; TR0=0; switch(i) //两角度之间切换 { case 1: { pwm=1; timer_set(pwm_val); }break; case 2: { pwm=0; timer_set(20000-pwm_val); i=0; }break; default: break; } i++;}void timer_set(uint value){ TH0=(65536-value)/256; TL0=(65536-value)%256; TR0=1;}void delayms(uint xms){uint i,j;for(i=xms;i>0;i--)for(j=125;j>0;j--);} |