单片机作业
答案:2 悬赏:30
解决时间 2021-11-07 09:26
- 提问者网友:精神病院里
- 2021-11-06 14:24
单片机作业
最佳答案
- 二级知识专家网友:神鬼未生
- 2021-11-06 15:05
自己写的。
第一题
查询法:
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
void main()
{
uchar i;
IT0=0;
led=1;
while(1)
{
if(IE0==1)
{
IE0=0;//清除中断标志
i++;
if(i==10)
{
i=0;
led=!led;
}
}
}
}
中断法:
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
void interrupt_init()
{
IT0=1;//设置外部中断0下降沿有效
EX0=1;//允许外部中断0有效时执行外部中断0的中断子函数
EA=1;//总中断允许
}
void EX_0() interrupt 0
{
static uchar i;
i++;
if(i==10)
{
i=0;
led=!led;
}
}
void main()
{
interrupt_init();
while(1);
}
第二题:
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit output=P1^2;
bit NEGATE=1;
void timer_0()//定时器的初始化 6M晶振一个周期2us
{
TMOD=0x01;//用定时器0.工作在方式1.最大可以容纳65536
TH0=65510/256;// 25*2us=50us into interrupt1
TL0=65510%256;
ET0=1;//允许定时器0执行中断子函数
EA=1;//允许总中断
TR0=1;//打开定时器0
}
void time_0() interrupt 1
{
output=!output;
NEGATE=!NEGATE;
if(NEGATE)
{
TH0=65510/256; //25*2us=50us
TL0=(65510%256)+TL0;//提高精确度
}
else
{
TH0=65360/256; //175*2us=350us
TL0=(65535%256)+TL0;
}
}
void main()
{
timer_0();
output=1;
while(1);
}
第一题
查询法:
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
void main()
{
uchar i;
IT0=0;
led=1;
while(1)
{
if(IE0==1)
{
IE0=0;//清除中断标志
i++;
if(i==10)
{
i=0;
led=!led;
}
}
}
}
中断法:
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
void interrupt_init()
{
IT0=1;//设置外部中断0下降沿有效
EX0=1;//允许外部中断0有效时执行外部中断0的中断子函数
EA=1;//总中断允许
}
void EX_0() interrupt 0
{
static uchar i;
i++;
if(i==10)
{
i=0;
led=!led;
}
}
void main()
{
interrupt_init();
while(1);
}
第二题:
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit output=P1^2;
bit NEGATE=1;
void timer_0()//定时器的初始化 6M晶振一个周期2us
{
TMOD=0x01;//用定时器0.工作在方式1.最大可以容纳65536
TH0=65510/256;// 25*2us=50us into interrupt1
TL0=65510%256;
ET0=1;//允许定时器0执行中断子函数
EA=1;//允许总中断
TR0=1;//打开定时器0
}
void time_0() interrupt 1
{
output=!output;
NEGATE=!NEGATE;
if(NEGATE)
{
TH0=65510/256; //25*2us=50us
TL0=(65510%256)+TL0;//提高精确度
}
else
{
TH0=65360/256; //175*2us=350us
TL0=(65535%256)+TL0;
}
}
void main()
{
timer_0();
output=1;
while(1);
}
全部回答
- 1楼网友:西岸风
- 2021-11-06 16:20
楼上的回答相当好
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