GD32F303ZET6三种低功耗例程
睡眠模式例程:MCU的UART3接收到数据 ,进入UART3接收中断 即唤醒睡眠模式。
int main(void)
{
/******** 本实验测试单片机睡眠模式 *********/
/* 功能:串口中断唤醒 唤醒后闪灯 闪烁5次 进入睡眠模式 */
int FlashingCount =0;
//init uart3
Modbus2_Init(9600);
//init led
gd_eval_led_init(LED2); //run light
while(1){
my_delay(1000);
//取反
gd_eval_led_toggle(LED2);
FlashingCount ++;
if(FlashingCount >= 10){
FlashingCount = 0;
gd_eval_led_off(LED2);
//进入睡眠模式
rcu_periph_clock_enable(RCU_PMU);
pmu_to_sleepmode(WFI_CMD);
}
}
}
void Modbus2_Init(unsigned int baud) //UART3
{
dma_parameter_struct dma_init_struct;
//初始化串口
/* enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOC); //tx PC10 rx PC11
/* enable USART clock */
rcu_periph_clock_enable(RCU_UART3);
/* connect port to USARTx_Tx */
gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* connect port to USARTx_Rx */
gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
/* USART configure */
usart_deinit(UART3);
usart_baudrate_set(UART3, baud);
usart_receive_config(UART3, USART_RECEIVE_ENABLE);
usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
//开启接收中断
usart_interrupt_enable(UART3, USART_INT_RBNE);
//中断注册
nvic_irq_enable(UART3_IRQn, 1U, 1U);
usart_enable(UART3);
// DMA使能
// /* enable DMA0 */
rcu_periph_clock_enable(RCU_DMA1);
/* deinitialize DMA channel4(UART3 tx) */
dma_deinit(DMA1, DMA_CH4); //对应STM32 DMA2 - 通道5
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
dma_init_struct.memory_addr = (uint32_t)(Modbus2_DataCtrl.SendData);
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = Modbus2_DataLen; //发送前 需要修改的数据
dma_init_struct.periph_addr = ((uint32_t)0x40004C04); //(unsigned int)USART_DATA(UART3);
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA1, DMA_CH4, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH4);
dma_memory_to_memory_disable(DMA1, DMA_CH4);
//没有设置DMA中断
/* DMA0 通道3 中断优先级设置并使能 */
nvic_irq_enable(DMA1_Channel3_Channel4_IRQn, 1, 1);
/* 使能 DMA0 通道3 传输完成、传输错误中断 */
dma_interrupt_enable(DMA1, DMA_CH4, DMA_INT_FTF|DMA_INT_ERR);
/* enable USART0 DMA channel transmission and reception */
dma_channel_enable(DMA1, DMA_CH4);
/* USART DMA enable for transmission and reception */
usart_dma_transmit_config(UART3, USART_DENT_ENABLE); //发送
}
void UART3_IRQHandler(void)
{
unsigned char ReceiveData = 0;
if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_RBNE) != RESET){
ReceiveData = usart_data_receive(UART3);
usart_interrupt_flag_clear(UART3, USART_INT_FLAG_RBNE);
}
//空闲中断 帧中断
if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_IDLE) != RESET) {
ReceiveData = usart_data_receive(UART3); //清除空闲中断
}
}
void gd_eval_led_init (led_typedef_enum lednum)
{
/* enable the led clock */
rcu_periph_clock_enable(GPIO_CLK[lednum]);
/* configure led GPIO port */
gpio_init(GPIO_PORT[lednum], GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN[lednum]);
GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
}
深度睡眠模式例程:
int main(void)
{
/******** 本实验测试单片机深度睡眠模式 *********/
/* 功能:深度睡眠模式需要EXTI的任何中断来唤醒 唤醒后闪灯 闪烁5次 进入深度睡眠模式
本测试设置PE3外部中断 下降沿触发 唤醒MCU
*/
int FlashingCount =0;
//init uart3
Modbus2_Init(9600);
//init led
gd_eval_led_init(LED2); //run light
//init PE3
init_PE3();
while(1){
my_delay(1000);
//取反
gd_eval_led_toggle(LED2);
FlashingCount ++;
if(FlashingCount >= 10){
FlashingCount = 0;
gd_eval_led_off(LED2);
//进入停止模式
rcu_periph_clock_enable(RCU_PMU);
pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
//notice:WFI need EXTI interrupt wakeup
//恢复HSE时钟
SYSCLKConfig_STOP();
}
}
}
void init_PE3(void)
{
//enable clock
rcu_periph_clock_enable(RCU_GPIOE);
rcu_periph_clock_enable(RCU_AF);
//init pe3 io
gpio_init(GPIOE, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
//set interrut
/* enable and set key EXTI interrupt to the lowest priority */
nvic_irq_enable(EXTI3_IRQn, 2U, 0U);
/* connect key EXTI line to key GPIO pin */
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOE, GPIO_PIN_SOURCE_3);
/* configure key EXTI line */
exti_init(EXTI_3, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
exti_interrupt_flag_clear(EXTI_3);
}
void EXTI3_IRQHandler(void)
{
if(RESET != exti_interrupt_flag_get(EXTI_3)){
gd_eval_led_toggle(LED2);
}
exti_interrupt_flag_clear(EXTI_3);
}
待机模式例程:
int main(void)
{
/******** 本实验测试单片机待机模式 *********/
/* 功能:待机模式唤醒方式:WKUP引脚上升沿(接3.3V 即可 已测试)
复位引脚(已测试)
独立看门狗中断
RTC闹钟
唤醒后程序重新执行。LED灯 闪烁5次 进入待机模式
*/
int FlashingCount =0;
//init uart3
Modbus2_Init(9600);
//init led
gd_eval_led_init(LED2); //run light
while(1){
my_delay(1000);
//取反
gd_eval_led_toggle(LED2);
FlashingCount ++;
if(FlashingCount >= 10){
FlashingCount = 0;
gd_eval_led_off(LED2);
//进入停止模式
rcu_periph_clock_enable(RCU_PMU);
pmu_wakeup_pin_enable();
pmu_to_standbymode(WFI_CMD); //notice:WFI need EXTI interrupt wakeup
}
}
}
版权声明:本文为CSDN博主「静水长安」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lubianshiyi/article/details/122395378
GD32F303ZET6三种低功耗例程
睡眠模式例程:MCU的UART3接收到数据 ,进入UART3接收中断 即唤醒睡眠模式。
int main(void)
{
/******** 本实验测试单片机睡眠模式 *********/
/* 功能:串口中断唤醒 唤醒后闪灯 闪烁5次 进入睡眠模式 */
int FlashingCount =0;
//init uart3
Modbus2_Init(9600);
//init led
gd_eval_led_init(LED2); //run light
while(1){
my_delay(1000);
//取反
gd_eval_led_toggle(LED2);
FlashingCount ++;
if(FlashingCount >= 10){
FlashingCount = 0;
gd_eval_led_off(LED2);
//进入睡眠模式
rcu_periph_clock_enable(RCU_PMU);
pmu_to_sleepmode(WFI_CMD);
}
}
}
void Modbus2_Init(unsigned int baud) //UART3
{
dma_parameter_struct dma_init_struct;
//初始化串口
/* enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOC); //tx PC10 rx PC11
/* enable USART clock */
rcu_periph_clock_enable(RCU_UART3);
/* connect port to USARTx_Tx */
gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* connect port to USARTx_Rx */
gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
/* USART configure */
usart_deinit(UART3);
usart_baudrate_set(UART3, baud);
usart_receive_config(UART3, USART_RECEIVE_ENABLE);
usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
//开启接收中断
usart_interrupt_enable(UART3, USART_INT_RBNE);
//中断注册
nvic_irq_enable(UART3_IRQn, 1U, 1U);
usart_enable(UART3);
// DMA使能
// /* enable DMA0 */
rcu_periph_clock_enable(RCU_DMA1);
/* deinitialize DMA channel4(UART3 tx) */
dma_deinit(DMA1, DMA_CH4); //对应STM32 DMA2 - 通道5
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
dma_init_struct.memory_addr = (uint32_t)(Modbus2_DataCtrl.SendData);
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = Modbus2_DataLen; //发送前 需要修改的数据
dma_init_struct.periph_addr = ((uint32_t)0x40004C04); //(unsigned int)USART_DATA(UART3);
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA1, DMA_CH4, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH4);
dma_memory_to_memory_disable(DMA1, DMA_CH4);
//没有设置DMA中断
/* DMA0 通道3 中断优先级设置并使能 */
nvic_irq_enable(DMA1_Channel3_Channel4_IRQn, 1, 1);
/* 使能 DMA0 通道3 传输完成、传输错误中断 */
dma_interrupt_enable(DMA1, DMA_CH4, DMA_INT_FTF|DMA_INT_ERR);
/* enable USART0 DMA channel transmission and reception */
dma_channel_enable(DMA1, DMA_CH4);
/* USART DMA enable for transmission and reception */
usart_dma_transmit_config(UART3, USART_DENT_ENABLE); //发送
}
void UART3_IRQHandler(void)
{
unsigned char ReceiveData = 0;
if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_RBNE) != RESET){
ReceiveData = usart_data_receive(UART3);
usart_interrupt_flag_clear(UART3, USART_INT_FLAG_RBNE);
}
//空闲中断 帧中断
if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_IDLE) != RESET) {
ReceiveData = usart_data_receive(UART3); //清除空闲中断
}
}
void gd_eval_led_init (led_typedef_enum lednum)
{
/* enable the led clock */
rcu_periph_clock_enable(GPIO_CLK[lednum]);
/* configure led GPIO port */
gpio_init(GPIO_PORT[lednum], GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN[lednum]);
GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
}
深度睡眠模式例程:
int main(void)
{
/******** 本实验测试单片机深度睡眠模式 *********/
/* 功能:深度睡眠模式需要EXTI的任何中断来唤醒 唤醒后闪灯 闪烁5次 进入深度睡眠模式
本测试设置PE3外部中断 下降沿触发 唤醒MCU
*/
int FlashingCount =0;
//init uart3
Modbus2_Init(9600);
//init led
gd_eval_led_init(LED2); //run light
//init PE3
init_PE3();
while(1){
my_delay(1000);
//取反
gd_eval_led_toggle(LED2);
FlashingCount ++;
if(FlashingCount >= 10){
FlashingCount = 0;
gd_eval_led_off(LED2);
//进入停止模式
rcu_periph_clock_enable(RCU_PMU);
pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
//notice:WFI need EXTI interrupt wakeup
//恢复HSE时钟
SYSCLKConfig_STOP();
}
}
}
void init_PE3(void)
{
//enable clock
rcu_periph_clock_enable(RCU_GPIOE);
rcu_periph_clock_enable(RCU_AF);
//init pe3 io
gpio_init(GPIOE, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
//set interrut
/* enable and set key EXTI interrupt to the lowest priority */
nvic_irq_enable(EXTI3_IRQn, 2U, 0U);
/* connect key EXTI line to key GPIO pin */
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOE, GPIO_PIN_SOURCE_3);
/* configure key EXTI line */
exti_init(EXTI_3, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
exti_interrupt_flag_clear(EXTI_3);
}
void EXTI3_IRQHandler(void)
{
if(RESET != exti_interrupt_flag_get(EXTI_3)){
gd_eval_led_toggle(LED2);
}
exti_interrupt_flag_clear(EXTI_3);
}
待机模式例程:
int main(void)
{
/******** 本实验测试单片机待机模式 *********/
/* 功能:待机模式唤醒方式:WKUP引脚上升沿(接3.3V 即可 已测试)
复位引脚(已测试)
独立看门狗中断
RTC闹钟
唤醒后程序重新执行。LED灯 闪烁5次 进入待机模式
*/
int FlashingCount =0;
//init uart3
Modbus2_Init(9600);
//init led
gd_eval_led_init(LED2); //run light
while(1){
my_delay(1000);
//取反
gd_eval_led_toggle(LED2);
FlashingCount ++;
if(FlashingCount >= 10){
FlashingCount = 0;
gd_eval_led_off(LED2);
//进入停止模式
rcu_periph_clock_enable(RCU_PMU);
pmu_wakeup_pin_enable();
pmu_to_standbymode(WFI_CMD); //notice:WFI need EXTI interrupt wakeup
}
}
}
版权声明:本文为CSDN博主「静水长安」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lubianshiyi/article/details/122395378
暂无评论