PCF8591AD转换程序.docx
- 文档编号:28270125
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:8
- 大小:14.45KB
PCF8591AD转换程序.docx
《PCF8591AD转换程序.docx》由会员分享,可在线阅读,更多相关《PCF8591AD转换程序.docx(8页珍藏版)》请在冰豆网上搜索。
PCF8591AD转换程序
#include
#defineuintunsignedint
#defineucharunsignedchar
ucharcodetable[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x83,0xf8,
0x80,0x98,0x88,0x83,
0xc6,0xa1,0x86,0x8e
};
sbitscl=P2^0;
sbitsda=P2^1;
uintbaiwei,gewei,shiwei;
voiddelay_pcf8591(ucharz);
voiddelay(ucharz);
voidinit_pcf8591();
voidstart_pcf8591();
voidstop_pcf8591();
voidack_pcf8591();
voidwrite_pcf8591_byte(ucharadd);
uintread_pcf8591_byte();
uintreadpcf8951_adc();
voiddisplay();
/******************************************************************
主函数
******************************************************************/
voidmain()
{
init_pcf8591();
while
(1)
{
display();
}
}
/******************************************************************
初始化函数
******************************************************************/
voidinit_pcf8591()
{
sda=1;
scl=1;
}
/******************************************************************
短延时函数
******************************************************************/
voiddelay_pcf8591(ucharz)
{
while(z--);
}
/******************************************************************
长延时函数
******************************************************************/
voiddelay(ucharz)
{
ucharx,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
/******************************************************************
开始函数
******************************************************************/
voidstart_pcf8591()
{
sda=1;
delay_pcf8591
(1);
scl=1;
delay_pcf8591
(1);
sda=0;
delay_pcf8591
(1);
}
/******************************************************************
停止函数
******************************************************************/
voidstop_pcf8591()
{
sda=0;
delay_pcf8591
(1);
scl=1;
delay_pcf8591
(1);
sda=1;
delay_pcf8591
(1);
}
/******************************************************************
等待应答函数
******************************************************************/
voidack_pcf8591()
{
uintt;
scl=1;
delay_pcf8591
(1);
while((sda==1)&&t<250)t++;
scl=0;
delay_pcf8591
(1);
}
/******************************************************************
写字节函数
******************************************************************/
voidwrite_pcf8591_byte(ucharadd)
{
uchari;
for(i=0;i<8;i++)
{
add<<=1;
scl=0;
delay_pcf8591
(1);
sda=CY;
scl=1;
delay_pcf8591
(1);
}
scl=0;
delay_pcf8591
(1);
sda=1;
delay_pcf8591
(1);
}
/******************************************************************
读字节函数
******************************************************************/
uintread_pcf8591_byte()
{
uchartemp,i;
for(i=0;i<8;i++)
{
scl=1;
delay_pcf8591
(1);
temp<<=1;
temp|=sda;
scl=0;
delay_pcf8591
(1);
}
returntemp;
}
/******************************************************************
读pcf8591数据函数
******************************************************************/
uintreadpcf8951_adc()
{
uintdat;
start_pcf8591();
write_pcf8591_byte(0x90);
ack_pcf8591();
write_pcf8591_byte(0x41);
ack_pcf8591();
start_pcf8591();
write_pcf8591_byte(0x91);
ack_pcf8591();
dat=read_pcf8591_byte();
stop_pcf8591();
returndat;
}
/******************************************************************
display函数
******************************************************************/
voiddisplay()
{
uintn;//vin;
n=readpcf8951_adc();
//vin=((n*50)/255);
baiwei=n/100;
shiwei=n/10%10;
gewei=n%10;
P0=table[baiwei];
P1=0x04;
delay(5);
P0=table[shiwei];
P1=0x02;
delay(5);
P0=table[gewei];
P1=0x01;
delay(5);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- PCF8591AD 转换 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)