ADS1115在STM32下的使用
时间:10-02
整理:3721RD
点击:
采用IO模拟I2C读写时序,程序如下图所示
#include "stm32f10x_lib.h" #include "ads1115.h" //#include "ap_i2c.h" static void Confige1115 (unsigned char port); static void PointRegister (void); static u16 ReadData (unsigned char chn, unsigned char cnt); unsigned char WriteIntBuf[4], WritepointBuf[2], count = 6,ReadBuffer[64];; void delay (unsigned int delay) { while(delay--); } /******************************************************************************* * Function Name : Confige1115 * Description : 延迟时间 * Input : None * Output : None * Return : None * Attention :获取ADS1115模拟转换结果 *******************************************************************************/ u16 data_get; float dianya; float Get_ATOD (unsigned char channel,unsigned char cnt) { static unsigned char chn; chn = channel; Confige1115(channel); delay(1000); PointRegister(); delay(1000); data_get=ReadData(chn,cnt); delay(1000); if(data_get>=0x8000) dianya=((float)(0xffff-data_get)/32768.0)*4.096; else dianya=((float)data_get/32768.0)*4.096; return dianya; } /******************************************************************************* * Function Name : Confige1115 * Description : 延迟时间 * Input : None * Output : None * Return : None * Attention :配置ADS1115 *******************************************************************************/ static void Confige1115 (unsigned char port) { static unsigned char chnel, i; switch (port) { case 0: //0通道 chnel=0xC2; break; case 1: //1通道 chnel=0xD2; break; case 2: //2通道 chnel=0xE2; break; case 3: //3通道 chnel=0xF2; break; default: break; } WriteIntBuf[0] = CMD_Write;//0x90 WriteIntBuf[1] = CMD_CONF_REG;//0x01 WriteIntBuf[2] = chnel;// tongdao WriteIntBuf[3] = CONF_L;//0xe3 if(!(TWI_START())) { for(i=0;i<4;i++) { TWI_SendByte(WriteIntBuf[i]); delay(20); } } TWI_STOP(); } /******************************************************************************* * Function Name : PointRegister * Description : 延迟时间 * Input : None * Output : None * Return : None * Attention :指向ADS1115指针寄存器用于准备读取数据 *******************************************************************************/ static void PointRegister (void) { unsigned char i; WritepointBuf[0] = CMD_Write; //90 WritepointBuf[1] = CMD_POINT_REG; //00 if(!(TWI_START())) { for(i=0;i<2;i++) { TWI_SendByte(WritepointBuf[i]); delay(20); } } TWI_STOP(); } /******************************************************************************* * Function Name : I2C_delay * Description : 延迟时间 * Input : None * Output : None * Return : None * Attention : CH1 PA4 CH2 PA7 CH3 PA6 CH4 PA5 *******************************************************************************/ static u16 ReadData (unsigned char chn,unsigned char cnt) { u16 data_rece; switch(chn) { case 0: //0通道 if(!(TWI_START())) { if(count == 0xff) count = 6; TWI_SendByte(CMD_Read); delay(20); ReadBuffer[cnt * 8] = TWI_ReceiveByte(); TWI_SendACK(); ReadBuffer[(cnt * 8) + 1] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); data_rece=ReadBuffer[cnt * 8]*256+ReadBuffer[(cnt * 8) + 1]; //ReadBuffer[56]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) << count); } break; case 1: //1通道 if(!(TWI_START())) { TWI_SendByte(CMD_Read); delay(20); ReadBuffer[(cnt * 8) + 2] = TWI_ReceiveByte(); TWI_SendACK(); ReadBuffer[(cnt * 8) + 3] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); data_rece=ReadBuffer[(cnt * 8) + 2]*256+ReadBuffer[(cnt * 8) + 3]; //ReadBuffer[57]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) << count); } break; case 2: //2通道 if(!(TWI_START())) { TWI_SendByte(CMD_Read); delay(20); ReadBuffer[(cnt * 8) + 4] = TWI_ReceiveByte(); TWI_SendACK(); // I2C_delay(20); ReadBuffer[(cnt * 8) + 5] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); data_rece=ReadBuffer[(cnt * 8) + 4]*256+ReadBuffer[(cnt * 8) + 5]; //ReadBuffer[59]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) << count); } break; case 3: //3通道 if(!(TWI_START())) { TWI_SendByte(CMD_Read); delay(20); // I2C_delay(20); ReadBuffer[(cnt * 8) + 6] = TWI_ReceiveByte(); TWI_SendACK(); // I2C_delay(20); ReadBuffer[(cnt * 8) + 7] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); data_rece=ReadBuffer[(cnt * 8) + 6]*256+ReadBuffer[(cnt * 8) + 7]; //ReadBuffer[58]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) << count--); } break; default: break; }return data_rece; }
呵呵, 楼主应该简要分析和说明一下这一大段的代码
注释不错,就是不知道效果怎样呢