51单片机占空比设计
时间:11-30
来源:互联网
点击:
Proteus仿真
P3.0输出占空比
T+、T-设置占空比时间,每按一次加或减100ms
R+、R-设置占空比加减1% 初始为100ms 占空比50%
数码管前两位显示时间单位s,后两位显示占空比单位%
示波器显示
使用定时器,避免了数码管显示和按键扫描延时,实现占空比实时输出。避免因延时等待。 12MHz晶振程序如下
#include#define CLOCK 12000L#define Fosc 12000000L#define TMR 1#define TMR_BASE (unsigned int)(65536-(Fosc*TMR/CLOCK))#define TMR_BASEH TMR_BASE/0x100#define MASK 0xaa#define AddT 0xa8 #define CutT 0xa2#define AddR 0x8a#define CutR 0x2asbit OUT=P3^0;code unsigned char Num[10]={0xc0,0xf9,0xa4,0x30,0x99,0x92,0x82,0xf8,0x80,0x90};bit T1ms_Fg;void Timer0_interrupt(void) interrupt 1{TR0=0;TH0=TMR_BASEH;TL0+=TMR_BASE;TR0=1;T1ms_Fg=1;}void TMR0_Init(void){TMOD=0x01;TH0=TMR_BASEH;TL0=TMR_BASE;TR0=1;ET0=1;} bit Tmr_1ms_Fg, Tmr_10ms_Fg, Tmr_100ms_Fg;unsigned char T100ms_count=10; void timer(void){Tmr_1ms_Fg=0;Tmr_10ms_Fg=0; Tmr_100ms_Fg=0; if (T1ms_Fg){T1ms_Fg=0; Tmr_1ms_Fg=1;--T100ms_count; if (T100ms_count%10==0){Tmr_10ms_Fg=1; }if (T100ms_count==0){T100ms_count=10;Tmr_100ms_Fg=1; }}} unsigned char T=1,R=50; void diaplay(void){static unsigned char status=0;if(!Tmr_10ms_Fg)return;P0=0xff; P2=0x01 30)T=1;break; case CutT:T--;if(T<1)T=30;break; case AddR:R++;if(R>99)R=1;break; case CutR:R--;if(R<1)R=99;break;}}bit Duty_fg=1;void duty(void){static unsigned int r=50;if(!Tmr_1ms_Fg)return;if(--r)return;OUT=Duty_fg;if(Duty_fg)r=R*T;else r=(100-R)*T;Duty_fg=~Duty_fg; }void main(void){ TMR0_Init();EA=1;while(1){timer();diaplay();duty();key();}}
51单片机占空 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)