|
- #include <reg51.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- sbit TLC1543_CLK=P3^3; //TLC1543硬件连接
- sbit TLC1543_DIN = P3^4;
- sbit TLC1543_DOUT= P3^5;
- sbit TLC1543_CS = P3^7;
- sbit TLC1543_EOC = P3^2; //这里没用到中断
- //*****************************************************************
- //数码管显示子程序,将一个数num显示到某一位weixuan数码管
- //*****************************************************************
- void numdis(uchar num,uchar weixuan,uchar brightness )
- {
- while(brightness--)P0=num*0x10+weixuan*0x01;
- P0=0xFF;
- }
- //*****************************************************************
- //将读到的温度显示在六个数码管上,只显示整数部分,即显示精度为一度
- //*****************************************************************
- void display(uint num)
- {
- numdis(num/10000, 4,20);
- numdis(num%10000/1000, 3,20);
- numdis(num%1000/100, 2,20);
- numdis(num%100/10, 1,20);
- numdis(num%10, 0,20);
-
- }
- uint read1543(uchar channel) //从TLC1543读取采样值,形参channel是采样的通道号
- {
- uint data ad;
- uint data i;
- uchar data al=0,ah=0;
-
- TLC1543_CLK = 0;
- TLC1543_CS = 0;
- for (i=0;i<4;i++) //把通道号打入1543
- {
- TLC1543_DIN=(channel&0x0f);
- TLC1543_CLK=1;
- TLC1543_CLK=0;
- channel<<=1;
- }
-
- for (i=0;i<6;i++) //填充6个TLC1543_CLK
- {
- TLC1543_CLK=1;
- TLC1543_CLK=0;
- }
-
- TLC1543_CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();
- TLC1543_CS=0; //等待AD转换
- _nop_();_nop_();_nop_();
-
- for (i=0;i<2;i++) //取D9,D8
- {
- TLC1543_DOUT=1;
- TLC1543_CLK=1;
- ah<<=1;
- if(TLC1543_DOUT) ah|=0x01;
- TLC1543_CLK=0;
- }
-
- for (i=0;i<8;i++) //取D7--D0
- {
- TLC1543_DOUT=1;
- TLC1543_CLK=1;
- al<<=1;
- if (TLC1543_DOUT) al|=0x01;
- TLC1543_CLK=0;
- }
-
- TLC1543_CS=1;
- ad=(uint)ah;
- ad<<=8;
- ad|=(uint)al; //得到AD值
- return (ad);
- }
- uint volt_convert(uchar channel)
- {
- uint temp_volt[4];
- ulong sum_volt=0;
- uchar i;
- for(i=0;i<4;i++)
- {
- temp_volt[i] = read1543(channel);
- sum_volt += temp_volt[i];
- }
- return (((sum_volt/4)*500)/1023);
- }
- main()
- {
- while(1)
- {
- display(volt_convert(2));
-
- }
- }
复制代码
|
|