用的是内部8M的时钟。是不是哪里时序不对? 请各位大侠指点
#include "msp430g2553.h"//声明库
/*************************ds18b20程序**************************/
unsigned int tvalue=0;//温度值
void delay_10us(unsigned int t)
{
unsigned int i;
for(i=0;i
__delay_cycles(80);
}
void ds1820rst()/*ds1820复位*/
{
//unsigned char x=0;
//DQ = 1; //DQ复位
P1DIR |=BIT4;
P1OUT |=BIT4;
//delay_18B20(4); //延时
delay_10us(1);
//DQ = 0; //DQ拉低
P1OUT &=~BIT4;
//delay_18B20(100); //精确延时大于480us
delay_10us(50);
//DQ = 1; //拉高
P1OUT |=BIT4;
//delay_18B20(40);
delay_10us(4);
}
unsigned char ds1820rd()/*读数据*/
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
//DQ = 0; //给脉冲信号
P1DIR |=BIT4;
P1OUT &=~BIT4;
dat>>=1;
//DQ = 1; //给脉冲信号
P1OUT |=BIT4;
P1DIR &=~BIT4;
if((P1IN&BIT4)==1)
{
dat|=0x80;
}//第一位拉高
//delay_18B20(10);
else
{
;
}
delay_10us(4);
}
return(dat);
}
void ds1820wr(unsigned char wdata)/*写数据*/
{
unsigned char i=0;
for (i=8; i>0; i--)
{
//DQ = 0;
P1DIR |=BIT4;
P1OUT &=~BIT4;
//DQ = wdata&0x01;//前7位拉低
if((wdata&0x01)==0)
{
P1OUT &=~BIT4;
}
else
{
P1OUT |=BIT4;
}
//delay_18B20(10);
delay_10us(4);
//DQ = 1;
P1OUT |=BIT4;
wdata>>=1;
}
}
unsigned int read_temp()/*读取温度值并转换*/
{
unsigned char a,b;
ds1820rst();
ds1820wr(0xcc);//*跳过读序列号*/
ds1820wr(0x44);//*启动温度转换*/
ds1820rst();
ds1820wr(0xcc);//*跳过读序列号*/
ds1820wr(0xbe);//*读取温度*/
a=ds1820rd();
b=ds1820rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
//if(tvalue<0x0fff)
//tflag=0;
//else
// {
//tvalue=~tvalue+1;
//tflag=1;
//}
tvalue=tvalue*(0.625);//温度值扩大10倍,精确到1位小数
return(tvalue);
}
程序是由之前51单片机移植过来的。51里驱动是没问题的,实物也制作成功的。
今天我也在做这个
读出来的数据是0
有点郁闷
#include
#define DQ1 P2OUT |= BIT4
#define DQ0 P2OUT &= ~BIT4
void delayms(unsigned int z) {
unsigned int x;
for (x = z; x > 0; x--) {
__delay_cycles(1000);
}
}
//写指令
void write_com(unsigned char com) {
P2OUT &= ~BIT1;
P1OUT = com;
delayms(5);
P2OUT |= BIT3;
delayms(5);
P2OUT &= ~BIT3;
}
//写数据
void write_data(unsigned char myData) {
P2OUT |= BIT1;
P1OUT = myData;
delayms(5);
P2OUT |= BIT3;
delayms(5);
P2OUT &= ~BIT3;
}
void write_string(char string[]){
int i = 0;
while(string != '\0'){
write_data(string);
i++;
}
}
void lcd_clean() {
write_com(0x01); //清屏
}
void init_lcd() {
P1DIR = 0xff;
P2DIR = 0xff;
P2OUT &= ~BIT2;
P2OUT &= ~BIT3; //写数据模式
write_com(0x38); //设置模式
write_com(0x0c); //后三位分别为 显示 光标 闪烁
write_com(0x06); //后两位分别为 光标移动(左右) 屏幕移动 (动或不动)
lcd_clean();
write_com(0x80);
}
void show(unsigned char run)
{
char string[] = "TEMPERATURE:";
write_string(string);
write_com(0x80+0x40);
if(run == 0){
write_string("error");
}
}
void showTemperature(unsigned int t)
{
unsigned char num1,num2;
num1 = t / 10;
num2 = t %10;
write_data(num1+48);
write_data(num2+48);
}
unsigned char init_18B20(void)
{
unsigned char Error = 0;
DQ0;
__delay_cycles(500);
DQ1;
__delay_cycles(60);
P2DIR &=~ BIT4;
_NOP();
if(P2IN & BIT4)
{
Error = 0; //初始化失败
P2DIR |= BIT4;
}
else
{
Error = 1; //初始化成功 P2DIR |= BIT4;DQ1;
}
__delay_cycles(400);
return Error;
}
unsigned char read_18B20(void)
{
unsigned char i;
unsigned char temp = 0;
P2DIR |= BIT4;
for(i = 0;i < 8;i++)
{
temp >>= 1;
DQ0;
__delay_cycles(1);
DQ1;
P2DIR &= ~BIT4;
_NOP();
if(P2IN & BIT4)
temp |= 0x80;
__delay_cycles(45); //延时45us
}
return temp;
}
void write_18B20(unsigned char wdata)
{
unsigned char i;
P2DIR |= BIT4;
for(i = 0; i < 8;i++)
{
DQ0;
__delay_cycles(10); //延时10us
if(wdata & 0X01)
DQ1;
else
DQ0;
__delay_cycles(40); //延时50us
DQ1;
__delay_cycles(2); //延时10us
wdata >>= 1;
}
}
unsigned int read_Temperature(void)
{
unsigned int tem;
union
{
char c[2];
int x;
} temp;
while(init_18B20() == 0);
write_18B20(0xCC);
write_18B20(0x44);
delayms(1000);
while(init_18B20() == 0);
write_18B20(0xCC);
write_18B20(0xBE);
//读取温度数据
temp.c[0]=read_18B20();
temp.c[1]=read_18B20();
tem=(unsigned int)(temp.x*0.0625);
return tem;
}
void main() {
WDTCTL = WDTPW + WDTHOLD;
if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF) {
while (1); // If cal const erased, 挂起
}
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation*/
BCSCTL2 = SELM_1 + DIVM_0;
TACTL |= TASSEL_2;
init_lcd();
show(init_18B20());
unsigned int t;
while (1)
{
t = read_Temperature();
showTemperature(t);
delayms(1000);
}
}
哥们调试出来分享一下,谢谢!我也调了好长时间,没有结果!
哥们调试出来分享一下,谢谢!我也调了好长时间,没有结果!qq:963295396,谢谢!