收到开发板有几天了,下面看看收到的开发板图
开发板内还附带了一根OTG的转接USB线。烧写器是ES-LINK II,带USB转串口调试功能。
首先进行的是IO端口的点灯,按键和串口输出测试。通过SDK内工程模板新建一个工程。在开发板上可以清晰的看到2个LED灯对应端口为PF0,PF1。
一个5向按键,对应PC10,PC11,PC12,PB2,PB12。串口对应的是PB10,PB11。
在程序中针对这些IO端口初始化,以及串口初始化。
然后main函数中添加点灯程序和按键扫描程序。主函数如下,很简单。
int main()
{
uint32_t ms,ledms;
uint32_t key_val,key_old,key_xor;
/* Initialize ALD */
ald_cmu_init();
/* Configure system clock */
ald_cmu_pll1_config(CMU_PLL1_INPUT_HOSC_3, CMU_PLL1_OUTPUT_96M);
ald_cmu_clock_config(CMU_CLOCK_PLL1, 96000000);
/* Enable peripheral clock */
ald_cmu_perh_clock_config(CMU_PERH_GPIO, ENABLE);
ald_cmu_perh_clock_config(CMU_PERH_UART0, ENABLE);
uart_stdio_init();
memset(uart_buf, 0x0, 64);
light_init();
key_init();
printf_e("System start...\n");
light_ctrl(LIGHT_IDX_2, LIGHT_CTRL_ON);
ald_delay_ms(1000);
light_ctrl(LIGHT_IDX_2, LIGHT_CTRL_OFF);
ald_delay_ms(1000);
ms = ald_get_tick()+10;
ledms = ald_get_tick()+1000;
key_old = 0;
while (1)
{
//按键扫描
if(ms < ald_get_tick())
{
ms = ald_get_tick()+10;
key_val = 0;
if(ald_gpio_read_pin(KEY_UP_PORT, KEY_UP_PIN) == 0) key_val |= 0x01;
if(ald_gpio_read_pin(KEY_DOWN_PORT, KEY_DOWN_PIN) == 0) key_val |= 0x02;
if(ald_gpio_read_pin(KEY_LEFT_PORT, KEY_LEFT_PIN) == 0) key_val |= 0x04;
if(ald_gpio_read_pin(KEY_RIGHT_PORT, KEY_RIGHT_PIN) == 0) key_val |= 0x08;
if(ald_gpio_read_pin(KEY_CENTER_PORT, KEY_CENTER_PIN) == 0) key_val |= 0x10;
key_xor = key_old ^ key_val;
if(key_xor & 0x01)
{
if(key_val &0x01)
{
printf_e("key up press +\n");
}else
{
printf_e("key up relese-\n");
}
}
if(key_xor & 0x02)
{
if(key_val &0x02)
{
printf_e("key down press +\n");
}else
{
printf_e("key down relese-\n");
}
}
if(key_xor & 0x04)
{
if(key_val &0x04)
{
printf_e("key left press +\n");
}else
{
printf_e("key left relese-\n");
}
}
if(key_xor & 0x08)
{
if(key_val &0x08)
{
printf_e("key right press +\n");
}else
{
printf_e("key right relese-\n");
}
}
if(key_xor & 0x10)
{
if(key_val &0x10)
{
printf_e("key center press +\n");
}else
{
printf_e("key center relese-\n");
}
}
key_old = key_val;
}
if(ledms < ald_get_tick())
{
ledms = ald_get_tick()+1000;
light_ctrl(LIGHT_IDX_1, LIGHT_CTRL_TOGGLE);
light_ctrl(LIGHT_IDX_2, LIGHT_CTRL_TOGGLE);
printf_e("Count: %d\n", __cnt++);
}
}
}
void uart_stdio_init(void)
{
gpio_init_t x;
/* Initialize tx pin */
x.mode = GPIO_MODE_OUTPUT;
x.odos = GPIO_PUSH_PULL;
x.pupd = GPIO_PUSH_UP;
x.podrv = GPIO_OUT_DRIVE_6;
x.nodrv = GPIO_OUT_DRIVE_6;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_TTL;
x.func = UART_FUNC_TX;
ald_gpio_init(UART_PORT_TX, UART_PIN_TX, &x);
/* Initialize rx pin */
x.mode = GPIO_MODE_INPUT;
x.odos = GPIO_PUSH_PULL;
x.pupd = GPIO_PUSH_UP;
x.podrv = GPIO_OUT_DRIVE_6;
x.nodrv = GPIO_OUT_DRIVE_6;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_TTL;
x.func = UART_FUNC_RX;
ald_gpio_init(UART_PORT_RX, UART_PIN_RX, &x);
/* Initialize uart */
h_uart.perh = UART0;
h_uart.init.baud = 115200;
h_uart.init.word_length = UART_WORD_LENGTH_8B;
h_uart.init.stop_bits = UART_STOP_BITS_1;
h_uart.init.parity = UART_PARITY_NONE;
h_uart.init.mode = UART_MODE_UART;
h_uart.init.fctl = UART_HW_FLOW_CTL_DISABLE;
h_uart.tx_cplt_cbk = NULL;
h_uart.rx_cplt_cbk = NULL;
h_uart.error_cbk = NULL;
ald_uart_init(&h_uart);
return;
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Initialize key.
* @retval None
*/
void key_init(void)
{
gpio_init_t x;
x.mode = GPIO_MODE_INPUT;
x.pupd = GPIO_PUSH_DOWN;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_CMOS;
x.odos = GPIO_PUSH_PULL;
x.nodrv = GPIO_OUT_DRIVE_6;
x.podrv = GPIO_OUT_DRIVE_6;
x.func = GPIO_FUNC_1;
ald_gpio_init(KEY_UP_PORT, KEY_UP_PIN, &x);
ald_gpio_init(KEY_DOWN_PORT, KEY_DOWN_PIN, &x);
ald_gpio_init(KEY_LEFT_PORT, KEY_LEFT_PIN, &x);
ald_gpio_init(KEY_RIGHT_PORT, KEY_RIGHT_PIN, &x);
ald_gpio_init(KEY_CENTER_PORT, KEY_CENTER_PIN, &x);
}
/**
* @brief Initialize light.
* @retval None
*/
void light_init(void)
{
gpio_init_t x;
x.mode = GPIO_MODE_OUTPUT;
x.odos = GPIO_PUSH_PULL;
x.pupd = GPIO_PUSH_UP;
x.podrv = GPIO_OUT_DRIVE_6;
x.nodrv = GPIO_OUT_DRIVE_6;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_CMOS;
x.func = GPIO_FUNC_1;
ald_gpio_init(LED1_PORT, LED1_PIN, &x);
ald_gpio_init(LED2_PORT, LED2_PIN, &x);
light_ctrl(LIGHT_IDX_1, LIGHT_CTRL_OFF);
light_ctrl(LIGHT_IDX_2, LIGHT_CTRL_OFF);
return;
}
效果如上图。代码很简单就不上传了。
|