#include "screen.h"
#include "uart4.h"
#include "norflash.h"
u16 cur_scan_val[8];
bit is_addr_scan = 0;//是否要进行变量地址扫描
bit flag_0 = 0;
bit flag_180 = 0;
void screen_init(void)
{
memset(cur_scan_val,0,sizeof(cur_scan_val));
}
//变量地址扫描函数
void val_addr_scan(void)
{
#define ADDR_SCAN_PERIOD 25 //时间间隔
static idata u16 tick = 0;
tick++;
if(tick==ADDR_SCAN_PERIOD)
{
tick = 0;
is_addr_scan = 1;
}
}
//变量地址数据处理函数
void val_addr_handler(void)
{
#define VAL_START_ADDR 0x3000
if(is_addr_scan==0)//是否有扫描信号,没有的话就啥也不做,直接返回
{
return;
}
is_addr_scan=0;//清除
//1.获得增量变量的数值
sys_read_vp(VAL_START_ADDR,(u8*)&cur_scan_val,8);//返回字
//uart4_send_byte((u8)cur_scan_val);
if(cur_scan_val[0])
{
motor_rotate(0);
lab_pop_clear(0,0);
}
if(cur_scan_val[1])
{
motor_rotate(1);
lab_pop_clear(1,0);
}
if(cur_scan_val[2])
{
motor_rotate(2);
lab_pop_clear(2,0);
}
motor_speed_get();
indicate_LED();
}
void lab_pop_clear(u16 n,u16 temp)
{
sys_write_vp(0x3000+n,(u8*)&temp,1);
}
void motor_rotate(u8 mode_or_FreeInput)
{
u16 angle_absolute = 0;
u16 last_angle_absolute = 0;
u16 angle_rotate = 0;
if(mode_or_FreeInput == 0)
{
sys_read_vp(ANGLE_ADDR,(u8*)&angle_absolute,1);//自由输入模式
}
norflash_read(FLASH_ADDR,(u8*)&last_angle_absolute,2);
if(mode_or_FreeInput == 1)
{
speed = 2;
angle_absolute = 30;//睡眠模式
}
if(mode_or_FreeInput == 2)
{
speed = 2;
angle_absolute = 90;//直吹模式
}
if(angle_absolute > last_angle_absolute)
{
angle_rotate = angle_absolute - last_angle_absolute;
motor_direction =0;//顺时针旋转
beats_calculate((u32)angle_rotate);
norflash_write(FLASH_ADDR,(u8*)&angle_absolute,2);
}
else
{
angle_rotate = last_angle_absolute - angle_absolute;
motor_direction =1;//逆时针旋转
beats_calculate((u32)angle_rotate);
norflash_write(FLASH_ADDR,(u8*)&angle_absolute,2);
}
if(angle_absolute == 0)
{
flag_0 = 1;
}else
{
flag_0 = 0;
}
if(angle_absolute == 180)
{
flag_180 = 1;
}else
{
flag_180 = 0;
}
}
void indicate_LED(void)
{
if((flag_0 == 1) && (stop_flag ==1))
{
P1 |=(0x01<<6);
}else
{
P1 &=~(0x01<<6);
}
if((flag_180 == 1) && (stop_flag ==1))
{
P1 |=(0x01<<7);
}else
{
P1 &=~(0x01<<7);
}
}
void motor_speed_get(void)
{
u8 n;
for(n=0;n<5;n++)
{
if(cur_scan_val[3+n])
{
speed = n+1;
lab_pop_clear(3+n,0);
}
}
}