请问有人写过51系列单片机LORA驱动程序吗

2019-07-18 14:00发布

有没有大佬写过51系列单片机驱动发烧友LORA模块的程序呀?参考学习一下。 本主题由 一只耳朵怪 于 2019-1-24 09:52 审核通过
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
asd011
1楼-- · 2019-07-18 16:42
这个纯串口的哦,我们没提供51例程,不过你移植过去也很简单的啊
wo97306730
2楼-- · 2019-07-18 17:43
不大会移植啊,自己写了个简单点的 ,好像不对,您可以帮忙看看吗?
[C] 纯文本查看 复制代码
#include "STC15W4K.H" #include "delay.h"/*************本地常量声明**************/#define MAIN_Fosc22118400L//定义主时钟#defineRX1_Length128/* 接收缓冲长度 */#defineRX2_Length128/* 接收缓冲长度 */#defineUART_BaudRate1115200UL /* 波特率 */#defineUART_BaudRate2115200UL /* 波特率 */ /*************本地变量声明**************/u8xdataRX1_Buffer[RX1_Length];//接收缓冲u8xdataRX2_Buffer[RX2_Length];//接收缓冲u8TX1_read,RX1_write;//读写索引(指针).u8TX2_read,RX2_write;//读写索引(指针).bitB_TX1_Busy,B_TX2_Busy;  u8 test[]={"5221121"}; voidUART1_config(u8 brt);voidUART2_config(u8 brt);// 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.void PrintString1(u8 *puts);void PrintString2(u8 *puts);void clear1(void);void  clear2(void);sbit AUX=P3^2;sbit MD0=P3^3;sbit KEY=P4^5;void LORA_SET(void);void port_mode()            // 端口模式{P0M1=0x00; P0M0=0x00;P1M1=0x00; P1M0=0x00;P2M1=0x00; P2M0=0x00;P3M1=0x00; P3M0=0x00;P4M1=0x00; P4M0=0x00;P5M1=0x00; P5M0=0x00;P6M1=0x00; P6M0=0x00;P7M1=0x00; P7M0=0x00; }void main(){port_mode();UART1_config(1);// 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.UART2_config(2);// 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效. EA = 1;LORA_SET();while(1){//if(!KEY)//{//PrintString2(test);//在UART2打印//}if((TX2_read != RX2_write) && !B_TX2_Busy)//判断UART4接收到数据且串口空闲{B_TX2_Busy = 1;//UART4置为忙状态delay760ms();PrintString1(RX2_Buffer);//在UART2打印 UART3的BUFF数据//PrintString1(RX3_Buffer);//在UART1打印 UART3的BUFF数据clear2();//打印完成后清除UART3 的BUFF 数据}}}void LORA_SET(void){AUX=0;MD0=1;PrintString2("AT");delay760ms();PrintString2("AT+ADDR=01,01");delay760ms();PrintString2("AT+WLRATE=23,5");delay760ms();PrintString2("AT+TPOWER=3");delay760ms();PrintString2("AT+CWMODE=0");delay760ms();PrintString2("AT+TMODE=0");delay760ms();PrintString2("AT+WLTIME=0");delay760ms();PrintString2("AT+UART=7,0");delay760ms();MD0=0;}void clear1(void){u8i;for(i=0; i<RX1_Length; i++)RX1_Buffer = 0;B_TX1_Busy  = 0;TX1_read    = 0;RX1_write   = 0;}/********************* 串口2 BUFF清初函数 ************************/void clear2(void){u8i;for(i=0; i<RX2_Length; i++)RX2_Buffer = 0;B_TX2_Busy  = 0;TX2_read    = 0;RX2_write   = 0;} voidSetTimer2Baudraye(u16 dat)// 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.{AUXR &= ~(1<<4);//Timer stopAUXR &= ~(1<<3);//Timer2 set As TimerAUXR |=  (1<<2);//Timer2 set as 1T modeTH2 = dat / 256;TL2 = dat % 256;IE2  &= ~(1<<2);//禁止中断AUXR |=  (1<<4);//Timer run enable}voidUART1_config(u8 brt)// 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.{u8i;/*********** 波特率使用定时器2 *****************/if(brt == 2){AUXR |= 0x01;//S1 BRT Use Timer2;SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / UART_BaudRate1);}/*********** 波特率使用定时器1 *****************/else{TR1 = 0;AUXR &= ~0x01;//S1 BRT Use Timer1;AUXR |=  (1<<6);//Timer1 set as 1T modeTMOD &= ~(1<<6);//Timer1 set As TimerTMOD &= ~0x30;//Timer1_16bitAutoReload;TH1 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate1) / 256;TL1 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate1) % 256;ET1 = 0;//禁止中断INT_CLKO &= ~0x02;//不输出时钟TR1  = 1;/*************************************************/}SCON = (SCON & 0x3f) | (1<<6);// 8位数据, 1位起始位, 1位停止位, 无校验//PS  = 1;//高优先级中断ES  = 1;//允许中断REN = 1;//允许接收P_SW1 = P_SW1 & 0x3f;//切换到 P3.0 P3.1//P_SW1 = (P_SW1 & 0x3f) | (1<<6);//切换到P3.6 P3.7//P_SW1 = (P_SW1 & 0x3f) | (2<<6);//切换到P1.6 P1.7 (必须使用内部时钟)for(i=0; i<RX1_Length; i++)RX1_Buffer = 0;B_TX1_Busy  = 0;TX1_read    = 0;RX1_write   = 0;}voidUART2_config(u8 brt)// 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.{u8i;/*********** 波特率固定使用定时器2 *****************/if(brt == 2)SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / UART_BaudRate2);S2CON &= ~(1<<7);// 8位数据, 1位起始位, 1位停止位, 无校验IE2   |= 1;//允许中断S2CON |= (1<<4);//允许接收P_SW2 &= ~1;//切换到 P1.0 P1.1//P_SW2 |= 1;//切换到 P4.6 P4.7for(i=0; i<RX2_Length; i++)RX2_Buffer = 0;B_TX2_Busy  = 0;TX2_read    = 0;RX2_write   = 0;}void PrintString1(u8 *puts){    for (; *puts != 0;puts++){B_TX1_Busy = 1;//标志发送忙SBUF = *puts;//发一个字节while(B_TX1_Busy);//等待发送完成}}void PrintString2(u8 *puts){    for (; *puts != 0;puts++){B_TX2_Busy = 1;//标志发送忙S2BUF = *puts;//发一个字节while(B_TX2_Busy);//等待发送完成}}/********************* UART1中断函数************************/void UART1_int (void) interrupt UART1_VECTOR{if(RI){RI = 0;RX1_Buffer[RX1_write] = SBUF;if(++RX1_write >= RX1_Length)RX1_write = 0;}if(TI){TI = 0;B_TX1_Busy = 0;}}/********************* UART2中断函数************************/void UART2_int (void) interrupt UART2_VECTOR{if(RI2){CLR_RI2();RX2_Buffer[RX2_write] = S2BUF;if(++RX2_write >= RX2_Length)RX2_write = 0;}if(TI2){CLR_TI2();B_TX2_Busy = 0;}}

一周热门 更多>