nRf24le1旺哥整理的代码3 - 串口通信
作者:星希望(QQ:411057286)
转载请声明出处
涉及代码文件已上传到本站,搜索 “ nRf24le1旺哥整理的代码 ” ,欢迎留言讨论;
创作背景
最近在搞一个RFID的项目,在短距离(0–5米内)识别电子标签,并能读取出标签的ID号;
迫于无源RFID读写距离太近,所以选择了有源RFID;
在查阅相关资料后,芯片锁定为nRf24LE1
在某宝上购买到开发板后,发现卖家给的资料简直一塌糊涂,遂决心自己编写寄存器驱动;
以下代码仅作为模块化参考,读者们在引用过程中需要根据自身情况做适当修改;
第3课 - 串口通信
nRf24le1芯片串口最高波特率为38400,本程序中直接使用38400波特率;
直接贴代码:
文件名:UART.h
/********************** 分 割 线 **********************/
#ifndef uart_H
#define uart_H
#include “sys.h”
void UART_init(void);
void UART_Send_Byte(u8 tmp);
void UART_Send_String(u8 *s);
void UART_Send_n_Byte(u16 n, u8 *s);
void UART_SendNum(unsigned char Width, unsigned int NUM);
void UART_Send_Backpace(u8 i);
#endif
/********************** 分 割 线 **********************/
文件名:UART.c
/************************************************************************
- 串口驱动(已校验)
- 作者:星希望(QQ:411057286)
- 日期:2019年5月8日
************************************************************************/
#include “uart.h”
#include “stdio.h”
/************************************************************************
-
初始化
***********************************************************************/
void UART_init(void)
{
/ IO */
P0DIR &= ~(BIT3); // P03 (TxD)方向, 0-out,1-input
P0DIR |= BIT4; // P04 (RxD) is inputP0 |= BIT3 + BIT4;
/* 串口配置 */
S0CON = BIT4 + BIT6; // 使能串行端口,8-bit UART模式
PCON |= BIT7; // SMOD = 1 倍增
ADCON |= BIT7; // 选择内部波特率发生器/* 波特率 */
S0RELL = 0xF3; // 38400 @ 16 MHz
S0RELH = 0x03;/* 中断 */
ES0 = 1; // 使能串口中断
EA = 1; // 使能总中断
}
/************************************************************************
- 发送1字节
************************************************************************/
void UART_Send_Byte(u8 tmp)
{
S0BUF = tmp;
while( !TI0 );
TI0 = 0;
}
/********************************************
- 发送字符串
********************************************/
void UART_Send_String(u8 *s)
{
while(*s) //检测字符串结束标志
{
UART_Send_Byte(*s++); //发送当前字符
}
}
/************************************************************************
- 发送 n 个字节数据
************************************************************************/
void UART_Send_n_Byte(u16 n, u8 *s)
{
while (n–) //检测字符串结束标志
{
UART_Send_Byte(*s++); //发送当前字符
}
}
void UART_interrupt(void) interrupt INTERRUPT_UART0
{
if( RI0 == 1 )
{
RI0 = 0;
/* 以下为用户代码 */
}
else if(TI0 == 1)
{
TI0 = 0;
}
}
///*****************************************************************************
//* 发送数字
///
//void UART_SendNum(unsigned char Width, unsigned int NUM)
//{
// unsigned char dat[6];
// unsigned char p=&dat[0];
//
// dat[0] = NUM / 10000 % 10 + ‘0’;
// dat[1] = NUM / 1000 % 10 + ‘0’;
// dat[2] = NUM / 100 % 10 + ‘0’;
// dat[3] = NUM / 10 % 10 + ‘0’;
// dat[4] = NUM % 10 + ‘0’;
// dat[5] = 0; // 结束
//
// if( Width < 5 ) p = &dat[5-Width];
//
// UART_Send_String§;
//}
//
///
//* 退格
//**********************************/
//void UART_Send_Backpace(u8 i)
//{
// while(i–)
// {
// UART_Send_Byte(0x08);
// }
//}
//
///
//* printf重定义
///
//char putchar(char c)
//{
// UART_Send_Byte©;
// return c;
//}
//
//void printf_test()
//{
// unsigned int i = 65535;
// int j = 32767;
//
// unsigned char k= 255;
// char l= 127;
//
// unsigned long m = 1234567890;
// long n = 1234567890;
//
// float o = 1.234;
// double p = 1.234;
//
// printf(“Hello World!\r\n”);
//
// printf("%d unsigned int\r\n", i);
// printf("%d int \r\n", j);
//
// printf("%u unsigned int u\r\n", i);
// printf("%u int u\r\n", j);
//
// printf("%d unsigned char\r\n", k);
// printf("%d char\r\n", l);
//
// printf("%u unsigned char u\r\n", k);
// printf("%u char u\r\n", l);
//
// printf("%ld unsigned long\r\n", m);
// printf("%ld long\r\n", n);
//
// printf("%f float\r\n", o);
// printf("%f double\r\n", p);
//
// printf("%g float_g\r\n", o);
// printf("%g double_g\r\n", p);
//}
//
/********************** 分 割 线 **********************/
文件名:main.c
/************************************************************************
- NRF24LE1
- UART - 已校验
- TxD - P03
- RxD - P04
- 作者:星希望(QQ:411057286)
- 日期:2019年5月8日
************************************************************************/
#include “LED.h”
#include “Key.h”
#include “delay.h”
#include “uart.h”
/************************************************************************
-
主函数
************************************************************************/
void main(void)
{
LED_init();
UART_init();while(1)
{
UART_Send_String(“hello world\r\n”);
led_0 = ~led_0;
delay_ms(500);
}
}