手上有STM32读取1路DS18B20温度信号程序,怎样在一路DS18B20的基础之上修改测量4路DS18B20温度,请做过类似的开发的前辈,指导!谢谢!
主函数:
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "ds18b20.h"
int main(void)
{
u8 t=0;
short temperature;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
LCD_Init();
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"WarShip STM32");
LCD_ShowString(30,70,200,16,16,"DS18B20 TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2016/10/20");
// DS18B20_Init(1);
/*while(DS18B20_Init())
{
LCD_ShowString(30,130,200,16,16,"DS18B20 Error");
delay_ms(200);
LCD_Fill(30,130,239,130+16,WHITE);
delay_ms(200);
} */
LCD_ShowString(30,130,200,16,16,"DS18B20 OK");
POINT_COLOR=BLUE;
LCD_ShowString(30,150,200,16,16,"Temp: . C");
while(1)
{
if(t%10==0)
{
temperature=DS18B20_Get_Temp();
if(temperature<0)
{
LCD_ShowChar(30+40,150,'-',16,0);
temperature=-temperature;
}else LCD_ShowChar(30+40,150,' ',16,0);
LCD_ShowNum(30+40+8,150,temperature/10,2,16);
LCD_ShowNum(30+40+32,150,temperature%10,1,16);
}
delay_ms(10);
t++;
if(t==20)
{
t=0;
LED0=!LED0;
}
}
}
//DS18B20程序
#include "ds18b20.h"
#include "delay.h"
void DS18B20_Rst(u8 x)
{
DS18B20_IO_OUT(x);
DS18B20_DQ_OUT(x)=0;
delay_us(750);
DS18B20_DQ_OUT(x)=1; //DQ=1
delay_us(15); //15US
}
u8 DS18B20_Check(u8 x)
{
u8 retry=0;
DS18B20_IO_IN(x); //SET PG11 INPUT
while (DS18B20_DQ_IN(x)&&retry<200)
{
retry++;
delay_us(1);
}
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN(x)&&retry<240)
{
retry++;
delay_us(1);
}
if(retry>=240)return 1;
return 0;
}
u8 DS18B20_Read_Bit(u8 x)
{
u8 data;
DS18B20_IO_OUT(x); //SET PG11 OUTPUT
DS18B20_DQ_OUT(x)=0;
delay_us(2);
DS18B20_DQ_OUT(x)=1;
DS18B20_IO_IN(x); //SET PG11 INPUT
delay_us(12);
if(DS18B20_DQ_IN(x))data=1;
else data=0;
delay_us(50);
return data;
}
u8 DS18B20_Read_Byte(u8 x)
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit(x);
dat=(j<<7)|(dat>>1);
}
return dat;
}
void DS18B20_Write_Byte(u8 x, u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT(x); //SET PG11 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT(x)=0; // Write 1
delay_us(2);
DS18B20_DQ_OUT(x)=1;
delay_us(60);
}
else
{
DS18B20_DQ_OUT(x)=0; // Write 0
delay_us(60);
DS18B20_DQ_OUT(x)=1;
delay_us(2);
}
}
}
void DS18B20_Start(u8 x)
{
DS18B20_Rst(x);
DS18B20_Check(x);
DS18B20_Write_Byte(x,0xcc); // skip rom
DS18B20_Write_Byte(x,0x44); // convert
}
u8 DS18B20_Init(u8 x)
{
RCC-APB2ENR|<<2;
if(x==1)
{GPIOA->CRL&=0XFFFF0FFF;
GPIOA->CRL|=0X00003000;
GPIOA->ODR|=1<<3;
}
if(x==2)
{GPIOA->CRL&=0XFFF0FFFF;
GPIOA->CRL|=0X00300000;
GPIOA->ODR|=1<<4;
}
if(x==3)
{GPIOA->CRL&=0XFF0FFFFF;
GPIOA->CRL|=0X00300000;
GPIOA->ODR|=1<<5;
}
if(x==4)
{GPIOA->CRL&=0XF0FFFFFF;
GPIOA->CRL|=0X03000000;
GPIOA->ODR|=1<<6;
}
//GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_Init(GPIOG, &GPIO_InitStructure);
//GPIO_SetBits(GPIOG,GPIO_Pin_11);
DS18B20_Rst(x);
return DS18B20_Check(x);
}
short DS18B20_Get_Temp(u8 x)
{
u8 temp;
u8 TL,TH;
short tem;
DS18B20_Start (x); // ds1820 start convert
DS18B20_Rst(x);
DS18B20_Check(x);
DS18B20_Write_Byte(x,0xcc); // skip rom
DS18B20_Write_Byte(x,0xbe); // convert
TL=DS18B20_Read_Byte(x); // LSB
TH=DS18B20_Read_Byte(x); // MSB
if(TH>7)
{
TH=~TH;
TL=~TL;
temp=0;
}else temp=1;
tem=TH;
tem<<=8;
tem+=TL;
tem=(float)tem*0.625;
if(temp)return tem;
else return -tem;
}
一周热门 更多>