在官网的例程提供了一个串口通信功能的例程,但比较奇葩的是其在硬件方面的设计,由于板上已丝印了GD-Link的提示,故在使用时不会产生歧异;在供电方面,也在切换开关作为标注“DC”和“Link”。然,其虚拟串口的使用就怪异了,居然竟不能使用虚拟串口端的USB来供电,也不能借用GD-Link来形成虚拟串口。其结果就是完成一个简单的串口通信测试却要同时使用2条USB线,见图1所示。 在不了解这些之前,是怎么也鼓捣不出虚拟串口的! 图1 串口通信测试连接 在鼓捣出虚拟串口后,测试就比较简单了。将程序编译下载后,即可见到图2所示的内容,即每按一下USER用户键,LED1就亮一下,与此同时通过串口也会输出一个“USART printf example”。 file:///C:/Users/fengqili/AppData/Local/Temp/msohtmlclip1/01/clip_image003.gif
图2 串口输出内容 那么其程序是如何设计的呢? 实现要点亮LED1,就需要对其定义和初始化,其初始化函数为: - void led_init(void)
- {
- gd_eval_led_init(LED1);
- }
复制代码
而使LED1闪烁的函数则如下: - void led_flash(int times)
- {
- int i;
- for(i=0; i<times; i++)
- {
- /* delay 400 ms */
- delay_1ms(400);
- /* turn on LEDs */
- gd_eval_led_on(LED1);
- /* delay 400 ms */
- delay_1ms(400);
- /* turn off LEDs */
- gd_eval_led_off(LED1);
- }
- }
复制代码 也就是说,每隔400毫秒就切换一次状态。 对于串行通讯来讲,则涉及到收与发,在发送时所使用的是经重新定向的printf语句,而接收则是通过fputc函数,其内容如下: - int fputc(int ch, FILE *f)
- {
- usart_data_transmit(EVAL_COM1, (uint8_t)ch);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- return ch;
- }
复制代码
最后再看一下主程序,功能不多但程序却写的很臃肿,其内容如下: - int main(void)
- {
- /* initialize the LEDs */
- led_init();
- /* configure systick */
- systick_config();
- /* flash the LEDs for 1 time */
- led_flash(1);
- /* configure EVAL_COM1 */
- gd_eval_com_init(EVAL_COM1);
- /* configure TAMPER key */
- gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
- /* output a message on hyperterminal using printf function */
- printf("\r\n USART printf example: please press the User key \r\n");
- /* wait for completion of USART transmission */
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TC)){
- }
- while(1)
- {
- /* check if the user key is pressed */
- if(RESET == gd_eval_key_state_get(KEY_WAKEUP))
- {
- delay_1ms(50);
- if(RESET == gd_eval_key_state_get(KEY_WAKEUP))
- {
- delay_1ms(50);
- if(RESET == gd_eval_key_state_get(KEY_WAKEUP))
- {
- /* turn on LED1 */
- gd_eval_led_on(LED1);
- /* output a message on hyperterminal using printf function */
- printf("\r\n USART printf example \r\n");
- /* wait for completion of USART transmission */
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TC)){
- }
- }
- else
- {
- /* turn off LED1 */
- gd_eval_led_off(LED1);
- }
- }
- else
- {
- /* turn off LED1 */
- gd_eval_led_off(LED1);
- }
- }
- else
- {
- /* turn off LED1 */
- gd_eval_led_off(LED1);
- }
- }
- }
复制代码
|