【前言】感谢@sipower为我提供串口1更换IO,复用启动串口1。【平头哥RVB2601创意应用开发】环境监测终端05-温湿度采集和显示 - 平头哥RISC-V RVB2601活动专区 - 电子工程世界-论坛 (eeworld.com.cn)根据他的帖子,这两天成功调通了串口。因为只有一个串口可以用,所以把方案更改为用无线串口接收到模块来接收信息。
【材料准备】1、电能计量监控模块;2、无线串口接收模块。
【实现步骤】
1、串口1的实始化:
static int my_uart1_init(void)
{
int ret = -1;
uart1.port = UART1_PORT_NUM;
csi_pin_set_mux(PA27, PA27_UART1_RX); //复用端口设置
csi_pin_set_mux(PA28, PA28_UART1_TX);
/* uart attr config */
uart1.config.baud_rate = 9600;
uart1.config.data_width = DATA_WIDTH_8BIT;
uart1.config.parity = NO_PARITY;
uart1.config.stop_bits = STOP_BITS_1;
uart1.config.flow_control = FLOW_CONTROL_DISABLED;
uart1.config.mode = MODE_TX_RX;
/* init uart1 with the given settings */
ret = hal_uart_init(&uart1);
if (ret != 0) {
printf("uart1 init error !\n");
}
else{
printf("uart1 init succsecc !\n");
}
return ret;
}
2、建立串口收发任务:
/*
* 功能:创建串口1接收任务,接收数据并把数据解析出来,存放在buff中,由U8g2显示函数定时刷新。
*
*
*/
static void application_task1_entry(void *arg)
{
int ret = -1;
uint32_t rx_size = 0;
float Irms=0; //电流有效值
float Vrms=0; //电压有效值
float Frequency=0; //频率
float PowerFactor=1;//功率因数
float PActive=0; //有功功率
double W_KWH=0; //累积功耗
int n=0;
while (1) {
ret = hal_uart_recv_II(&uart1, uart_data_buf, 24,
&rx_size, UART_RX_TIMEOUT);
LOGD(TAG, "RECV len:%d data:%x",rx_size,uart_data_buf[0]);
if (rx_size == 24)
{
if((uart_data_buf[0] == 0x55) && (uart_data_buf[0] == 0x55))
{
Vrms=(double)(((uint32_t)uart_data_buf[6]<<24)|((uint32_t)uart_data_buf[7]<<16)|((uint32_t)uart_data_buf[8]<<8)|((uint32_t)uart_data_buf[9]<<0))/1000.0;
Irms=(double)(((uint32_t)uart_data_buf[10]<<24)|((uint32_t)uart_data_buf[11]<<16)|((uint32_t)uart_data_buf[12]<<8)|((uint32_t)uart_data_buf[13]<<0))/1000.0;
PActive=(double)(((uint32_t)uart_data_buf[14]<<24)|((uint32_t)uart_data_buf[15]<<16)|((uint32_t)uart_data_buf[16]<<8)|((uint32_t)uart_data_buf[17]<<0))/1000.0;
Frequency=(double)(((uint32_t)uart_data_buf[18]<<24)|((uint32_t)uart_data_buf[19]<<16)|((uint32_t)uart_data_buf[n++]<<8)|((uint32_t)uart_data_buf[20]<<0))/1000.0;
sprintf(disp_Vrms,"%.1fV",Vrms);
sprintf(disp_Irms,"%.2fA",Irms);
sprintf(disp_PActive,"%.2fW",PActive);
sprintf(disp_Frequency,"%.2fHz",Frequency);
}
LOGD(TAG, "RECV len:%d data:%x",rx_size,uart_data_buf[0]);
}
/* sleep 100ms */
aos_msleep(50);
//memset(uart_data_buf,0,UART_BUF_SIZE);
}
aos_task_exit(0);
}
/*
* 功能:创建串口1发送任务,向电源监控模块发送获取监控数据的命令
*
*
*
*/
static void application_task2_entry(void *arg)
{
int ret = -1;
uint8_t uart_tx_buf[] = {0x55,0x55,0x01,0x02,0x00,0x00,0xAD};
while (1) {
ret = hal_uart_send(&uart1, uart_tx_buf, sizeof(uart_tx_buf), UART_TX_TIMEOUT);
if (ret == 0) {
printf("uart1 data send succeed !\n");
}
aos_msleep(1000);
}
aos_task_exit(0);
}
3、任务启动后由u8g2定时刷新显示:
sprintf(disp,"%02d-%02d %02d:%02d:%02d",this_time.tm_mon+1,this_time.tm_mday,this_time.tm_hour,this_time.tm_min,this_time.tm_sec);
u8g2_DrawStr(&u8g2,10,60,disp);
u8g2_SetFont(&u8g2,u8g2_font_7x13_tr);
u8g2_DrawStr(&u8g2,4,24,disp_Vrms);//显示电压值
u8g2_DrawStr(&u8g2,80,24,disp_Irms);//显示电压值
u8g2_DrawStr(&u8g2,4,40,disp_PActive);//显示功率值
u8g2_SendBuffer(&u8g2);
4、实验成果:
【讨论】经过快一个多月的学习,基本上项目快成形了。下一步采集温度用无线串口发送给RVB2601。基本上就快可以交作业了。
|