撸主在一个帖子看的KL26Z的串口通讯的两种方法,查询法、中断法。功能为通过USB调试口与PC相连,PC发送数据,KL26Z接收后返回相同的数据 首先是查询法,系统时钟配置,GPIO时钟使能 ,UART0初始化 while(1)循环判断 串口接收到的数据 ,并返回接收到的数据。
- #include "includes.h"
- int main (void)
- {
- SystemCoreClockUpdate(); /* 时钟配置*/
- PORT_ENABLE_CLK(MKL_PORTA); /* GPIO时钟使能 */
- uart0Init(9600,0,0,8,1); /* UART0初始化 */
- while (1) {
- while(UART0_S1_REG(UART0_BASE_PTR) & UART0_S1_RDRF_MASK){ /* 清除中断标志 */
- uart0SendChar(UART0_D_REG(UART0_BASE_PTR)); /* 返回接收数据 */
- }
- }
- }
其次是中断法。
- #if UART0_IRQ_ENABLE
- #if UART0_SEND_IRQ
- UART0_C2_REG(uartPtr) |= UART0_C2_TCIE_MASK;
- #endif
- #if UART0_RECEIVE_IRQ
- UART0_C2_REG(uartPtr) |= UART0_C2_RIE_MASK;
- #endif
- #include "includes.h"
- int main (void)
- {
- SystemCoreClockUpdate(); /* 时钟配置*/
- PORT_ENABLE_CLK(MKL_PORTA); /* GPIO时钟使能 */
- uart0Init(9600,0,0,8,1); /* UART0初始化 */
- while (1)
|