123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- #include "PressureSensor.h"
- #include "string.h"
- #include "gpio.h"
- #include "stdio.h"
- #include "ac780x.h"
- #define CPS135B_ADDR (0x6D)
- static uint8_t iic_error_count = 0;
- static uint8_t iic_exception = 0;
- static uint8_t iic_exception2 = 0;
- static float cps_pressure = 0;
- static float cps_temperature = 0;
- static uint32_t ret_status = 0;
- /**
- * @prototype I2C_ParaInit(void)
- *
- * @param[in] void
- * @return void
- *
- * @brief 初始化I2C0.
- */
- void I2C0_ParaInit(void)
- {
- I2C_ConfigType i2cConfig;
-
- /*初始化引脚功能为I2C.*/
- GPIO_SetFunc(GPIOA, GPIO_PIN0, GPIO_FUN3);//I2C0_SCL
- GPIO_SetFunc(GPIOA, GPIO_PIN1, GPIO_FUN3);//I2C0_sda
-
- /*清零结构体变量.*/
- memset(&i2cConfig, 0x00, sizeof(i2cConfig));
-
- /*无论是主机还是从机模式都需要配置的参数.*/
- i2cConfig.mode = I2C_MASTER;/*设置主从机模式.*/
- i2cConfig.extAddrEn = DISABLE;/*10bit扩展地址禁能.*/
- i2cConfig.dmaRxEn = DISABLE;/*设置DMA接收数据.*/
- i2cConfig.dmaTxEn = DISABLE;/*设置DMA发送数据.*/
- i2cConfig.interruptEn = DISABLE;/*I2C中断,BND/SAMF/ARBLOST.*/
- i2cConfig.nackInterruptEn = DISABLE;/*NACK中断.*/
- i2cConfig.ssInterruptEn = DISABLE;/*总线start或stop中断.*/
- i2cConfig.i2cEn = ENABLE;/*使能模块.*/
- i2cConfig.callBack = NULL;/*中断回调函数.*/
-
- /*主机模式需要配置的参数,配置成从机模式可忽略.*/
- i2cConfig.masterConfig.sampleCnt = 9;/*设置波特率为100Kbps,bandrate=(24M/(10*12*2))=100Kbps.*/
- i2cConfig.masterConfig.stepCnt = 11;
- i2cConfig.masterConfig.ARBEn = ENABLE;/*设置主机仲裁功能.*/
- i2cConfig.masterConfig.SYNCEn = ENABLE;/*设置主机SCL同步功能.*/
-
- /*从机模式需要配置的参数,配置成主机模式可忽略.*/
-
- //i2cConfig.slaveConfig.slaveAddr = AT24C02_DEV_ADDR;/*从机地址.*/
- //i2cConfig.slaveConfig.slaveRangeAddr = 0;/*从机范围地址.*/
- //i2cConfig.slaveConfig.glitchFilterCnt= 0;/*毛刺过滤.*/
- //i2cConfig.slaveConfig.stretchEn = DISABLE;/*从机SCL延伸功能.*/
- //i2cConfig.slaveConfig.rangeAddrEn = DISABLE;/*禁能范围地址.*/
- //i2cConfig.slaveConfig.monitorEn = DISABLE;/*禁能从机监测功能.*/
- //i2cConfig.slaveConfig.generalCallEn = DISABLE;/*从机SCL广播地址.*/
- //i2cConfig.slaveConfig.wakeupEn = DISABLE;/*唤醒功能,仅从机时有效.*/
- //i2cConfig.slaveConfig.rxOFInterruptEn = DISABLE;/*接收缓存溢出中断.*/
- //i2cConfig.slaveConfig.txUFInterruptEn = DISABLE;/*发送缓存溢出中断.*/
-
-
- I2C_Init(I2C0, &i2cConfig);
- }
- /*
- uint8_t cps135b_readbyte(uint8_t addr)
- {
- uint8_t data;
-
- I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, &addr, 1, DISABLE);//发送读取地址
- I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, &data, 1);//读取数据
-
- return data;
- }
- void cps135b_writebyte(uint8_t addr, uint8_t data)
- {
- uint8_t buffer[2];
- buffer[0] = addr;
- buffer[1] = data;
-
- I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, buffer, 2, ENABLE);//发送读取地址
- }
- void cps135b_mr()
- {
- uint8_t buffer[2];
- buffer[0] = 0x30;
- buffer[1] = 0x0A;
-
- I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, buffer, 2, ENABLE);//发送读取地址
- }
- uint8_t cps135b_gd(float* pressure, float* temperature)
- {
- uint32_t status = I2C_HW_STATUS_ERROR_NULL;
- uint32_t tmp = 0;
- uint8_t reg_addr = 0x06;
- uint8_t data[5];
-
- status |= I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, ®_addr, 1, DISABLE);//发送读取地址
- status |= I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, data, 5);//读取数据
-
- if(I2C_HW_STATUS_ERROR_NULL == status){
-
- tmp = (uint32_t)data[0]<<16;
- tmp += (uint32_t)data[1]<<8;
- tmp += data[2];
-
- *pressure = (1.0*tmp)/256;
- tmp = 0;
- tmp += (uint32_t)data[3]<<8;
- tmp += data[4];
-
-
- if(data[3] <= 128){
- *temperature = 1.0*tmp/256;
- }else{
- *temperature = 1.0*(tmp-65535)/256;
- }
-
-
- return 0;
-
- }else{
- return 1;
- }
-
-
- }
- */
- void PressureSensor_Init(void)
- {
- I2C0_ParaInit();
- iic_error_count = 0;
- cps_pressure = 0;
- cps_temperature = 0;
-
- }
- uint8_t PressureSensor_MR(void)
- {
-
- uint8_t buffer[2];
- buffer[0] = 0x30;
- buffer[1] = 0x0A;
-
- //反复几次出现问题,直接返回异常
- if(iic_exception2 > 3){
- return 1;
- }
-
- ret_status = I2C_HW_STATUS_ERROR_NULL;
- ret_status = I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, buffer, 2, ENABLE);//发送读取地址
-
- if(I2C_HW_STATUS_ERROR_NULL == ret_status){
- return 0;
- }else{
- return 1;
- }
-
- }
- uint8_t PressureSensor_Read(void)
- {
- uint32_t tmp = 0;
- uint8_t reg_addr = 0x06;
- uint8_t data[5];
-
- if(iic_exception2 > 3){
- //反复几次出现问题,直接返回异常
- return 1;
- }
-
- ret_status = I2C_HW_STATUS_ERROR_NULL;
- ret_status |= I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, ®_addr, 1, DISABLE);//发送读取地址
- ret_status |= I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, data, 5);//读取数据
-
- if(I2C_HW_STATUS_ERROR_NULL == ret_status){
-
- tmp = (uint32_t)data[0]<<16;
- tmp += (uint32_t)data[1]<<8;
- tmp += data[2];
-
- cps_pressure = (1.0*tmp)/256/1000; //kPa
- tmp = 0;
- tmp += (uint32_t)data[3]<<8;
- tmp += data[4];
-
-
- if(data[3] <= 128){
- cps_temperature = 1.0*tmp/256;
- }else{
- cps_temperature = 1.0*(tmp-65535)/256;
- }
-
- iic_error_count = 0;
- iic_exception =0;
-
- }else{
-
- iic_error_count++;
- }
-
- if(iic_error_count >= 3){
- iic_exception =1;
-
- PressureSensor_DeInit();
- PressureSensor_Init();
-
- iic_exception2++;
-
- }
-
- return iic_exception;
-
- }
- uint8_t PressureSensor_Getvalue(float* pressure)
- {
- uint32_t status = I2C_HW_STATUS_ERROR_NULL;
- uint32_t tmp = 0;
- uint8_t reg_addr = 0x06;
- uint8_t data[3];
-
- status |= I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, ®_addr, 1, DISABLE);//发送读取地址
- status |= I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, data, 3);//读取数据
-
- if(I2C_HW_STATUS_ERROR_NULL == status){
-
- tmp = (uint32_t)data[0]<<16;
- tmp += (uint32_t)data[1]<<8;
- tmp += data[2];
-
- *pressure = (1.0*tmp)/256;
- return 0;
-
- }else{
- return 1;
- }
-
- }
- uint8_t PressureSensor_Getvalue2(float* pressure, float* temperature)
- {
- *pressure = cps_pressure;
- *temperature = cps_temperature;
-
- return iic_exception;
- }
- uint8_t PressureSensor_Print(void)
- {
- float pressure = 0.0;
- float temperature = 0.0;
-
- //PressureSensor_Getvalue(&pressure);
- PressureSensor_Getvalue2(&pressure, &temperature);
-
- printf("PressureSenso: pressure:%f temperature:%f \r\n", pressure, temperature);
-
-
- }
- void PressureSensor_DeInit(void)
- {
- I2C_DeInit(I2C0);
- }
- void PressureSensor_Release(void)
- {
- I2C_DeInit(I2C0);
- }
|