【测评SGP40】+UART通信+at32f407开发板使用
本帖最后由 damiaa 于 2021-2-17 21:43 编辑<p>【测评SGP40】+UART通信+at32f407开发板使用 </p>
<p>1,准备at32f407开发板1块,连接它的串口2和传感器( P0.5 == 传感器PIN3 P0.6 == 传感器PIN4 ,传感器的VCC PIN1和地 PIN2 接到开发板的VCC和地)</p>
<p> at32f407 VCC to 传感器的VCC PIN1</p>
<p> at32f407 地 to 传感器的地 PIN2</p>
<p> USART2 Tx pin (PD.05) to 传感器PIN3<br />
USART2 Rx pin (PD.06) to 传感器PIN4</p>
<p>2,准备程序:</p>
<pre>
<code>定义了一个串口收发数据的结构体
typedef struct
{
uint8_t TxBuffer;
uint8_t RxBuffer;
uint16_t NbrOfDataToTransfer;
uint16_t NbrOfDataToRead;
uint16_t TxCounter;
uint16_t RxCounterIn;
uint16_t RxCounterOut;
}TXDEF;
主程序中定义TXDEF tx1,tx2,tx3;</code></pre>
<p>初始化串口1,2,3未用到。</p>
<pre>
<code>int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* Initialize Leds mounted on board */
AT32_Board_Init();
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
/* Configure USART2 */
USART_Init(USART2, &USART_InitStructure);
/* Configure USART3 */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
USART_INTConfig(USART1, USART_INT_RDNE, ENABLE);
// USART_INTConfig(USART1, USART_INT_TDE, ENABLE);
/* Enable USART2 Receive and Transmit interrupts */
USART_INTConfig(USART2, USART_INT_RDNE, ENABLE);
USART_INTConfig(USART2, USART_INT_TDE, ENABLE);
/* Enable USART3 Receive and Transmit interrupts */
USART_INTConfig(USART3, USART_INT_RDNE, ENABLE);
USART_INTConfig(USART3, USART_INT_TDE, ENABLE);
USART_Cmd(USART1, ENABLE); /* Enable the USART1 */
USART_Cmd(USART2, ENABLE); /* Enable the USART2 */
USART_Cmd(USART3, ENABLE); /* Enable the USART3 */
</code></pre>
<p>中断里发送和接收处理。</p>
<pre>
<code>void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_INT_RDNE) != RESET)
{
/* Read one byte from the receive data register */
tx1.RxBuffer = (USART_ReceiveData(USART1) & 0x7F);
if(tx1.RxCounterIn == tx1.NbrOfDataToRead)
{
tx1.RxCounterIn =0;
}
}
if(USART_GetITStatus(USART1, USART_INT_TDE) != RESET)
{
/* Write one byte to the transmit data register
USART_SendData(USART1, tx1.TxBuffer);
if(tx1.TxCounter == tx1.NbrOfDataToTransfer)
{
/* Disable the EVAL_COM1 Transmit interrupt */
USART_INTConfig(USART1, USART_INT_TDE, DISABLE);
}
}
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_INT_RDNE) != RESET)
{
/* Read one byte from the receive data register */
tx2.RxBuffer = USART_ReceiveData(USART2);
if(tx2.RxCounterIn == tx2.NbrOfDataToRead)
{
tx2.RxCounterIn =0;
}
}
if(USART_GetITStatus(USART2, USART_INT_TDE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART2, tx2.TxBuffer);
if(tx2.TxCounter == tx2.NbrOfDataToTransfer)
{
/* Disable the USART2 Transmit interrupt */
USART_INTConfig(USART2, USART_INT_TDE, DISABLE);
}
}
}
</code></pre>
<p>主程序里发送开启测量命令一次,</p>
<p>svm40_start_measurement 开启测量</p>
<p>每次读都发送 svm40_get_signals 命令</p>
<pre>
<code>
svm40_start_measurement();
delayms(500);
static uint8_t cont=0;
static uint16_t tempH,tempL,temp, humiH,humiL,humi,voc;
uint8_t buf_T[]={"Temperature is :\n\r"};
uint8_t buf_H[]={"Humidity is :\n\r"};
uint8_t buf_V[]={"Noxious gas is :\n\r"};
uint8_t buf_K[]={"\n\r"};
uint8_t buf1;
//uint8_t buf2;
while(1)
{
delayms(1000);
tx2.RxCounterIn = tx2.RxCounterOut=0;
read_svm40(&voc,&humi,&temp);
humiH = humi / 100;
humiL = humi %100;
tempH =temp / 100;
tempL =temp % 100;
tx_send(&tx1,buf_T,sizeof(buf_T));
delayms(10);
sprintf(buf1,"%2d.%2d\n\r",tempH,tempL);
tx_send(&tx1,buf1,10);
delayms(10);
tx_send(&tx1,buf_H,sizeof(buf_H));
delayms(10);
sprintf(buf1,"%2d.%2d\n\r",humiH,humiL);
tx_send(&tx1,buf1,10);
delayms(10);
tx_send(&tx1,buf_V,sizeof(buf_V));
delayms(10);
sprintf(buf1,"%4d \n\r",voc);
tx_send(&tx1,buf1,10);
delayms(10);
tx_send(&tx1,buf_K,sizeof(buf_K));
delayms(10);
tx_send(&tx1,buf_K,sizeof(buf_K));
delayms(10);
tx_send(&tx1,buf_K,sizeof(buf_K));
delayms(10);
}
void read_svm40(uint16_t *pvoc,uint16_t *phumi,uint16_t *ptemp)
{
uint8_t buf2;
svm40_get_signals();
delayms(200);
uint8_t cont=0;
//read SWVM40
//0x7E 0x00 0x03 0x00 0x06 0x00 0x00 0x00 0x00 0x00 0x00 0xF6 0x7E
while(tx2.RxCounterIn != tx2.RxCounterOut)
{
buf2 = tx2.RxBuffer;
}
// buf2 buf2
//int16 provides the VOC Index (no unit) with a scaling factor of 10, e.g., an output of +250 corresponds to a VOC Index of +25.0.
*pvoc = buf2;
*pvoc <<=8;
*pvoc += buf2;
*pvoc =*pvoc / 10;
//buf2 buf2 int16 provides the relative humidity (in % RH) compensated for the temperature offset with a scaling factor of 100, e.g., an output of +2500 corresponds to +25.00 % RH.
*phumi = buf2;
*phumi <<=8;
*phumi += buf2;
//buf2 buf2 int16 provides the temperature (in °C) with a scaling factor of 200, e.g., an output of +5’000 corresponds to +25.00C
*ptemp = buf2;
*ptemp <<=8;
*ptemp += buf2;
*ptemp = *ptemp/2;
}
void tx_send(TXDEF *tx,uint8_t *ptxbuf,uint8_t num)
{
for(uint8_t i=0;i<num;i++)
tx->TxBuffer<i> =*(ptxbuf+i);
tx->TxCounter =0;
tx->NbrOfDataToTransfer =num;
if(tx == &tx1)
USART_INTConfig(USART1, USART_INT_TDE, ENABLE);
if(tx == &tx2)
USART_INTConfig(USART2, USART_INT_TDE, ENABLE);
if(tx == &tx3)
USART_INTConfig(USART3, USART_INT_TDE, ENABLE);
}
//Start Measurementsvm40_start_measurement
//This command triggers the operation mode of all sensors. It must be called once prior to the
//svm40_get_signals or svm40_get_raw_signals commands, respectively. Per default the SVM40 starts in idle
//mode after powering up.
//0x7E 0x00 0x00 0x01 0x00 0xFE 0x7E
//0x7E 0x00 0x00 0x00 0x00 0xFF 0x7E
void svm40_start_measurement(void)
{
uint8_t buf2[]={0x7E, 0x00, 0x00, 0x01, 0x00, 0xFE, 0x7E};
tx_send(&tx2,buf2,sizeof(buf2));
}
//Get Signals
//this command reads out VOC Index, relative humidity, and temperature. It returns 6 bytes.
//0, 1int16 provides the VOC Index (no unit) with a scaling factor of 10, e.g., an output of +250
//corresponds to a VOC Index of +25.0.
//2, 3int16 provides the relative humidity (in % RH) compensated for the temperature offset with a
//scaling factor of 100, e.g., an output of +2’500 corresponds to +25.00 % RH.
//4, 5int16 provides the temperature (in °C) with a scaling factor of 200, e.g., an output of +5’000
//corresponds to +25.00 °C.
//0x7E 0x00 0x03 0x01 0x0A 0xF1 0x7E
//0x7E 0x00 0x03 0x00 0x06 0x00 0x00 0x00 0x00 0x00 0x00 0xF6 0x7E
void svm40_get_signals(void)
{
uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0A, 0xF1, 0x7E};
tx_send(&tx2,buf2,sizeof(buf2));
} </i></code></pre>
<p><i>编译运行连接putty查看结果:</i></p>
<p><i></i></p>
<p><i> 效果还行。</i></p>
<p><i> </i></p>
<p><i> </i></p>
<p><i> </i></p>
<p>室温15摄氏度,不冻手吗?</p>
<div class="quote">
<blockquote><font size="2"><a href="forum.php?mod=redirect&goto=findpost&pid=3042753&ptid=1156884" target="_blank"><font color="#999999">littleshrimp 发表于 2021-2-21 13:03</font></a></font> 室温15摄氏度,不冻手吗?</blockquote>
</div>
<p>还好。是有一点。春节这个温度已经不容易了。</p>
damiaa 发表于 2021-2-23 09:14
littleshrimp 发表于 2021-2-21 13:03 室温15摄氏度,不冻手吗?
还好。是有一点。春节这个温度已经 ...
<p>南方人,就是抗冻</p>
页:
[1]