DIY编程器网

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 127|回复: 0
打印 上一主题 下一主题

DS18B20 MSP430 C程序

[复制链接]
跳转到指定楼层
楼主
发表于 2011-4-30 13:25:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
DS18B20 MSP430程序
MSP430高级开发板上的DS18B20温度传感器的程序

MSP430高级开发板: >>点击进入

欢迎转载本站程序 请注明来自于www.just430.cn
/**********************************************************/
//MSP430 Advanced Developping Components - [DS18B20_430.C]
////DS18B20 One-Wire Digital Thermometer Demos
////->>> http://www.just430.cn
////->>> tel 0)13500717874
/**********************************************************/
//MSP430高级实验开发组件 - DS18B20单线温度传感器示例程序
//硬件连接:
// MSP430 Broad

DS18B20
// ------------------ ------------------
// | P23 | ------> | DQ |
// ------------------ ------------------
//当前演示程序功能描述:
//循环进行温度检测,将结果分别保存在
//ResultSignal温度符号(0x01为负)
//ResultTemperatureH温度整数
//ResultTemperatureLH温度小数(高两位)
//ResultTemperatureLL温度小数(低两位)
//如果系统存在ZLG7289键盘显示模块,则可以显示温度
//**更多资料参考DS18B20数据手册以及ZLG7289_430.C
/*********************************************************/
#include
#include "ZLG7289_430.C"
#define DS18B20_DIRP2DIR
#define DS18B20_INP2IN
#define DS18B20_OUTP2OUT
#define DS18B20_DQBIT3//定义DS18B20的接口
// DS18B20 ROM命令宏定义
#define ReadRom0x33
#define MatchRom0x55
#define SearchRom0xf0
#define AlertSearch0xec
#define CopyScratchpad0x48
#define SkipRom0xcc
#define ReadPower0xb4
// DS18B20 功能命令宏定义
#define ConvertTemperature0x44
#define ReadScratchpad0xbe
#define WriteScratchpad0x4e
#define RecallE0xb8
#define SMCLK4000//(KHz)用于
系统延时



// 温度的十进制编码(查表用)
unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
// 变量定义
unsigned char GetScratchpad[9];
unsigned char ResultTemperatureH;//温度的整数部分
unsigned char ResultTemperatureLH;//温度的小数部分(高位)
unsigned char ResultTemperatureLL;//温度的小数部分(低位)

void DS18B20_WriteBit(unsigned char oww_dat);
void DS18B20_WriteByte(unsigned char oww_dat);
void DS18B20_R

eadTemp(void);
unsigned char DS18B20_Init(void);
unsigned char DS18B20_ReadBit(void);
unsigned char DS18B20_ReadByte(void);
void Delay10us(void);
void DelayX10us(unsigned char x10us);
/****************************
void main(void) {
volatile unsigned int i,j;
WDTCTL = WDTPW + WDTHOLD;//停止看门狗

BCSCTL1 &= "XT2OFF;//XT2 = HF XTAL
do {
IFG1 &= "OFIFG;//Clear OSCFault flag
for (i=0xFF;i>0;i--);//Time for flag to set
}while((IFG1&OFIFG));//OSCFault flag still set?
BCSCTL2 |= (SELM1 + SELS);//MCLK = SMCLK = XT2

DS18B20_Init();//初始化DS18B20

Z7289_Init();

while(1) {
DS18B20_ReadTemp();
i=ResultTemperatureH/10;
j=ResultTemperatureH-(i*10);
Z7289_Show(7,1,i);
Z7289_Show(6,1,j);
Z7289_Show(5,1,0x0C);
i=ResultTemperatureLH/10;
j=ResultTemperatureLH-(i*10);
Z7289_Show(4,1,i);
Z7289_Show(3,1,j);
i=ResultTemperatureLL/10;
j=ResultTemperatureLL-(i*10);
Z7289_Show(2,1,i);
Z7289_Show(1,1,j);
}
}
****************************/
// 功能函数定义
unsigned char DS18B20_Init(void){
unsigned char result;
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= "DS18B20_DQ;&
sp; // DS18B20_DQ=0;
DelayX10us(48); // Bus master pulling low 480us minimum;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
DelayX10us(6); // Resister pull up 15-60us;
DS18B20_DIR &= "DS18B20_DQ; // ow input
result = DS18B20_IN & DS18B20_DQ;
DS18B20_DIR |= DS18B20_DQ;

// ow output
DelayX10us(48); // End of timeslot total 480us;
return(result); // any 1 wire device ?result:=1 no devide; ?result:=0 have device;
}//Intialization the 1-wire devices;

