51单片机PCF8591-AD-DA转换,中断实现切换。
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL=P2^1; //串行时钟输入端
sbit SDA=P2^0; //串行数据输入端
sbit HC595_RCLK = P2^5;
sbit HC595_SRCLK = P2^6;
sbit HC595_SER = P2^7;
uchar flag,n,a,b,c,yi,er,san,si,wu,liu,qi,ba;
uchar code shuma_duan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40,0x00};
void display();
void delay() //延时4-5个微秒
{;;}
//
void Delay_Ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//
void Send_595(uchar dat)
{
uchar i;
for(i=0; i<8; i++)
{
if(dat & 0x80)
HC595_SER = 1;
else
HC595_SER = 0;
HC595_SRCLK = 0;
HC595_SRCLK = 1;
dat = dat << 1;
}
}
//
void start()//开始信号
{
SDA=1;
delay();
SCL=1;
delay();
SDA=0;
delay();
}
//
void stop() //停止信号
{
SDA=0;
delay();
SCL=1;
delay();
SDA=1;
delay();
}
//
void respons()//应答 相当于一个智能的延时函数
{
uchar i;
SCL=1;
delay();
while((SDA==1)&&(i<250))i++;
SCL=0;
delay();
}
//
void init() //初始化
{
SDA=1;
delay();
SCL=1;
delay();
}
uchar read_byte()
{
uchar i,k;
SCL=0;
delay();
SDA=1;
delay();
for(i=0;i<8;i++)
{
SCL=1;
delay();
k=(k<<1)|SDA;//先左移一位,再在最低位接受当前位
SCL=0;
delay();
}
return k;
}
//
void write_byte(uchar date) //写一字节数据
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1; //左移一位 移出的一位在CY中
SCL=0; //只有在scl=0时sda能变化值
delay();
SDA=CY;
delay();
SCL=1;
delay();
SCL=0;
delay();
}
SDA=1;
delay();
}
//
void write_process(uchar kongzhizi,uchar date)
{
start();
write_byte(0x90); //1010 0000 前四位固定 接下来三位全部被接地了 所以都是0 最后一位是写 所以为低电平
respons();
write_byte(kongzhizi);
respons();
write_byte(date);
respons();
stop();
}
//
uchar read_process(uchar kongzhizi)
{
uchar date;
start();
write_byte(0x90);
respons();
write_byte(kongzhizi);
respons();
start();
write_byte(0x91); //把最后一位变成1,读
respons();
date = read_byte();
stop();
return date;
}
//
void main()
{
flag = 0;
EA = 1;
EX0 = 1;
IT0 = 1;
while(1)
{
//A/D转换
//通过AD转换,把数值显示。
if(flag == 0)
{
init();
n = read_process(0x00); //rb2滑动变阻器
// n = read_process(0x01); //光敏电阻 光度越强 数值越小
// n = read_process(0x02); //热敏电阻 温度越高 数值越小
// n = read_process(0x03); //接地为0
yi=n/100;er=n%100/10;san=n%10;
si=11;wu=11;liu=11;qi=11;ba=11;
display();
}
//D/A转换
//慢慢变亮后再慢慢变暗,如此循环。
if(flag == 1)
{
if(a <= 250)
{
a++;
c = a;
if(a == 251)
{
b = 250;
}
}
if(b >= 2)
{
b--;
c = b;
if(b == 1)
{
a = 0;
}
}
Delay_Ms(5);
write_process(0x40,c);
}
}
}
//
void exter() interrupt 0 //控制D/A转换还是A/D转换。
{
flag++;
if(flag == 2)
{
flag = 0;
}
}
//
void display()
{
Send_595(0xfe);
Send_595(shuma_duan[yi]);
HC595_RCLK = 0;
HC595_RCLK = 1;
Send_595(0xfd);
Send_595(shuma_duan[er]);
HC595_RCLK = 0;
HC595_RCLK = 1;
Send_595(0xfb);
Send_595(shuma_duan[san]);
HC595_RCLK = 0;
HC595_RCLK = 1;
Send_595(0xf7);
Send_595(shuma_duan[si]);
HC595_RCLK = 0;
HC595_RCLK = 1;
Send_595(0xef);
Send_595(shuma_duan[wu]);
HC595_RCLK = 0;
HC595_RCLK = 1;
Send_595(0xdf);
Send_595(shuma_duan[liu]);
HC595_RCLK = 0;
HC595_RCLK = 1;
Send_595(0xbf);
Send_595(shuma_duan[qi]);
HC595_RCLK = 0;
HC595_RCLK = 1;
Send_595(0x7f);
Send_595(shuma_duan[ba]);
HC595_RCLK = 0;
HC595_RCLK = 1;
}
个人见解,感谢阅读。