用的
STM32的
单片机,调试AM2320,不知谁有调试通过的,看波形发送的是对的,但是发送出去总是收不到ACK,有谁调试通过的,帮看下,或是有调试通过的代码,谢谢
/*************************************************************
ÎļþÃû : TVOC_IIC.c
×÷ Óà £ºTVOC³ÌÐòÄ£¿é
˵ Ã÷ £ºÊ¹ÓÃIIC×ÜÏßÊäÈë
*************************************************************
* Ó²¼þÁ¬½Ó :
* -----------------
* | PB5- SCL | KQM2801A TVOC
* | PB6- SDA |
* -----------------
*************************************************************
********************************************************************/
//==============================================================================
//1 Óû§Ðè×ÔÐж¨ÒåIO¿Ú;
//2 Óû§Ðè×Ô¶¨Òådelay_usÑÓʱº¯Êý£»
//3 Ö±½Óµ÷ÓÃiic_rX(unsigned char *data)£¬¶¨ÒåÄ¿±ê´æ´¢Êý×éµØÖ·
//==============================================================================
#include "TVOC_iic.h"
#include "usart.h"
#include "LCD12864.h"
extern void delay_us(u32 i);
unsigned int tvoc;
#define SlaveAddress 0xB8 //¶¨ÒåÆ÷¼þÔÚIIC×ÜÏßÖдӵØÖ·
u8 BUF_TP[8];
int dis_data_Tem; //È«¾ÖζÈ
int dis_data_Hum; //È«¾Öʪ¶È
void I2C_TVOC_Init(void) //ÅäÖÃIO¿Ú KQM2801A
{
GPIO_InitTypeDef GPIO_InitStructure;
/* ??? I2C1 ????? */
RCC_APB2PeriphClockCmd(TVOC_RCC,ENABLE);
GPIO_InitStructure.GPIO_Pin=TVOC_SCL|TVOC_SDA;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(TVOC_GPIO,&GPIO_InitStructure);
}
void I2C_GPIO_SDA(uint16_t Mode) //SDAIOģʽѡÔñ KQM2801A
{
GPIO_InitTypeDef GPIO_InitStructure;
/* ??? I2C1 ????? */
RCC_APB2PeriphClockCmd(TVOC_RCC,ENABLE);
/* PB10-I2C1_SCL?PB11-I2C1_SDA*/
GPIO_InitStructure.GPIO_Pin = TVOC_SDA;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = Mode; //
GPIO_Init(TVOC_GPIO, &GPIO_InitStructure);
}
/*
¿ªÊ¼ ______ __ //SCL¸ßµçƽSDAϽµ
SCL £º |__| |
____
SDA: |__|
*/
void iic_start(void)
{
MO_SDA();
// delay_us(0x05);
OH_SDA();
delay_us(10);
OH_SCL();
delay_us(10);
OL_SDA();
delay_us(10);
OL_SCL();
delay_us(10);
}
/*
½áÊø __ //SCL¸ßµçƽSDAÉÏÉý
SCL £º|__| |______
________
SDA: |__|
*/
void iic_stop(void)
{
MO_SDA();
OL_SDA();
delay_us(10);
OH_SCL();
delay_us(10);
OH_SDA();
delay_us(10);
}
void iic_ack(_Bool flag) //Ó¦´ðλ
{
MO_SDA();
if(flag)
{OH_SDA();}
else
{OL_SDA();}
//delay_us(0x01);
OL_SCL();
delay_us(10);
OH_SCL();
delay_us(10);
OL_SCL();
OH_SDA();
delay_us(10);
}
_Bool iic_recvAck(void) //½ÓÊÕÓ¦´ð
{
MI_SDA();
//OH_SDA();
OL_SCL();
delay_us(10);
OH_SDA();
delay_us(10);
OH_SCL();
delay_us(10);
if(DI_SDA())
{
OL_SCL();
delay_us(10);
return 0;
}
else
{
OL_SCL();
delay_us(10);
return 1;
}
}
void AM232X_SendFrist(u8 iic_data)
{
unsigned char n;
MO_SDA();
for(n=0;n<8;n++)
{
//delay_us(0x1);
if(iic_data&0x80)
{OH_SDA();}
else
{OL_SDA();}
delay_us(5);
OH_SCL();
delay_us(10);
OL_SCL();
delay_us(5);
iic_data<<=1;
}
OH_SDA();
delay_us(5);
OH_SCL();
delay_us(10);
OL_SCL();
OL_SDA();
delay_us(5);
}
void iic_datasend(unsigned char iic_data) //·¢ËÍ8λÊý¾Ý
{
unsigned char n;
MO_SDA();
for(n=0;n<8;n++)
{
//delay_us(0x1);
if(iic_data&0x80)
{OH_SDA();}
else
{OL_SDA();}
delay_us(5);
OH_SCL();
delay_us(10);
OL_SCL();
delay_us(5);
iic_data<<=1;
}
iic_recvAck();
}
unsigned char iic_datareceive(void) // ¶ÁÈ¡8λÊý¾Ý
{
unsigned char n;
unsigned char data=0;
MI_SDA();
OH_SDA();
for(n=0;n<8;n++)
{
//delay_us(0x01);
OH_SCL();
delay_us(10);
if(DI_SDA())
{data|=0x01;}
if(n>1)
data<<=1;
//delay_us(0x01);
OL_SCL();
delay_us(10);
}
return data;
}
void Waken_AM232X(void)
{
iic_start();
AM232X_SendFrist(SlaveAddress);
}
void Write_AM232X(void)
{
iic_start();
iic_datasend(SlaveAddress);
delay_us(10);
delay_us(10);
iic_datasend(0x03);
iic_datasend(0x00);
iic_datasend(0x04);
iic_stop();
}
//Á¬Ðø¶Á³öAM232XÄÚ²¿Êý¾Ý
void Mul
tiple_read_AM232X(void)
{
u8 i;
iic_start();
iic_datasend(SlaveAddress|0x01);
delay_us(10);
delay_us(10);
delay_us(10);
delay_us(10);
delay_us(10);
for (i=0; i<8; i++)
{
BUF_TP[i] = iic_datareceive();
if(i==8)
iic_ack(1);
else
iic_ack(0);
}
iic_stop();
}
void Waken_Stop_AM232X(void)
{
iic_stop();
}
//CRC¼ÆËã
unsigned int CRC16(unsigned char *ptr, unsigned char len)
{
unsigned int crc=0xffff;
unsigned char i;
while(len--)
{
crc ^=*ptr++;
for(i=0;i<8;i++)
{
if(crc & 0x1)
{
crc>>=1;
crc^=0xa001;
}
else
{
crc>>=1;
}
}
}
return crc;
}
///CRCÊÇ·ñÕýÈ·
unsigned char CheckCRC(unsigned char *ptr,unsigned char len)
{
unsigned int crc;
crc=(unsigned int)CRC16(ptr,len-2);
if(ptr[len-1]==(crc>>8) && ptr[len-2]==(crc & 0x00ff))
{
return 0xff;
}
else
{
return 0x0;
}
}
void TEM_AM232X(void)//1ms????,????
{
static u32 TEM_c;
static u8 TEM_Err = 0;
if((++TEM_c) > 2000)TEM_c = 0;
if(TEM_c == 0)
{
Waken_AM232X(); // power on
}
else if(TEM_c == 900)
{
Waken_Stop_AM232X(); //??????
}
else if(TEM_c == 905)
{
Write_AM232X(); //?????
}
else if(TEM_c == 910)
{
Multiple_read_AM232X(); //??????,???BUF?
}
else if(TEM_c == 911)
{
if(CheckCRC(BUF_TP,8)) //??CRC16????
{
dis_data_Tem = BUF_TP[4]*256+BUF_TP[5];
dis_data_Hum = BUF_TP[2]*256+BUF_TP[3];
TEM_Err = 0; //???
TEM_c++;//???
}
}
else if(TEM_c == 912) //????
{
if((++TEM_Err) > 5) //??????5?
{
TEM_Err = 1; //??????
dis_data_Tem = 0xffff; //???????0xffff???,?????
dis_data_Hum = 0xffff;
}
}
}
一周热门 更多>