用c语言实现80c51 Int0 外部事件中断引脚接一个按钮;(2)P0 口接8位 D/A;(3)Int0中断时,D/A输出一个方波;(4)可输出其它波形(可选项)
时间: 2024-01-21 17:02:40 浏览: 64
好的,让我来回答你的问题。
1. 首先需要初始化引脚,将P3.2设置为外部中断引脚。代码如下:
```c
EA = 1; //总中断使能
EX0 = 1; //开启外部中断0
IT0 = 1; //设置外部中断0为下降沿触发
```
2. 接下来需要将P0口设置为输出模式,并初始化D/A芯片。代码如下:
```c
sbit CS = P1^4; //D/A芯片片选引脚
sbit WR = P1^5; //D/A芯片写入引脚
void Init_DAC()
{
P0 = 0x00; //将P0口设置为输出模式
CS = 1; //使能D/A芯片
WR = 1; //D/A芯片写入引脚置高
}
```
3. 当外部中断0触发时,产生一个方波输出到D/A芯片上。代码如下:
```c
void Int0_ISR() interrupt 0
{
unsigned int i,j;
for(i=0;i<255;i++)
{
for(j=0;j<1000;j++)
{
P0 = i; //P0口输出方波
WR = 0; //将D/A芯片的写入引脚置低
WR = 1; //将D/A芯片的写入引脚置高
}
}
}
```
4. 如果需要输出其它波形,可以通过改变P0口的输出值来实现。例如,如果要输出正弦波,可以通过查表的方式来实现。代码如下:
```c
int sin_table[256] = {0,21,43,64,86,107,128,149,170,191,211,231,251,270,289,308,326,344,361,378,395,411,426,441,455,469,482,495,507,518,529,539,549,558,567,574,581,587,593,598,602,606,609,611,613,614,614,614,613,611,609,606,602,598,593,587,581,574,567,558,549,539,529,518,507,495,482,469,455,441,426,411,395,378,361,344,326,308,289,270,251,231,211,191,170,149,128,107,86,64,43,21,-1,-22,-44,-65,-87,-108,-129,-150,-171,-192,-212,-232,-252,-271,-290,-309,-327,-345,-362,-379,-396,-412,-427,-442,-456,-470,-483,-496,-508,-519,-530,-540,-550,-559,-568,-575,-582,-588,-594,-599,-603,-607,-610,-612,-614,-615,-615,-615,-614,-612,-610,-607,-603,-599,-594,-588,-582,-575,-568,-559,-550,-540,-530,-519,-508,-496,-483,-470,-456,-442,-427,-412,-396,-379,-362,-345,-327,-309,-290,-271,-252,-232,-212,-192,-171,-150,-129,-108,-87,-65,-44,-22,-1};
void Int0_ISR() interrupt 0
{
unsigned int i,j;
for(i=0;i<256;i++)
{
for(j=0;j<1000;j++)
{
P0 = sin_table[i]; //P0口输出正弦波
WR = 0; //将D/A芯片的写入引脚置低
WR = 1; //将D/A芯片的写入引脚置高
}
}
}
```
以上就是用C语言实现80c51 Int0 外部事件中断引脚接一个按钮,P0口接8位D/A,Int0中断时D/A输出一个方波,并可输出其它波形的代码实现。
阅读全文