通过SPI用51单片机和TLC2543进行AD转换读取电压值
单片机源程序如下:
- #include "reg52.h"
- #include"intrins.h"
- //void Delay500ms(); //@11.0592MHz
- unsigned int AD_Read() ;
- //void Delay10us(); //@11.0592MHz
- //void Delay3000ms(); //@11.0592MHz
- sbit SCK= P1^0;
- sbit RES_CS= P1^1;
- sbit DI = P1^2;
- sbit DO = P1^3;
- code unsigned char seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0-9段码显示
- int main()
- {
- unsigned char j,a,aa,aaa,aaaa;
- unsigned int advalue=0;
- float value;
- while(1)
- {
- advalue=AD_Read();
- // value=(advalue*5)/65536.0;
- // advalue=value*10000;
- a=advalue%10;
- advalue=advalue/10;
- aa=advalue%10;
- advalue=advalue/10;
- aaa=advalue%10;
- aaaa=advalue/10;
- P3=0xfe;
- P2=~seg[aaaa];
- for(j=0;j<100;j++);
- P3=0xfd;
- P2=~seg[aaa];
- for(j=0;j<100;j++);
- P3=0xfb;
- P2=~seg[aa];
- for(j=0;j<100;j++);
- P3=0xf7;
- P2=~seg[a];
- for(j=0;j<100;j++);
- }
- }
- unsigned int AD_Read()
- {
- unsigned char i,j,command=0x0c;
- unsigned int value;
- RES_CS = 1;
- _nop_();
- SCK = 0;
- _nop_();
- RES_CS = 0;
- _nop_();
- for(i=8;i>0;i--)
- {
- command <<= 1;
- DI = CY; //CY为状态寄存器进位标志位,将command的最高位赋给DI
- _nop_();
- SCK = 1;
- _nop_();
- SCK = 0;
- _nop_();
- value=value<<1;
- j=DO;
- if(j==1)
- {
- value|=0x01;
- }
- }
- for(i=8;i>0;i--)
- {
- _nop_();
- SCK = 1;
- _nop_();
- SCK = 0;
- _nop_();
- value=value<<1;
- j=DO;
- if(j==1)
- {
- value|=0x01;
- }
- }
- return value;
- }
复制代码
所有资料51hei提供下载:
TLC2543 SPI通信.zip
(31.82 KB, 下载次数: 17)
|