PC端:WIN10
开发软件:Keil 5.26
开发板:GD32F310G8
一、搭建环境出现的问题
1、pack包下载和安装。
下载链接:http://www.gd32mcu.com/cn/dow...
2、pack包打开的2种方法。
①、双击,一般都会有反应,但是我电脑没反应,可能是运行内存或者磁盘内存不够了。然后我就去keil安装目录下用packUnzip.exe来安装
②、使用pack install导入的方法
3、找不到GD32的对应flash算法
安装pack包好了之后就下载一个官方写好的LED例程,看能不能烧写进去,但是这是候又有问题了,就是找不到GD32的对应flash算法了。
就pack包也安装好了,下载的仿真器GD-LINK也连接上了,就是没有对应芯片的下载算法,就离谱。。。。。。搞了我好久。。。
然后就百度搜呀搜,然后看到其它的什么其它flash算法文件在这个文件路径下面
然后我灵光一闪,我不是可以把GD32的flash算法文件考到这下面就行了。
然后它就被我硬生生从这里
拷到了这里
然后它就出现了
然后下载就行了
淦,一个简单的东西让我搞了这么久,让我还以为是别人一直说的版本问题,还好我没下。
二、USART
①、PA9-Tx、PA10-Rx串口功能配置
void uart_init(void)
{
//使能时钟
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
//rcu_periph_clock_enable(RCU_AF);
//Tx PA9 复用推挽输出
gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
//Rx PA10
gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
nvic_irq_enable(USART0_IRQn,0,0);
gpio_af_set(GPIOA,GPIO_AF_1,GPIO_PIN_9|GPIO_PIN_10);
/*USART 设置*/
usart_deinit(USART0);
usart_baudrate_set(USART0,115200);
usart_word_length_set(USART0,USART_WL_8BIT);
usart_stop_bit_set(USART0,USART_STB_1BIT);
usart_parity_config(USART0,USART_PM_NONE);
usart_hardware_flow_rts_config(USART0,USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART0,USART_CTS_DISABLE);
usart_receive_config(USART0,USART_RECEIVE_ENABLE);
usart_transmit_config(USART0,USART_TRANSMIT_ENABLE);
usart_enable(USART0);
usart_interrupt_enable(USART0,USART_INT_RBNE);
usart_interrupt_enable(USART0, USART_INT_IDLE); /* 使能USART0空闲中断 */
}
②、串口中断函数
uint16_t t=0;
void USART0_IRQHandler(void)
{
if(RESET!=usart_interrupt_flag_get(USART0,USART_INT_FLAG_RBNE))
{
gd_led_toggle();
t=usart_data_receive(USART0);//接收数据
//usart_data_transmit(USART0,USART_RX[t]);
//usart_interrupt_disable(USART0,USART_INT_RBNE);
usart_data_transmit(USART0,t);
}else if(RESET!=usart_interrupt_flag_get(USART0,USART_INT_FLAG_IDLE))
{
usart_interrupt_disable(USART0,USART_INT_IDLE);
}
}
③、串口打印函数
//#include<stdio.h>
//printf("Hello,GD32F310G8");
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t) ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
③、实现现象
https://www.bilibili.com/vide...
④、硬件连接
三、TIMER1实现LED闪烁
①、定时器配置
//定时器初始化和配置
//arr:重装载值 prescaler:预分频值
//
//72MHz/(prescaler/2)*arr(5000)=1000ms
void gd_timer0_config(uint32_t arr,uint16_t prescaler)
{
timer_parameter_struct timer0initpara;
/* enable the timer0 clock */
rcu_periph_clock_enable(RCU_TIMER1);//挂在AHB=72MHz上面
/* 初始化timer0 定时器参数,设置自动重装载值,分频系数,计数方式 */
timer0initpara.clockdivision=TIMER_CKDIV_DIV1;//分频因子
timer0initpara.alignedmode=TIMER_COUNTER_EDGE;//边沿对齐
timer0initpara.counterdirection=TIMER_COUNTER_UP;//向上计数
timer0initpara.period=arr;//自动重新加载值
timer0initpara.prescaler=prescaler;//预分频值 if=1时就是72Mhz else=72/(prescaler/2)
//timer0initpara.repetitioncounter=;//重复计数器值
timer_init(TIMER1,&timer0initpara);
//允许更新中断
timer_interrupt_enable(TIMER1,TIMER_INT_UP);
//TIMER0中断优先级设置
nvic_irq_enable(TIMER1_IRQn,1,1);//抢占优先级,响应优先级
//使能TIME0
timer_enable(TIMER1);
}
②、定时器中断
void TIMER1_IRQHandler(void)
{
if(SET==timer_interrupt_flag_get(TIMER1,TIMER_INT_UP))
{
gd_led_toggle();
}
timer_interrupt_flag_clear(TIMER1,TIMER_INT_UP);
}
③实现现象
https://www.bilibili.com/vide...
四、全部代码
#include "gd32f3x0.h"
#include "gd32f310g_start.h"
#include "systick.h"
#include "gd32f3x0_timer.h"
#include "gd32f3x0_usart.h"
#include "gd32f3x0_gpio.h"
#include "stdio.h"
#define SIZE 200
uint32_t USART_RX[SIZE];
void gd_led_config(void);
void gd_led_toggle(void);
void gd_timer0_config(uint32_t arr,uint16_t prescaler);
void uart_init(void);
void pwm_init(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
gd_led_config();
systick_config();
//定时器让LED以500ms闪烁
gd_timer0_config(500000-1,144-1);//72MHz/(144/2)=1us 1us*1000=1ms 1us*500000=500ms
uart_init();
pwm_init();
while(1)
{
printf("Hello,GD32F310G8\n");
delay_1ms(500);
//gd_led_toggle();
delay_1ms(500);
}
}
//PA8 TIMER0_CH0
void pwm_init(void)
{
timer_parameter_struct timer0initpara;
timer_oc_parameter_struct timerocinitpara;
//使能时钟
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_TIMER0);
//PA8 复用TIMER0 推挽输出
gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_af_set(GPIOA,GPIO_AF_2,GPIO_PIN_8);
/* enable the timer0 clock */
rcu_periph_clock_enable(RCU_TIMER1);//挂在AHB=72MHz上面
/* 初始化timer0 定时器参数,设置自动重装载值,分频系数,计数方式 */
timer0initpara.clockdivision=TIMER_CKDIV_DIV1;//分频因子
timer0initpara.alignedmode=TIMER_COUNTER_EDGE;//边沿对齐
timer0initpara.counterdirection=TIMER_COUNTER_UP;//向上计数
timer0initpara.period=500000-1;//自动重新加载值
timer0initpara.prescaler=144-1;//预分频值 if=1时就是72Mhz else=72/(prescaler/2)
timer_init(TIMER0,&timer0initpara);
//TIMER0_CH0初始化
timerocinitpara.ocidlestate=TIMER_OC_IDLE_STATE_LOW;
timerocinitpara.ocnidlestate=TIMER_OCN_IDLE_STATE_LOW;
timerocinitpara.ocpolarity=TIMER_OC_POLARITY_HIGH;
timerocinitpara.ocnpolarity=TIMER_OCN_POLARITY_HIGH;
timerocinitpara.outputstate=TIMER_CCX_ENABLE;
timerocinitpara.outputnstate=TIMER_CCXN_DISABLE;
timer_channel_output_config(TIMER0,TIMER_CH_0,&timerocinitpara);
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,2000);//占空比
timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);//
timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);//
timer_primary_output_config(TIMER0,ENABLE);
//使能TIME0
timer_enable(TIMER0);
}
void uart_init(void)
{
//使能时钟
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
//rcu_periph_clock_enable(RCU_AF);
//Tx PA9 复用推挽输出
gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
//Rx PA10
gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
nvic_irq_enable(USART0_IRQn,0,0);
gpio_af_set(GPIOA,GPIO_AF_1,GPIO_PIN_9|GPIO_PIN_10);
/*USART 设置*/
usart_deinit(USART0);
usart_baudrate_set(USART0,115200);
usart_word_length_set(USART0,USART_WL_8BIT);
usart_stop_bit_set(USART0,USART_STB_1BIT);
usart_parity_config(USART0,USART_PM_NONE);
usart_hardware_flow_rts_config(USART0,USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART0,USART_CTS_DISABLE);
usart_receive_config(USART0,USART_RECEIVE_ENABLE);
usart_transmit_config(USART0,USART_TRANSMIT_ENABLE);
usart_enable(USART0);
usart_interrupt_enable(USART0,USART_INT_RBNE);
usart_interrupt_enable(USART0, USART_INT_IDLE); /* 使能USART0空闲中断 */
}
uint16_t t=0;
void USART0_IRQHandler(void)
{
if(RESET!=usart_interrupt_flag_get(USART0,USART_INT_FLAG_RBNE))
{
gd_led_toggle();
t=usart_data_receive(USART0);//接收数据
//usart_data_transmit(USART0,USART_RX[t]);
//usart_interrupt_disable(USART0,USART_INT_RBNE);
usart_data_transmit(USART0,t);
}else if(RESET!=usart_interrupt_flag_get(USART0,USART_INT_FLAG_IDLE))
{
usart_interrupt_disable(USART0,USART_INT_IDLE);
}
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t) ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
/*!
\brief configure TIMER0
\param[in] none
\param[out] none
\retval none
*/
//定时器初始化和配置
//arr:重装载值 prescaler:预分频值
//
//72MHz/(prescaler/2)*arr(5000)=1000ms
void gd_timer0_config(uint32_t arr,uint16_t prescaler)
{
timer_parameter_struct timer0initpara;
/* enable the timer0 clock */
rcu_periph_clock_enable(RCU_TIMER1);//挂在AHB=72MHz上面
/* 初始化timer0 定时器参数,设置自动重装载值,分频系数,计数方式 */
timer0initpara.clockdivision=TIMER_CKDIV_DIV1;//分频因子
timer0initpara.alignedmode=TIMER_COUNTER_EDGE;//边沿对齐
timer0initpara.counterdirection=TIMER_COUNTER_UP;//向上计数
timer0initpara.period=arr;//自动重新加载值
timer0initpara.prescaler=prescaler;//预分频值 if=1时就是72Mhz else=72/(prescaler/2)
//timer0initpara.repetitioncounter=;//重复计数器值
timer_init(TIMER1,&timer0initpara);
//允许更新中断
timer_interrupt_enable(TIMER1,TIMER_INT_UP);
//TIMER0中断优先级设置
nvic_irq_enable(TIMER1_IRQn,1,1);//抢占优先级,响应优先级
//使能TIME0
timer_enable(TIMER1);
}
void TIMER1_IRQHandler(void)
{
if(SET==timer_interrupt_flag_get(TIMER1,TIMER_INT_UP))
{
gd_led_toggle();
}
timer_interrupt_flag_clear(TIMER1,TIMER_INT_UP);
}
/*!
\brief configure led
\param[in] none
\param[out] none
\retval none
*/
void gd_led_config(void)
{
/* enable the led clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* configure led GPIO port */
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
GPIO_BC(GPIOA) = GPIO_PIN_1;
}
/*!
\brief toggle led
\param[in] none
\param[out] none
\retval none
*/
void gd_led_toggle(void)
{
GPIO_TG(GPIOA) = GPIO_PIN_1;
}
五、总结
剩下的有时间再写了。。。。。