不管学习哪款MCU,串口都是最好的调试工具。
使用工具:USB转TTL串口和CC Debugger仿真器。
使用UART0收发送数据接线:
USB转TTL串口 BLE CC2541最小模块
GND接GND
RXD接P0_3(TX引脚)
TXD接P0_2(RX引脚)
3.3V接VCC。
串口初始化函数:
/****************************************************************************
名 称: InitUart()
功 能: 串口初始化函数
入口参数: 无
出口参数: 无
****************************************************************************/
void InitUart(void)
{
PERCFG = 0x00; //位置1 P0口
P2DIR &= ~0xc0; //USART0 优先级最高
P0SEL = 0x3c; //P0_2,P0_3,P0_4,P0_5用作串口
U0CSR |= 0x80; //配置当前为UART,非SPI
U0GCR |= 8;
U0BAUD |= 59; //波特率为9600
UTX0IF = 0; //位寄存器,直接操作,清除中断标志
U0CSR |= 0X40; //允许接收数据
IEN0 |= 0x84; //打开接收中断
}
系统时钟初始化函数:
/****************************************************************************
名 称: System_Clock()
功 能: 系统时钟函数
入口参数: 无
出口参数: 无
****************************************************************************/
void System_Clock(void)
{
CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定
CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ
}
串口发送字符串函数:
#if 1
/****************************************************************************
名 称: Uart_Send_String()
功 能: 串口发送字符串函数
入口参数: Data:发送缓冲区 length:发送长度
出口参数: 无
****************************************************************************/
void Uart_Send_String(char *Data, uint16 length)
{
uint16 i;
for(i=0; i
串口发送函数:
/****************************************************************************
名 称: Uart_Send_Data()
功 能: 串口发送数据函数
入口参数: Data:发送数据
出口参数: 无
****************************************************************************/
void Uart_Send_Data(char Data)
{
U0DBUF = Data;
while(UTX0IF == 0);
UTX0IF = 0;
}
中断服务函数:
/****************************************************************************
名 称: UART0_ISR(void) 串口中断处理函数
描 述: 产生接收中断
入口参数: 无
出口参数: 无
****************************************************************************/
#pragma vector = URX0_VECTOR
__interrupt void UART0_ISR(void)
{
char Receive_Data=0;
Receive_Data = U0DBUF;
Uart_Send_Data(Receive_Data); //收到数据后立即发送出去
}
/****************************************************************************
名 称: main
描 述: 主函数
入口参数: 无
出口参数: 无
****************************************************************************/
void main(void)
{
System_Clock(); //系统时钟配置
InitUart(); //调置串口相关寄存器
while(1);
}
头文件部分:
#include
#include
#include"header.h"
#ifndef HEADER_H
#define HEADER_H
typedef unsigned short int uint16;
typedef unsigned char uint8;
void InitUart(void);
void System_Clock(void);
void Uart_Send_String(char *Data, uint16 length);
void Uart_Send_Data(char Data);
#endif
更多技术文章浏览请关注:
百家号:
https://author.baidu.com/home?context=%7B%22app_id%22%3A%221646108714303504%22%7D&wfr=bjh
头条号:
https://www.toutiao.com/c/user/8115738721/#mid=1646025109246987