假设系统的晶振频率为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
}本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd
- ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
- ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
- ¥15 Windows11, backspace, enter, space键失灵