仅供学习
单片机源程序如下:
- #include<intrins.h>
- sbit SDA=P1^0;
- sbit SCL=P1^1;
- #define VREF 5.0
- /****************开始位********************/
- void start()
- {
- SDA = 1;
- SCL = 1;
- delaynus(50);
- SDA = 0;
- delaynus(50);
- SCL = 0;
- }
- // 停止位
- void stop()
- {
- SDA = 0;
- delaynus(50);
- SCL = 1;
- delaynus(50);
- SDA = 1;
- }
- bit ACK()
- {
- bit ack_bit;
- SCL = 1;
- delaynus(50);
- ack_bit = SDA;
- SCL = 0;
- return ack_bit;
- }
- unsigned char ReadData()
- {
- unsigned char i;
- unsigned char x;
- for(i = 0; i < 8; i++)
- {
- SCL = 1;
- x<<=1;
- x|=(unsigned char)SDA;
- SCL = 0;
- }
- return(x);
- }
- void WriteCurrent(unsigned char y)
- {
- unsigned char i;
- for(i = 0; i < 8; i++)
- {
- SDA = (bit)(y&0x80);
- delaynus(50);
- SCL = 1;
- delaynus(50);
- SCL = 0;
- y <<= 1;
- }
- }
- float ReadVoltage()
- {
- unsigned char dat1,dat2;
- unsigned int dat3;
- start();
- WriteCurrent(0x97);
- ACK();
- dat1=ReadData();
- ACK();
- dat2=ReadData();
- ACK();
- dat3=ReadData();
- ACK();
- stop();
- delay(20);
- dat3=(int)(dat1*256+dat2);
- return (dat3*VREF/32768);
- }
- /*
- *配置寄存器
- *BIT 7 6 5 4 3 2 1 0
- * ST/BSY 0 0 SC |DR1 DR0| |PGA1 PGA0|
- */
- void AD1100_initi(unsigned char com)
- {
- start();
- WriteCurrent(0x96);
- ACK();
- WriteCurrent(com);
- ACK();
- stop();
- delay(10);
- }
复制代码
所有资料51hei提供下载:
AD1100.zip
(56.68 KB, 下载次数: 5)
|