熱電偶溫度表.PNG (55.78 KB, 下載次數: 67)
下載附件
2018-1-18 18:51 上傳
串口時序圖.PNG (39.93 KB, 下載次數: 67)
下載附件
2018-1-19 11:16 上傳
說明:程序是根據串口時序圖做的,在時鐘高電平期間讀取,不是在時鐘下降沿讀取。手冊上舉例說明強調時鐘下降沿讀取,目前網上程序有這二種。
單片機源程序:
- #include "reg51.h"
- #include "intrins.h" //_nop_();延時函數用
- #define uchar unsigned char //用uchar代替unsigned char,1字節0-255
- #define uint unsigned int //用uint代替nsigned int,2字節0-26653
- sfr P3M0=0XB2;
- sfr P3M1=0XB1;
- sbit SO = P1 ^ 0; //P1.0口與SO相連
- sbit SCK = P1 ^ 1; //P1.1口與SCK相連
- sbit CS = P1 ^ 2; //P1.2口與CS相連
- uint j;
- uint wendu;
- uint Read_AD(); //AD轉換數據數據讀取,并返回值
- void Display_temp(); //溫度顯示
- uchar qian = 0, bai = 0, shi = 0, ge = 0, xiao = 0; //初始化LED
- uint temp;
- uchar code tab_1[10] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90 }; //共陽LED段碼表
- uchar code tab_2[10] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10 };
- //含小數點共陽段碼 "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
- uchar code tab_3[4] = { 0x01, 0x02, 0x04, 0x08 }; //位碼
- uint Read_AD()// AD轉換數據讀取子函數,并返回值
- {
- uchar i;
- unsigned int Temp_2;
- Temp_2 = 0;
- CS = 0;
- _nop_();
- for (i = 0; i < 16; i++) //16位數據讀取
- {
- Temp_2 <<= 1;//向左移一位
- SCK = 1;//上升沿脈沖
- _nop_();
- _nop_();
- _nop_();
- if (SO == 1) Temp_2 = Temp_2 | 0x01;
- SCK = 0;
- }
- CS = 1;
- SCK = 0;
- Temp_2 = Temp_2 & 0x7FF8; //取3-14位,變換為溫度值
- Temp_2 = Temp_2 >>3;
- return (Temp_2);
- }
- void zhuanhuan()
- { uint tempB;
- tempB = (wendu&3)*25;
- wendu=wendu>>2;
- wendu = wendu;
-
- if (wendu >= 999) //最高讀取溫度設定為999攝氏度
- wendu=999;
- bai = wendu / 100; //取百位數字
- wendu = wendu % 100;
- shi = wendu / 10; //取十位數字
- wendu = wendu % 10;
- ge = wendu %10; //取個位數字
- xiao = tempB / 10;
-
- }
- void Display_temp() //溫度顯示子函數
- {
- P3 = 0x00;
- P0 = tab_1[bai];
- P3 = tab_3[0];//顯示百位數字
- for (j = 300; j > 0; j--) ; //延時
-
- P3 = 0x00;
- P0 = tab_1[shi];
- P3 = tab_3[1];//顯示十位數字
- for (j = 300; j > 0; j--) ;
-
- P3 = 0x00;
- P0 = tab_2[ge];
- P3 = tab_3[2]; //顯示個位數字
- for (j = 300; j > 0; j--) ;
-
- P3 = 0x00;
- P0 = tab_1[xiao];
- P3 = tab_3[3];//顯示小數位
- for (j = 300; j > 0; j--) ;
-
- }
- void main() //主程序
- { unsigned char i;
- P3M0=0X00;
- P3M1=0X0F;
- CS = 1;
- SCK = 0;
- for (j = 300; j > 0; j--) ;
- while (1)
- {
- wendu = Read_AD(); // 熱電偶數據讀取,返回溫度
- zhuanhuan();
- for(i=20;i>0;i--)
- Display_temp(); //溫度顯示
- }
- }
復制代碼
|