OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 7006|回复: 4

51系列单片机LORA驱动程序

[复制链接]

6

主题

37

帖子

0

精华

初级会员

Rank: 2

积分
131
金钱
131
注册时间
2018-7-27
在线时间
11 小时
发表于 2018-9-29 21:10:40 | 显示全部楼层 |阅读模式
2金钱
有没有大佬写过51系列单片机驱动正点原子LORA模块的程序呀?参考学习一下。

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

558

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
164897
金钱
164897
注册时间
2010-12-1
在线时间
2100 小时
发表于 2018-9-30 01:30:37 | 显示全部楼层
这个纯串口的哦,我们没提供51例程,不过你移植过去也很简单的啊
回复

使用道具 举报

6

主题

37

帖子

0

精华

初级会员

Rank: 2

积分
131
金钱
131
注册时间
2018-7-27
在线时间
11 小时
 楼主| 发表于 2018-10-1 21:36:49 | 显示全部楼层
正点原子 发表于 2018-9-30 01:30
这个纯串口的哦,我们没提供51例程,不过你移植过去也很简单的啊

不大会移植啊,自己写了个简单点的 ,好像不对,您可以帮忙看看吗?
[mw_shl_code=c,true]
#include "STC15W4K.H"
#include "delay.h"




/*************        本地常量声明        **************/
#define MAIN_Fosc                22118400L        //定义主时钟

#define        RX1_Length        128                /* 接收缓冲长度 */
#define        RX2_Length        128                /* 接收缓冲长度 */
#define        UART_BaudRate1        115200UL         /* 波特率 */
#define        UART_BaudRate2        115200UL         /* 波特率 */

         /*************        本地变量声明        **************/
u8        xdata        RX1_Buffer[RX1_Length];        //接收缓冲
u8        xdata        RX2_Buffer[RX2_Length];        //接收缓冲
u8        TX1_read,RX1_write;        //读写索引(指针).
u8        TX2_read,RX2_write;        //读写索引(指针).
bit        B_TX1_Busy,B_TX2_Busy;  
u8 test[]={"5221121"};
                 
void        UART1_config(u8 brt);       
void        UART2_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=0x001M1=0x00; P1M0=0x002M1=0x00; P2M0=0x003M1=0x00; P3M0=0x00;
        P4M1=0x00; P4M0=0x005M1=0x00; P5M0=0x006M1=0x00; P6M0=0x007M1=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)
{
        u8        i;
        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)
{
        u8        i;
        for(i=0; i<RX2_Length; i++)                RX2_Buffer = 0;
        B_TX2_Busy  = 0;
        TX2_read    = 0;
        RX2_write   = 0;
}
void        SetTimer2Baudraye(u16 dat)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
        AUXR &= ~(1<<4);        //Timer stop
        AUXR &= ~(1<<3);        //Timer2 set As Timer
        AUXR |=  (1<<2);        //Timer2 set as 1T mode
        TH2 = dat / 256;
        TL2 = dat % 256;
        IE2  &= ~(1<<2);        //禁止中断
        AUXR |=  (1<<4);        //Timer run enable
}

void        UART1_config(u8 brt)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
        u8        i;
        /*********** 波特率使用定时器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 mode
                TMOD &= ~(1<<6);        //Timer1 set As Timer
                TMOD &= ~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;
}

void        UART2_config(u8 brt)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
{
        u8        i;
        /*********** 波特率固定使用定时器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.7

        for(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;
        }

}[/mw_shl_code]
回复

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
21
金钱
21
注册时间
2019-3-30
在线时间
1 小时
发表于 2019-3-30 15:27:59 | 显示全部楼层
大佬,解决了吗
回复

使用道具 举报

0

主题

3

帖子

0

精华

新手上路

积分
24
金钱
24
注册时间
2019-4-28
在线时间
3 小时
发表于 2019-4-28 11:38:24 | 显示全部楼层
大佬怎么样了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-5-29 08:24

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表