由于RA4M2的低功耗只做到了6个毫安,这里利用RA2E1进行低功耗采集,并用无线串口模块进行传输。
1、 建立工程:
依次按提示创建,完成以后如下图:
2、添加串口、HS3003详见我以前写的帖子:
【新提醒】【RA4M2设计挑战赛】HS3003读取数据
3、配置RTC模块:
这里要开启alarm中断,并设置回调函数。
4、配置低功耗模块:
主要原理是数据采集完后,进入睡眠模式,由闹钟定时唤醒,唤醒后采集完数据,继续睡眠。
Hs3003采集部分:
void hs300x_data_entry(void)
{
fsp_err_t err;
uint8_t r_buf[4] = {0};
int i;
uint8_t tx_buf[tx_buf_len] = {0x5A,0xA5,0x00,0x00,0x00,0x00,0xAA,0x0A};
uint8_t data[2] = {0x00,0x00};
g_comms_i2c_bus0_quick_setup();
err = R_IIC_MASTER_Write(&g_i2c_master0_ctrl,data,1,0);
R_BSP_SoftwareDelay(30000,BSP_DELAY_UNITS_MICROSECONDS);
err = R_IIC_MASTER_Read(&g_i2c_master0_ctrl,&r_buf,4,1);
if(err == FSP_SUCCESS)
{
if ((r_buf[0] & RM_HS300X_MASK_STATUS_0XC0) != RM_HS300X_DATA_STATUS_VALID)
{
printf("turn time out");
}
printf("\n");
tx_buf[2] = r_buf[0];
tx_buf[3] = r_buf[1];
tx_buf[4] = r_buf[2];
tx_buf[5] = r_buf[3];
for(i=0; i<8; i++)
printf("%c",tx_buf[i]);
}
else {
printf("read hs3003 erro!\n");
}
}
这里采集,为了节约能量,只做采集,由上位机来做温度换算。
hal_entry.c
首先要配置rtc,以及初始化时间:
/* rtc_time_t is an alias for the C Standard time.h struct 'tm' */
rtc_time_t set_time =
{
.tm_sec = 0, /* 秒,范围从 0 到 59 */
.tm_min = 30, /* 分,范围从 0 到 59 */
.tm_hour = 12, /* 小时,范围从 0 到 23*/
.tm_mday = 20, /* 一月中的第几天,范围从 1 到 31*/
.tm_mon = 11, /* 月份,范围从 0 到 11*/
.tm_year = 121, /* 自 1900 起的年数,2021为121*/
.tm_wday = 5, /* 一周中的第几天,范围从 0 到 6*/
// .tm_yday=0, /* 一年中的第几天,范围从 0 到 365*/
// .tm_isdst=0; /* 夏令时*/
};
rtc_alarm_time_t set_alarm_time=
{
.time.tm_sec = 5,
.time.tm_min = 30, /* 分,范围从 0 到 59 */
.time.tm_hour = 12, /* 小时,范围从 0 到 23*/
.time.tm_mday = 20, /* 一月中的第几天,范围从 1 到 31*/
.time.tm_mon = 11, /* 月份,范围从 0 到 11*/
.time.tm_year = 121, /* 自 1900 起的年数,2021为121*/
.time.tm_wday = 5, /* 一周中的第几天,范围从 0 到 6*/
.sec_match = 1,
.min_match = 0,
.hour_match = 0,
.mday_match = 0,
.mon_match = 0,
.year_match = 0,
.dayofweek_match = 0,
};
volatile bool rtc_flag = 0;//RTC延时1s标志位
volatile bool rtc_alarm_flag = 0;//RTC闹钟
/* Callback function */
void rtc_callback(rtc_callback_args_t *p_args)
{
/* TODO: add your own code here */
if(p_args->event == RTC_EVENT_PERIODIC_IRQ)
rtc_flag=1;
else if(p_args->event == RTC_EVENT_ALARM_IRQ)
rtc_alarm_flag=1;
}
打开RTC,并且设置闹钟唤醒,这里设置为一分钟一次。
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* R_RTC_CalendarTimeSet must be called at least once to start the RTC */
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time);
主循环里,先把E31的M0M1拉低,从休眠状态醒来准备发送数据,发送完成转到M1模式,然后RA2E1也进入睡眠模式:
if(rtc_alarm_flag)
{
rtc_alarm_flag=0;
//两个IO拉低,E31进入发射模式
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_14, BSP_IO_LEVEL_LOW);
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_15, BSP_IO_LEVEL_LOW);
hs300x_data_entry();
//两个IO拉高,E31进入低功耗模式。
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_14, BSP_IO_LEVEL_HIGH);
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_15, BSP_IO_LEVEL_HIGH);
/*打开LPM,进入深度软件待机模式*/
R_LPM_Open (g_lpm0.p_ctrl, g_lpm0.p_cfg);
R_LPM_LowPowerModeEnter (g_lpm0.p_ctrl);
}
}
每一分钟醒来一次,采集数据并发送。待机功耗为1ma左右。
【小结】RA2E1可以实现低功耗采集,整个采集模块待机1ma左右,基本能满足项目需求,如果投入商用,可以再做较调,官方的数是80微安。加上E31+HS3003,我的目标是200微安的待机功耗。
更多回帖