鄙人在做电压测量一个项目,用到了mcp3208。在测试1、2、3、4、5V时只有0、1、2示数正确。3、4、5分别显示00、01、02。
网上的参考资料比较少,所以想问问大家这款12位高精度AD芯片你们用过吗?
程序代码我已经删减到最简洁了:
- #include "reg51.h"
- #include "stdio.h"
- sbit ADCS = P1^3;
- sbit DIN = P1^2;
- sbit DOUT = P1^1;
- sbit CLK = P1^0;
- float getdata;
- float t;
- char sum[12];
- unsigned char s[]="Hello";
- void Output_Control(char TempChar)
- {
- unsigned int Mask = 0x10;
- unsigned char Count;
- for(Count = 0;Count<5;Count++)
- {
- DIN = 0;
- if(TempChar&Mask)
- {
- DIN = 1;
- }
- CLK = 1;
- Mask >>= 0x01;
- CLK = 0;
- }
- }
- float trans(char a[12]){
- int i, num = 0;
- for(i=11; i>=0; i--)
- {
- num*=2;
- num+=a[i]-'0';
- }
- return num;
- }
- float Input_Data()
- {
- unsigned char Count;
- CLK = 1;
- CLK = 0;
- for(Count = 0;Count <12;Count++)
- {
- sum[Count]=DOUT;
- CLK = 1;
- CLK = 0;
- }
- return(trans(sum));
- }
- void main(void)
- {
- ADCS = 1;
- while(1)
- {
- ADCS = 0;
- Output_Control((char)0x18);
- getdata=Input_Data();
- ADCS = 1;
- t=(5*getdata/8191);
- P2=t;
- }
- }
复制代码
|