MSP430與DS18B20的C程序
發(fā)布時(shí)間:2008-10-22 21:53
發(fā)布者:
MSP430
#include
//#define DELTA 256 // target DCO = DELTA*(4096) = 1048576
#define DELTA 244 // target DCO = DELTA*(4096) = 1048576
unsigned char err_flag=0;
//unsigned char result=0;
//unsigned char result1=0;
unsigned int result=0;
unsigned int result1=0;
void Set_DCO (void);
void delay(unsigned int dd);
void delay1(unsigned char dd1);
void reset(void);
void write_command(unsigned char command);
//unsigned char read_data(void);
unsigned int read_data(void);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
Set_DCO(); // Set DCO
/*P1DIR |= 0x01; // Set P1.0 to output direction
for (;
{
P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR
delay(250000);
}*/
P1OUT |= BIT4;
P1DIR |= BIT4;
while(1)
{
reset();
write_command(0xcc);
write_command(0x44);
delay1(6);
reset();
write_command(0xcc);
write_command(0xBE);
result = read_data();
reset();
write_command(0xcc);
write_command(0xBE);
result1 = read_data();
_NOP();
result = 0;
}
}
void reset(void)
{
P1OUT &= ~BIT4;
delay(150);
P1DIR &= ~BIT4;
//************************
/*delay(3);
delay(15);
if(P1IN&BIT4)
{err_flag = 1;}
else err_flag =0;*/
//*****************************
delay(9);
if(P1IN&BIT4)
{err_flag = 1;}
else err_flag =0;
delay(125);
P1OUT |= BIT4;
P1DIR |= BIT4;
}
void write_command(unsigned char command)
{
unsigned char i;
for(i=0;i<8;i++)
{
P1OUT &= ~BIT4;
if(command&0x01)
{
//delay(2);
_NOP();
_NOP();
_NOP();
_NOP();
//P1OUT |= BIT4;
P1DIR &= ~BIT4;
}
command=command>>1;
//delay(15);
delay(20);
//P1OUT |= BIT4;
P1DIR &= ~BIT4;
delay(3);
P1OUT |= BIT4;
P1DIR |= BIT4;
}
}
//unsigned char read_data(void)
unsigned int read_data(void)
{
//unsigned char data=0,i;
unsigned int data=0,i;
//data = 0;
for(i=0;i<8;i++)
//for(i=0;i<16;i++)
{
P1OUT &= ~BIT4;
_NOP();
_NOP();
_NOP();
P1DIR &= ~BIT4;
_NOP();
if(P1IN&BIT4)
//{data |= 0x80;}
{data |= 0x0001;}
data = data<<1;
delay(23);
P1OUT |= BIT4;
P1DIR |= BIT4;
}
return data;
}
void delay(unsigned int dd)
{
unsigned int i;
for(i=0;i
}
void delay1(unsigned char dd1)
{
unsigned char i;
for(i=0;i
{
delay(50000);
}
}
//------------------------------------------------------------------------------
void Set_DCO (void) // Set DCO to selected frequency
//------------------------------------------------------------------------------
{
//#define DELTA 900 // target DCO = DELTA*(4096) = 3686400
//#define DELTA 256 // target DCO = DELTA*(4096) = 1048576
//#define DELTA 64 // target DCO = DELTA*(4096) = 262144
unsigned int Compare, ldcapture = 0;
BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
CCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK
TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear
while (1)
{
while (!(CCIFG & CCTL2)); // Wait until capture occured
CCTL2 &= ~CCIFG; // Capture occured, clear flag
Compare = CCR2; // Get current captured SMCLK
Compare = Compare - Oldcapture; // SMCLK difference
ldcapture = CCR2; // Save current captured SMCLK
if (DELTA == Compare) break; // If equal, leave "while(1)"
else if (DELTA < Compare) // DCO is too fast, slow it down
{
DCOCTL--;
if (DCOCTL == 0xFF) BCSCTL1--; // Did DCO role under?, Sel lower RSEL
}
else
{
DCOCTL++;
if (DCOCTL == 0x00) BCSCTL1++; // Did DCO role over? Sel higher RSEL
}
}
CCTL2 = 0; // Stop CCR2
TACTL = 0; // Stop Timer_A
BCSCTL1 &= ~DIVA_3;
}
|