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

204 lines
4.4 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 Seep_model = 0;
extern unsigned char pwm_value;
uchar play = 0;
// model
uchar model = 0;
struct UserInfo usr;
uchar Receive;
uchar Recive_table[40];
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;
At24c02Write(3, usr.Seep_model);
delayms(2);
target = (usr.target_mileage) >> 8;
At24c02Write(5, target);
delayms(2);
target = (usr.target_mileage) & 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 m/min mileage:%d m\r\n",speed, mileage);
Bluetooth_Set(Recive_table);
}else if(strstr(Recive_table, "model") != NULL){
if(model == 0) model = 1;
else model = 0;
if(model == 0) Seep_model = 0;
memset(Recive_table, 0, sizeof(Recive_table));
sprintf(Recive_table, "Model SET OK\r\n");
Bluetooth_Set(Recive_table);
At24c02Write(4, model);
}else if(strstr(Recive_table, "clear") != NULL){
// 清空里程
mileage = 0;
Bluetooth_Set("reset mileage OK!\r\n");
}
else if(strstr(Recive_table, "speed") != NULL){
Seep_model++;
if(Seep_model >= 4) Seep_model = 0;
usr.Seep_model = Seep_model;
}
else if(strstr(Recive_table, "set:") != NULL){
target1 = strstr(Recive_table, "(") - Recive_table;
target2 = strstr(Recive_table, ",") - Recive_table;
usr.Seep_model = 0;
for(i = target1 + 1; i < target2; i++){
usr.Seep_model = usr.Seep_model * 10 + (*(Recive_table + i) - 0x30);
}
if(usr.Seep_model > 1) usr.Seep_model = 1;
target1 = strstr(Recive_table, ",") - Recive_table;
target2 = strstr(Recive_table, ")") - Recive_table;
usr.target_mileage = 0;
for(i = target1 + 1; i < target2; i++){
usr.target_mileage = usr.target_mileage * 10 + (*(Recive_table + i) - 0x30);
}
if(usr.Seep_model > 9999) usr.Seep_model = 9999;
Bluetooth_Set("User SET OK!\r\n");
saveUserInfo();
}
memset(Recive_table, 0, sizeof(Recive_table));
accept = 0;
}
}
// 读取数据
void ReadData(){
// 里程
mileage = At24c02Read(1) * 255 + At24c02Read(2);
// 用户的模式
usr.Seep_model = At24c02Read(3);
model = At24c02Read(4);
// 用户的目标里程
usr.target_mileage = At24c02Read(5) * 255 + At24c02Read(6);
}
// 写入数据
void WriteData(){
int target = mileage >> 8;
At24c02Write(1, target);
us_delay(200);
target = mileage & 0x00ff;
At24c02Write(2, target);
us_delay(200);
}
// main 函数
3 years ago
void main(void)
{
LcdInit();
3 years ago
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;
Recive_table[i]=Receive;
if((Recive_table[i]=='\r')){
i=0;
accept = 1;
}
else i++;
}
else TI=0;
3 years ago
}