1、DS18B20MSP4304位数码管显示温度DS18B20 MSP430 4位数码管显示温度/实验材料:MSP430F149开发板,DS18B20一个,杜邦线15条,3元普通4位共阴极数码管一个/由于本人没有排阻,没有采用灌电流方式给4位共阴极数码管供电,MSP430的I/O口直接接4位共阴极数码管的12个脚/接线说明:DS18B20数据口接P17,电源接3.3V直流;P40P47接数码管a、b、c、d、e、f、g、dp/ P50P53接位1、位2、位3,位4,数码管接线图请XX/Made By Zhong Sir From Wuhan University#include /延时函数#def
2、ine CPU_F (double)8000000) /外部高频晶振8MHZ/#define CPU_F (double)32768) /外部低频晶振32.768KHZ#define delay_us(x) _delay_cycles(long)(CPU_F*(double)x/1000000.0) #define delay_ms(x) _delay_cycles(long)(CPU_F*(double)x/1000.0) /自定义数据结构#define uchar unsigned char#define uint unsigned int#define ulong unsigned lo
3、ng/DS18B20控制脚,单脚控制#define DQ_IN P1DIR &= BIT7 / DS18B20接单片机P17口#define DQ_OUT P1DIR |= BIT7 /设置输出#define DQ_CLR P1OUT &= BIT7 /置低电平#define DQ_SET P1OUT |= BIT7 /置高电平#define DQ_R P1IN & BIT7 /读电平int temp_value;int temp; /定义的变量,显示数据处理int A1,A2,A3;/*/ 系统时钟初始化,外部8M晶振/*void Clock_Init() uchar i; BCSCTL1&
4、=XT2OFF; /打开XT2振荡器 BCSCTL2|=SELM1+SELS; /MCLK为8MHZ,SMCLK为8MHZ do IFG1&=OFIFG; /清楚振荡器错误标志 for(i=0;i480uS) if (i) return 0x00; else return 0x01; /* / DS18B20读一个字节函数/* unsigned char ds1820_read_byte(void) unsigned char i; unsigned char value = 0; for (i = 8; i != 0; i-) value = 1; DQ_OUT; DQ_CLR; delay
5、_us(4); /*延时4uS DQ_SET; DQ_IN; delay_us(10); /*延时10uS if (DQ_R) value|=0x80; delay_us(60); /*延时60uS return(value);/* / 向18B20写一个字节函数/* /*DS18B20字节写入函数*/void ds1820_write_byte(unsigned char value) unsigned char i; for (i = 8; i != 0; i-) DQ_OUT; DQ_CLR; delay_us(4); /延时4uS if (value & 0x01) DQ_SET; d
6、elay_us(80); /延时80uS DQ_SET; /位结束 value = 1; /*/ 发送温度转换命令/* /*启动ds1820转换*/void ds1820_start(void) DS18B20_Reset(); ds1820_write_byte(0xCC); /勿略地址 ds1820_write_byte(0x44); /启动转换/*/ DS8B20读取温度信息/* unsigned int ds1820_read_temp(void) unsigned int i; unsigned char buf9; DS18B20_Reset(); ds1820_write_byt
7、e(0xCC); /勿略地址 ds1820_write_byte(0xBE); /读取温度 for (i = 0; i 9; i+) bufi = ds1820_read_byte(); i = buf1; i = 8; i |= buf0; temp_value=i; temp_value=(uint)(temp_value*0.625); /不是乘以0.0625的原因是为了把小数点后一位数据也转化为可以显示的数据 return i;/*/ 温度数据处理函数/* void data_do(int temp_d) int A2t; A1=temp_d/100; /分出百,十,和个位 A2t=t
8、emp_d%100; A2=A2t/10; A3=A2t%10;/*/ 主程序/*void main(void) const unsigned char seg=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f; const unsigned char zww=0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09; uchar j; WDT_Init(); /看门狗初始化 Clock_Init(); /时钟初始化 Port_Init(); /端口初始化,用于控制IO口输入或输出 DS18B20_Re
9、set(); /复位D18B20 while(1) ds1820_start(); /启动一次转换 ds1820_read_temp(); /读取温度数值 data_do(temp_value); /处理数据,得到要显示的值 if(A1=zww0) A1=seg0; / 以下将DS18B20反馈的数值换成数码管的字符编码以显示 else if(A1=zww1) A1=seg1; else if(A1=zww2) A1=seg2; else if(A1=zww3) A1=seg3; else if(A1=zww4) A1=seg4; else if(A1=zww5) A1=seg5; else
10、if(A1=zww6) A1=seg6; else if(A1=zww7) A1=seg7; else if(A1=zww8) A1=seg8; else if(A1=zww9) A1=seg9; else A1=A1; if(A2=zww0) A2=seg0; else if(A2=zww1) A2=seg1; else if(A2=zww2) A2=seg2; else if(A2=zww3) A2=seg3; else if(A2=zww4) A2=seg4; else if(A2=zww5) A2=seg5; else if(A2=zww6) A2=seg6; else if(A2=z
11、ww7) A2=seg7; else if(A2=zww8) A2=seg8; else if(A2=zww9) A2=seg9; else A2=A2; if(A3=zww0) A3=seg0; else if(A3=zww1) A3=seg1; else if(A3=zww2) A3=seg2; else if(A3=zww3) A3=seg3; else if(A3=zww4) A3=seg4; else if(A3=zww5) A3=seg5; else if(A3=zww6) A3=seg6; else if(A3=zww7) A3=seg7; else if(A3=zww8) A3=seg8; else if(A3=zww9) A3=seg9; else A3=A3; for(j=0;j8;j+) P4OUT=A1; P5OUT=0X0E; delay_ms(0.4); P4OUT=A2+BIT7; P5OUT=0x0D; delay_ms(0.4); P4OUT=A3; P5OUT=0x0B; delay_ms(0.4); P4OUT=0x58; P5OUT=0x07; delay_ms(0.4); P4OUT=0x00; /缺少该句,数码管会有微弱重影,有该句无重影
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1