假设系统的晶振频率为12MHz,使用定时器T0采用方式1定时,在P1.0端口输出频率为1HZ,占空比为2:5的矩形脉冲。
1条回答 默认 最新
- 乐观的study123 2022-06-22 15:30关注
#include "reg51.h"
unsigned char BYTE;
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 second/* define variables */
unsigned int count,count_h=0; //1000 times counter//-----------------------------------------------
/* Timer0 interrupt routine /
void tm0_isr() interrupt 1
{
unsigned int j=500;
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8; //reload timer0 high byte
j=10002/7;
if(count_h<j) /////占空比值
count_h++;
else
TEST_LED=0;
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1000; //reset counter
count_h=0;
TEST_LED =1; //work LED flash
}
}//-----------------------------------------------
/* main program */
void main()
{
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 =1000; //initial counter
count_h=0;
TEST_LED =1;
while (1); //loop
}本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报