标题:
stm32驱动NBlot模块程序实例
[打印本页]
作者:
WitheredLotus
时间:
2018-9-29 21:09
标题:
stm32驱动NBlot模块程序实例
NBlot+stm32单片机源程序如下:
#include "stm32f10x.h"
#include "USART.h"
#include "dht11.h"
#include "delay.h"
#include "string.h"
char error[6]={"ERROR"};
u8 num1,num2;
int jieshou(u16 i)
{
u16 t,n=0;
while(1)
{
if(flag)
{
if((n==0)&&(strstr(USART_RX_BUF,error)!=NULL))
{
flag=0;
length=0;
return 1;
}
for(t=0;t<length;t++)
{
USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
flag=0;
length=0;
n++;
}
if(n==i) return 0;
}
}
void delay(int x)
{
int y,z;
for(z=0;z<x;z++)
for(y=0;y<7000;y++);
}
//extern uint8_t ucTemp1,ucTemp2;
int main()
{
int h=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();
uart_init(115200); //串口初始化为115200
usart2_init(9600);
DHT11_Init();
delay(6000);
USART2_SendData("AT+MIPLCREATE=49,130031F10003F2002304001100000000000000123138332E3233302E34302E34303A35363833000131F30008C000000000,0,49,0\r\n");
jieshou(2);
USART2_SendData("AT+MIPLADDOBJ=0,3303,1,\"1\",1,0\r\n");
jieshou(1);
USART2_SendData("AT+MIPLADDOBJ=0,3304,1,\"1\",1,0\r\n");
jieshou(1);
USART2_SendData("AT+MIPLNOTIFY=0,0,3303,0,5700,4,4,\"29.8\",0,0\r\n");
jieshou(1);
USART2_SendData("AT+MIPLNOTIFY=0,0,3304,0,5700,4,4,\"29.8\",0,0\r\n");
jieshou(1);
USART2_SendData("AT+MIPLOPEN=0,3000,30\r\n");
while(jieshou(6))
USART2_SendData("AT+MIPLOPEN=0,3000,30\r\n");
k=1;
while(1)
{
if(DHT11_Read_Data(&num1,&num2)==0)
{
usart2_send1(num1);
usart2_send2(num2);
}
for(h=0;h<5;h++)
delay_ms(1000);
}
}
复制代码
#include "sys.h"
#include "usart.h"
#include "string.h"
//////////////////////////////////////////////////////////////////////////////////
//如果使用ucos,则包括下面的头文件即可.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos 使用
#endif
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
USART2->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX //如果使能了接收
//串口1中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误
char USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
u16 USART_RX_STA=0; //接收状态标记
u16 length=0,len;
u8 flag=0;
u8 k=0;
char ok[3]={"OK"};
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
USART_Cmd(USART1, ENABLE); //使能串口1
}
void usart2_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART2_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//打开串口2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//打开GPIOA时钟
//初始化USART2-TX
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化USART2-RX
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2中断优先级设置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART2——初始化设置
USART2_InitStructure.USART_BaudRate=bound;
USART2_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART2_InitStructure.USART_Parity=USART_Parity_No;
USART2_InitStructure.USART_StopBits=USART_StopBits_1;
USART2_InitStructure.USART_WordLength=USART_WordLength_8b;
USART2_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART2, &USART2_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启接收中断
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//开启空闲中断
USART_Cmd(USART2, ENABLE); //使能串口
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
}
void USART2_SendData(char* buf)
{
u8 i;
i=strlen((char *)buf);
for(;i>0;i--)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
USART_SendData(USART2,*buf++);
}
}
void Usart_SendByte(USART_TypeDef * pUSARTX,uint8_t ch)
{
USART_SendData(pUSARTX,ch);
while(USART_GetFlagStatus(pUSARTX,USART_FLAG_TXE)==RESET); //等待发送寄存器为空
}
void Usart_SendString(USART_TypeDef * pUSARTX,char *str)
{
unsigned int k=0;
while(*(str+k)!='\0')
{
Usart_SendByte(pUSARTX,*(str+k));
k++;
}
while(USART_GetFlagStatus(pUSARTX,USART_FLAG_TC)==RESET);
}
void USART2_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART2,USART_IT_RXNE)==!RESET)
{
USART_RX_BUF[length++]=USART2->DR;
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
if(USART_GetITStatus(USART2,USART_IT_IDLE)!=RESET)
{
Res=Res;
Res=USART2->SR; //状态寄存器
Res=USART2->DR;
flag=1;
}
if(k!=0)
{
if(flag)
{
for(len=0;len<length;len++)
{
USART_SendData(USART1, USART_RX_BUF[len]);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
flag=0;
length=0;
}
}
}
void usart2_send1(u8 y)
{
u8 ge,shi;
ge=y%10+48;
shi=y/10+48;
Usart_SendString(USART2,"\r\nAT+MIPLNOTIFY=0,0,3303,0,5700,4,4,\"");
Usart_SendByte(USART2,shi);
Usart_SendByte(USART2,ge);
Usart_SendString(USART2,"\",0,0\r\n");
}
void usart2_send2(u8 y)
{
u8 ge,shi;
ge=y%10+48;
shi=y/10+48;
Usart_SendString(USART2,"\r\nAT+MIPLNOTIFY=0,0,3304,0,5700,4,4,\"");
Usart_SendByte(USART2,shi);
Usart_SendByte(USART2,ge);
Usart_SendString(USART2,"\",0,0\r\n");
}
#endif
复制代码
所有资料51hei提供下载:
NB IOT 串口.rar
(371.74 KB, 下载次数: 61)
2018-9-29 23:21 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1