电子指南针所有资料51hei提供下载:
OW091-C.zip
(754.47 KB, 下载次数: 11)
单片机源程序如下:
- /* ======================================================================= */
- /* File Name : main.c */
- /* Description : main function for user program */
- /* Body : GPL833F series 6502 CPU */
- /* Toolchain : gp65cc Compiler V0.9x */
- /* Date : 2014/09/09 */
- /* Author : */
- /* Version : 1.0.0 */
- /* ======================================================================= */
- #include "GPL833F.h"
- #include"main.h"
- #include <math.h>
- #define shift_cant 16
- //void heat_index(void)__banked;
- void weath_read(void);
- void read_temp_hum_1(void);
- void read_temp_hum_2(void);
- void ow61_init(void)__banked;
- void clrr_dis_ram(void);
- void read_comp_data(void)__banked;
- void disp()__banked;
- void key_fun(void)__banked;
- void disp_time()__banked;
- void disp_temp()__banked;
- void press_unit()__banked;
- void disp_press()__banked;
- void disp_anlg()__banked;
- void disp_weath()__banked;
- void rfc_start()__banked;
- void dark_pres_unit()__banked;
- void dark_spend_unit()__banked;
- void spend_unit(void)__banked;
- unsigned char weath_mode=0;
- //void compass_calibration(void)__banked;
- unsigned char *display_buf;
- unsigned char comp_init_flag=0x1f,axis_test_time;
- unsigned int angle=0;
- unsigned char cant_10s=0,key_stater=2,time_display_mode=0,pres_disp_mode=0;
- unsigned char cant_5s=0,weath_stater=1;
- unsigned char inc_8_time=0;
- unsigned char key_data=0;
- unsigned char press_max_stater=0,press_min_stater=0,press_stater1=0,press_stater2=0;
- unsigned char press_send_data,spend_danwei=3,press_danwei;
- unsigned long int press_read_data;
- unsigned char canat_60_minute=0;
- unsigned char data_buf;
- unsigned char press_wait_time;
- unsigned long int press_data_buf=101625,press_data=101325;
- unsigned long int press_save_data=0;
- float wr0,wr1,wr3,dis_spend=0,dis_spend1=5.6;
- int in_temp,in_hum,in_temp_f;
- unsigned int spend,spend1;
- long int wr4;
- unsigned char *pointer1;
- unsigned int wr5;
- unsigned int wr6;
- int high_value,high_value1=0;
-
- int save_shift_high=0;
- unsigned char fishing_data;
- unsigned char porta_buf;
- unsigned char portd_buf;
- unsigned char *pointer1;
- unsigned char compass_data;
- unsigned char flag0=0x10;//bit0半秒标志
- //bit1秒位置是显示周或秒标志
- //bit2表示上电第一次测气压
- //bit3表示温度单位是华氏度还是摄氏度0/1,C/F
- //bit4表示FISHING
- //bit5表示温度测量是否有错误
- //bit6表示测完温度或湿度
- //bit7表示正在测量温湿度
- unsigned char flag1=0x10;//bit0 风速测试ON/OFF 1/0
- //bit1 气压测试模式
- //bit2 开启/关闭 1/0指南针模块
- //bit3表示气压错误标志
- //bit4表示正在测气压
- //bit5表示完成方向测量
- //bit6表示测量方向是错误
- //bit7表示时间模式
- unsigned char flag2=2; //bit0-bit4表示天气状态
- //bit5表示短按
- //bit6表示长按
- unsigned char flag3=0xb4;//bit0 temp test done
- //bit1 hum test done
- //bit4 init 开始读指南针数据
- //bit5 time updata
- //bit6 24/12hour
- //bit7 rfc start/stop 1/0
- unsigned char flag4=0xf8;//bit0 rfc测完等待计算
- //bit1 检测到按键
- //bit2 温度是用rf还是用sen
- //bit3 刷新时间显示
- //bit4 刷新天气图案显示
- //bit5刷新气压显示
- //bit6 刷新温度显示
- //bit7刷新角度显示
- unsigned char flag6=0;//1s标志
- //bit1 指南针是否校验标志
- unsigned char flag5;//湿度和压力OFFSET
- unsigned char cant_30s=0,temp_test_time;
- unsigned char year=17,moth=1,day=1,hour=0,mint=0,second=0,week_data=53;
- unsigned char cant_15s;
- unsigned int XRaw=0;
- unsigned int YRaw=0;
- unsigned char cant_3s;
- unsigned int Xoffset=0;
- unsigned int Yoffset=0;
- unsigned int Xsens=0;
- unsigned int Ysens=0;
- unsigned int X_raw=0,Y_raw=0;
- unsigned int xmax=0,ymax=0,xmin=0,ymin=0;
- unsigned long int x_value=0,y_value=0;
- unsigned int Angle=0;
- unsigned int I2cAngleBuf[2];
- unsigned long int xvalue_buf,yvalue_buf;
- int dis_pres,disp_pres_num=10120;
- unsigned int rfc_ref,rfc_sen1,rfc_sen2;
- unsigned char cant_20s=0;
- unsigned char spend_on_5s=0;
- #define cood_char const unsigned int
- const unsigned char moth_table[12]=
- {
- 31,28,31,30,31,30,31,31,30,31,30,31
- };
- void coter_20second();
- void time_inc(void)
- {
- flag4|=0x08;
- spend_on_5s++;
- if(cant_10s<11)cant_10s++;
- second++;
- cant_30s++;
- if(cant_15s<21)cant_15s++;
- if(cant_5s<6)cant_5s++;
- cant_3s++;
- if(second>59)
- {
- second=0;
- mint++;
- canat_60_minute++;
- if(canat_60_minute>59)flag1|=0x10;
- }
- if(mint>59){hour++;mint=0;}
- if(hour>23){day++;hour=0;}
- if(moth==2)
- {
- if(year&0x03)
- {
- if(day>28)day=1;
- }
- else
- {
- if(day>29)day=1;
- }
- }
- else if(day>moth_table[moth-1])
- {
- day=1;
- }
- if(moth>12){year++;moth=1;}
- if(year>99)year=16;
- }
- void rfc_init()
- {
- static unsigned char stater=0,cant_2=0;
- unsigned char d;
- if(!(flag3&0x80))return;
- if(flag4&0x04)return;
- cant_2++;
- if(cant_2<8)return;
- cant_2=0;
- switch(stater)
- {
- case 0:
- stater=1;
- P_CTS_Ctrl2&=~D_CTSStart;
- P_CTS_TM0Cnt_LB=0x00;
- P_CTS_TM0Cnt_HB=0x00;
- P_IO_RFC_Ctrl=0;
- P_IO_RFC_Ctrl=D_REF2En;
- rfc_ref=P_CTS_TM0Cnt_HB<<8;
- rfc_ref+=P_CTS_TM0Cnt_LB;
- P_CTS_Ctrl2|=D_CTSStart;
- break;
- case 1:
- P_CTS_Ctrl2&=~D_CTSStart;
- rfc_ref=P_CTS_TM0Cnt_HB<<8;
- rfc_ref+=P_CTS_TM0Cnt_LB;
- P_IO_RFC_Ctrl=0;
- P_IO_RFC_Ctrl=D_SEN2AEn;
- P_CTS_TM0Cnt_LB=0x00;
- P_CTS_TM0Cnt_HB=0x00;
- P_CTS_Ctrl2|=D_CTSStart;
- stater=2;
- break;
- case 2:
- P_CTS_Ctrl2&=~D_CTSStart;
- rfc_sen2=P_CTS_TM0Cnt_HB<<8;
- rfc_sen2+=P_CTS_TM0Cnt_LB;
- P_IO_RFC_Ctrl=0;
- P_IO_RFC_Ctrl=D_SEN2BEn;
- P_CTS_TM0Cnt_LB=0x00;
- P_CTS_TM0Cnt_HB=0x00;
- P_CTS_Ctrl2|=D_CTSStart;
- stater=3;
- break;
- case 3:
- P_CTS_Ctrl2&=~D_CTSStart;
- rfc_sen1=P_CTS_TM0Cnt_HB<<8;
- rfc_sen1+=P_CTS_TM0Cnt_LB;
- P_IO_Ctrl2&=~D_RFC2En;
- P_CTS_Ctrl2&=~D_CTSEn;
- P_IO_RFC_Ctrl=0;
- P_CTS_TM0Cnt_LB=0x00;
- P_CTS_TM0Cnt_HB=0x00;
- P_IO_PortB_Dir|=~0xf0;
- P_IO_PortB_Attrib&=~0x0f;
- P_IO_PortB_Data|=~0xf0;
- P_IO_PortB_Buffer&=0xf0;
- flag3&=0x7f;
- flag4|=0x01;
- stater=0;
- break;
- }
- }
- void scan_key(void)
- {
- static unsigned char stater=0,old_key;
- unsigned char i;
- i=P_IO_PortA_Data&0xf0;
- switch(stater)
- {
- case 0:
- if(i!=0xf0)
- {
- if(i!=old_key)//等待安检稳定,可以检测组合键。
- {
- inc_8_time=0;
- old_key=i;
- }
- else inc_8_time++;
- }
- else
- {
- old_key=0;
- inc_8_time=0;
- }
- if(inc_8_time>13){stater=1;key_data=i;}//按键稳定了
- break;
- case 1:
- if(i==0xf0)stater=2;
- else
- {
- inc_8_time++;
- }
- if(inc_8_time>=0xfe)
- {
- stater=3;
- flag2|=0x40;
- cant_5s=0;
- }
- break;
- case 2:
- if(i==0xf0)
- {
- flag2|=0x20;
- stater=0;
- inc_8_time=0;
- P_INT_KEY_Clear=0xff;
- P_INT_Ctrl2|=D_KeyIntEn;
- flag4&=0xfd;
- }
- else stater=1;
- break;
- case 3:
- if(i==0xf0)stater=4;
- else inc_8_time++;
- break;
- case 4:
- if(i==0xf0)
- {
- P_INT_KEY_Clear=0xff;
- P_INT_Ctrl2|=D_KeyIntEn;
- flag4&=0xfd;
- flag2&=0xbf;
- inc_8_time=0;
- stater=0;
- }
- else stater=3;
- break;
- }
- }
- int main(void)
- {
- display_buf=&p1;
- ow61_init();
- clrr_dis_ram();
- rfc_start();
- while(second<3);
- second=0;
- dark_pres_unit();
- dark_spend_unit();
- *(display_buf+4+6)&=0xfb;//p1
- *(display_buf+4)&=0x7f;//data
- if(key_data==0x90)flag1|=0x02;
- flag2&=0x9f;
- while(1)
- {
- weath_read();
- read_temp_hum_1();
- read_temp_hum_2();
- read_comp_data();
- key_fun();
- coter_20second();
- disp_time();
- disp_temp();
- disp_press();
- disp_anlg();
- disp_weath();
- if(flag6&0x01)
- {
- spend_unit();flag5=flag5&0xfe;
- }
- if(!(flag3&0x80)&&!(flag4&0x02)&&!(comp_init_flag&0x10))
- P_TIMER_TimeBase_Ctrl2&=~D_128HzEn;
- if(!(flag3&0x80))
- {
- P_CLK_PLL_Ctrl&=0xcf;
- P_SLP_Ctrl=0xa5;
- P_CLK_PLL_Ctrl|=0x30;
- }
- else P_CLK_PLL_Ctrl|=0x30;
- //rfc_start();
- }
- }
- #pragma codesec ROM_keyscan_NBank0
- void coter_20second()
- {
- if((cant_20s==40)&&((key_stater==1)||(key_stater==2)))
- {
- spend_danwei=0;
- key_stater=0;
- time_display_mode=0;
- flag4|=0xf8;
- time_display_mode=0;
- pres_disp_mode=0;
- press_unit();
- dark_spend_unit();
- }
- if(cant_10s==10)
- {
- P_IO_PortD_Data=P_IO_PortD_Buffer&0xf7;
- }
- if(cant_30s>33)
- {
- cant_30s=0;
- rfc_start();
- }
- if((flag1&0x02)&&(cant_3s>3))
- {
- flag1|=0x10;
- cant_3s=0;
- }
-
- }
- const unsigned char dis_data_tabl1[]=
- {
- 0X3F,//0
- 0X06,// 1
- 0X5B,// 2
- 0X4F,// 3
- 0X66,// 4
- 0X6D,//5
- 0X7D,//6
- 0X07,//7
- 0X7F,//8
- 0X6F,//9
- 0X00,//DARK 10
- 0x40,//- 11
- 0x76,//H 12
- 0x50,//r 13
- 0x6e,//y 14
- 0X79,//E 15
- 0X36,//W 16
- 0X09,//S 17
- 0X36,//N 18
- 0x39,//c 19
- 0x71,//F 20
- 0x63,//o 21
- 0x37,//n 22
- 0x38,//L 23
-
- };
- const unsigned char dis_data_tabl2[]=
- {
- 0X00,//0
- 0X00,// 1
- 0X01,// 2
- 0X01,// 3
- 0X01,// 4
- 0X01,//5
- 0X01,//6
- 0X00,//7
- 0X01,//8
- 0X01,//9
- 0X00,//DARK
- 0x01,//-
- 0x01,//H
- 0x01,//r
- 0x01,//y
- 0x00,//E
- 0x0c,//w
- 0x06,//s
- 0x06,//N
- };
- const unsigned char weath_buf[]=
- {
- 0x02,0x04,0x08,0x01
- };
- cood_char di_1ge_8[]=
- {
- 0x4000+1,
- 0x4006+1,
- 0x400c+1,
- 0x8012+1,
- 0x010c,
- 0x8000+1,//dark
- 0x800c+1
- };
- cood_char di_2ge_8[]=
- {
- 0x0100+4,//a
- 0x0200+4,//b
- 0x020c+4,//c
- 0x0112+4,//d
- 0x010c+4,//e
- 0x0106+4,//f
- 0x0206+4//g
- };
- cood_char di_3ge_8[]=
- {
- 0x0800+4,//a
- 0x1000+4,//b
- 0x100c+4,//c
- 0x0812+4,//d
- 0x080c+4,//e
- 0x0806+4,//f
- 0x1006+4//g
- };
- cood_char di_4ge_8[]=
- {
- 0x2000+4,//a
- 0x4000+4,//b
- 0x400c+4,//c
- 0x2012+4,//d
- 0x200c+4,//e
- 0x2006+4,//f
- 0x4006+4//g
- };
- cood_char di_5ge_8[]=
- {
- 0x0200+3,//a
- 0x0206+3,//b
- 0x0212+3,//c
- 0x010c+3,//d
- 0x0106+3,//e
- 0x0100+3,//f
- 0x020c+3//g
- };
- cood_char di_6ge_8[]=
- {
- 0x0400+3,//a
- 0x0800+3,//b
- 0x080c+3,//c
- 0x0412+3,//d
- 0x040c+3,//e
- 0x0406+3,//f
- 0x0806+3//g
- };
- cood_char di_7ge_8[]=
- {
- 0x4012+1,//a
- 0x0118+4,//b
- 0x011e+4,//c
- 0x401e+1,//d
- 0x801E+1,//e
- 0x8018+1,//f
- 0x4018+1//g
- };
- cood_char di_8ge_8[]=
- {
- 0x040c+4,//a
- 0x0412+4,//b
- 0x0418+4,//c
- 0x041e+4,//d
- 0x021e+4,//e
- 0x0212+4,//f
- 0x0218+4//g
- };
- cood_char di_9ge_8[]=
- {
- 0x1012+4,//a
- 0x2018+4,//b
- 0x201e+4,//c
- 0x101e+4,//d
- 0x081E+4,//e
- 0x0818+4,//f
- 0x1018+4//g
- };
- cood_char di_10ge_8[]=
- {
- 0x800c+4,//a
- 0x0112+3,//b
- 0x011e+3,//c
- 0x801e+4,//d
- 0x8018+4,//e
- 0x8012+4,//f
- 0x0118+3//g
- };
- cood_char di_11ge_8[]=
- {
- 0x0418+3,//a
- 0x0812+3,//b
- 0x081e+3,//c
- 0x021e+3,//d
- 0x0218+3,//e
- 0x041e+3,//f
- 0x0818+3//g
- };
- cood_char di_15ge_8[]=
- {
- 0x081e+2,//a
- 0x0418+2,//b
- 0x0412+2,//c
- 0x0812+2,//d
- 0x1012+2,//e
- 0x1018+2,//f
- 0x0818+2//g
- };
- cood_char di_16ge_8[]=
- {
- 0x021e+2,//a
- 0x011e+2,//b
- 0x0112+2,//c
- 0x020c+2,//d
- 0x0212+2,//e
- 0x0218+2,//f
- 0x0118+2//g
- };
- cood_char di_17ge_8[]=
- {
- 0x801e+3,//a
- 0x4018+3,//b
- 0x400c+3,//c
- 0x800c+3,//d
- 0x8012+3,//e
- 0x8018+3,//f
- 0x4012+3//g
- };
- cood_char di_18ge_8[]=
- {
- 0x100c+1,//a
- 0x0806+1,//b
- 0x0800+1,//c
- 0x1000+1,//d
- 0x2000+1,//e
- 0x2006+1,//f
- 0x1006+1//g
- };
- cood_char di_19ge_8[]=
- {
- 0x020c+1,//a
- 0x0106+1,//b
- 0x0100+1,//c
- 0x0200+1,//d
- 0x0400+1,//e
- 0x0406+1,//f
- 0x0206+1//g
- };
- cood_char di_20ge_8[]=
- {
- 0x400c+2,//a
- 0x2006+2,//b
- 0x2000+2,//c
- 0x4000+2,//d
- 0x8000+2,//e
- 0x8006+2,//f
- 0x4006+2//g
- };
- cood_char di_21ge_8[]=
- {
- 0x080c+2,//a
- 0x040c+2,//b
- 0x0406+2,//c
- 0x0800+2,//d
- 0x1006+2,//e
- 0x100c+2,//f
- 0x0806+2//g
- };
- cood_char di_22ge_8[]=
- {
- 0x010c+2,//a
- 0x8006+3,//b
- 0x8000+3,//c
- 0x0100+2,//d
- 0x0200+2,//e
- 0x0206+2,//f
- 0x0106+2//g
- };
- cood_char di_12ge_8_1[]=
- {
- 0x201e+1,//a
- 0x081e+1,//b
- 0x080c+1,//c
- 0x200c+1,//d
- 0x2012+1,//e
- 0x2018+1,//f
- 0x1018+1//g
- };
- cood_char di_12ge_8_2[]=
- {
- 0x0818+1,//H
- 0x101e+1,//I
- 0x0812+1,//L
- 0x1012+1,//N
- 0x8000+1,//dark
- 0x8000+1,//dark
- 0x8000+1,//dark
- };
- cood_char di_14ge_8_1[]=
- {
- 0x801e+2,//a
- 0x201e+2,//b
- 0x200c+2,//c
- 0x800c+2,//d
- 0x8012+2,//e
- 0x8018+2,//f
- 0x4018+2//g
- };
- cood_char di_14ge_8_2[]=
- {
- 0x2018+2,//H
- 0x401e+2,//I
- 0x2012+2,//L
- 0x4012+2,//N
- 0x8000+1,//dark
- 0x8000+1,//dark
- 0x8000+1,//dark
- };
- cood_char di_13ge_8_1[]=
- {
- 0x041e+1,//a
- 0x011e+1,//b
- 0x010c+1,//c
- 0x040c+1,//d
- 0x0412+1,//e
- 0x0418+1,//f
- 0x0218+1//g
- };
- cood_char di_13ge_8_2[]=
- {
- 0x0118+1,//H
- 0x021e+1,//I
- 0x0112+1,//L
- 0x0212+1,//N
- 0x8000+1,//dark
- 0x8000+1,//dark
- 0x8000+1,//dark
- };
- #if 1
- const float temp_table[]=
- {
- 262.70 ,
- 249.21 ,
- 238.49 ,
- 224.49 ,
- 213.16 ,
- 202.46 ,
- 192.36 ,
- 182.82 ,
- 173.80 ,
- 165.28 ,
- 157.22 ,
- 149.59 ,
- 142.37 ,
- 135.55 ,
- 129.08 ,
- 122.96 ,
- 117.16 ,
- 111.66 ,
- 108.45 ,
- 101.51 ,
- 96.83 ,
- 92.38 ,
- 88.18 ,
- 84.16 ,
- 80.36 ,
- 76.75 ,
- 73.32 ,
- 70.06 ,
- 66.96 ,
- 64.02 ,
- 61.22 ,
- 58.55 ,
- 56.02 ,
- 53.60 ,
- 51.31 ,
- 49.12 ,
- 47.04 ,
- 45.05 ,
- 43.16 ,
- 41.36 ,
- 39.64 ,
- 38.00 ,
- 36.44 ,
- 34.95 ,
- 33.52 ,
- 32.16 ,
- 30.87 ,
- 29.63 ,
- 28.44 ,
- 27.31 ,
- 26.23 ,
- 25.20 ,
- 24.21 ,
- 23.27 ,
- 22.27 ,
- 21.50 ,
- 20.68 ,
- 19.89 ,
- 19.13 ,
- 18.40 ,
- 17.71 ,//60
- /*
- 8.22 ,
- 7.94 ,
- 7.67 ,
- 7.41 ,
- 7.16 ,
- 6.9513 ,
- 6.6837 ,
- 6.4068 ,
- 6.2483 ,
- 6.0397 ,
- 5.8407 ,
- 5.6492 ,
- 5.4646 ,
- 5.2888 ,
- 5.1155 ,
- 4.9504 ,
- 4.7913 ,
- 4.6379 ,
- 4.49 ,
- 4.3474 ,
- 4.2099 ,
- 4.0772 ,
- 3.9493 ,
- 3.8259 ,
- 3.7088 ,
- 3.5917 ,
- 3.4087 ,
- 3.3735 ,
- 3.2701 ,
- */
- };
- #endif
- const float hum_table[]=
- {
- 4460.0,
- 1945.0,
- 895.8,
- 434.3,
- 221.1,
- 117.9,
- 65.63,
- 38.09,
- 23.00,
- 14.42,
- 9.363,
- 6.291,
- 4.366,
- 3.125,
- 2.303,
- 1.746
- };
- #if 1
- const unsigned char cAtanStep[19]=
- {
- 0x11,0x12,0x13,0x14,0x15,
- 0x16,0x17,0x18,0x19,0x1a,
- 0x1b,0x1c,0x1d,0x1e,0x1f,
- 0x20,0x21,0x22,0x23
- };
- const unsigned int iAtanPoint[19]=
- {
- 0x020C,0x0998,0x0d60,0x1057,0x12cd,
- 0x1508,0x1702,0x18ca,0x1a8c,0x1c2c,
- 0x1da6,0x1f12,0x206e,0x21b8,0x2258,
- 0x244d,0x2576,0x26a8,0x2710
- };
- const unsigned int iAtanAngle[19]=
- {
- 0x001e,0x008a,0x00bd,0x00e3,0x0101,
- 0x011b,0x0131,0x0144,0x0156,0x0166,
- 0x0174,0x0181,0x018d,0x0198,0x01a3,
- 0x01ad,0x01b6,0x01bf,0x01c2
- };
- #endif
- #if 1
- const unsigned int angle_buf[]=
- {
- 0x0057,0x00b0,0x010b,0x016b,0x01d2,0x0241,0x02bc,0x0347,0x03e7
- };
- #endif
- void ow61_init(void)
- {
- P_CLK_PLL_Ctrl=0x30;
- P_IO_Ctrl1=D_ExtI2En;
- P_IO_Ctrl2=0;
- P_IO_RFC_Ctrl=0;
- P_IO_LCDPORT_Ctrl=0x37;
-
- P_IO_PortA_Dir=0x0d;
- P_IO_PortA_Attrib=0x02;//PA1为输入做外部中断口
- P_IO_PortA_Data=0xf1;
-
- P_IO_PortB_Dir=0xff;
- P_IO_PortB_Attrib=0x10;
- P_IO_PortB_Data=0xff;
- P_IO_PortD_Dir=0xc8;
- P_IO_PortD_Attrib=0x20;
- P_IO_PortD_Data=0xe8;
- if(P_IO_PortD_Data&0x10)
- {
- flag4|=0x04;
- P_IO_PortD_Data|=0x10;
- }
- else flag4&=~0x04;
-
-
-
- P_IO_PortE_Dir=0x00;
- P_IO_PortE_Attrib=0x00;
- P_IO_PortE_Data=0x00;
- flag5=P_IO_PortE_Data;
- P_IO_PortE_Data=flag5;
- //;==========================
-
- //=======================
- P_LCD_BIAS_Ctrl=D_LCDBias3;
- P_LCD_VLCD_Ctrl=0x0f;//1.5v
- //P_LCD_VLCD_Ctrl=0x0e;//1.467v
- //P_LCD_VLCD_Ctrl=0x0d;//1.433v
- //P_LCD_VLCD_Ctrl=0x0c;//1.4v
- //P_LCD_VLCD_Ctrl=0x0b;//1.367v
- //P_LCD_VLCD_Ctrl=0x0a;//1.333v
- P_LCD_PUMP_Ctrl=D_PumpEn+D_PumpClk4K;
- P_LCD_Ctrl2=D_LCDAllOn;
- P_LCD_Clock=0x19;
- P_LCD_COM_Num=5;
- P_LCD_SEG_Num=5;
- P_IO_LCDPORT_Ctrl=D_ShiftSeg24+D_PEEn+D_PCEn+D_PDEn;
- P_LCD_StartAddr_LB=0;
- P_LCD_Ctrl1=D_LCDEn+D_DisplayOn;
- P_IO_KeyChange_Ctrl1=0xf0;
- //====================
- //P_LCD_Ctrl2=0x20;all off
- //P_LCD_Ctrl2=0x40;all on
- P_LCD_Ctrl2=0x00;
- //======================
- //中断初始化
- disable_interrupt();//禁止中断
- P_INT_TBL_Clear=0;
- P_INT_TBH_Clear=0;
- P_INT_TM0_Clear=0;
-
- P_TIMER_TimeBase_Status=D_2Hz_Flag;//清除溢出标志
- P_TIMER_TimeBase_Ctrl2=D_2HzEn+D_128HzEn;//使能2HZ中断
- P_TIMER_TimeBase_Ctrl1=D_TBRST;
- P_TIMER_TimeBase_Ctrl1=D_TBRUN;
- P_INT_Ctrl2=D_TBLIntEn+D_TBHIntEn+D_KeyIntEn;//+D_CtsTM0IntEn;
- P_INT_Ctrl1=D_TM0IntEn;
- P_TIMER_Timer_Ctrl=D_TM1Clk32K;
- /*
- P_TIMER_EN=0;
- P_TIMER_TM1Data_LB=(65536-4)%256;
- P_TIMER_TM1Data_HB=(65536-4)/256;//4kHz
- P_AUDIO_BZ_Ctrl=0x05;
- P_TIMER_EN=D_TM1En;
- */
- //P_TIMER_TM1CntData_LB=(65536-8)%256;
- //P_TIMER_TM1CntData_HB=(65536-8)/256;
- P_CTS_Timer_Ctrl=D_CtsTM0ClkRFC2;
- // P_TIMER_TM0Data_LB=(65536-2)%256;
- // P_TIMER_TM0Data_HB=(65536-2)%256;
- // P_TIMER_TM0CntData_LB=(65536-2)%256;
- // P_TIMER_TM0CntData_HB=(65536-2)%256;
- P_INT_KEY_Clear=0xff;
-
- //P_TIMER_EN=D_TM1En;
- //P_AUDIO_BZ_Ctrl=0x85;//bzp tim1 on
- enable_interrupt();//使能中断
-
- }
- void delay(void)
- {
- // unsigned char i;
- //for(i=0;i<20;i++)
- {
- nop_instruction();
- nop_instruction();
- nop_instruction();
- }
- }
- void clr_press_sclk_pb7(void)
- {
- P_IO_PortB_Data&=0x7f;
- }
- void set_press_sclk_pb7(void)
- {
- P_IO_PortB_Data|=0x80;
- }
- void clr_press_sdo_pb6(void)
- {
- P_IO_PortB_Data&=0xbf;
- }
- void set_press_sdo_pb6(void)
- {
- P_IO_PortB_Data|=0x40;
- }
- void sdo_pb6_set_in(void)
- {
- P_IO_PortB_Dir&=0xbf;
- P_IO_PortB_Attrib|=0x40;
- P_IO_PortB_Data|=0x40;
- }
- void sdo_pb6_set_out(void)
- {
- P_IO_PortB_Dir|=0x40;
- P_IO_PortB_Data|=0x40;
- }
- void i2c_press_start(void)
- {
- sdo_pb6_set_out();
- delay();
- clr_press_sclk_pb7();
- delay();
- set_press_sdo_pb6();
- delay();
- set_press_sclk_pb7();
- delay();
- clr_press_sdo_pb6();
- delay();
- clr_press_sclk_pb7();
-
- }
- void i2c_press_stop(void)
- {
- sdo_pb6_set_out();
- clr_press_sdo_pb6();
- delay();
- set_press_sclk_pb7();
- delay();
- set_press_sdo_pb6();
- }
- unsigned char i2c_press_wait_ack(void)
- {
- unsigned char wr0;
- sdo_pb6_set_in();
- set_press_sclk_pb7();
- delay();
- wr0=P_IO_PortB_Data;
- clr_press_sclk_pb7();
- delay();
- return wr0;
- }
- void i2c_press_send_ack(void)
- {
- sdo_pb6_set_out();
- delay();
- clr_press_sdo_pb6();
- delay();
- set_press_sclk_pb7();
- delay();
- clr_press_sclk_pb7();
- delay();
- }
- void i2c_press_no_ack(void)
- {
- sdo_pb6_set_out();
- delay();
- set_press_sdo_pb6();
- delay();
- set_press_sclk_pb7();
- delay();
- clr_press_sclk_pb7();
- delay();
- }
- void i2c_press_send_8bit( unsigned char data1)
- {
- unsigned data_buf,i;
- data_buf=data1;
- sdo_pb6_set_out();
- for(i=0;i<8;i++)
- {
- if(data_buf&0x80)set_press_sdo_pb6();
- else clr_press_sdo_pb6();
- set_press_sclk_pb7();
- delay();
- clr_press_sclk_pb7();
- delay();
- data_buf<<=1;
- }
- }
- void i2c_press_read_8bit(void)
- {
- unsigned char i;
- sdo_pb6_set_in();
- for(i=0;i<8;i++)
- {
- press_data_buf<<=1;
- set_press_sclk_pb7();
- delay();
- if(P_IO_PortB_Buffer&0x40)press_data_buf=press_data_buf|0x00000001;
- else press_data_buf=press_data_buf&0xfffffffe;
- clr_press_sclk_pb7();
- delay();
- }
- }
- #if 0
- void weath(void)
- {
- unsigned char buf1;
- pointer1=&wr4;
- wr4=press_save_data-press_data_buf;
- buf1=*(pointer1+3);
- if(buf1&0x80)wr4=~wr4;//取最高8位
- wr4+=1;
- if(!weath_stater)//判断是否是晴天
- {
- if(buf1&0x80)press_save_data=press_data_buf;
- else
- {
- if((wr4>300)&&(wr0<600))flag2=0x04;//阴天
- else if(wr4>=600){flag2=0x08;press_save_data=press_data_buf;}//小雨
- }
- }
- else
- {
- if(buf1&0x80)//判断气压是增还是减
- {
- if((wr4>200)&&(wr0<500))flag2=0x02;//半晴天
- else if(wr4>=500)flag2=0x01;//晴天
- }
- else
- {
- if(flag2&0x10);//press_save_data=press_data_buf;
- else if(flag2&0x08)
- {
- if(wr4>=200)flag2=0x10;//大雨
- }
- else {if(wr4>100)flag2=0x08;}//小雨
- press_save_data=press_data_buf;
- }
- }
- }
- #endif
- void weath(void)
- {
-
- if(flag0&0x4)
- {
- if(canat_60_minute<60)return;
- flag4|=0x10;
- canat_60_minute=0;
- wr4=press_save_data-press_data_buf;
- switch(weath_stater)
- {
- case 0:
- if(wr4<=0)press_save_data=press_data_buf;
- else if(wr4>=60)
- {
- press_save_data=press_data_buf;
- weath_stater=3;
- }
- else if(wr4>=30)
- {
- weath_stater=2;
- }
- break;
- case 1:
- if(wr4<=-50)
- {
- press_save_data=press_data_buf;
- weath_stater=0;
- }
- else if(wr4>=10)
- {
- press_save_data=press_data_buf;
- weath_stater=3;
- }
- break;
- case 2:
- if(wr4<=-50)
- {
- press_save_data=press_data_buf;
- weath_stater=0;
- }
- else if(wr4<=-20)weath_stater=1;
- else if(wr4>=10)
- {
- press_save_data=press_data_buf;
- weath_stater=3;
- }
- break;
- case 3:
- if(wr4<=-50)
- {
- press_save_data=press_data_buf;
- weath_stater=0;
- }
- else if(wr4<=-20)weath_stater=1;
- else if(wr4>0)press_save_data=press_data_buf;
- break;
- }
- }
- else
- {
- press_save_data=press_data_buf-30;
- flag0|=0x4;
- }
- }
- void weath_read(void)
- {
- unsigned char wr0;
- if(!(flag1&0x10))return;
- i2c_press_start();
- i2c_press_send_8bit(0xd8);
- wr0=i2c_press_wait_ack();
- if(wr0&0x40){i2c_press_stop();return;}
- i2c_press_send_8bit(0x30);
- wr0=i2c_press_wait_ack();
- if(wr0&0x40){i2c_press_stop();return;}
- i2c_press_send_8bit(0x0a);
- wr0=i2c_press_wait_ack();
- if(wr0&0x40){i2c_press_stop();return;}
- i2c_press_stop();
-
- i2c_press_start();
- i2c_press_send_8bit(0xd8);
- wr0=i2c_press_wait_ack();
- if(wr0&0x40){i2c_press_stop();return;}
- i2c_press_send_8bit(0x06);
- wr0=i2c_press_wait_ack();
- if(wr0&0x40){i2c_press_stop();return;}
- i2c_press_start();
- i2c_press_send_8bit(0xd9);
- wr0=i2c_press_wait_ack();
- if(wr0&0x40){i2c_press_stop();return;}
- press_data_buf=0;
- //i2c_press_start();
- i2c_press_read_8bit();
- i2c_press_send_ack();
- i2c_press_read_8bit();
- i2c_press_send_ack();
- i2c_press_read_8bit();
- i2c_press_no_ack();
- i2c_press_stop();
- press_data_buf=press_data_buf>>6;//unit pa
- press_data_buf=press_data_buf/100*10;
- if(!(flag0&0x4))
- {
- high_value=10120-press_data_buf;
- }
- //wr1=powf((float)press_data_buf/(float)press_data,0.190263);
- //wr1=1-wr1;
- //wr1=wr1*44330.77;
- //high_value=wr1;
- //high_value+=save_shift_high;
- //if(high_value>9999)high_value=9999;
- //else if(high_value<0)high_value=0;
-
- weath();
- press_unit();
- flag1&=0xef;
- }
- void clrr_dis_ram(void)
- {
- unsigned char i;
- for(i=0;i<0xff;i++)
- {
- *(display_buf+i)=0x00;
- }
- for(i=0;i<0xff;i++)
- {
- *(display_buf+i)=0xFF;
- }
- }
- #if 1
- void clr_temp_sclk_pd7(void)
- {
- P_IO_PortD_Data&=0x7f;
- }
- void set_temp_sclk_pd7(void)
- {
- P_IO_PortD_Data|=0x80;
- }
- void clr_temp_sdo_pd6(void)
- {
- P_IO_PortD_Data&=0xbf;
- }
- void set_temp_sdo_pd6(void)
- {
- P_IO_PortD_Data|=0x40;
- }
- void sdo_pd6_set_in(void)
- {
- P_IO_PortD_Dir&=0xbf;
- P_IO_PortD_Attrib|=0x40;
- P_IO_PortD_Data|=0x40;
- }
- void sdo_pd6_set_out(void)
- {
- P_IO_PortD_Dir|=0x40;
- P_IO_PortD_Data|=0x40;
- }
- void teamp_start(void)
- {
- sdo_pd6_set_out();
- set_temp_sdo_pd6();
- set_temp_sclk_pd7();
- delay();
- clr_temp_sdo_pd6();
- clr_temp_sclk_pd7();
- delay();
- }
- void temp_stop(void)
- {
- sdo_pd6_set_out();
- clr_temp_sdo_pd6();
- set_temp_sclk_pd7();
- delay();
- set_temp_sdo_pd6();
- delay();
- }
- void temp_send_ack(void)
- {
- sdo_pd6_set_out();
- clr_temp_sdo_pd6();
- set_temp_sclk_pd7();
- delay();
- clr_temp_sclk_pd7();
- delay();
- }
- void temp_no_ack(void)
- {
- sdo_pd6_set_out();
- set_temp_sdo_pd6();
- set_temp_sclk_pd7();
- delay();
- clr_temp_sclk_pd7();
- delay();
- }
- void temp_send_8bit(unsigned char data)
- {
- unsigned char i,data_buf=0;
- sdo_pd6_set_out();
- data_buf=data;
- for(i=0;i<8;i++)
- {
- if(data_buf&0x80)sdo_pd6_set_out();
- else clr_temp_sdo_pd6();
- data_buf<<=1;
- set_temp_sclk_pd7();
- delay();
- clr_temp_sclk_pd7();
- delay();
- }
- sdo_pd6_set_in();
- set_temp_sclk_pd7();
- delay();
- if(P_IO_PortD_Data&0x40)flag0|=0x20;
- else flag0&=0xdf;
- clr_temp_sclk_pd7();
- }
- void temp_read(void)
- {
- unsigned char i;
- sdo_pd6_set_in();
- for(i=0;i<8;i++)
- {
- wr6<<=1;
- set_temp_sclk_pd7();
- delay();
- if(P_IO_PortD_Data&0x40)wr6|=0x0001;
- else wr6=wr6&0xfffe;
- clr_temp_sclk_pd7();
- delay();
- }
- }
- void read_temp_hum_2(void)
- {
- static unsigned char stater=0;
- if(!(flag3&0x80))return;
- if(!(flag4&0x04))return;
- switch(stater)
- {
- case 0:
- teamp_start();
- temp_send_8bit(0x80);
- temp_send_8bit(0xe5);
- temp_test_time=0;
- stater++;
- break;
- case 1:
- if(temp_test_time>2)stater++;
- break;
- case 2:
- teamp_start();
- temp_send_8bit(0x81);
- temp_read();
- temp_send_ack();
- temp_read();
- temp_no_ack();
- temp_stop();
- if(!(flag0&0x20))
- {
- in_hum=(int)(125.0*wr6/65536.0-6.0);
- stater++;
- }
- else stater=0;
- break;
- case 3:
- teamp_start();
- temp_send_8bit(0x80);
- temp_send_8bit(0xe0);
- teamp_start();
- temp_send_8bit(0x81);
- temp_read();
- temp_send_ack();
- temp_read();
- temp_no_ack();
- temp_stop();
- if(!(flag0&0x20))
- {
- in_temp=(int)(1757.2*wr6/65536-468.5);
- in_temp_f=(int)(1757.2*wr6/117964.8/*65536*1.8=117964.8*/-843.3/*468.5*1.8=843.3*/+320);
- }
- stater=0;
- flag3&=0x7f;
- flag4|=0x40;
- break;
- }
- }
- #endif
- unsigned char Rfc_SeekTab(float res,float *tab_addr)
- {
- unsigned char x,y,z;
- unsigned long res_m;
- //初始化"实测电阻值"在表中的范围
- //起点
- x=0;
- //终点
- z=60;
- //递归缩小包围圈,找到"实测电阻值"在表中的具体位置
- while((x+2) <= z)
- {
- //取表格中间的数据
- y = (x+z)/2;
- res_m = tab_addr[y];
- //缩小实测电阻值所在表格的范围
- if(res > res_m)
- {
- z = y;
- }
- else
- {
- x = y;
- }
- }
- return x;
- }
- void cal_temp(unsigned int d1)
- {
- wr0=rfc_ref;
- wr0=wr0*50.0/d1;
- wr0=wr0-50;
- if(wr0<1000)
- wr0=wr0*1000.0/(1000.0-wr0);
- else wr0=4460;
- }
- void read_temp_hum_1(void)
- {
- unsigned char i;
- if(!(flag4&0x01))return;
- cal_temp(rfc_sen1);
- i=Rfc_SeekTab(wr0,temp_table);
- if(i==0)wr0=-10;
- else if(wr0>=temp_table[i])
- {
- wr0=(temp_table[i-1]-wr0)/(temp_table[i-1]-temp_table[i]);
- wr0=(float)i-1+wr0-10.0;
- }
- else
- {
- wr0=(temp_table[i]-wr0)/(temp_table[i]-temp_table[i+1]);
- wr0=(float)i+wr0-10.0;
- }
- wr1=wr0;
- in_temp=(int)(wr0*10.0);
- in_temp_f=(wr0*1.8+32)*10.0;
- cal_temp(rfc_sen2);
- i=0;
- for(;;)
- {
- if(wr0>=hum_table[i])break;
- i++;
- if(i==15)break;
- }
- if(i==0)in_hum=20;
- else if(i==15)in_hum=99;
- else
- {
- wr0=(wr0-hum_table[i])/(hum_table[i-1]-hum_table[i])*5;
- in_hum=(int)(20.0+(float)i*5.0-wr0);
- }
- wr1=(wr1-25)*0.5;
- in_hum=(int)(in_hum-wr1);
- flag4&=0xfe;
- flag4|=0x40;
- }
- #pragma codesec ROM_keyscan_NBank1
- #if 0
- void heat_index(void)
- {
- wr0=in_temp*1.8+32;
- wr3=-42.379+2.04901523*wr0+10.14333172*in_hum;
- wr3=wr3-0.22475541*wr0*in_hum;
- wr3=wr3+0.00122874*wr0*wr0*in_hum;
- wr3=wr3+0.00085282*in_hum*in_hum*wr0;
- wr3=wr3-0.00000199*in_hum*in_hum*wr0*wr0;
- if(in_hum<13)
- {
- //wr3=wr3-sqrtf((17-fabsf(95-wr0))/17.0)*(13-in_hum)/4.0;
- wr3=wr3-powf((17-fabsf(95-wr0))/17.0,0.5)*(13-in_hum)/4.0;
- }
- else if(((in_hum>85)&&(80<wr0<87)))
- {
- wr3=wr3+(in_hum-85)/10.0*(87-wr0);
- }
- heat_buf=wr3;
- wr3=35.74+0.6215*wr0-35.75*powf(spend,0.16) + 0.4275*wr0*powf(spend,0.16);
- chilltemp=wr3;
- }
- #endif
- void set_comp_sclk_high(void)
- {
- P_IO_PortB_Data|=0x20;
- }
- void set_comp_sclk_low(void)
- {
- P_IO_PortB_Data&=0xdf;
- }
- void set_comp_sda_high(void)
- {
- P_IO_PortB_Data|=0x10;
- }
- void set_comp_sda_low(void)
- {
- P_IO_PortB_Data&=0xef;
- }
- void set_comp_sda_in(void)
- {
- P_IO_PortB_Dir&=0xef;
- //P_IO_PortB_Attrib|=0x10;
- //P_IO_PortB_Data|=0x10;
- }
- void set_comp_sda_out(void)
- {
- P_IO_PortB_Dir|=0x10;
- //P_IO_PortB_Attrib&=0xef;
- //P_IO_PortB_Data|=0x10;
- }
- void comp_start(void)
- {
- //set_comp_sda_out();
- set_comp_sda_high();
- delay();
- set_comp_sclk_high();
- delay();
- set_comp_sda_low();
- delay();
- set_comp_sclk_low();
- delay();
- }
- void comp_stop(void)
- {
- //set_comp_sda_out();
- set_comp_sclk_low();
- delay();
- set_comp_sda_low();
- delay();
- set_comp_sclk_high();
- delay();
- set_comp_sda_high();
- delay();
- set_comp_sclk_high();
- delay();
- }
- void comp_sed_ack(void)
- {
- //set_comp_sda_out();
- set_comp_sda_low();
- set_comp_sclk_high();
- delay();
- delay();
- delay();
- set_comp_sclk_low();
- delay();
- }
- void comp_no_ack(void)
- {
- //set_comp_sda_out();
- set_comp_sda_high();
- delay();
- set_comp_sclk_high();
- delay();
- delay();
- set_comp_sclk_low();
- delay();
- }
- void comp_send_8_bit(unsigned char send_data)
- {
- unsigned char i,d;
- d=send_data;
- //set_comp_sda_out();
- for(i=0;i<8;i++)
- {
- if(d&0x80)set_comp_sda_high();
- else set_comp_sda_low();
- set_comp_sclk_high();
- delay();
- set_comp_sclk_low();
- delay();
- d<<=1;
- }
- set_comp_sda_in();
- delay();
- set_comp_sclk_high();
- delay();
- if(P_IO_PortB_Data&0x10)flag1|=0x40;
- else flag1&=0xbf;
- set_comp_sclk_low();
- delay();
- set_comp_sda_out();
- }
- unsigned char comp_read_8bit(void)
- {
- unsigned char i=0,d=0;
- set_comp_sda_in();
- for(i=0;i<8;i++)
- {
- d<<=1;
- set_comp_sclk_high();
- delay();
- if(P_IO_PortB_Data&0x10)d|=0x01;
- else d&=0xef;
- set_comp_sclk_low();
- delay();
- }
- set_comp_sda_out();
- return d;
- }
- unsigned char read_stater(void)
- {
- unsigned char return_data;
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x06);
- comp_start();
- comp_send_8_bit(0x61);
- if(flag1&0x40)return 0;
- return_data=comp_read_8bit();
- comp_no_ack();
- comp_stop();
- return return_data;
- }
- void rest_set(unsigned char dd)
- {
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x07);
- comp_send_8_bit(dd);
- comp_stop();
- }
- void max_min_init(void)
- {
- xmax=0x0000;
- xmin=0xffff;
- ymax=0;
- ymin=0xffff;
- xvalue_buf=0;
- yvalue_buf=0;
- }
- void read_data(void)
- {
- unsigned char r_data;
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x00);
- comp_start();
- comp_send_8_bit(0x61);
- XRaw=comp_read_8bit();
- comp_sed_ack();
- r_data=comp_read_8bit();
- XRaw|=r_data<<8;
- //XRaw=r_data;
- comp_sed_ack();
- YRaw=comp_read_8bit();
- comp_sed_ack();
- r_data=comp_read_8bit();
- YRaw|=r_data<<8;
- //YRaw=r_data;
- comp_sed_ack();
- r_data=comp_read_8bit();
- comp_sed_ack();
- r_data=comp_read_8bit();
- comp_no_ack();
- comp_stop();
- }
- unsigned long int arcTan(unsigned long int lAcc);
- /****************************************************
- ****************************************************/
- /***************************************************
- ***************************************************/
- /***************************************************
- ***************************************************/
- /******************************************************************************
- ;---Function Description: compass processs
- ******************************************************************************/
- void cal_compass(void)
- {
- unsigned long int X_raw, Y_raw; // signed X & Y Raw data
- unsigned long int lAcc;
- int Vangle;
- if( XRaw > Xoffset )
- X_raw = XRaw - Xoffset;
- else
- X_raw = Xoffset - XRaw ;
- if( YRaw > Yoffset)
- Y_raw = YRaw - Yoffset;
- else
- Y_raw = Yoffset - YRaw ;
- if( Xsens > Ysens )
- {
- Y_raw*=Xsens;
- Y_raw = Y_raw / Ysens; // perform gain matching
- }
- else
- {
- X_raw*=Ysens;
- X_raw = X_raw/ Xsens; // perform gain matching
- }
- if( X_raw >= Y_raw)
- {
- lAcc = Y_raw;
- lAcc *= 1000;
- lAcc /= X_raw;
- lAcc /= 1;
- lAcc = arcTan(lAcc);
- Vangle = lAcc / 10;
- // Vangle=arcTan(lAcc);
- }
- else
- {
- lAcc = X_raw;
- lAcc *= 1000;
- lAcc = lAcc/Y_raw;
- lAcc = arcTan(lAcc);
- Vangle = 90 - (lAcc/10);
- //Vangle=90-arcTan(lAcc);
- }
- #if 0
- if(XRaw>=Xoffset)
- {
- if(YRaw>=Yoffset)Vangle=Vangle;
- else Vangle=180-Vangle;
- }
- else
- {
- if(YRaw>=Yoffset)Vangle=360-Vangle;
- else Vangle=180+Vangle;
- }
- #endif
- #if 1
- if((XRaw > Xoffset) && (YRaw > Yoffset)) Vangle = Vangle; // quadrant +X, +Y (0 to 90)
- if((XRaw < Xoffset) && (YRaw > Yoffset)) Vangle = 180 - Vangle; // quadrant X, +y (91 to 180)
- else if((XRaw < Xoffset) && (YRaw < Yoffset)) Vangle = 180 + Vangle; // quadrant X, -Y (181 to 270)
- else if((XRaw > Xoffset) && (YRaw < Yoffset)) Vangle = 360 - Vangle; // quadrant +X, -Y (271 to 359)
- //Vangle-=180;
- // if(Vangle>=360)
- // {
- // Vangle -=360;
- // }
- //else if(Vangle<0)
- // {
- // Vangle +=360;
- // }
- #if 0
- if(XRaw == Xoffset)
- {
- if(YRaw == Yoffset)
- {
- Vangle = 0;
- }
- else if(YRaw > Yoffset)
- {
- Vangle = 90;
- }
- else
- {
- Vangle = 270;
- }
- }
- if(YRaw == Yoffset)
- {
- if(XRaw == Xoffset)
- {
- Vangle = 0;
- }
- else if(XRaw > Xoffset)
- {
- Vangle = 0;
- }
- else
- {
- Vangle = 180;
- }
- }
- if(Vangle>=360)
- {
- Vangle -=360;
- }
- else
- {
- if(Vangle <= 0)
- {
- Vangle +=360;
- }
- if(Vangle == 360)
- {
- Vangle = 0;
- }
- }
- #endif
- #if 0
- if(!((XRaw < Xoffset) && (YRaw < Yoffset)))
- {
- if(Vangle < 360)
- {
- Vangle = 360 - Vangle;
- }
- else
- {
- Vangle -= 360;
- }
- Vangle += 270;
- if(Vangle >= 360)
- {
- Vangle -= 360;
- }
- }
- #endif
- //I2cAngleBuf[0] = (unsigned char)(Vangle & 0xff);
- //Vangle >>= 8;
- //I2cAngleBuf[1] = (unsigned char)(Vangle & 0xff);
- #endif
- angle=Vangle;
- flag4|=0x80;
- }
- /***************************************************
- ***************************************************/
- #if 1
- unsigned long int arcTan(unsigned long int lAcc) // arc Tan function
- {
- char I;
- lAcc *= 10;
- if(lAcc>10000)
- lAcc=10000;
- for(I=0;I<19;I++)
- {
- if (lAcc <= iAtanPoint[I])
- {
- if(I== 0)
- {
- lAcc /= cAtanStep[0];
- }
- else
- {
- lAcc -= iAtanPoint[I-1];
- lAcc /= cAtanStep[I-1];
- lAcc += iAtanAngle[I-1];
- }
- break;
- }
- }
- return(lAcc);
- }
- #endif
- #if 0
- unsigned char arcTan(unsigned long int lAcc)
- {
- unsigned char i,j=0;
- for(i=0;i<9;i++)
- {
- if (lAcc <= angle_buf[i])
- {
- if(i== 0)
- {
- j = 5-(angle_buf[i]-lAcc)*10/angle_buf[i]/10;
- }
- else
- {
- j = 5*i-(angle_buf[i]-lAcc)*10/(angle_buf[i]-angle_buf[i-1])/10;
- }
- break;
- }
- }
- if(i>=8)j=45;
- return j;
- }
- #endif
- void test_comp()
- {
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x07);
- comp_send_8_bit(0x01);
- comp_stop();
- }
- #if 0
- void read_comp_data(void)
- {
- unsigned char r_data;
- unsigned long int data_1;
- static unsigned char i;
- static unsigned char j=0;
- if(!(comp_init_flag&0x10))return;
- if(comp_init_flag&0x01)//init1
- {
- //if(axis_test_time<4)return;
- axis_test_time=0;
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x07);
- comp_send_8_bit(0x80);
- comp_stop();
- comp_init_flag&=0xfe;
- i=0;
- }
- else if(comp_init_flag&0x02)//init2
- {
- if(axis_test_time<16)return;
- axis_test_time=0;
- // comp_start();
- // comp_send_8_bit(0x60);
- //comp_send_8_bit(0x08);
- // comp_send_8_bit(0x01);
- // comp_stop();
- rest_set(0x23);
- comp_init_flag&=0xfd;
- i=0;
- max_min_init();
- //test_comp();
- j=0;
- }
- else if(comp_init_flag&0x04)
- {
- r_data=read_stater();
- if(r_data&0x01)
- {
- read_data();
- //test_comp();
- if(xmax<XRaw)
- {
- xmax=XRaw;
- //test11=YRaw;
- }
- if(xmin>XRaw)
- {
- xmin=XRaw;
- //test21=YRaw;
- }
- if(ymax<YRaw)
- {
- ymax=YRaw;
- //test31=XRaw;
- }
- if(ymin>YRaw)
- {ymin=YRaw;}//test41=XRaw;
- i++;
- if(i>=8)
- {
- i=0;
- compass_data++;
- j++;
- }
- if(j>=40)comp_init_flag&=~0x04;
- }
- }
- else if(comp_init_flag&0x08)
- {
- i=0;
- data_1=xmax;
- data_1+=xmin;
- Xoffset = data_1/2;
- data_1=ymax;
- data_1+=ymin;
- Yoffset =data_1/2;
- Xsens = (xmax - xmin)/2;
- Ysens = (ymax - ymin)/2;
- max_min_init();
- comp_init_flag&=0xf7;
- }
- else
- {
- r_data=read_stater();
- if(r_data&0x01)
- {
- read_data();
- //test_comp();
- xvalue_buf += XRaw;
- yvalue_buf += YRaw;
- if(xmax<XRaw)
- xmax=XRaw;
- if(xmin>XRaw)
- xmin=XRaw;
- if(ymax<YRaw)
- ymax=YRaw;
- if(ymin>YRaw)
- ymin=YRaw;
- i++;
- if(i==6)
- {
- XRaw=(xvalue_buf-xmax-xmin)>>2;
- YRaw=(yvalue_buf-ymin-ymax)>>2;
- cal_compass();
- max_min_init();
- i=0;
- }
- }
- }
-
-
- }
- #endif
- unsigned int h1x,h1y,h2x,h2y;
- void read_comp_data(void)
- {
- unsigned char r_data;
- static unsigned char comp_stater=0,i=0,j=0;
- unsigned long addx,addy;
- if(!(comp_init_flag&0x10)){comp_stater=0;return;}
- switch(comp_stater)
- {
- case 0:
- axis_test_time=0;
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x07);
- comp_send_8_bit(0x80);
- comp_stop();
- comp_stater++;
- break;
- case 1:
- if(axis_test_time<16)break;
- rest_set(0x21);
- comp_stater++;
- break;
- case 2:
- r_data=read_stater();
- if(!(r_data&0x01))break;
- read_data();
- h1x=XRaw;
- h1y=YRaw;
- comp_stater++;
- break;
- case 3:
- axis_test_time=0;
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x07);
- comp_send_8_bit(0x80);
- comp_stop();
- comp_stater++;
- break;
- case 4:
- if(axis_test_time<16)break;
- rest_set(0x41);
- comp_stater++;
- break;
- case 5:
- r_data=read_stater();
- if(!(r_data&0x01))break;
- read_data();
- h2x=XRaw;
- h2y=YRaw;
- addx=h2x;
- addx+=h1x;
- addx/=2;
- addy=h2y;
- addy+=h1y;
- addy/=2;
- max_min_init();
- comp_stater++;
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x07);
- comp_send_8_bit(0x03);
- comp_stop();
- break;
- case 6:
- r_data=read_stater();
- if(!(r_data&0x01))break;
- read_data();
- //test_comp();
- if(xmax<XRaw)
- {
- xmax=XRaw;
- //test11=YRaw;
- }
- if(xmin>XRaw)
- {
- xmin=XRaw;
- //test21=YRaw;
- }
- if(ymax<YRaw)
- {
- ymax=YRaw;
- //test31=XRaw;
- }
- if(ymin>YRaw)
- {ymin=YRaw;}//test41=XRaw;
- i++;
- if(i>=8)
- {
- i=0;
- compass_data++;
- j++;
- }
- if(j>=40)comp_stater++;
- break;
- case 7:
- comp_init_flag&=~0x04;
- i=0;
- addx=xmax;
- addx+=xmin;
- Xoffset = addx/2;
- addx=ymax;
- addx+=ymin;
- Yoffset =addx/2;
- Xsens = (xmax - xmin)/2;
- Ysens = (ymax - ymin)/2;
- max_min_init();
- comp_stater++;
- break;
- case 8:
- r_data=read_stater();
- if(!(r_data&0x01))break;
- read_data();
- //test_comp();
- xvalue_buf += XRaw;
- yvalue_buf += YRaw;
- if(xmax<XRaw)
- xmax=XRaw;
- if(xmin>XRaw)
- xmin=XRaw;
- if(ymax<YRaw)
- ymax=YRaw;
- if(ymin>YRaw)
- ymin=YRaw;
- i++;
- if(i==6)
- {
- XRaw=(xvalue_buf-xmax-xmin)>>2;
- YRaw=(yvalue_buf-ymin-ymax)>>2;
- cal_compass();
- max_min_init();
- i=0;
- }
- break;
-
- }
- }
- #define uch unsigned char
- void display1(uch *p1, unsigned int*p2, uch d1)
- {
- uch buf1,i,buf2,buf6;
- unsigned int buf3;
- buf1=*(p1+d1);
- for(i=0;i<7;i++)
- {
- buf3=*(p2+i);
- buf2=(uch)buf3;
- buf6=(uch)(buf3>>8);
- if(buf1&0x01)*(display_buf+buf2)|=buf6;
- else *(display_buf+buf2)&=~buf6;
- buf1>>=1;
- }
- }
- void getspeedms(void)
- {
- if(spend<114)dis_spend1=0.0272;
- else if(spend<174)dis_spend1=0.0241;
- else dis_spend1=0.0234;
- dis_spend1=((float)spend*dis_spend1+0.05)*10;
- if(dis_spend1>711)dis_spend1=711;
- }
- void spend_unit(void)
- {
- if(!pres_disp_mode)return;
- flag4|=0x20;
- getspeedms();
- switch(spend_danwei)
- {
- case 0:
- //dis_spend=wr0;
- dis_spend=dis_spend1;//spend;//dis_spend1;//m/s
- break;
- case 1:
- dis_spend=dis_spend1*194.4;//knots/h
- //dis_pres=(unsigned int)dis_spend;
- break;
- case 2:
- dis_spend=dis_spend1*360;//km/h
-
- break;
- case 3:
- dis_spend=dis_spend1*223.69;//mph
- break;
- }
- dis_pres=(unsigned int)dis_spend;
- }
- void press_unit()
- {
- int d;
- if(pres_disp_mode)return;
- flag4|=0x20;
- d=press_data_buf/10+(high_value+high_value1)/10;
- switch(spend_danwei)
- {
- case 0:
- dis_pres=d;//hpa 1位小数点
- if(d>1050)dis_pres=1050;
- if(d<850)dis_pres=850;
- break;
- case 1:
- if(d>1050)d=1050;
- if(d<850)d=850;
- wr0=2.9529988*(float)d;//inhg 2位小数点
- dis_pres=(unsigned long int)wr0;
- break;
- case 2:
- if(d>1050)d=1050;
- if(d<850)d=850;
- wr0=0.75*(float)d;
- dis_pres=(unsigned long int)wr0;//mmhg 2位小数点
- break;
- case 3:
- dis_pres=high_value+high_value1;//m 0位小数点
- break;
- case 4:
- wr0=(float)(high_value+high_value1)*3.24254;// 1/0.3084=3.24254;//0.3048;
- dis_pres=(unsigned long int)wr0;//feet 0位小数点
- break;
- }
- }
- void key_fun(void)
- {
- int k;
- if(!(flag2&0x60))return;
- cant_10s=0;
- cant_20s=0;
- flag4|=0x08;
- if(key_data==0xe0)
- {
- if(flag2&0x40)
- {
- if(cant_5s>3)
- {
- flag2&=0x9f;
- if(P_IO_PortA_Data&0x04)P_IO_PortA_Buffer&=~0x04;
- else P_IO_PortA_Buffer|=0x04;
- }
- }
- else
- {
- P_IO_PortD_Data=P_IO_PortD_Buffer|0x08;
- cant_5s=0;
- flag2&=0x9f;
- }
- return;
- }
- switch(key_stater)
- {
- case 0:
- cant_15s=0;
- if(flag2&0x20)//短按
- {
- flag3|=0x20;
- flag3|=0x04;
- if(key_data==0x70)
- {
- time_display_mode++;
- if(time_display_mode>2)
- {
- flag0&=0xfd;
- time_display_mode=0;
- }
- else flag0|=0x02;
- }
- else if(key_data==0xb0)
- {
- spend_danwei=0;
- pres_disp_mode++;
- if(pres_disp_mode>1)pres_disp_mode=0;
- if(pres_disp_mode)spend_unit();
- else press_unit();
- dark_spend_unit();
- dark_pres_unit();
-
-
- }
- else if(key_data==0xd0)
- {
- if(pres_disp_mode)
- {
- spend_danwei++;
- if(spend_danwei>3)spend_danwei=0;
- spend_unit();
- }
- else
- {
- spend_danwei++;
- if(spend_danwei>4)spend_danwei=0;
- press_unit();
- }
- }
- }
- else
- {
- if(key_data==0x70)
- {
- cant_15s=0;
- time_display_mode=0;
- //flag1|=0x03;
- key_stater=1;
- flag0|=0x02;
- }
- else if(key_data==0xb0)
- {
- key_stater=2;
- weath_mode=0;
- pres_disp_mode=0;
- spend_danwei=3;
- dark_spend_unit();
- dark_pres_unit();
- press_unit();
- }
- else if(key_data==0xd0)
- {
- //key_stater=3;
- //if((comp_init_flag&0x10)&&(comp_init_flag&0x04))comp_init_flag&=0xfb;
- if(!(comp_init_flag&0x10))
- {
- comp_init_flag|=0x1f;
- }
- else
- {
- comp_start();
- comp_send_8_bit(0x60);
- comp_send_8_bit(0x07);
- comp_send_8_bit(0x80);
- comp_stop();
- comp_init_flag&=0xef;
- angle=0;
- compass_data=0;
- }
- flag4|=0x80;
- }
- else if(key_data==0x90)
- {
-
- flag4|=0x20;
- spend1=0;
- flag1^=0x01;
- if(flag1&0x01){P_INT_Ctrl2|=D_ExtI2IntEn;P_IO_PortA_Buffer&=~0x01;}
- else {P_INT_Ctrl2&=~D_ExtI2IntEn;P_IO_PortA_Buffer|=0x01;}
- spend_on_5s=0;
- }
- }
- flag2&=0x9f;
- break;
- case 1:
- if(key_data==0x70)
- {
- cant_15s=0;
- flag2&=0x9f;
- time_display_mode++;
- //flag1&=0xf8;
- if(time_display_mode>3)flag0&=0xfd;
- if(time_display_mode>6)
- {
- time_display_mode=0;
- key_stater=0;
- }
- }
- else if(key_data==0xb0)
- {
- if((flag2&0x20)||(inc_8_time>=shift_cant))
- {
- inc_8_time=0;
- cant_15s=0;
- flag3|=0x20;
- flag2&=0xdf;
- inc_8_time=0;
- switch(time_display_mode)
- {
- case 0:
- flag3^=0x40;
- flag2&=0x9f;
- break;
- case 1:
- year++;
- if(year>99)year=17;
- //get_wek_data();
- break;
- case 2:
- moth++;
- if(moth>12)moth=1;
- if(moth==2)
- {
- if(!(year&0x03))
- {
- while(day>29)day--;
- }
- else while(day>28)day--;
- }
- else while(day>moth_table[moth-1])day--;
- //get_wek_data();
- break;
- case 3:
- day++;
- if(moth==2)
- {
- if(!(year&0x03))
- {
- if(day>29)day=1;
- }
- else if(day>28)day=1;
- }
- else
- {
- if(day>moth_table[moth-1])
- day=1;
- }
- //get_wek_data();
- break;
- case 4:
- hour++;
- second=0;
- if(hour>=24)hour=0;
- break;
- case 5:
- mint++;
- second=0;
- if(mint>59)mint=0;
- break;
- case 6:
- flag2&=0x9f;
- flag0^=0x08;
- flag4|=0x40;
- break;
- }
- }
- }
- else if(key_data==0xd0)
- {
- if((flag2&0x20)||(inc_8_time>=shift_cant))
- {
- inc_8_time=0;
- cant_15s=0;
- flag3|=0x20;
- flag2&=0xdf;
- inc_8_time=0;
- switch(time_display_mode)
- {
- case 0:
- flag3^=0x40;
- flag2&=0x9f;
- break;
- case 1:
- year--;
- if(year<17)year=99;
- // get_wek_data();
- break;
- case 2:
- moth--;
- if(moth<1)moth=12;
- if(moth==2)
- {
- if(!(year&0x03))
- {
- while(day>29)day--;
- }
- else while(day>28)day--;
- }
- else while(day>moth_table[moth-1])day--;
- //get_wek_data();
- break;
- case 3:
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
|