51單片機(jī)占空比設(shè)計(jì)
P3.0輸出占空比
T+、T-設(shè)置占空比時(shí)間,每按一次加或減100ms
R+、R-設(shè)置占空比加減1% 初始為100ms 占空比50%
數(shù)碼管前兩位顯示時(shí)間單位s,后兩位顯示占空比單位%
示波器顯示
使用定時(shí)器,避免了數(shù)碼管顯示和按鍵掃描延時(shí),實(shí)現(xiàn)占空比實(shí)時(shí)輸出。避免因延時(shí)等待。 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;
評(píng)論