#include "wsd.h"
#include "delay.h"
GPIO_InitTypeDef GPIO_InitStructure;
void io_in(void)
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
void io_out(void)
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
uint8_t wsd_init(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed =GPIO_High_Speed;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_2);
start();
return check();
}
uint8_t check(void)
{
uint8_t retry=0;
while((!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2))&&retry<100)
{
retry++;
delay_us(1);
}
if(retry>=100)
return 1;
else
retry=0;
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)&&retry<100)
{
retry++;
delay_us(1);
}
if(retry>=100)
return 1;
else
return 0;
}
uint8_t read_bit(void)
{
uint8_t retry=0;
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)&&retry<100)
{
retry++;
delay_us(1);
}
retry=0;
while((!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2))&&retry<100)
{
retry++;
delay_us(1);
}
delay_us(40);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2))
return 1;
else
return 0;
}
uint8_t read_byte(void)
{
uint8_t i,dat;
dat=0;
for(i=0;i<8;i++)
{
dat<<=1;
dat|=read_bit();
}
return dat;
}
uint8_t read_data(uint8_t *temp,uint8_t *shidu)
{
uint8_t buf[5];
uint8_t i;
start();
if(check()==0)
{
for(i=0;i<5;i++)
{
buf[i]=read_byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*shidu=buf[0];
*temp=buf[2];
}
}
else
return 1;
return 0;
}
void start(void)
{
io_out();
GPIO_SetBits(GPIOC,GPIO_Pin_2);
delay_us(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
delay_ms(20);
GPIO_SetBits(GPIOC,GPIO_Pin_2);
delay_us(30);
io_in();
}
----------------------
这个是干啥的。?
|