51单片机 方波
产生一个占空比为80%的方波。
#include <reg51.h> unsigned char a,b; sbit OUT = P0^0; // 定时器中断初始化 void timer0_init() { EA = 0; TMOD = 0X01; ET0 = 1; TR0 =1; TH0 = (65535-10000)/256; TL0 = (65535-10000)%256; EA = 1; } // 定时器中断 void timer0() interrupt 1 { TH0 = (65535-10000)%256; TL0 = (65535-10000)%256; a++; } void main() { void timer0_init(); while(1) { if(a==4) { OUT = 0; } else { OUT = 1; } if(a==5) { a = 0; } } }