本次设计采用了32单片机来完成,真正的完成了电能的采集与管理
单片机源程序如下:
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "usart3.h"
- #include "esp8266.h"
- #include "string.h"
- #include "timer.h"
- #include "led.h"
- #include "adc.h"
- #include "lcd.h"
- /*
- 项目的主要内容:STM32配合ESP8266模块与服务器数据交互
- ESP8266的连接:USART3(PB10、PB11)
- 如何判断数据接收完全?
- 1、出现了换行符;
- 2、如果超过10ms了都没有下一条数据(TIM7来进行10ms的定时)。
- */
- int main(void)
- { char a[15];
- float k,kk;
- float w;
- u8 * m="zhinengchazuo";
- u16 adcx;
- float temp;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- //LED_Init(); //LED端口初始化
- LCD_Init();
- Adc_Init(); //ADC初始化
- POINT_COLOR=RED;//设置字体为红色
- LCD_ShowString(60,50,200,16,16,m);
- //LCD_ShowString(60,70,200,16,16,"信通学院");
- //LCD_ShowString(60,90,200,16,16,"智能插座");
- LCD_ShowString(60,110,200,16,16,"2019/12/9");
- //显示提示信息
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(60,130,200,16,16,"ADC_CH0_VAL:");
- LCD_ShowString(60,150,200,16,16," V :0.000V");
- LCD_ShowString(60,170,200,16,16," W :0.000W");
- //delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- //uart_init(115200); //串口初始化为115200
- usart3_init(115200); //串口初始化为115200
- LED_Init();
-
- esp8266_start_trans(); //esp8266进行初始化
-
- while(1)
- {
- if(USART3_RX_STA&0xf000)
- {
-
- adcx=Get_Adc_Average(ADC_Channel_1,10);
- LCD_ShowxNum(156,130,adcx,4,16,0);//显示ADC的值
- temp=(float)adcx*(3.3/4096);
- adcx=temp;
- w=adcx*4;
- LCD_ShowxNum(156,150,w,1,16,0);//显示电压值
- temp-=adcx;
- temp*=1000;
- LCD_ShowxNum(172,150,temp,3,16,0X80);
-
- kk=(float)temp*(3.3/4000);
- adcx=kk;
- k=w/2;
- LCD_ShowxNum(156,170,k,1,16,0);//显示电压值
- kk-=adcx;
- kk*=1000;
- LCD_ShowxNum(172,170,kk,3,16,0X80);
-
- delay_ms(250);
-
-
- printf("USART3_RX_BUF=%s\r\n",USART3_RX_BUF);
- sprintf(a,"%s",USART3_RX_BUF);
- printf("a=%s",a);
-
- if(strstr((const char*)a,"on")) GPIO_ResetBits(GPIOB,GPIO_Pin_5);
- if(strstr((const char*)a,"off")) GPIO_SetBits(GPIOB,GPIO_Pin_5);
- USART3_RX_STA=0;
- }
-
- }
- }
复制代码
所有程序51hei提供下载:
设计程序代码.7z
(235.75 KB, 下载次数: 23)
|