PC端:WIN10
开发软件:Keil 5.26
开发板:GD32F310G8
一、搭建环境出现的问题
1、pack包下载和安装。
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);
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);
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_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);
}
②、串口中断函数
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,t);
}else if(RESET!=usart_interrupt_flag_get(USART0,USART_INT_FLAG_IDLE))
{
usart_interrupt_disable(USART0,USART_INT_IDLE);
}
}
③、串口打印函数
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t) ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
```④、硬件连接
![1649561010.jpg](
**三、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上面
timer0initpara.clockdivision=TIMER_CKDIV_DIV1;
timer0initpara.alignedmode=TIMER_COUNTER_EDGE;
timer0initpara.counterdirection=TIMER_COUNTER_UP;
timer0initpara.period=arr;
timer0initpara.prescaler=prescaler;
timer_init(TIMER1,&timer0initpara);
timer_interrupt_enable(TIMER1,TIMER_INT_UP);
nvic_irq_enable(TIMER1_IRQn,1,1);
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);
}
#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();
gd_timer0_config(500000-1,144-1);
uart_init();
pwm_init();
while(1)
{
printf("Hello,GD32F310G8\n");
delay_1ms(500);
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);
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);
rcu_periph_clock_enable(RCU_TIMER1);
timer0initpara.clockdivision=TIMER_CKDIV_DIV1;
timer0initpara.alignedmode=TIMER_COUNTER_EDGE;
timer0initpara.counterdirection=TIMER_COUNTER_UP;
timer0initpara.period=500000-1;
timer0initpara.prescaler=144-1;
timer_init(TIMER0,&timer0initpara);
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);
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);
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);
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_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);
}
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上面
timer0initpara.clockdivision=TIMER_CKDIV_DIV1;
timer0initpara.alignedmode=TIMER_COUNTER_EDGE;
timer0initpara.counterdirection=TIMER_COUNTER_UP;
timer0initpara.period=arr;
timer0initpara.prescaler=prescaler;
timer_init(TIMER1,&timer0initpara);
timer_interrupt_enable(TIMER1,TIMER_INT_UP);
nvic_irq_enable(TIMER1_IRQn,1,1);
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;
}
原作者:兆易创新GD32 MCU 陈有乐