|
//*******************************************************************************
// MSP-FET430X110 LCD_KEY DEMO SOFTWARE
//
// MSP430F149
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P3.2|<-->DS18B20 DATA
// | |
// vss|--共地
// 2003年1月17日 DS18B20要求I/O口位双向的,MSP430实现较烦琐
//******************************************************************************
#include "msp430x14x.h"
void init(void);
float get_temp(void);
void reset_18B20(void); //reset 18B20
void get_respond(void); //get respond
void send_18B20(char data); //send command
char read_18B20(void); //得到温度值 存于temp
void delay_time(int time_data); //按照指定的参数延时函数
char flag,temp_count;
char i=0;
char id[8]; //序号
unsigned char ta××e[17]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a};
float temperate;
void main(void)
{
// TACTL = TASSEL1 + TACLR; // SMCLK, clear TAR
// CCTL0 = CCIE; // CCR0 interrupt ena××ed
// CCR0 = 800;
init();
// _EINT();
// delay_time(200);
while(1)
{
// disp_time();
// key_read();
// key_resp();
temperate=get_temp();
_NOP();
// disp_temp();
// }
}
}
//interrupt[TIMERA0_VECTOR] void Timer_A (void)
//{
//}
void init(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
DCOCTL=0X60;
BCSCTL1=0X84;
BCSCTL2=0X00;
P3DIR |=0x04; //P3.2OUTPUT
P3OUT |=0x04;
flag=0;temp_count=0;
}
float get_temp(void)
{char data;
int time;
char temp_high,temp_low; //温度值
// float temperate;
//------------------- get id 没有实现,原因未知
reset_18B20();
get_respond();
data=0xf0; //
send_18B20(data);
data=0x33; //read scratchpad
send_18B20(data);
for (i=0;i<=7;i++) //scratchpad structure: look P6
{ id[i]=read_18B20(); //configuration:[ 0 r1 r0 1 1 1 1 1 ] r1,r0代表温度精度
}
//--------------------get scratchpad
/*
reset_18B20();
get_respond();
data=0xcc; //skip rom
send_18B20(data);
data=0xbe; //read scratchpad
send_18B20(data);
for (i=0;i<=7;i++) //scratchpad structure: look P6
{ id[i]=read_18B20(); //configuration:[ 0 r1 r0 1 1 1 1 1 ] r1,r0代表温度精度
}
*/
//-------------------begin measure
reset_18B20(); //经过测试,1ms执行WHILE 循环约75次,以次为基础延时
get_respond();
data=0x0CC;
send_18B20(data); //send CCH,Skip ROM command
data=0x44;
send_18B20(data); //启动在线DS18B20作温度A/D转换
time=60000; //750*50=56250
delay_time(time); //750 ms
//-------------------get temperature
reset_18B20();
get_respond();
data=0x0CC;
send_18B20(data); //send CCH,Skip ROM command
data=0x0be;
send_18B20(data); //从暂存器读12位温度值和CRC值
temp_low=read_18B20(); //得到温度值 存于temp
temp_high=read_18B20();
return (temp_high*16+temp_low/16);
}
void reset_18B20(void) //reset 18B20
{
P3DIR |=0X04; //SET P3.2 OUTPUT
P3OUT |=0X04; //setb
delay_time(50);
P3OUT &=0Xfb; //CLEAR
delay_time(40); //480~960us
P3OUT |=0x04; //SETB
}
void get_respond(void) //get respond
{
P3DIR |=0X04; //SET P3.2 OUTPUT
P3OUT |=0X04; //SETB P3.2
// P2DIR &=0Xfb;
delay_time(70); //简单起见 没有判断
// while ((P2IN>>2 &0X01)==0)
// break;
/* while((P2IN &0X04)==0 )
break;
delay_time(5000);
while((P2IN &0X04)==1 )
break;
delay_time(5000);
*/
}
void send_18B20(char data) //send command
{
char val,i,j;
val=data;
P3DIR |=0X04; //SET P3.2 OUTPUT
i=val;
// P2OUT |=0x04; //setb P3.2
// delay_time(50); //无用
P3OUT &=0xfb; //clear P3.2
delay_time(0); // >1us
P3OUT |=(i<<2) & 0x04; //send bit
delay_time(5); //60~120us
P3OUT |=0x04; //setb P3.2
delay_time(1); // >1us
for (j=1;j<=7;j++)
{
val=val>>1;
i=val;
P3OUT &=0xfb; //clear P3.2
// delay_time(0); //改为1则无法采集数据
//把上句注释掉,149程序才好用
P3OUT |=(i<<2) & 0x04; //send bit
delay_time(5);
P3OUT |=0x04; //setb P3.2
delay_time(1); // >1us
}
}
char read_18B20(void) //得到温度值 存于temp
{
char data,val;
char j;
data=0;
// for (i=1;i<=2;i++)
P3DIR |=0X04; //SET P3.2 OUTPUT
P3OUT |=0X04; //setb P3.2
delay_time(100);
for (j=1;j<=8;j++)
{
data=data>>1;
P3OUT &=0Xfb; //clear
// delay_time(2); //绝对不能延时
P3OUT |=0X04; //SETB P3.2
P3DIR &=0Xfb; //CHANGE INPUT MODE
// delay_time(1); //3us
val =(P3IN &0x04)<<5; //左移五位到最高位
data |=val;
//After we read the current bit,we should pull-up the I/O pin,
// further more we should give a delay at least 60us
P3DIR |=0X04; //SET P3.2 OUTPUT
P3OUT |=0X04; //setb P3.2
delay_time(5); //60us
}
return data;
}
void delay_time(int time_data) //按照指定的参数延时函数
{
int i;
i=time_data;
while(i!=0)
i--;
} |
|