兼职项目 智能自行车
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

3 years ago
#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>
3 years ago
#define uint unsigned int
#define uchar unsigned char
3 years ago
typedef unsigned char uint8;
typedef unsigned int uint16;
3 years ago
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;
/*****************<EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>**************/
uchar Receive;
uchar Recive_table[40]; //<EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD>wifiģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MCU<EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bit accept = 0;
3 years ago
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);
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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);
}
3 years ago
void main(void)
{
InitLcd1602();
IRIN_Init();
Uart_Init();
ReadData();
3 years ago
while(1)
{
run();
display();
Key_scan();
Bluetooth_Accept();
WriteData();
3 years ago
}
}
void Uart_Interrupt() interrupt 4
{
static uchar i=0;
if( accept) return;
if(RI==1) {
RI=0;
Receive=SBUF; //MCU<EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifiģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Recive_table[i]=Receive;
if((Recive_table[i]=='\r')){
i=0;
accept = 1;
}
else i++; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>װֵ
}
else TI=0;
3 years ago
}