📄 main.c
字号:
#include "common.h"
#include "oled.h"
static float eepXsf EEMEM;
static float eepYsf EEMEM;
static float eepXoff EEMEM;
static float eepYoff EEMEM;
static uint8_t eepHMC1052CalibrationisOK EEMEM = 1;
static uint16_t eepACCXaxis_0g EEMEM = 0;
static uint16_t eepACCYaxis_0g EEMEM = 0;
static uint8_t eepACCCalibrationisOK EEMEM = 1;
static uint8_t eepFirstTimeON EEMEM = 1;
/*********************eeprom_read/write program***********************/
/** **/
/** load the data from the eeprom memory and **/
/** save the useful data in the eeprom memory **/
/*********************************************************************/
/*uchar eeprom_read_asm(uchar eepAddr_asm)
{
asm volatile ( " aa:sbic 0x1F,1 \n\t"
" rjmp aa \n\t"
" clr r25 \n\t"
" out 0x22,r25 \n\t"
" out 0x21,r24 \n\t"
" sbi 0x1F,0 \n\t"
" clr r25 \n\t"
" in r24,0x20 \n\t"
: :
);
}
void eeprom_write_asm(uchar eepAddr_asm,uchar eepData_asm)
{
asm volatile ( " bb:sbic 0x1F,1 \n\t"
" rjmp bb \n\t"
" clr r25 \n\t"
" out 0x22,r25 \n\t"
" out 0x21,r24 \n\t"
" out 0x20,r22 \n\t"
" sbi 0x1F,2 \n\t"
" sbi 0x1F,1 \n\t"
: :
);
}*/
/*********************************************************************/
SIGNAL(SIG_OVERFLOW2){}
uchar updata_OSCCAL(void)
{
asm volatile ( " ldi r31,0x0f \n\t"
" ldi r30,0xfe \n\t"
" lpm \n\t"
" mov r24,r0 \n\t"
: :
);
}
void ioinit(void)
{
DDRB = 0Xff;PORTB = 0X00;
DDRC = 0X07;PORTC = 0X04;
DDRD = 0Xff;PORTD = 0X00;
OSCCAL = updata_OSCCAL();
}
void main()
{
uchar row1,row2,n,m,Backtodisplay=1,AutoOFFtimer=0;
char *string={"NEW COMPASS"};
char string1[10],string2[10]={" ^COURSE "};
ioinit();
//oled_initial();
//clear_screen();
eeprom_read_block((void*)&Xsf,(void*)&eepXsf,4);
eeprom_read_block((void*)&Ysf,(void*)&eepYsf,4);
eeprom_read_block((void*)&Xoff,(void*)&eepXoff,4);
eeprom_read_block((void*)&Yoff,(void*)&eepYoff,4);
HMC1052CalibrationisOK=eeprom_read_byte(&eepHMC1052CalibrationisOK);
ACCXaxis_0g=eeprom_read_word(&eepACCXaxis_0g);
ACCYaxis_0g=eeprom_read_word(&eepACCYaxis_0g);
ACCCalibrationisOK=eeprom_read_byte(&eepACCCalibrationisOK);
FirstTimeON=eeprom_read_byte(&eepFirstTimeON);
TCNT2=0x00; // 256/(8/256)=8.192mS
TCCR2B=(1<<CS22)|(1<<CS21); // Frequency:8MHz/256
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
TIMSK2=(1<<TOIE2);sei();
while(1)
{
Key();
if((KeyCounter>=500)&(UnitONmark))
{
//clear_screen();
//string="CALIBRATION";
//print_asc8x16_center(0,string,ASCII8X16,0);
string=" OBLIQUITU ";
print_asc8x16_center(2,string,ASCII8X16,0);
FirstTimeON=0;
eeprom_write_byte(&eepFirstTimeON,FirstTimeON);
}
else if((KeyCounter>=250)&(UnitONmark))
{
//clear_screen();
string="CALIBRATION";
print_asc8x16_center(0,string,ASCII8X16,0);
string="GYROCOMPASS";
print_asc8x16_center(2,string,ASCII8X16,0);
}
else
{
if(UnitONmark)
{
while(HMC1052CalibrationisOK!=1)
{
TCCR2B=00; // close timer
TIMSK2=(0<<TOIE2);cli();
Calibration();
if(HMC1052CalibrationisOK==1)
{
string=" SUCCEED ";
eeprom_write_block((void*)&Xsf,(void*)&eepXsf,4);
eeprom_write_block((void*)&Ysf,(void*)&eepYsf,4);
eeprom_write_block((void*)&Xoff,(void*)&eepXoff,4);
eeprom_write_block((void*)&Yoff,(void*)&eepYoff,4);
eeprom_write_byte(&eepHMC1052CalibrationisOK,HMC1052CalibrationisOK);
}
else
{
eeprom_read_block((void*)&Xsf,(void*)&eepXsf,4);
eeprom_read_block((void*)&Ysf,(void*)&eepYsf,4);
eeprom_read_block((void*)&Xoff,(void*)&eepXoff,4);
eeprom_read_block((void*)&Yoff,(void*)&eepYoff,4);
string=" EXIT... ";
HMC1052CalibrationisOK=1;
}
print_asc8x16_center(2,string,ASCII8X16,0);
for(n=0;n<8;n++) delaytimeforCBRT();
TCNT2=0x00; // 256/(8/256)=8.192mS
TCCR2B=(1<<CS22)|(1<<CS21); // Frequency:8MHz/256
TIMSK2=(1<<TOIE2);sei();
}
while(ACCCalibrationisOK!=1)
{
TCCR2B=00; // close timer
TIMSK2=(0<<TOIE2);cli();
string=" KEEP LEVEL ";
print_asc8x16_center(0,string,ASCII8X16,0);
string=" ";
print_asc8x16_center(2,string,ASCII8X16,0);
while(1)
{
delaytimeforCBRT();
Sample_ACCXandYaxis();
ACCXaxis_0g=ACC_XaxisADC;
ACCYaxis_0g=ACC_YaxisADC;
itoa(ACCXaxis_0g,string1,10);
print_asc8x16(2,16,string1,ASCII8X16,0);
itoa(ACCYaxis_0g,string1,10);
print_asc8x16(2,48,string1,ASCII8X16,0);
if(KeyValue)
{
_delay_loop_1(100);
if(KeyValue)
{
while(KeyValue);
ACCCalibrationisOK=1;
break;
}
}
}
string="CALIBRATION";
print_asc8x16_center(0,string,ASCII8X16,0);
string=" SUCCEED ";
print_asc8x16_center(2,string,ASCII8X16,0);
for(n=0;n<8;n++) delaytimeforCBRT();
string=" COMPASS ";
print_asc8x16_center(0,string,ASCII8X16,0);
eeprom_write_word(&eepACCXaxis_0g,ACCXaxis_0g);
eeprom_write_word(&eepACCYaxis_0g,ACCYaxis_0g);
eeprom_write_byte(&eepACCCalibrationisOK,ACCCalibrationisOK);
TCNT2=0x00; // 256/(8/256)=8.192mS
TCCR2B=(1<<CS22)|(1<<CS21); // Frequency:8MHz/256
TIMSK2=(1<<TOIE2);sei();
}
if(FirstTimeON==0)
{if(m==125)
{
m=0;
Sample_ACCXandYaxis();
ACC_XaxisADC=abs(ACCXaxis_0g-ACC_XaxisADC);
ACC_YaxisADC=abs(ACCYaxis_0g-ACC_YaxisADC);
if((ACC_XaxisADC>30)|(ACC_YaxisADC>30))
{
if(!Backtodisplay)
{
Backtodisplay=1;
clear_screen();
oled_reset();
SPI_MasterInit();
MainOLED_WCom(0xAD); // SET DC/DC BOOSTER(8AH=OFF,8BH=ON)
MainOLED_WCom(0x8A); // built-in DC-DC turn off
MainOLED_WCom(0xAE); // Display turn off
PORTD&=0b00011111;
PORTD|=0b11100000;
OLED_POWER_OFF;
}
}
else
{
if(Backtodisplay)
{
AutoOFFtimer=0;
Backtodisplay=0;
oled_initial();
clear_screen();
}
Sample_XandYaxis();
CalculateforAngle();
GetCurrentPointer();
if(Angle<10)
{
row1=20;
//row2=0;
//string2[10]=" ^COURSE ";
}
else if(Angle<100)
{
row1=12;
//row2=4;
//string2[10]=" ^COURSE ";
}
else
{
row1=4;
//row2=8;
//string2[10]=" ^COURSE ";
}
itoa(Angle,string1,10);
print_asc8x16(2,4,string2,ASCII8X16,0);
print_asc8x16(2,row1,string1,ASCII8X16,0);
string=" COMPASS ";
print_asc8x16_center(0,string,ASCII8X16,0);
AutoOFFtimer++; // if there is no display
if(AutoOFFtimer==60) // it turns off automaticly after 1 min.
{
KeyProcess(1);
}
}
}
}
else
{
string=" COMPASS ";
print_asc8x16_center(0,string,ASCII8X16,0);
}
m++;
}
}
sleep_mode();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -