|
电路原理图如下:
单片机源程序如下:
- #include "STC15W.H"
- #include "iic.h"
- //#include "zifu.h"
- #include "delay.h"
- #include "stdio.h"
- #include "intrins.h"
- #include "temp.h"
- /*******定义时间变量********************/
- unsigned int count=0;
- unsigned int count1=9;
- unsigned int second0;
- unsigned int second1;
- unsigned int min0;
- unsigned int min1;
- unsigned int hour0;
- unsigned int hour1;
- unsigned char s[3];
- unsigned char h1[1],h0[1],m1[1],m0[1],s1[1],s0[1];
- /****************************************************/
- bit flag = 0; //定义秒针闪烁的标志位
- /**********************定义温度的参数变量*****************/
- code unsigned char float_tab[16]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};//小数表
- unsigned int Display_Digit[]={0,0,0,0};
- unsigned char wendu=0;
- unsigned char aa,bb;
- unsigned char CurrentT=0;
- unsigned char Temp_Value[]={0x00,0x00};
- unsigned char DS18B20_IS_OK;
- unsigned char bai[1],shi[1],ge[1];
- /********************************************************/
- /* 定时器0的配置**************************************/
- void Timer0Init() //定时50ms出发一次中断
- {
- AUXR &= 0x7F;
- TMOD &= 0xF0;
- TL0 = 0x00;
- TH0 = 0x4C;
- //TF0 = 0;
- TR0 = 1;
- ET0=1;
- EA=1;
- }
- void TIMER0() interrupt 1
- {
- count++; //50毫秒触发一次
- if(count==20) //定时为1秒
- {
- count=0;second0+=1; flag=~flag;
- }
- if(second0>9)
- {
- second0=0;second1+=1;
- }
- if(second1>5)
- {
- second1=0;min0+=1;
- }
- if(min0>9)
- {
- min0=0;min1+=1;
- }
- if(min1>5)
- {
- min1=0;hour0+=1;
- }
- if(hour1<2)
- {
- if(hour0>9)
- {
- hour0=0;hour1+=1;
- }
-
- }
- if(hour1==2)
- {
- if(hour0>3)
- {
- second0=0;second1=0;
- min0=0;min1=0;
- hour0=0;hour1=0;
- }
- }
-
-
-
-
-
- }
- //读取温度
- void Read_Temperature()
- {
- if(Init_DS18B20()==1)
- DS18B20_IS_OK=0;
- else
- {
-
- WriteOneByte(0xcc); //跳过序列号
- WriteOneByte(0x44); //启动温度转换
- Init_DS18B20();
- WriteOneByte(0xcc);
- WriteOneByte(0xbe); //读取温度
- Temp_Value[0] = ReadOneByte(); //低八位
- Temp_Value[1] = ReadOneByte(); //高八位
- DS18B20_IS_OK=1;
-
-
- }
-
- }
- void main()
- {
-
- Timer0Init() ;
- Initial_M096128x64_ssd1306();
- OLED_CLS();
- Delay_ms(5);
-
-
- while(1)
- {
-
- Read_Temperature();
- if((Temp_Value[1]&0xf8)==0xf8) //判断高5位,如果都为1则是负数
- {
- Temp_Value[1] = ~Temp_Value[1]; //如果为负数。高八位需要取反,第八位需要取反后加1.
- Temp_Value[0] = ~Temp_Value[0]+1;
- if(Temp_Value[0]==0x00)
- Temp_Value[1]++;
- wendu = 1;
-
- }
- else{wendu = 0;}
- aa=Temp_Value[0]&0x0f; //小数点后一位算法
- bb=aa*0.0625*10;
- CurrentT= ((Temp_Value[0]&0xf0)>>4) | ((Temp_Value[1]&0x07)<<4); //整数值
- Display_Digit[1] = CurrentT/10; //百位 CurrentT%1000/100;
- Display_Digit[2] = CurrentT%10; //十位 CurrentT%100/10;
- Display_Digit[3] = bb%10; //个位 CurrentT%10;
-
-
-
- OLED_P8x16Str(0,0,"PARA_SYSTEM-V0");
-
-
- OLED_ShowCHinese(0,2,0); //当
- OLED_ShowCHinese(16,2,1); //前
- OLED_ShowCHinese(32,2,2); //温
- OLED_ShowCHinese(48,2,3); //度
- OLED_P8x16Str(64,2,":");
-
- //当温度为负数的时候,显示-号
- if(wendu==1)
- {
- OLED_P8x16Str(72,2,"-");
-
- }
- else
- {
- OLED_P8x16Str(72,2," ");
- }
-
-
- sprintf(bai,"%d",Display_Digit[1]);
- OLED_P8x16Str(80,2,bai);
- sprintf(shi,"%d",Display_Digit[2]);
- OLED_P8x16Str(88,2,shi);
- OLED_P8x16Str(96,2,".");
- sprintf(ge,"%d",Display_Digit[3]);
- OLED_P8x16Str(104,2,ge);
-
-
-
- OLED_ShowCHinese(0,4,4); //时
- OLED_ShowCHinese(16,4,5); //间
- OLED_P8x16Str(32,4,":");
-
-
-
-
- sprintf(h1,"%d",hour1);
- OLED_P8x16Str(40,4,h1);
- sprintf(h0,"%d",hour0);
- OLED_P8x16Str(48,4,h0);
-
- if(flag==1)
- {
- OLED_P8x16Str(56,4,":");
- }
- else
- {
- OLED_P8x16Str(56,4," ");
- }
-
- sprintf(m1,"%d",min1);
- OLED_P8x16Str(64,4,m1);
- sprintf(m0,"%d",min0);
- OLED_P8x16Str(72,4,m0);
-
- if(flag==1)
- {
- OLED_P8x16Str(80,4,":");
- }
- else
- {
- OLED_P8x16Str(80,4," ");
- }
-
- sprintf(s1,"%d",second1);
- OLED_P8x16Str(88,4,s1);
- sprintf(s0,"%d",second0);
- OLED_P8x16Str(96,4,s0);
-
-
-
-
- Delay_ms(10);
-
-
- }
-
-
-
- }
复制代码
所有资料51hei提供下载:
|
评分
-
查看全部评分
|