前段时间出差,耽误了更新,今天来补上,主要做了星空派的串口和定时器的功能测试,但是,针对GD的定时器,还是有一些问题值得说一下的,
1:GD32的定时器预分频只有部分的定时器可用
2:部分的定时器模式设置需要注意
我们具体来看一下这个吧。
- void timer_init(uint32_t timer_periph, timer_parameter_struct* initpara)
- {
- /* configure the counter prescaler value */
- TIMER_PSC(timer_periph) = (uint16_t)initpara->prescaler;
- /* configure the counter direction and aligned mode */
- if((TIMER0 == timer_periph) || (TIMER1 == timer_periph) || (TIMER2 == timer_periph)
- || (TIMER3 == timer_periph) || (TIMER4 == timer_periph) || (TIMER7 == timer_periph)){
- TIMER_CTL0(timer_periph) &= ~(uint32_t)(TIMER_CTL0_DIR|TIMER_CTL0_CAM);
- TIMER_CTL0(timer_periph) |= (uint32_t)initpara->alignedmode;
- TIMER_CTL0(timer_periph) |= (uint32_t)initpara->counterdirection;
- }
- /* configure the autoreload value */
- TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
- if((TIMER5 != timer_periph) && (TIMER6 != timer_periph)){
- /* reset the CKDIV bit */
- TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_CKDIV;
- TIMER_CTL0(timer_periph) |= (uint32_t)initpara->clockdivision;
- }
- if((TIMER0 == timer_periph) || (TIMER7 == timer_periph))
- {
- /* configure the repetition counter value */
- TIMER_CREP(timer_periph) = (uint32_t)initpara->repetitioncounter;
- }
- /* generate an update event */
- TIMER_SWEVG(timer_periph) |= (uint32_t)TIMER_SWEVG_UPG;
- }
复制代码
这个是GD给出的库函数,里面对于各个定时器的功能做了限制,即使你在初始化结构体的时候设置了参数也不一定起作用,拿预分频来说,我首先设置了预分频为2分频和4分频,结果是一样的,我们再将GD的库函数内部的限制去掉,还是同样的效果,那么我们就会很容易的得出一个结论,GD的部分定时器在设计之初是没有将预分频的功能设计进去的,不知道是出于什么原因。所以开发的时候还是要注意一下。
定时器的使用还是点灯,这里就简单的吧代码放一下就行。
#include "SysTime.h"
void SysTime_Init(void)
{
//--Timer 1 的主时钟树是60M,
timer_parameter_struct timer_parameter;
rcu_periph_clock_enable(RCU_TIMER1);
//预分频
timer_parameter.prescaler = 1999,
//对齐模式
timer_parameter.alignedmode = TIMER_COUNTER_EDGE,
//定时器增长方向
timer_parameter.counterdirection = TIMER_COUNTER_UP,
//定时器自动加载值--设置为
timer_parameter.period = 59999,
//时钟分频值--设置为不分频
timer_parameter.clockdivision = TIMER_CKDIV_DIV1,
timer_init(TIMER1, &timer_parameter);
timer_interrupt_enable(TIMER1, TIMER_INT_UP);
nvic_irq_enable(TIMER1_IRQn, 0, 2);
timer_enable(TIMER1);
}
unsigned long int SysTimeCount=0;
void TIMER1_IRQHandler()
{
if (timer_interrupt_flag_get(TIMER1, TIMER_INT_UP) != RESET)
{
SysTimeCount++;
timer_interrupt_flag_clear(TIMER1, TIMER_INT_UP);
}
}
#include "gd32f30x.h" #include "Led.h" #include "UART.h" #include "SysTime.h" #include "Sys_Struct.h"
void TimeTest(void) { int i=0; if(SysTimeCount%2==0) { for(i=0;i { rcu_periph_clock_enable(Bord_Info.Sys_LED.CLK ); gpio_bit_reset(Bord_Info.Sys_LED.Port,Bord_Info.Sys_LED.Pin); Delay_ms(500); }; } else { for(i=0;i { rcu_periph_clock_enable(Bord_Info.Sys_LED.CLK ); gpio_bit_set(Bord_Info.Sys_LED.Port,Bord_Info.Sys_LED.Pin); Delay_ms(500); }; } }
int main(void) { LED_Port_Init(); key_init(); Uart_init(); SysTime_Init(); while(1) { // LED_Flash(); // KEY_Test(); // UART_Test(); TimeTest();
} }
串口方面其实还是很简单的,这里就不多说,直接上代码吧。
- #include "UART.h"
- void Uart_init(void)
- {
- /* enable GPIO clock */
- rcu_periph_clock_enable(RCU_GPIOA);
- /* enable USART clock */
- rcu_periph_clock_enable(RCU_USART0);
- /* 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, 9600);
- usart_word_length_set(USART0,USART_WL_8BIT);
- usart_parity_config(USART0,USART_PM_NONE);
- usart_stop_bit_set(USART0, USART_STB_1BIT);
- //--
- usart_receive_config(USART0, USART_RECEIVE_ENABLE);
- usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
- usart_enable(USART0);
- // nvic_irq_enable(USART0_IRQn, 0, 0);
- // /* 使能 USART0 接收中断 */
- // usart_interrupt_enable(USART0, USART_INT_RBNE);
-
- }
- void UART1_SendChar(unsigned char Data)
- {
- usart_data_transmit(USART0,(unsigned char)Data);
- while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
- }
- void UART1_SendStr( unsigned char *Data,unsigned int Len)
- {
- int i=0;
- for(i=0;i
- {
- UART1_SendChar(Data[i]);
- }
- }
- void USART0_IRQHandler(void)
- {
- if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
- {
- usart_interrupt_flag_clear(USART0,USART_INT_FLAG_RBNE);
- }
-
- if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE))
- {
- usart_interrupt_flag_clear(USART0,USART_INT_FLAG_TBE);
- }
- }
- void UART_Test(void)
- {
- unsigned char Test[]={" 星空派 rn GD32Fxx UART_Test Demo!rn"};
- UART1_SendStr( Test,sizeof(Test));
- }
- /*---------------------------------*/
复制代码
|