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發(fā)送數(shù)據(jù)
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待發(fā)送結(jié)束
- }
- 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函數(shù),而不需要選擇use MicroLIB
- #if 1
- #pragma import(__use_no_semihosting)
- //標準庫需要的支持函數(shù)
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- //定義_sys_exit()以避免使用半主機模式
- _sys_exit(int x)
- {
- x = x;
- }
- //重定義fputc函數(shù)
- int fputc(int ch, FILE *f)
- {
- while((USART2->SR&0X40)==0);//循環(huán)發(fā)送,直到發(fā)送完畢
- USART2->DR = (u8) ch;
- return ch;
- }
- #endif
-
- #if EN_USART1_RX //如果使能了接收
- //串口1中斷服務(wù)程序
- //注意,讀取USARTx->SR能避免莫名其妙的錯誤
- char USART_RX_BUF[USART_REC_LEN]; //接收緩沖,最大USART_REC_LEN個字節(jié).
- u16 USART_RX_STA=0; //接收狀態(tài)標記
- u16 length=0,len;
- u8 flag=0;
- u8 k=0;
- char ok[3]={"OK"};
- void uart_init(u32 bound){
- //GPIO端口設(shè)置
- 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 ;//搶占優(yōu)先級3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優(yōu)先級3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
-
- //USART 初始化設(shè)置
- USART_InitStructure.USART_BaudRate = bound;//串口波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
- 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中斷優(yōu)先級設(shè)置
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//搶占優(yōu)先級3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子優(yōu)先級3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
- //USART2——初始化設(shè)置
- 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); //等待發(fā)送寄存器為空
- }
- 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; //狀態(tài)寄存器
- Res=USART2->DR;
- flag=1;
- }
- if(k!=0)
- {
- if(flag)
- {
- for(len=0;len<length;len++)
- {
- USART_SendData(USART1, USART_RX_BUF[len]);//向串口1發(fā)送數(shù)據(jù)
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待發(fā)送結(jié)束
- }
- 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, 下載次數(shù): 61)
2018-9-29 23:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|