【平头哥RVB2601创意应用开发】电源监控
<p>【前言】感谢@<a href="https://home.eeworld.com.cn/space-uid-383818.html" target="_blank">sipower</a>为我提供串口1更换IO,复用启动串口1。<a href="https://bbs.eeworld.com.cn/thread-1200906-1-1.html">【平头哥RVB2601创意应用开发】环境监测终端05-温湿度采集和显示 - 平头哥RISC-V RVB2601活动专区 - 电子工程世界-论坛 (eeworld.com.cn)</a>根据他的帖子,这两天成功调通了串口。因为只有一个串口可以用,所以把方案更改为用无线串口接收到模块来接收信息。</p><p>【材料准备】1、电能计量监控模块;2、无线串口接收模块。</p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle">【实现步骤】</p>
<p class="imagemiddle">1、串口1的实始化:</p>
<pre>
<code>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;
}
</code></pre>
<p> 2、建立串口收发任务:</p>
<pre>
<code>/*
* 功能:创建串口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);
if (rx_size ==24)
{
if((uart_data_buf == 0x55) && (uart_data_buf == 0x55))
{
Vrms=(double)(((uint32_t)uart_data_buf<<24)|((uint32_t)uart_data_buf<<16)|((uint32_t)uart_data_buf<<8)|((uint32_t)uart_data_buf<<0))/1000.0;
Irms=(double)(((uint32_t)uart_data_buf<<24)|((uint32_t)uart_data_buf<<16)|((uint32_t)uart_data_buf<<8)|((uint32_t)uart_data_buf<<0))/1000.0;
PActive=(double)(((uint32_t)uart_data_buf<<24)|((uint32_t)uart_data_buf<<16)|((uint32_t)uart_data_buf<<8)|((uint32_t)uart_data_buf<<0))/1000.0;
Frequency=(double)(((uint32_t)uart_data_buf<<24)|((uint32_t)uart_data_buf<<16)|((uint32_t)uart_data_buf<<8)|((uint32_t)uart_data_buf<<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);
}
/* 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);
}
</code></pre>
<p>3、任务启动后由u8g2定时刷新显示:</p>
<pre>
<code>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);</code></pre>
<p>4、实验成果:</p>
<p> </p>
<p class="imagemiddle" style="text-align: center;"> </p>
<p class="imagemiddle" style="text-align: center;"> </p>
<p> 【讨论】经过快一个多月的学习,基本上项目快成形了。下一步采集温度用无线串口发送给RVB2601。基本上就快可以交作业了。<br />
</p>
<p><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&lessonid=33158" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
视频添加如下:有时间显示,IP地址显示、信号强度显示:</p>
<p> </p>
<p> </p>
<p></p>
<p>谢谢分享</p> <p>这个电能计上面是不是有一个电流互感器呀?</p>
梦溪开物 发表于 2022-5-11 16:05
这个电能计上面是不是有一个电流互感器呀?
<p>这个东西呀,他有电源互感器,还有个电压互感器。</p>
<p>不错,学习学习<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan41.gif" width="48" /></p>
页:
[1]