#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "rs485.h"
/************************************************
ALIENTEK战舰STM32开发板实验25
485通信实验
技术支持:www.openedv.com
淘宝店铺:http://eboard.taobao.com
关注微信公众平台微信号:"正点原子",免费获取STM32资料。
广州市星翼电子科技有限公司
作者:正点原子 @ALIENTEK
************************************************/
int main(void)
{
u8 key;
u8 i=0,t=0;
u8 cnt=0;
u8 rs485buf[9];
u8 *pMem;
float p ;
u8 tag;
uint8_t mytag;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //初始化与LED连接的硬件接口
LCD_Init(); //初始化LCD
KEY_Init(); //按键初始化
RS485_Init(9600); //初始化RS485
POINT_COLOR=RED;//设置字体为红色
LCD_ShowString(30,50,200,16,16,"WarShip STM32");
LCD_ShowString(30,70,200,16,16,"RS485 TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2015/1/15");
LCD_ShowString(30,130,200,16,16,"KEY0:Send"); //显示提示信息
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowString(30,150,200,16,16,"Count:"); //显示当前计数值
LCD_ShowString(30,170,200,16,16,"Send Data:"); //提示发送的数据
LCD_ShowString(30,230,200,16,16,"Receive Data:"); //提示接收到的数据
while(1)
{
key=KEY_Scan(0);
if(key==KEY0_PRES)//KEY0按下,发送一次数据
{
// for(i=0;i<5;i )
// {
// rs485buf[i]=cnt i;//填充发送缓冲区
// LCD_ShowxNum(30 i*32,190,rs485buf[i],3,16,0X80); //显示数据
// }
//rs485buf[0]=0x00;
rs485buf[0]=0x01;
rs485buf[1]=0x03;
rs485buf[2]=0x20;
rs485buf[3]=0x00;
rs485buf[4]=0x00;
rs485buf[5]=0x02;
rs485buf[6]=0xCF;
rs485buf[7]=0xCB;
// rs485buf[9]=0x01;
// rs485buf[10]=0x01;
LCD_ShowxNum(30 0*32,190,rs485buf[0],3,16,0X80);
LCD_ShowxNum(30 1*32,190,rs485buf[1],3,16,0X80);
LCD_ShowxNum(30 2*32,190,rs485buf[2],3,16,0X80);
LCD_ShowxNum(30 3*32,190,rs485buf[3],3,16,0X80);
LCD_ShowxNum(30 4*32,190,rs485buf[4],3,16,0X80);
LCD_ShowxNum(30 5*32,190,rs485buf[5],3,16,0X80);
LCD_ShowxNum(30 0*32,210,rs485buf[6],3,16,0X80);
LCD_ShowxNum(30 1*32,210,rs485buf[7],3,16,0X80);
RS485_Send_Data(rs485buf,8);//发送5个字节
tag=0;
}
if(key==KEY1_PRES)//KEY0按下,发送一次数据
{
rs485buf[0]=0x01;
rs485buf[1]=0x03;
rs485buf[2]=0x20;
rs485buf[3]=0x02;
rs485buf[4]=0x00;
rs485buf[5]=0x02;
rs485buf[6]=0x6E;
rs485buf[7]=0x0B;
// rs485buf[9]=0x01;
// rs485buf[10]=0x01;
LCD_ShowxNum(30 0*32,190,rs485buf[0],3,16,0X80);
LCD_ShowxNum(30 1*32,190,rs485buf[1],3,16,0X80);
LCD_ShowxNum(30 2*32,190,rs485buf[2],3,16,0X80);
LCD_ShowxNum(30 3*32,190,rs485buf[3],3,16,0X80);
LCD_ShowxNum(30 4*32,190,rs485buf[4],3,16,0X80);
LCD_ShowxNum(30 5*32,190,rs485buf[5],3,16,0X80);
LCD_ShowxNum(30 0*32,210,rs485buf[6],3,16,0X80);
LCD_ShowxNum(30 1*32,210,rs485buf[7],3,16,0X80);
RS485_Send_Data(rs485buf,8);//发送5个字节
tag=1;
}
if(key==KEY2_PRES)//KEY0按下,发送一次数据
{
rs485buf[0]=0x01;
rs485buf[1]=0x03;
rs485buf[2]=0x20;
rs485buf[3]=0x04;
rs485buf[4]=0x00;
rs485buf[5]=0x02;
rs485buf[6]=0x8E;
rs485buf[7]=0x0A;
// rs485buf[9]=0x01;
// rs485buf[10]=0x01;
LCD_ShowxNum(30 0*32,190,rs485buf[0],3,16,0X80);
LCD_ShowxNum(30 1*32,190,rs485buf[1],3,16,0X80);
LCD_ShowxNum(30 2*32,190,rs485buf[2],3,16,0X80);
LCD_ShowxNum(30 3*32,190,rs485buf[3],3,16,0X80);
LCD_ShowxNum(30 4*32,190,rs485buf[4],3,16,0X80);
LCD_ShowxNum(30 5*32,190,rs485buf[5],3,16,0X80);
LCD_ShowxNum(30 0*32,210,rs485buf[6],3,16,0X80);
LCD_ShowxNum(30 1*32,210,rs485buf[7],3,16,0X80);
RS485_Send_Data(rs485buf,8);//发送5个字节
tag=2;
}
if(key==WKUP_PRES)//KEY0按下,发送一次数据
{
rs485buf[0]=0x02;
rs485buf[1]=0x03;
rs485buf[2]=0x40;
rs485buf[3]=0x00;
rs485buf[4]=0x00;
rs485buf[5]=0x02;
rs485buf[6]=0xD1;
rs485buf[7]=0xCB;
// rs485buf[9]=0x01;
// rs485buf[10]=0x01;
LCD_ShowxNum(30 0*32,190,rs485buf[0],3,16,0X80);
LCD_ShowxNum(30 1*32,190,rs485buf[1],3,16,0X80);
LCD_ShowxNum(30 2*32,190,rs485buf[2],3,16,0X80);
LCD_ShowxNum(30 3*32,190,rs485buf[3],3,16,0X80);
LCD_ShowxNum(30 4*32,190,rs485buf[4],3,16,0X80);
LCD_ShowxNum(30 5*32,190,rs485buf[5],3,16,0X80);
LCD_ShowxNum(30 0*32,210,rs485buf[6],3,16,0X80);
LCD_ShowxNum(30 1*32,210,rs485buf[7],3,16,0X80);
RS485_Send_Data(rs485buf,8);//发送5个字节
tag=3;
}
RS485_Receive_Data(rs485buf,&key);
if(key)//接收到有数据
{
if(key>9)key=9;//最大是5个数据.
for(i=0;i<key;i )
{
if(i<=5){
LCD_ShowxNum(30 i*32,250,rs485buf[i],3,16,0X80); //显示数据
}else if(i==6){
LCD_ShowxNum(30 0*32,270,rs485buf[i],3,16,0X80); //显示数据
}else if(i==7){
LCD_ShowxNum(30 1*32,270,rs485buf[i],3,16,0X80); //显示数据
}
else if(i==8){
LCD_ShowxNum(30 2*32,270,rs485buf[i],3,16,0X80); //显示数据
}
}
pMem[0] = rs485buf[5];
pMem[1] = rs485buf[6];
pMem[2] = rs485buf[3];
pMem[3] = rs485buf[4];
p = get_float_from_4u8(pMem);
// printf("%d\r\n",rs485buf[3]);
// printf("%d\r\n",rs485buf[4]);
// printf("%d\r\n",rs485buf[5]);
// printf("%d\r\n",rs485buf[6]);
if(tag==0){
printf("\n智能电表的电压(V)为:");
}
if(tag==1){
printf("\n智能电表的电流(A)为:");
//if(GPIO_ReadOutputDataBit (GPIOD,GPIO_Pin_7)==0x01)
// mytag = GPIO_ReadInputDataBit (GPIOD,GPIO_Pin_7);
// printf("mytag:%d",mytag);
}
if(tag==2){
printf("\n智能电表的有功功率(W)为:");
p=p*1000;
}
if(tag==3){
printf("\n智能电表计算电能(kw.h)为:");
}
printf("%g\r\n\n",p);
}
t ;
delay_ms(10);
if(t==20)
{
LED0=!LED0;//提示系统正在运行
t=0;
cnt ;
LCD_ShowxNum(30 48,150,cnt,3,16,0X80); //显示数据
}
}
}
评论