IoT_SCV_CH584M/APP/bsp_wf5803.c

232 lines
5.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "bsp_wf5803.h"
#include "CONFIG.h"
static uint8_t Peripheral_TaskID = 0xff;
#define WF5803_CSB1_HIGH() GPIOB_SetBits(GPIO_Pin_17)
#define WF5803_CSB1_LOW() GPIOB_ResetBits(GPIO_Pin_17)
void SPICs_Start(void)
{
// GPIOA_ResetBits(GPIO_Pin_6);
WF5803_CSB1_LOW();
}
void SPICs_Stop(void)
{
// GPIOA_SetBits(GPIO_Pin_6);
WF5803_CSB1_HIGH();
}
uint8_t WF5803_SendByte(uint8_t data)
{
R8_SPI0_BUFFER = data;
while (!(R8_SPI0_INT_FLAG & RB_SPI_FREE));
return (R8_SPI0_BUFFER);
}
void WF5803_WriteReg(uint8_t Address, uint8_t value)
{
SPICs_Start();
WF5803_SendByte(0x00);
WF5803_SendByte(Address);
WF5803_SendByte(value);
SPICs_Stop();
}
uint8_t WF5803_ReadReg(uint8_t addr)
{
uint8_t value;
SPICs_Start();
WF5803_SendByte(0x80);
WF5803_SendByte(addr);
value = WF5803_SendByte(0xFF);
SPICs_Stop();
return value;
}
void WF5803_Init(void)
{
/**
* CSB: PA6
* SCL: PA0
* SDA: PA1
* SDO: PA2
*/
/**
* CSB: PB17
* SCL: PA13
* SDA: PA14
* SDO: PA15
*/
// SDA: MOSI
// SDO: MISO
#if 1
// CSB1: PA3
GPIOA_SetBits(GPIO_Pin_3);
GPIOA_ModeCfg(GPIO_Pin_3, GPIO_ModeOut_PP_5mA);
GPIOA_SetBits(GPIO_Pin_3);
// CSB2: PB9
GPIOB_ResetBits(GPIO_Pin_9);
GPIOB_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);
GPIOB_ResetBits(GPIO_Pin_9);
// WF5803_CSB1_HIGH();
// CSB3: PB4
GPIOB_ResetBits(GPIO_Pin_4);
GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeOut_PP_5mA);
GPIOB_ResetBits(GPIO_Pin_9);
// CSB4: PB17
GPIOB_SetBits(GPIO_Pin_17);
GPIOB_ModeCfg(GPIO_Pin_17, GPIO_ModeOut_PP_5mA);
// WF5803_CSB1_HIGH();
// PRINT("WF5803_CSB1_HIGH()\r\n");
// spi³õʼ»¯£¬Ä£Ê½0
GPIOA_ModeCfg(GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14, GPIO_ModeOut_PP_5mA);
GPIOA_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_PU);
#endif
// PRINT("GPIOA_ModeCfg ok\r\n");
SPI0_MasterDefInit();
// PRINT("SPI1_MasterDefInit ok\r\n");
SPICs_Stop();
// PRINT("SPICs_Stop ok\r\n");
WF5803_WriteReg(0x00, 0x81); // ÅäÖÃspiΪËÄÏßģʽ
// PRINT("WF5803_WriteReg ok\r\n");
// PRINT("SPI_Init ok\r\n");
}
void WF5803_DeInit(void)
{
// GPIOA_ModeCfg(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6, GPIO_ModeIN_PD);
// GPIOA_ModeCfg(GPIO_Pin_2, GPIO_ModeIN_PD);
// GPIOA_ResetBits(GPIO_Pin_4);
// GPIOA_ResetBits(GPIO_Pin_5);
// GPIOA_ResetBits(GPIO_Pin_12);
// GPIOA_ModeCfg(GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12, GPIO_ModeIN_PD);
}
long reading = 0;
float press = 0;
float temp = 0;
float fDat = 0;
float press_air = 101.0f;
int a = 1;
uint8_t RawData[5];
uint8_t *GetSensorData(void)
{
memset(RawData, 0, sizeof(uint8_t) * 5);
// WF5803_WriteReg(0x30, 0x0a);
// DelayMs(50);
PRINT("DRDTY = %02x\r\n", WF5803_ReadReg(0x02));
// ÆøÑ¹Êý¾Ý
RawData[0] = WF5803_ReadReg(0x06);
RawData[1] = WF5803_ReadReg(0x07);
RawData[2] = WF5803_ReadReg(0x08);
// ζÈÊý¾Ý
RawData[3] = WF5803_ReadReg(0x09);
RawData[4] = WF5803_ReadReg(0x0a);
// for(int i=0; i<5; i++)
// {
// PRINT("RawData[%d] = %x\r\n", i,RawData[i]);
// }
return RawData;
}
void WF5803_Start(void)
{
WF5803_WriteReg(0x21, 0x47); // ¿ªÆôÆøÑ¹ºÍζȴ«¸ÐÆ÷
WF5803_WriteReg(0x27, 0x41); // ¿ªÆôÆøÑ¹ºÍζȴ«¸ÐÆ÷
}
void SensorData_Process(void)
{
uint8_t *p1 = GetSensorData();
reading = p1[0];
reading = reading << 8;
reading |= p1[1];
reading = reading << 8;
reading |= p1[2];
if (reading >= 8388608)
{
fDat = (int32_t)(reading - 16777216) / 8388608.0f;
}
else
{
fDat = reading / 8388608.0f;
}
press = fDat * 125 + 17.5; // WF5803_1BAR Èç¹ûÊÇʹÓÃ10m¼¶±ðµÄÉî´«¾ÍʹÓÃÕâ¸ö
// if (a) {press_air = press ; a = 0;}
// press = press_air > press ? press_air : press;
//
// int deep_100 = (int)(100*((press - press_air)/0.098f));
// //printf("P=%d.%d pa\r\n",(int)press,((int)(press*10000.0f)%10000));`
// printf("D=%d.%d cm\r\n",deep_100/100,deep_100%100);
reading = p1[3];
reading = reading << 8;
reading |= p1[4];
if (reading > 32768)
{
temp = (reading - 65844) / 256.0f;
}
else
{
temp = (reading - 308) / 256.0f;
}
// PRINT("--s-1--\r\n");
PRINT("P=%d.%d pa\r\n", (int)(press * 1000), ((int)(press * 10000000.0f) % 10000));
PRINT("T=%d.%d \r\n", (int)temp, ((int)(temp * 100.0f) % 100));
}
uint16_t wf5803_ProcessEvent(uint8_t task_id, uint16_t events)
{
if (events & WF5803_EVT_START)
{
// SensorData_Process();
// WF5803_WriteReg(0x30, 0x0a);
WF5803_WriteReg(0x30, 0x0A);
tmos_start_task(Peripheral_TaskID, WF5803_EVT_READ, MS1_TO_SYSTEM_TIME(50));
return (events ^ WF5803_EVT_START);
}
else if (events & WF5803_EVT_READ)
{
SensorData_Process();
// WF5803_WriteReg(0x30, 0x0a);
tmos_start_task(Peripheral_TaskID, WF5803_EVT_START, MS1_TO_SYSTEM_TIME(800));
return (events ^ WF5803_EVT_READ);
}
return 0;
}
void BSPWF5803_Init(void)
{
WF5803_Init();
Peripheral_TaskID = TMOS_ProcessEventRegister(wf5803_ProcessEvent);
// WF5803_WriteReg(0x30, 0x2B);
tmos_set_event(Peripheral_TaskID, WF5803_EVT_START);
// tmos_start_task(Peripheral_TaskID, WF5803_EVT_READ, MS1_TO_SYSTEM_TIME(50));
// PRINT("WF5803_Init\r\n");
}