875|2

3423

帖子

1

资源

版主

【测评SGP40】+UART通信+at32f407开发板使用 [复制链接]

本帖最后由 damiaa 于 2021-2-17 21:43 编辑

【测评SGP40】+UART通信+at32f407开发板使用 

1,准备at32f407开发板1块,连接它的串口2和传感器( P0.5 == 传感器PIN3   P0.6 == 传感器PIN4 ,传感器的VCC PIN1和地 PIN2 接到开发板的VCC和地)

     at32f407 VCC   to  传感器的VCC  PIN1

     at32f407  地      to  传感器的地      PIN2

     USART2 Tx pin (PD.05) to 传感器PIN3
     USART2 Rx pin (PD.06) to 传感器PIN4

微信图片_20210217211923.jpg 2,准备程序:

定义了一个串口收发数据的结构体
typedef struct
{
	uint8_t TxBuffer[200];
	uint8_t RxBuffer[200];
    uint16_t NbrOfDataToTransfer;
	uint16_t NbrOfDataToRead;
	uint16_t TxCounter; 
	uint16_t RxCounterIn;
	uint16_t RxCounterOut;	
}TXDEF;
主程序中定义TXDEF tx1,tx2,tx3;

初始化串口1,2,3未用到。

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 */
  

中断里发送和接收处理。

void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_INT_RDNE) != RESET)
  {
    /* Read one byte from the receive data register */
    tx1.RxBuffer[tx1.RxCounterIn++] = (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[tx1.TxCounter++]);
    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[tx2.RxCounterIn++] = 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[tx2.TxCounter++]);
    if(tx2.TxCounter == tx2.NbrOfDataToTransfer)
    {
      /* Disable the USART2 Transmit interrupt */
      USART_INTConfig(USART2, USART_INT_TDE, DISABLE);
    }    
  }
}

主程序里发送开启测量命令一次,

svm40_start_measurement 开启测量

每次读都发送 svm40_get_signals 命令


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[200];
//uint8_t buf2[200];
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[200];
	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[cont++] = tx2.RxBuffer[tx2.RxCounterOut++];
    }
	// buf2[5] buf2[6] 
	//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[5];
	*pvoc <<=8;
	*pvoc += buf2[6];
	*pvoc =*pvoc / 10;
	//buf2[7] buf2[8] 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[7];
	*phumi <<=8;
    *phumi += buf2[8];
	//buf2[9] buf2[10] int16 provides the temperature (in °C) with a scaling factor of 200, e.g., an output of +5’000 corresponds to +25.00  C
	*ptemp = buf2[9];
	*ptemp <<=8;
	*ptemp += buf2[10];
	*ptemp = *ptemp/2;
}	
void tx_send(TXDEF *tx,uint8_t *ptxbuf,uint8_t num)
{
	for(uint8_t i=0;i<num;i++)
		tx->TxBuffer =*(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 Measurement  svm40_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, 1  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.
//2, 3  int16 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, 5  int16 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));
}	

编译运行连接putty查看结果:

24.PNG

 效果还行。

 

 

 

此帖出自传感器论坛

回复

7954

帖子

20

资源

版主

室温15摄氏度,不冻手吗?

个人签名虾扯蛋,蛋扯虾,虾扯蛋扯虾

回复

3423

帖子

1

资源

版主

littleshrimp 发表于 2021-2-21 13:03 室温15摄氏度,不冻手吗?

还好。是有一点。春节这个温度已经不容易了。

点评

nmg
南方人,就是抗冻  详情 回复 发表于 2021-2-26 11:32

回复

4303

帖子

233

资源

管理员

damiaa 发表于 2021-2-23 09:14 littleshrimp 发表于 2021-2-21 13:03 室温15摄氏度,不冻手吗? 还好。是有一点。春节这个温度已经 ...

南方人,就是抗冻


回复
您需要登录后才可以回帖 登录 | 注册

最新文章 更多>>
    关闭
    站长推荐上一条 1/4 下一条

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

    电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2021 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表