/*
* main implementation: use this 'C' sample to create your own application
*
*/
#include "derivative.h" /* include peripheral declarations SSKEAZ128M4 */
#include "uart.h"
#include "nvic.h"
UART_ConfigType *pConfig;
void Clk_Init_20Mhz()
{
ICS_C1|=ICS_C1_IRCLKEN_MASK; /* Enable the internal reference clock*/
ICS_C3= 0x90; /* Reference clock frequency = 31.25 kHz*/
while(!(ICS_S & ICS_S_LOCK_MASK)); /* Wait for PLL lock, now running at 40 MHz (1280
*31.25 kHz) */
ICS_C2|=ICS_C2_BDIV(1) ; /*BDIV=2, Bus clock = 20 MHz*/
ICS_S |= ICS_S_LOCK_MASK ; /* Clear Loss of lock sticky bit */
}
void UART_Init_marvin ()
{
SIM_SCGC |= SIM_SCGC_UART2_MASK; /* Enable bus clock in UART2 UART2PS=0 PTD7 TX,PTD6 RX*/
UART2_BDH = 0; /* One stop bit*/
UART2_BDL = 128; /* Baud rate at 9600*/
UART2_C1 = 0; /* No parity enable,8-bit format*/
UART2_C2 |= UART_C2_TE_MASK; /* Enable Transmitter*/
UART2_C2 |= UART_C2_RE_MASK; /* Enable Receiver*/
UART2_C2 |= UART_C2_RIE_MASK; /* Enable Receiver interrupts*/
}
void delay500ms(void)
{
unsigned char i,j,k;
for(i=15;i>0;i--)
for(j=202;j>0;j--)
for(k=81;k>0;k--);
}
int main(void)
{
Clk_Init_20Mhz();
UART_Init_marvin();
Enable_Interrupt(UART2_IRQn);
for(;;) {
delay500ms();
while((UART2_S1 & UART_S1_TDRE_MASK)==0);/* Wait for transmit buffer to be
empty*/
(void)UART2_S1; /* Read UART2_S1 register*/
UART2_D= '2'; /* Send data*/
}
/* to avoid the warning message for GHS: statement is unreachable*/
#if defined (__ghs__)
#pragma ghs nowarning 111
#endif
#if defined (__ICCARM__)
#pragma diag_suppress=Pe111
#endif
return 0;
}
void UART2_SCI2_IRQHandler ()
{
// char data = 0;
// (void)UART2_S1; /* Clear reception flag mechanism*/
// data = UART2_D; /* Receive data*/
}
使用PL2102的usb串口模块(其他芯片不兼容,出乱码),模块的RX接单片机的22脚D7(uart2的tx)
使用自带的kea128的库,调试器选择segger
image.png image.png
https://www.nxp.com.cn/docs/en/application-note/AN4942.pdf
参考这个网址操作uart和clk的设置
注意这个pdf使用了没有定义的寄存器名称,通过使用uart.h里的中断使能函数替换下图中的两行代码
网友评论