我的代码:#include "ds18b20.h"
#include "delay.h"
void DS18B20_Rst(void)
{
DS18B20_IO_OUT(); //SET PG11 OUTPUT
DS18B20_DQ_OUT=0; //ÀµÍDQ
delay_us(750); //ÀµÍ750us
DS18B20_DQ_OUT=1; //DQ=1
delay_us(60); //15US
}
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PG11 INPUT
while (DS18B20_DQ_IN&&retry<200)
{
retry++;
delay_us(1);
};
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240)
{
retry++;
delay_us(1);
};
if(retry>=240)return 1;
return 0;
}
u8 DS18B20_Read_Bit(void) // read one bit
{
u8 data;
DS18B20_IO_OUT();//SET PG11 OUTPUT
DS18B20_DQ_OUT=0;
delay_us(2);
DS18B20_DQ_OUT=1;
DS18B20_IO_IN();//SET PG11 INPUT
delay_us(12);
if(DS18B20_DQ_IN)data=1;
else data=0;
delay_us(60);
return data;
}
u8 DS18B20_Read_Byte(void) // read one byte
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT();//SET PG11 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT=0;// Write 1
delay_us(2);
DS18B20_DQ_OUT=1;
delay_us(60);
}
else
{
DS18B20_DQ_OUT=0;// Write 0
delay_us(60);
DS18B20_DQ_OUT=1;
delay_us(2);
}
}
}
void DS18B20_Start(void)// ds1820 start convert
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0x44);// convert
}
u8 DS18B20_Init(void)
{
DS18B20_Rst();
return DS18B20_Check();
}
short ReadTemperature(void)
{
u8 temp;
u8 TL,TH;
short tem;
DS18B20_Start (); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);
DS18B20_Write_Byte(0xbe);
TL=DS18B20_Read_Byte(); // LSB
TH=DS18B20_Read_Byte();
if(TH>7)
{
TH=~TH;
TL=~TL;
temp=0;
}else temp=1;
tem=TH;
tem<<=8;
tem+=TL;
tem=(double)tem*0.625;
if(temp)return tem;
else return -tem;
}
LCD1602:
#include "lcd1602.h"
#include "delay.h"
#include "sys.h"
u8 Dao_xu(u8 data)
{
u8 i = 0 ,temp = 0;
for(i = 0; i < 8; i++)
{
temp += (((data >> i) & 0x01) << (7 - i));
}
return temp;
}
/*------------------------------------------------
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
LCD1602_RS=0; //ÃüÁî
delay_us(1);
LCD1602_RW=0;
delay_us(1);
LCD1602_EN=1;
delay_us(1);
GPIO_Write(GPIOA,(GPIO_ReadOutputData(GPIOA)&0XFF00)+Dao_xu(com));
delay_us(100);
LCD1602_EN=0;
}
/*------------------------------------------------
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
LCD1602_RS=1;
delay_us(1);
LCD1602_RW=0;
delay_us(1);
LCD1602_EN=1;
delay_us(1);
GPIO_Write(GPIOA,(GPIO_ReadOutputData(GPIOA)&0XFF00)+Dao_xu(Data));
delay_us(100);
LCD1602_EN=0;
}
/*------------------------------------------------
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
while (*s)
{
LCD_Write_Data( *s);
s ++; //Ö¸Õë¼Ó1
}
}
/*------------------------------------------------
дÈë×Ö·ûº¯Êý
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
delay_ms(5);
}
/*------------------------------------------------*/
void LCD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯GPIOA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_10; // LCD1602 RS-RW-EN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIOB
LCD_Write_Com(0x38);
delay_ms(5);
LCD_Write_Com(0x38);
delay_ms(5);
LCD_Write_Com(0x38);
delay_ms(5);
LCD_Write_Com(0x08);
delay_ms(5);
LCD_Write_Com(0x01);
delay_ms(5);
LCD_Write_Com(0x06);
delay_ms(5);
LCD_Write_Com(0x0C);
delay_ms(5);
}
|
如果不能显示温度,就学习人民日报编一个温度数据看看能不能显示。如果也不能显示,审查没通过。