昨天晚上迫不及待的想要做一个基础工程模板,后面自己使用起来方便一点,昨天晚上做的代码,今天分享一下。由于比较简单,这里仅仅展示代码和效果。
具体的代码还是比较简单的
下面这一段是对LED的初始化以及闪灯的操作,我们保留了官方程序的时钟配置和延时的操作,并且对延时函数进行了封装。
#include "at32f435_437_clock.h"
#include "DRV_Delay.h"
void LED_Init(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE ); /* enable the led clock */
crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE );
gpio_default_para_init(&gpio_init_struct);/* set default parameter */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; /* configure the led gpio */
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins =GPIO_PINS_13 |GPIO_PINS_14|GPIO_PINS_15;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOD, &gpio_init_struct);
}
void LED_Flash(void)
{
gpio_bits_set(GPIOD,GPIO_PINS_13);
gpio_bits_set(GPIOD,GPIO_PINS_14);
gpio_bits_set(GPIOD,GPIO_PINS_15);
Delay_ms(100);
gpio_bits_reset(GPIOD,GPIO_PINS_13);
gpio_bits_reset(GPIOD,GPIO_PINS_14);
gpio_bits_reset(GPIOD,GPIO_PINS_15);
Delay_ms(100);
}
int main(void)
{
system_clock_config();
Delay_init();
LED_Init();
while(1)
{
LED_Flash();
}
}
下面的是延时函数的封装为了更好的移植,这里对原有的数据定义进行了修改,
#include "DRV_Delay.h"
#define STEP_DELAY_MS 50
/* delay variable */
unsigned int fac_us;
unsigned int fac_ms;
void Delay_init(void)
{
/* configure systick */
systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
fac_us = system_core_clock / (1000000U);
fac_ms = fac_us * (1000U);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] inserts a delay time.
* @param nus: specifies the delay time length, in microsecond.
* @retval none
*/
void Delay_us(unsigned int nus)
{
unsigned int temp = 0;
SysTick->LOAD = (unsigned int )(nus * fac_us);
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
do
{
temp = SysTick->CTRL;
}while((temp & 0x01) && !(temp & (1 << 16)));
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}
/**
* @brief inserts a delay time.
* @param nms: specifies the delay time length, in milliseconds.
* @retval none
*/
void Delay_ms(unsigned short nms)
{
unsigned int temp = 0;
while(nms)
{
if(nms > STEP_DELAY_MS)
{
SysTick->LOAD = (unsigned int )(STEP_DELAY_MS * fac_ms);
nms -= STEP_DELAY_MS;
}
else
{
SysTick->LOAD = (unsigned int )(nms * fac_ms);
nms = 0;
}
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
do
{
temp = SysTick->CTRL;
}while((temp & 0x01) && !(temp & (1 << 16)));
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}
}
void Delay_sec(unsigned short sec)
{
unsigned short index;
for(index = 0; index < sec; index++)
{
Delay_ms(500);
Delay_ms(500);
}
}
下面看看效果,就是闪灯,翼辉将代码上传上来。
工程代码:
AT32F437_Demo.rar
(2.72 MB)
|