平台
平台: stm32f103c8t6
协议
协议:IIC
原理
驱动源码
IIC_dif_pressure.c
#include <stm32f10x_lib.h>
#include "IIC_dif_pressure.h"
#include "stdio.h"
#include "delay.h"
unsigned char crc = 0xff;
float dif_pressure;
float tempre_value;
float Scale_Factors;
void delay_xus(u16 time)
{
u16 i=0;
while(time--)
{
i=10;
while(i--) ;
}
}
void delay_xms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--) ;
}
}
void IIC2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_12|GPIO_Pin_11);
}
void IIC2_Start(void)
{
SDA2_OUT();
IIC2_SDA=1;
IIC2_SCL=1;
delay_xus(4);
IIC2_SDA=0;
delay_xus(4);
IIC2_SCL=0;
}
void IIC2_Stop(void)
{
SDA2_OUT();
IIC2_SCL=0;
IIC2_SDA=0;
delay_xus(4);
IIC2_SCL=1;
IIC2_SDA=1;
delay_xus(4);
}
uint8_t IIC2_Wait_Ack(void)
{
uint8_t ucErrTime=0;
SDA2_IN();
IIC2_SDA=1;
delay_xus(1);
IIC2_SCL=1;
delay_xus(1);
while(READER_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC2_Stop();
return 1;
}
}
IIC2_SCL=0;
return 0;
}
void IIC2_Ack(void)
{
IIC2_SCL=0;
SDA2_OUT();
IIC2_SDA=0;
delay_xus(2);
IIC2_SCL=1;
delay_xus(2);
IIC2_SCL=0;
}
void IIC2_NAck(void)
{
IIC2_SCL=0;
SDA2_OUT();
IIC2_SDA=1;
delay_xus(2);
IIC2_SCL=1;
delay_xus(2);
IIC2_SCL=0;
}
void IIC2_Send_Byte(u8 txd)
{
uint8_t t;
SDA2_OUT();
IIC2_SCL=0;
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
IIC2_SDA=1;
else
IIC2_SDA=0;
txd<<=1;
delay_xus(2);
IIC2_SCL=1;
delay_xus(2);
IIC2_SCL=0;
delay_xus(2);
}
}
uint8_t IIC2_Read_Byte(unsigned char ack)
{
uint8_t i,receive=0;
SDA2_IN();
for(i=0;i<8;i++ )
{
IIC2_SCL=0;
delay_xus(2);
IIC2_SCL=1;
if(READER_SDA)receive++;
receive<<=1;
delay_xus(1);
}
if (ack==0)
IIC2_NAck();
else
IIC2_Ack();
return receive;
}
void SDP810_Init(void)
{
IIC2_Init();
}
void SDP810_ReadData(u16 cmd,int8_t *p)
{
uint8_t i=0;
uint8_t ack=0;
IIC2_Start();
IIC2_Send_Byte(0X4a);
IIC2_Wait_Ack();
IIC2_Send_Byte(cmd>>8);
IIC2_Wait_Ack();
IIC2_Send_Byte(cmd);
IIC2_Wait_Ack();
IIC2_Stop();
delay_xms(10);
IIC2_Start();
IIC2_Send_Byte(0X4b);
IIC2_Wait_Ack();
for(i=0;i<9;i++)
{
if(i<8)
{
ack=1;
}
else
{
ack=0;
}
p[i]=IIC2_Read_Byte(ack);
}
IIC2_Stop();
delay_xms(10);
}
void SDP810_Write_Cmd(uint16_t cmd)
{
IIC2_Start();
IIC2_Send_Byte(0X4a);
IIC2_Wait_Ack();
IIC2_Send_Byte(cmd>>8);
IIC2_Wait_Ack();
IIC2_Send_Byte(cmd);
IIC2_Wait_Ack();
IIC2_Stop();
delay_xms(10);
}
void Sdp810_GetValue(void)
{
int8_t p[9];
SDP810_ReadData(0x3615,p);
dif_pressure=(float)(((p[0]*256)+p[1])/240.0);
tempre_value=(float)(((p[3]*256)+p[4])/200.0);
Scale_Factors=(float)((p[6]*256)+p[7]);
}
IIC_dif_pressure.h
#ifndef __IICPRESSURE_H
#define __IICPRESSURE_H
#include "sys.h"
#define SDA2_IN() {GPIOA->CRH&=0XFFFF0FFF;GPIOA->CRH|=(u32)8<<12;}
#define SDA2_OUT() {GPIOA->CRH&=0XFFFF0FFF;GPIOA->CRH|=(u32)3<<12;}
#define IIC2_SCL PAout(12)
#define IIC2_SDA PAout(11)
#define READER_SDA PAin(11)
#define SLAVE_ADDRESS 0x25
#define SDP_TYPE SLAVE_ADDRESS
void IIC2_Init(void);
void IIC2_Start(void);
void IIC2_Stop(void);
void IIC2_Send_Byte(uint8_t txd);
uint8_t IIC2_Read_Byte(uint8_t ack);
uint8_t IIC2_Wait_Ack(void);
void IIC2_Ack(void);
void IIC2_NAck(void);
void IIC2_Write_One_Byte(uint8_t daddr,uint8_t addr,uint8_t data);
uint8_t IIC2_Read_One_Byte(uint8_t daddr,uint8_t addr);
void SDP810_ReadData(u16 cmd,int8_t *p);
void SDP810_Write_Cmd(uint16_t cmd);
extern void Sdp810_GetValue(void);
void SDP810_Init(void);
#endif
|