研究了一下KEIL的MDK以及它的文档
总结一下,KEIL的入门要比arduino要复杂的多,arduino是为硬件小白及非专业人士准备的。但是它的功能并不弱。
KEIL入门请一定要认真花点时间看一下它的文档,我是用这份文档入门的(http://www2.keil.com/docs/default-source/default-document-library/mdk5-getting-started.pdf?sfvrsn=0)
原以为找个hello world例子就可以轻松入门,后来发现错了。所以认认真真去看完了上面的文档。
不多说了,上面的文档很专业,看完就足够入门了。
我把例子放给大家,官方的例子在我的板子上运行有问题(只能发送一次),做了一些改动
#include "Driver_USART.h" extern ARM_DRIVER_USART Driver_USART2; /*---------------------------------------------------------------------------- * Application main thread *---------------------------------------------------------------------------*/ void myUART_Thread(void const *argument); osThreadId_t tid_myUART_Thread; osEventFlagsId_t evt_id ; #define FLAGS_MSK1 0x00000001ul void myUSART_callback(uint32_t event) { // uint32_t mask; 以下几行原有代码被注释掉 // mask = ARM_USART_EVENT_RECEIVE_COMPLETE | // ARM_USART_EVENT_TRANSFER_COMPLETE | // ARM_USART_EVENT_SEND_COMPLETE | // ARM_USART_EVENT_TX_COMPLETE ; //if (event & mask) { /* Success: Wakeup Thread */ if( event & ARM_USART_EVENT_TX_COMPLETE )//新增加代码 osEventFlagsSet(evt_id, ARM_USART_EVENT_TX_COMPLETE); if( event & ARM_USART_EVENT_RECEIVE_COMPLETE ) osEventFlagsSet(evt_id, ARM_USART_EVENT_RECEIVE_COMPLETE); //osEventFlagsSet(evt_id, FLAGS_MSK1); } // if (event & ARM_USART_EVENT_RX_TIMEOUT) { // __breakpoint(0); /* Error: Call debugger or replace with custom error handling */ // } // if (event & (ARM_USART_EVENT_RX_OVERFLOW | ARM_USART_EVENT_TX_UNDERFLOW)) { // __breakpoint(0); /* Error: Call debugger or replace with custom error handling */ // } } void app_main (void *argument) { static ARM_DRIVER_USART * USARTdrv = &Driver_USART2; ARM_DRIVER_VERSION version; ARM_USART_CAPABILITIES drv_capabilities; char cmd; #ifdef DEBUG version = USARTdrv->GetVersion(); if (version.api < 0x200) /* requires at minimum API version 2.00 or higher */ { /* error handling */ return; } drv_capabilities = USARTdrv->GetCapabilities(); if (drv_capabilities.event_tx_complete == 0) { /* error handling */ return; } #endif /*Initialize the USART driver */ USARTdrv->Initialize(myUSART_callback); /*Power up the USART peripheral */ USARTdrv->PowerControl(ARM_POWER_FULL); /*Configure the USART to 4800 Bits/sec */ USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 | ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, 4800); /* Enable Receiver and Transmitter lines */ USARTdrv->Control (ARM_USART_CONTROL_TX, 1); USARTdrv->Control (ARM_USART_CONTROL_RX, 1); evt_id = osEventFlagsNew(NULL); USARTdrv->Send("\nPress Enter to receive a message", 34); //osSignalWait(0x01, osWaitForever); osEventFlagsWait(evt_id,ARM_USART_EVENT_TX_COMPLETE,osFlagsWaitAny, osWaitForever);//修改过 while (1) { cmd = 0;//新增 USARTdrv->Receive(&cmd, 1); /* Get byte from UART */ osEventFlagsWait(evt_id,ARM_USART_EVENT_RECEIVE_COMPLETE,osFlagsWaitAny,osWaitForever);//修改过 if (cmd == 13) /* CR, send greeting */ { USARTdrv->Send("\nHello World!", 12); osEventFlagsWait(evt_id,ARM_USART_EVENT_TX_COMPLETE,osFlagsWaitAny,osWaitForever);//修改过 } } }
PC端运行截图如下:
完整的工程代码请到这里下载(什么都比不过自己运行代码&研究来的明白)
https://download.csdn.net/download/ani/10439846
文章来源: STM32F407 上手-USART 收发数据