unsigned char DS18B20_ReadBit(void){
unsigned char result;
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= "DS18B20_DQ; // DS18B20_DQ=0;
_NOP(); // Start of timeslot;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
_NOP();_NOP();_NOP();_NOP();
// Wait from the start;
DS18B20_DIR &= "DS18B20_DQ; // ow input
result = DS18B20_IN & DS18B20_DQ;
DS18B20_DIR |= DS18B20_DQ; // ow output
return(result); // return the result of the 1-wire devide;
}//Read a bit on the 1-wire bus;
void DS18B20_WriteBit(unsigned char oww_dat){
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= "DS18B20_DQ; // DS18B20_DQ=0;
if (1 == oww_dat)
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
DelayX10us(10);
// Remain the state for 100us;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
}//Write a bit to the 1-wire bus;

unsigned char DS18B20_ReadByte(void){
unsigned char i;
unsigned char result="0";
for(i = 0; i [/td][/tr][/table]ult); // return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
void DS18B20_WriteByte(unsigned char oww_dat){
unsigned char i,temp;
for(i = 0; i > i;
temp &= 0x01;
DS18B20_WriteBit(temp);
}
DelayX10us(7); // ??
}//Write a byte to the 1-wire bus;

void DS18B20_ReadTemp(void)
{unsigned char tempH,tempL;
DS18B20_Init();
DS18B20_WriteByte(SkipRom);
_NOP();
//There is just one DS1820 on the bus;
DS18B20_WriteByte(ConvertTemperature);
DelayX10us(10);
//Start to convert temperature;
DS18B20_Init();
DS18B20_WriteByte(SkipRom);
_NOP();
DS18B20_WriteByte(ReadScratchpad);
GetScratchpad[0]=DS18B20_ReadByte();
//Master samples the LSB temperature from the scratchpad;
GetScratchpad[1]=DS18B20_ReadByte();
//Master samples the MSB temperature from the scratchpad;
GetScratchpad[2]=DS18B20_ReadByte();
//Master samples the Th register or userbyte1 from the scratchpad;
GetScratchpad[3]=DS18B20_ReadByte();
//Master samples the Tl register or userbyte0 from the scratchpad;
GetScratchpad[4]=DS18B20_ReadByte();
//Master samples the configuration register from the scratchpad;
GetScratchpad[5]=DS18B20_ReadByte();
//Master sa
mples the reservedbyte from the scratchpad;
GetScratchpad[6]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[7]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[8]=DS18B20_ReadByte();
//Master samples the CRC from the scratchpad;
tempH=(GetScratchpad[1] > 4);
tempL=(GetScratchpad[0] & 0x0f);
DS18B20_Init();
&nbs

p;//Issue a reset to terminate left parts;
//if(tempH & 0x80)
//{tempH="tempH;
//tempL="tempL+1;
//ResultSignal=1;
////Negative temperature;
//}
ResultTemperatureH=tempH;
ResultTemperatureLL=decimalL[tempL];
ResultTemperatureLH=decimalH[tempL];
//Result of temperature;
}//Read the byte0 and byte1 from scratchpad;


void Delay10us(void){
unsigned char i;
for (i=0; i<(SMCLK/500-3); i++);
}
// Time is accurately !!
void DelayX10us(unsigned char x10us){
unsigned int i;
for (i=0; i<X10US; i++)
Delay10us();
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|文字版|手机版|DIY编程器网 ( 桂ICP备14005565号-1 )

GMT+8, 2024-9-27 20:13 , 耗时 0.088458 秒, 19 个查询请求 , Gzip 开启.

各位嘉宾言论仅代表个人观点,非属DIY编程器网立场。

桂公网安备 45031202000115号

DIY编程器群(超员):41210778 DIY编程器

DIY编程器群1(满员):3044634 DIY编程器1

diy编程器群2:551025008 diy编程器群2

QQ:28000622;Email:libyoufer@sina.com

本站由桂林市临桂区技兴电子商务经营部独家赞助。旨在技术交流,请自觉遵守国家法律法规,一旦发现将做封号删号处理。

快速回复 返回顶部 返回列表