|
熱電偶溫度表.PNG (55.78 KB, 下載次數(shù): 67)
下載附件
2018-1-18 18:51 上傳
串口時(shí)序圖.PNG (39.93 KB, 下載次數(shù): 67)
下載附件
2018-1-19 11:16 上傳
說(shuō)明:程序是根據(jù)串口時(shí)序圖做的,在時(shí)鐘高電平期間讀取,不是在時(shí)鐘下降沿讀取。手冊(cè)上舉例說(shuō)明強(qiáng)調(diào)時(shí)鐘下降沿讀取,目前網(wǎng)上程序有這二種。
單片機(jī)源程序:
- #include "reg51.h"
- #include "intrins.h" //_nop_();延時(shí)函數(shù)用
- #define uchar unsigned char //用uchar代替unsigned char,1字節(jié)0-255
- #define uint unsigned int //用uint代替nsigned int,2字節(jié)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轉(zhuǎn)換數(shù)據(jù)數(shù)據(jù)讀取,并返回值
- 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 }; //共陽(yáng)LED段碼表
- uchar code tab_2[10] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10 };
- //含小數(shù)點(diǎn)共陽(yáng)段碼 "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
- uchar code tab_3[4] = { 0x01, 0x02, 0x04, 0x08 }; //位碼
- uint Read_AD()// AD轉(zhuǎn)換數(shù)據(jù)讀取子函數(shù),并返回值
- {
- uchar i;
- unsigned int Temp_2;
- Temp_2 = 0;
- CS = 0;
- _nop_();
- for (i = 0; i < 16; i++) //16位數(shù)據(jù)讀取
- {
- 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) //最高讀取溫度設(shè)定為999攝氏度
- wendu=999;
- bai = wendu / 100; //取百位數(shù)字
- wendu = wendu % 100;
- shi = wendu / 10; //取十位數(shù)字
- wendu = wendu % 10;
- ge = wendu %10; //取個(gè)位數(shù)字
- xiao = tempB / 10;
-
- }
- void Display_temp() //溫度顯示子函數(shù)
- {
- P3 = 0x00;
- P0 = tab_1[bai];
- P3 = tab_3[0];//顯示百位數(shù)字
- for (j = 300; j > 0; j--) ; //延時(shí)
-
- P3 = 0x00;
- P0 = tab_1[shi];
- P3 = tab_3[1];//顯示十位數(shù)字
- for (j = 300; j > 0; j--) ;
-
- P3 = 0x00;
- P0 = tab_2[ge];
- P3 = tab_3[2]; //顯示個(gè)位數(shù)字
- for (j = 300; j > 0; j--) ;
-
- P3 = 0x00;
- P0 = tab_1[xiao];
- P3 = tab_3[3];//顯示小數(shù)位
- 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(); // 熱電偶數(shù)據(jù)讀取,返回溫度
- zhuanhuan();
- for(i=20;i>0;i--)
- Display_temp(); //溫度顯示
- }
- }
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|