c8051f310 DS18B20 UART
时间:10-02
整理:3721RD
点击:
上位机接不到数据 求大神指点
#include <C8051F310.h>
typedef unsigned char BYTE;
BYTE TPH; //′?·????è?μμ???×??ú
BYTE TPL;
unsigned int Temper;
sbit DQ = P1^1;
void Oscillator_Init(); //ê±?ó3?ê??ˉ
void Port_Init(); //???ú3?ê??ˉ
void UART0_Init (void);
void Send_Char(unsigned char ch);
void ADC0_Init (void);
void Delayus(unsigned int us);
void Delayms(unsigned int ms);
void Init18b20 (void);
void DS18B20_WriteByte(BYTE dat); //μ¥×??úD′è?
BYTE DS18B20_ReadByte();
void TemperatuerResult(void);
#define SYSCLK 24500000
void main(void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
Oscillator_Init(); //ê±?ó3?ê??ˉ
Port_Init(); //???ú3?ê??ˉ
UART0_Init ();
ADC0_Init ();
AD0BUSY = 1;
while(1)
{
while(AD0INT==1)
{
Send_Char(ADC0H);
Send_Char(ADC0L);
AD0INT==0;
AD0BUSY = 1;
TemperatuerResult();
//Send_Char(TPL);
Send_Char(TPH);
}
}
}
void Oscillator_Init() //ê±?ó3?ê??ˉ
{
OSCICN = 0x83;//?ú2???μ′?÷ê1?ü£??μí3ê±?ó2?·??μ£?×????μ?ê24.5M
RSTSRC = 0x04;//ê1?üê±?ó?aê§?ì2a?÷
}
void Port_Init() //???ú3?ê??ˉ
{
XBR0 = 0x01; // UART0 TX and RX pins enabled
XBR1 = 0x40; //è?é?à-ê1?ü£???2??a1?ê1?ü
P0MDOUT |= 0x10; // Enable TX0 as a push-pull output P0.4
}
void UART0_Init (void)
{
SCON0 |= 0x00; //?óê??êDí
CKCON = 0x00; //?¨ê±?÷1ê1ó??μí3ê±?ó/12
TMOD = 0x20; //T11¤×÷·?ê?8??×??ˉ??×°??
TH1 = 0x96; //2¨ì??ê?a9600
TL1 = TH1;
TR1 = 1;
}
//·¢?íμ¥??×?·?
void Send_Char(unsigned char ch)
{
SBUF0 = ch; //?íè??o3???
while(TI0 == 0); //μè′y·¢?ííê
TI0 = 0; //èí?t??á?
}
void ADC0_Init (void)
{
REF0CN = 0x0A; // Enable on-chip VREF, select Vdd as
// voltage reference.
AMX0P = 0x02; // ADC0 positive input = P1.2
AMX0N = 0x1F; // ADC0 negative input = GND
// i.e., single ended mode
P1MDIN = 0xFB;
P1SKIP = 0x04;
ADC0CF = ((SYSCLK/50000)-1)<<3; // set SAR clock to 50KHz
ADC0CF |=0X00;
AD0EN = 1; // enable ADC0
}
/************************************************************
*Function:?óê±′|àí
*parameter:
*Return:
*Modify:
*************************************************************/
void Delayus(unsigned int us)
{
while(us--); //35,15us;140,63us;1100,490us;1400,625us;
}
void Delayms(unsigned char ms)
{
for(ms;ms>0;ms--)
Delayus(65530);
}
/************************************************************
*Function:18B203?ê??ˉ
*parameter:
*Return:
*Modify:
*************************************************************/
void Init18b20 (void)
{
CY = 1;
while (CY)
{
DQ = 0; //?í3?μíμ????′??D?o?
Delayus(1100); //?óê±?áéù480us(490us)
DQ = 1; //êí·?êy?Y??
Delayus(150); //μè′y60us(70us)
CY = DQ; //?ì2a′??ú??3?
Delayus(1000); //μè′yéè±?êí·?êy?Y??(420us)
}
}
/************************************************************
*Function:?ò18B20D′è?ò???×??ú
*parameter:
*Return:
*Modify:
*************************************************************/
void DS18B20_WriteByte(BYTE dat)
{
char i;
for (i=0; i<8; i++) //8????êy?÷
{
DQ = 0; //?aê?ê±????
Delayus(2); //?óê±μè′y(1us)
dat >>= 1;
DQ=CY ; //?í3?êy?Y
Delayus(140); //μè′yê±?????áê?(63us)
DQ = 1; //???′êy?Y??
Delayus(15); //???′?óê±
}
}
/************************************************************
*Function:?á18B20μ?ò???×??ú
*parameter:
*Return:
*Modify:
*************************************************************/
BYTE DS18B20_ReadByte()
{
BYTE i;
BYTE dat = 0;
for (i=0; i<8; i++) //8????êy?÷
{
dat >>= 1;
DQ = 0; //?aê?ê±????
Delayus(3); //?óê±μè′y
DQ = 1; //×?±??óê?
Delayus(15); //?óê??óê±
if (DQ) dat |= 0x80; //?áè?êy?Y
Delayus(120); //μè′yê±?????áê?(63us)
}
return dat;
}
/************************************************************
*Function:18B20è?′|àí
*parameter:
*Return:
*Modify:
*************************************************************/
void TemperatuerResult(void)
{
Init18b20 (); //éè±??′??
DS18B20_WriteByte(0xCC); //ì?1yROM?üá?
DS18B20_WriteByte(0x44); //?aê?×a???üá?
Delayms(26);
Init18b20 (); //éè±??′??
DS18B20_WriteByte(0xCC); //ì?1yROM?üá?
DS18B20_WriteByte(0xBE); //?á?Y′?′?′¢?÷?üá?
TPL = DS18B20_ReadByte(); //?á???èμí×??ú
TPH = DS18B20_ReadByte(); //?á???è??×??ú
Init18b20 ();
Temper=TPH*0X100+TPL;
Temper/=16;
TPH=Temper;
}
#include <C8051F310.h>
typedef unsigned char BYTE;
BYTE TPH; //′?·????è?μμ???×??ú
BYTE TPL;
unsigned int Temper;
sbit DQ = P1^1;
void Oscillator_Init(); //ê±?ó3?ê??ˉ
void Port_Init(); //???ú3?ê??ˉ
void UART0_Init (void);
void Send_Char(unsigned char ch);
void ADC0_Init (void);
void Delayus(unsigned int us);
void Delayms(unsigned int ms);
void Init18b20 (void);
void DS18B20_WriteByte(BYTE dat); //μ¥×??úD′è?
BYTE DS18B20_ReadByte();
void TemperatuerResult(void);
#define SYSCLK 24500000
void main(void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
Oscillator_Init(); //ê±?ó3?ê??ˉ
Port_Init(); //???ú3?ê??ˉ
UART0_Init ();
ADC0_Init ();
AD0BUSY = 1;
while(1)
{
while(AD0INT==1)
{
Send_Char(ADC0H);
Send_Char(ADC0L);
AD0INT==0;
AD0BUSY = 1;
TemperatuerResult();
//Send_Char(TPL);
Send_Char(TPH);
}
}
}
void Oscillator_Init() //ê±?ó3?ê??ˉ
{
OSCICN = 0x83;//?ú2???μ′?÷ê1?ü£??μí3ê±?ó2?·??μ£?×????μ?ê24.5M
RSTSRC = 0x04;//ê1?üê±?ó?aê§?ì2a?÷
}
void Port_Init() //???ú3?ê??ˉ
{
XBR0 = 0x01; // UART0 TX and RX pins enabled
XBR1 = 0x40; //è?é?à-ê1?ü£???2??a1?ê1?ü
P0MDOUT |= 0x10; // Enable TX0 as a push-pull output P0.4
}
void UART0_Init (void)
{
SCON0 |= 0x00; //?óê??êDí
CKCON = 0x00; //?¨ê±?÷1ê1ó??μí3ê±?ó/12
TMOD = 0x20; //T11¤×÷·?ê?8??×??ˉ??×°??
TH1 = 0x96; //2¨ì??ê?a9600
TL1 = TH1;
TR1 = 1;
}
//·¢?íμ¥??×?·?
void Send_Char(unsigned char ch)
{
SBUF0 = ch; //?íè??o3???
while(TI0 == 0); //μè′y·¢?ííê
TI0 = 0; //èí?t??á?
}
void ADC0_Init (void)
{
REF0CN = 0x0A; // Enable on-chip VREF, select Vdd as
// voltage reference.
AMX0P = 0x02; // ADC0 positive input = P1.2
AMX0N = 0x1F; // ADC0 negative input = GND
// i.e., single ended mode
P1MDIN = 0xFB;
P1SKIP = 0x04;
ADC0CF = ((SYSCLK/50000)-1)<<3; // set SAR clock to 50KHz
ADC0CF |=0X00;
AD0EN = 1; // enable ADC0
}
/************************************************************
*Function:?óê±′|àí
*parameter:
*Return:
*Modify:
*************************************************************/
void Delayus(unsigned int us)
{
while(us--); //35,15us;140,63us;1100,490us;1400,625us;
}
void Delayms(unsigned char ms)
{
for(ms;ms>0;ms--)
Delayus(65530);
}
/************************************************************
*Function:18B203?ê??ˉ
*parameter:
*Return:
*Modify:
*************************************************************/
void Init18b20 (void)
{
CY = 1;
while (CY)
{
DQ = 0; //?í3?μíμ????′??D?o?
Delayus(1100); //?óê±?áéù480us(490us)
DQ = 1; //êí·?êy?Y??
Delayus(150); //μè′y60us(70us)
CY = DQ; //?ì2a′??ú??3?
Delayus(1000); //μè′yéè±?êí·?êy?Y??(420us)
}
}
/************************************************************
*Function:?ò18B20D′è?ò???×??ú
*parameter:
*Return:
*Modify:
*************************************************************/
void DS18B20_WriteByte(BYTE dat)
{
char i;
for (i=0; i<8; i++) //8????êy?÷
{
DQ = 0; //?aê?ê±????
Delayus(2); //?óê±μè′y(1us)
dat >>= 1;
DQ=CY ; //?í3?êy?Y
Delayus(140); //μè′yê±?????áê?(63us)
DQ = 1; //???′êy?Y??
Delayus(15); //???′?óê±
}
}
/************************************************************
*Function:?á18B20μ?ò???×??ú
*parameter:
*Return:
*Modify:
*************************************************************/
BYTE DS18B20_ReadByte()
{
BYTE i;
BYTE dat = 0;
for (i=0; i<8; i++) //8????êy?÷
{
dat >>= 1;
DQ = 0; //?aê?ê±????
Delayus(3); //?óê±μè′y
DQ = 1; //×?±??óê?
Delayus(15); //?óê??óê±
if (DQ) dat |= 0x80; //?áè?êy?Y
Delayus(120); //μè′yê±?????áê?(63us)
}
return dat;
}
/************************************************************
*Function:18B20è?′|àí
*parameter:
*Return:
*Modify:
*************************************************************/
void TemperatuerResult(void)
{
Init18b20 (); //éè±??′??
DS18B20_WriteByte(0xCC); //ì?1yROM?üá?
DS18B20_WriteByte(0x44); //?aê?×a???üá?
Delayms(26);
Init18b20 (); //éè±??′??
DS18B20_WriteByte(0xCC); //ì?1yROM?üá?
DS18B20_WriteByte(0xBE); //?á?Y′?′?′¢?÷?üá?
TPL = DS18B20_ReadByte(); //?á???èμí×??ú
TPH = DS18B20_ReadByte(); //?á???è??×??ú
Init18b20 ();
Temper=TPH*0X100+TPL;
Temper/=16;
TPH=Temper;
}