You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
4.5 KiB
216 lines
4.5 KiB
#include <reg51.h> |
|
#include <intrins.h> |
|
#include "lcd.h" |
|
#include "pwm.h" |
|
#include "i2c.h" |
|
#include "IRIN.h" |
|
#include <string.h> |
|
#include <stdio.h> |
|
# include <stdlib.h> |
|
|
|
#define uint unsigned int |
|
#define uchar unsigned char |
|
|
|
typedef unsigned char uint8; |
|
typedef unsigned int uint16; |
|
|
|
extern uint mileage; |
|
extern uint speed; |
|
|
|
uint model = 0; |
|
|
|
extern unsigned char pwm_value; |
|
|
|
|
|
struct UserInfo{ |
|
int height; |
|
int weight; |
|
int target_mileage; |
|
int target_speed; |
|
}; |
|
|
|
uchar set = 0; |
|
|
|
struct UserInfo usr; |
|
|
|
/*****************相关变量**************/ |
|
|
|
uchar Receive; |
|
uchar Recive_table[40]; //用于接收wifi模块反馈到MCU上的数据 |
|
|
|
bit accept = 0; |
|
|
|
void delayms(unsigned int ms) |
|
{ |
|
unsigned char i=100,j; |
|
for(;ms;ms--) |
|
{ |
|
while(--i) |
|
{ |
|
j=10; |
|
while(--j); |
|
} |
|
} |
|
} |
|
|
|
void Uart_Init() |
|
{ |
|
TMOD = 0x20 | 0x01; |
|
SCON = 0x50; |
|
TH1 = 0xFD; |
|
TL1 = TH1; |
|
PCON = 0x00; |
|
EA = 1; |
|
ES = 1; |
|
TR1 = 1; |
|
} |
|
|
|
void us_delay(uchar t) |
|
{ |
|
while(t--); |
|
} |
|
|
|
void Send_Uart(uchar value) |
|
{ |
|
ES=0; |
|
TI=0; |
|
SBUF=value; |
|
while(TI==0); |
|
TI=0; |
|
ES=1; |
|
} |
|
|
|
void Bluetooth_Set(uchar *puf) |
|
{ |
|
while(*puf!='\0') |
|
{ |
|
Send_Uart(*puf); |
|
us_delay(5); |
|
puf++; |
|
} |
|
} |
|
|
|
|
|
void saveUserInfo(){ |
|
int target = 0; |
|
target = usr.height >> 8; |
|
At24c02Write(3, target); |
|
delayms(2); |
|
target = (usr.height) & 0x00ff; |
|
At24c02Write(4, target); |
|
delayms(2); |
|
target = (usr.weight) >> 8; |
|
At24c02Write(5, target); |
|
delayms(2); |
|
target = (usr.weight) & 0x00ff; |
|
At24c02Write(6, target); |
|
delayms(2); |
|
} |
|
|
|
|
|
void Bluetooth_Accept(){ |
|
int target1 = 0, target2 = 0, i = 0; |
|
if(accept){ |
|
if(strstr(Recive_table, "inquire") != NULL){ |
|
memset(Recive_table, 0, sizeof(Recive_table)); |
|
sprintf(Recive_table, "speed:%d km/h mileage:%d m\r\n",speed, mileage); |
|
Bluetooth_Set(Recive_table); |
|
}else if(strstr(Recive_table, "set:") != NULL){ |
|
target1 = strstr(Recive_table, "(") - Recive_table; |
|
target2 = strstr(Recive_table, ",") - Recive_table; |
|
usr.height = 0; |
|
for(i = target1 + 1; i < target2; i++){ |
|
usr.height = usr.height * 10 + (*(Recive_table + i) - 0x30); |
|
} |
|
Bluetooth_Set(Recive_table); |
|
target1 = strstr(Recive_table, ",") - Recive_table; |
|
target2 = strstr(Recive_table, ")") - Recive_table; |
|
usr.weight = 0; |
|
for(i = target1 + 1; i < target2; i++){ |
|
usr.weight = usr.weight * 10 + (*(Recive_table + i) - 0x30); |
|
} |
|
memset(Recive_table, 0, sizeof(Recive_table)); |
|
sprintf(Recive_table, "SET OK\r\n",usr.height, usr.weight); |
|
Bluetooth_Set(Recive_table); |
|
|
|
// 计算 |
|
usr.target_mileage = (usr.weight / usr.height) * (usr.weight / usr.height) / 22 * 3000; |
|
usr.target_speed = 10 - (usr.weight / usr.height) * (usr.weight / usr.height) / 22 ; |
|
saveUserInfo(); |
|
} |
|
memset(Recive_table, 0, sizeof(Recive_table)); |
|
accept = 0; |
|
} |
|
} |
|
|
|
void ReadData(){ |
|
mileage = At24c02Read(1) * 255 + At24c02Read(2); |
|
set = At24c02Read(7); |
|
if(set){ |
|
usr.height = 0; |
|
usr.weight = 0; |
|
|
|
At24c02Write(1, 0); |
|
delayms(2); |
|
At24c02Write(2, 0); |
|
delayms(2); |
|
|
|
At24c02Write(3, 0); |
|
delayms(2); |
|
At24c02Write(4, 0); |
|
delayms(2); |
|
At24c02Write(5, 0); |
|
delayms(2); |
|
At24c02Write(6, 0); |
|
|
|
}else{ |
|
usr.height = At24c02Read(3) * 255 + At24c02Read(4); |
|
usr.weight = At24c02Read(5) * 255 + At24c02Read(6); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void WriteData(){ |
|
int target = mileage >> 8; |
|
At24c02Write(1, target); |
|
delayms(2); |
|
target = mileage & 0x00ff; |
|
At24c02Write(2, target); |
|
delayms(2); |
|
} |
|
|
|
void main(void) |
|
{ |
|
InitLcd1602(); |
|
IRIN_Init(); |
|
Uart_Init(); |
|
ReadData(); |
|
while(1) |
|
{ |
|
run(); |
|
display(); |
|
Key_scan(); |
|
Bluetooth_Accept(); |
|
WriteData(); |
|
} |
|
} |
|
|
|
void Uart_Interrupt() interrupt 4 |
|
{ |
|
static uchar i=0; |
|
if( accept) return; |
|
if(RI==1) { |
|
RI=0; |
|
Receive=SBUF; //MCU接收wifi模块反馈回来的数据 |
|
Recive_table[i]=Receive; |
|
if((Recive_table[i]=='\r')){ |
|
i=0; |
|
accept = 1; |
|
} |
|
else i++; //遇到换行 重新装值 |
|
} |
|
else TI=0; |
|
} |