C语言CPU测试温度程序//Target:M8//Crystal:8.0000Mhz/********************************************************************************不可在RESET时做LCD显示,因为DS18B20的复位回应时间只有80us,显示一个字符的时间远大于这个值********************************************************************************/#include#include#include"1602Driver.h"#defineSet_DQ1DDRC|=0x08;PORTC|=0x08;//总线拉高#defineSet_DQ0DDRC|=0x08;PORTC&=0xf7;//总线置低#defineRead_DQPINC&0x08//读总线#defineMatchROM0xcc//匹配ROM#defineWriteMode0x4e//写模式#defineTH0x64//设置温度上限100#defineTL0x8a//设置温度下限-10#defineMatchTemp0x7f//写温度匹配寄存器,12bit#defineConverTem0x44//DS18B20温度转换命令#defineGet_Value0xbe//读取温度寄存器值charDelayMs=0;voidport_initvoid{DDRD=0xff;PORTD=0xff;DDRB=0xff;PORTB=0xff;DDRC=0xff;PORTC=0xff;}//TIMER1initialize-prescale:1//WGM:0Normal,TOP=0xFFFF//desiredvalue:15uSec//actualvalue:14.875uSec0.8%voidtimer1_initvoid{TCCR1B=0x00;//stopTCNT1H=0xFF;//setupTCNT1L=0x89;OCR1AH=0x00;OCR1AL=0x77;OCR1BH=0x00;OCR1BL=0x77;ICR1H=0x00;ICR1L=0x77;TCCR1A=0x00;TCCR1B=0x01;//startTimer}#pragmainterrupt_handlertimer1_ovf_isr:9voidtimer1_ovf_isrvoid{//TIMER1hasoverflowedTCNT1H=0xFF;//reloadcounterhighvalueTCNT1L=0x89;//reloadcounterlowvalueifDelayMs>0{DelayMs--;}}voidDelay_15usunsignedintn/*---Ms延时函数---*/{DelayMs=n;whileDelayMs>0;}/****************************************************************功能:主机向总线写0****************************************************************/voidWritr0void{Set_DQ1;Set_DQ0;Delay_15us5;Set_DQ1;Delay_15us1;}/****************************************************************功能:主机向总线写1****************************************************************/voidWritr1void{Set_DQ1;Set_DQ0;asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";Set_DQ1;Delay_15us2;}/****************************************************************功能:向总线写一个字节**输入:需要写的字****************************************************************/voidWriteBitscharByte{chari=0;fori=0;i>=1;}}/****************************************************************功能:DS18B20复位程序**返回:总线复位成功,返回1****************************************************************/charReset1820void{staticcharCheckTimes=0;staticcharCheckValue=1;Delay_nms1;Set_DQ1;Set_DQ0;//拉低总线480usDelay_15us34;Set_DQ1;DDRC&=0xf7;//设置端口为输入状态,读取数据PORTC|=0x08;whilePINC&0x08;//等待,直至确认复位成功CheckValue=Read_DQ;Delay_15us32;returnCheckValue;}voidInit1820void{ifReset1820==0x08{LCD_Write_String0,0,"1820NotDetect!";LCD_Write_String0,1,"";}else{LCD_Write_String0,0,"InitDS18B20OK!";LCD_Write_String0,1,"";}WriteBitsMatchROM;WriteBitsWriteMode;WriteBitsTH;WriteBitsTL;WriteBitsMatchTemp;}/****************************************************************功能:从总线中读取数据位**返回:读取值****************************************************************/charReadBitvoid{chari=0;charRead_Value=0;Set_DQ1;Set_DQ0;asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";Set_DQ1;asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";asm"nop";DDRC&=0xf7;//设置端口为输入状态,读取数据PORTC|=0x08;asm"nop";Read_Value=Read_DQ;Delay_15us3;returnRead_Value;}/****************************************************************功能:从总线读取一个字节**返回:读取到的字符****************************************************************/charReadBitsvoid{chari=0;charb=0;charReadBits_Value=0;fori=8;i>0;i--{ReadBits_Value=ReadBits_Value>>1;b=ReadBit;ifb{ReadBits_Value=ReadBits_Value|0x80;}}//DEC_Num_Disp0,1,ReadBits_Value,5;returnReadBits_Value;}/****************************************************************功能:获得温度值**返回:温度值--摄氏温度****************************************************************/charGet_Temperaturevoid{staticintTempLow=0;staticintTempHi=0;staticcharTemp2=0;staticlongfinal=0;staticSign=0;//温度的符号位Reset1820;WriteBitsMatchROM;WriteBitsConverTem;//Delay_15us6;Reset1820;WriteBitsMatchROM;WriteBitsGet_Value;Delay_15us5;TempLow=ReadBits;//温度低位Delay_15us5;Temp2=ReadBits;Sign=Temp2&0xf8;//符号位取高5位TempHi=Temp2&0x07;//温度高位ifSign==0//正温的数据处理{final=TempHi=37400{LCD_Write_String10,1,"Fever!";}iffinal36000{LCD_Write_String10,1,"Normal";}iffinal<=36000{LCD_Write_String10,1,"Low!";}Point_Disp1,1,final,6,3;returnfinal;}//callthisroutinetoinitializeallperipheralsvoidinit_devicesvoid{//stoperrantinterruptsuntilsetupCLI;//disableallinterruptsport_init;timer1_init;LCD_Init;LCD_Write_Char0x01,0;//清屏MCUCR=0x00;GICR=0x00;TIMSK=0x04;//timerinterruptsourcesSEI;//re-enableinterrupts//allperipheralsarenowinitialized}voidmainvoid{init_devices;Delay_nms5;LCD_Write_String0,0,"1820NotDetect!";LCD_Write_String0,1,"Resetaftersure";Init1820;Delay_nms100;LCD_Write_String0,0,"BodyTemp.Test:";LCD_Write_String0,1,"C";while1{Get_Temperature;}。
黄盱宁2020-12-08 06:59:01.0