编写一个完整的单片机C51程序

2024-12-25 20:08:42
推荐回答(4个)
回答1:

#include
sbit led=P1^0; //单片机管脚位声明
void main()
{
TMOD=0x01; //定时器TO工作在方式1
TH0=(65536-5000)/256; //装初值,12M晶振 1为1us; 5000为5000us=5ms;
TL0=(65536-5000)%256;
EA =1; //开总中断
ET0=1; //开定时器TO中断
TR0=1; //启动定时器
P1=0; //初始化P1口
while(1) ; //程序在这里等待中断发生
}
void T0_time() interrupt 1
{
unsigned char num;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
num++;
if(num==100) //0.5S (1s闪烁1次==0.5S亮0.5S灭)
{
num=0;
led=~led; //led状态取反
}
}

回答2:

12Mhz的51单片机,经过12分频后为1MHz,则一个指令周期为1/1Mhz = 1us. 使能定时器 2 使能定时器中断 3 编写中断程序我估计你是觉得1秒的时间, 一定要采纳哦

回答3:

#include
#define uchar unsigned char
#define uint unsigned int
sbit LED = P1^0;
uchar T_Count = 0;
void main()
{
TMOD = 0x01;
TH0 = (65535-5000)/256;
TL0 = (65535-5000)%256;
IE = 0x82;
TR0 = 1;
while(1);
}

void LED_Flash() interrupt 1
{
TH0 = (65535-5000)/256;
TL0 = (65535-5000)%256;
if(++T_Count == 100)
{
LED = !LED;
T_Count = 0;
}
}

回答4:

你正在考试吧?这个简单,就是考虑定时器中断一次时间不够,一次定时5ms,要中断200次,然后P1.0取反