首先感谢21ic和小管家举办的这个活动【第1期】你选开发板,二姨家买单;很荣幸因为得到了这次活动的中奖名额,很抱歉这么晚才发,实在是太忙(lan)了。 好了闲话少说,还是开启这次主题任务。 本次的开发板是兆易的开发板“GD32F103C8”,为什么选择这款开发板呢,其实原因有三。第一,因为最近ST芯片涨价实在太多,做小项目预算不够;第二,兆易的芯片和ST的基本兼容,上手没有难度;第三,这个开发板提供了CAN接口,和一些代码,可以学习下。因此,众多理由让我选择了这款开发板。到手之后,果然没让我失望。接下来就来欣赏这款开发板的样子吧。 接下来就是搭环境了,其实GD32的开发环境和STM32的其实一样,也是安装MDK,推荐大家从米尔科技下载(下载中心 - 米尔科技 (myir-tech.com));GD32的文档和资料可以从兆易官网下载(兆易创新 (gd32mcu.com));我们需要下载MDK的pack包(GD32F1x0 AddOn)并安装,固件库(GD32F1x0 Firmware Library),固件库中包含了一些例程。 开始用DEMO测试,新建工程,芯片选择GD32F103C8,创建完成工程后,添加固件库中的库,然后把例程中LED驱动,串口驱动,定时器驱动都移植过来,然后编译下载测试。LED灯顺利亮灭,串口也打印出信息。 兆易的固件库例程给的很详细,基本不用改什么,只要把管脚替换下就OK了。具体代码如下: /*!
\file main.c
\brief led spark with systick, USART print and key example
\version 2014-12-26, V1.0.0, firmware for GD32F10x
\version 2017-06-20, V2.0.0, firmware for GD32F10x
\version 2018-07-31, V2.1.0, firmware for GD32F10x
\version 2020-09-30, V2.2.0, firmware for GD32F10x
*/
/*
Copyright (c) 2020, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "gd32f10x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
void delay(void)
{
int i=0, j = 0;
for(i=0; i<100; i++)
for(j=0; j<100000; j++);
}
void led_config(void)
{
/* enable the led clock */
rcu_periph_clock_enable(RCU_GPIOC);
/* configure led GPIO port */
gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_14);
GPIO_BC(GPIOC) = GPIO_PIN_14;
}
void timer_config(void)
{
/* ----------------------------------------------------------------------------
TIMER1 Configuration:
TIMER1CLK = SystemCoreClock/10800 = 10KHz, the period is 1s(10000/10000 = 1s).
---------------------------------------------------------------------------- */
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER1);
timer_deinit(TIMER1);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&timer_initpara);
/* TIMER1 configuration */
timer_initpara.prescaler = 10799;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER1, &timer_initpara);
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER1, TIMER_INT_UP);
timer_enable(TIMER1);
}
void nvic_config(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(TIMER1_IRQn, 1, 1);
}
void TIMER1_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP)){
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
gpio_bit_write(GPIOC, GPIO_PIN_14, (bit_status)(1-gpio_input_bit_get(GPIOC, GPIO_PIN_14)));
}
}
void usart_config(void)
{
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* connect port to USARTx_Tx */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/* connect port to USARTx_Rx */
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* USART configure */
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200U);
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);
}
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t)ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
void init(void)
{
led_config();
timer_config();
nvic_config();
usart_config();
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
init();
delay();
printf("a usart transmit test example!");
while(1)
{
GPIO_BOP(GPIOC) = GPIO_PIN_14;
delay();
GPIO_BC(GPIOC) = GPIO_PIN_14;
delay();
}
}
最后感谢小管家这个给力的活动。
|