#include #include
#define uchar unsigned char #define uint unsigned int
#define DQ_1 P2OUT |= BIT3 //DS18B20数据脚接P2.3,LaunchPad上TXD、RXD跳线帽,由“‖”接改为“〓”。用USB连接电脑后就可用超级终端看到温度了。
#define DQ_0 P2OUT &= ~BIT3 #define DQ_in P2DIR &= ~BIT3 #define DQ_out P2DIR |= BIT3 #define DQ_val (P2IN & BIT3)
#define Read_ROM 0x33 //读ROM #define Match_ROM 0x55 //匹配ROM #define Skip_ROM 0xcc //跳过ROM #define Search_ROM 0xf0 //搜索ROM #define Alarm_Search 0xec //告警搜索
#define Convert_Temperature 0x44 //温度转换
#define Read_Scratchpad 0xbe //读暂存存储器9字节内容 #define Write_Scratchpad 0x4e //写暂存存储器,写的是TH and TL , 接着发送两位数据就可以
unsigned int Check_val; //初始化检测变量 unsigned int Temp;//温度整数值 void UartPutchar(unsigned char c); unsigned char UartGetchar();
unsigned int DS18b20_init(void) { DQ_out; DQ_0;
__delay_cycles(600); DQ_1;
__delay_cycles(60); DQ_in; _NOP(); if(DQ_val) {
Check_val = 0; //初始化失败 } else {
Check_val = 1; //初始化成功 }
__delay_cycles(10); DQ_out; DQ_1;
__delay_cycles(100); return Check_val; }
void DS18b20_write_byte(unsigned int dat) {
unsigned int i; for(i = 0; i < 8;i++) {
DQ_0;
__delay_cycles(2); if(dat & 0X01) DQ_1; else DQ_0;
__delay_cycles(60); dat >>= 1;; DQ_1;
__delay_cycles(10); } }
unsigned int DS18b20_read_byte(void) {
unsigned i;
unsigned int byte = 0; for(i = 0;i < 8;i++) {
byte >>= 1; DQ_0;
__delay_cycles(2); DQ_1;
__delay_cycles(2); DQ_in; _NOP(); if(DQ_val) byte |= 0x80; __delay_cycles(60); DQ_out; DQ_1;
__delay_cycles(10); }
return byte; }
unsigned int get_one_temperature(void) {
unsigned int Temp_l; unsigned int Temp_h; unsigned int t; float tt; DS18b20_init();
DS18b20_write_byte(Skip_ROM);
DS18b20_write_byte(Convert_Temperature); __delay_cycles(1000000);
DS18b20_init();
DS18b20_write_byte(Skip_ROM); DS18b20_write_byte(Read_Scratchpad); Temp_l=DS18b20_read_byte(); Temp_h=DS18b20_read_byte(); t=Temp_h; t<<=8; t=t|Temp_l; tt=t*0.0625;
t= tt*10+0.5; //放大10倍输出并四舍五入 return(t); }
#define DataDir P1DIR
#define DataPort P1OUT #define Busy 0x80 #define CtrlDir P2DIR
#define CLR_RS P2OUT&=~BIT0; //RS = P3.0 #define SET_RS P2OUT|=BIT0;
#define CLR_RW P2OUT&=~BIT1; //RW = P3.1 #define SET_RW P2OUT|=BIT1;
#define CLR_EN P2OUT&=~BIT2; //EN = P3.2 #define SET_EN P2OUT|=BIT2; void De
layNus(unsigned int n) {
CCR0 = n;
TACTL |= MC_1; //增计数到CCR0 while(!(TACTL & BIT0)); //等待 TACTL &= ~MC_1; //停止计数 TACTL &= ~BIT0; //清除中断标志 }
void Delay5ms(void) {
//unsigned int i; //i=40000; //while (i != 0) // { // i--; // }
DelayNus(5000); }
void WaitForEnable(void)
{
P1DIR &= 0x00; //将P1口切换为输入状态 CLR_RS; SET_RW; _NOP(); SET_EN; _NOP(); _NOP();
while((P1IN & Busy)!=0); //检测忙标志 CLR_EN;
P1DIR |= 0xFF; //将P4口切换为输出状态 }
void write_com(unsigned char cmd) {
WaitForEnable(); // 检测忙信号? CLR_RS; CLR_RW; _NOP();
DataPort =cmd ; //将命令字写入数据端口 _NOP();
SET_EN; //产生使能脉冲信号 _NOP(); _NOP(); CLR_EN; }
void write_data( unsigned int data )
{
WaitForEnable(); //等待液晶不忙 SET_RS; CLR_RW; _NOP();
DataPort = data; //将显示数据写入数据端口 _NOP();
SET_EN; //产生使能脉冲信号 _NOP(); _NOP(); CLR_EN; }
void zifuchuan(unsigned int *ch) {
while(*ch!=0) write_data(*ch++); Delay5ms(); }
void LcdReset(void) {
CtrlDir |= 0x07; //控制线端口设为输出状态 DataDir = 0xFF; //数据端口设为输出状态 write_com(0x38); //显示模式设置
write_com(0x0c); //显示开,不开游标,不闪烁 write_com(0x06); //写字符时整体不移动 write_com(0x01); //显示清屏 __delay_cycles(200); }
/************************************************************* * 名 称:void dis_temp(uint t)
* 功 能:分出十位、个位等 * 入口参数:t * 出口参数:无 * 说 明: 送到1602显示
*************************************************************/ void main(void) {
unsigned int a,b,c,d; // BCSCTL1|=DIVA_0;
WDTCTL=WDTPW+WDTHOLD;
// WDTCTL = WDT_ADLY_1000; // Stop watchdog timer // IE1 |=WDTIE;
P1DIR = 0XFF;P1OUT = 0XFF; P2DIR = 0XFF;P2OUT = 0XFF;
BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0; //dco不分频用作mclk,不分频默认用作smclk
if (CALBC1_1MHZ != 0xFF) {
DCOCTL = 0x00;
BCSCTL1 = CALBC1_1MHZ; /* Set DCO to 1MHz */ DCOCTL = CALDCO_1MHZ; }
BCSCTL1 |= XT2OFF + DIVA_0; LcdReset(); while(1) {
a=get_one_temperature(); b=a/100; c=(a%100)/10; d=a%10;
P2SEL&=~BIT6; if(a>300) {
P2DIR|=BIT5; P2OUT|=BIT5; P2DIR|=BIT6; P2OUT|=BIT6; } else {
P2DIR|=BIT5; P2OUT&=~BIT5; P2DIR|=BIT6; P2OUT&=~BIT6; }
write_com(0x80+0x05); write_data(b+0x50); write_data(c+0x50); write_data(0x4e); write_data(d+0x50);
write_data(0xbf); //显示温度的小圈 write_data(0x23); //__delay_cycles (); } }
/*pragma vector=WDT_VECTOR __interrupt void watchdog_timer(void)
{
_BIS_SR_IRQ(LPM3_bits); }*/
因篇幅问题不能全部显示,请点此查看更多更全内容