Академический Документы
Профессиональный Документы
Культура Документы
void initialize(void){
count=countMS;
gps_time=0;
DDRB = (1<<PINB3) ;
PORTB = (1<<PIN6)|(1<<PIN7);
locationIndex = 0;
//delay table
int r;
for(r=0; r<91; r++){
delayIncrementTable[r] = DELAY_INCREMENT*r;
}
// make B.3 an output
DDRD = (1<<PIND7) | (1<<PIND6);
// init the sine table
for (i=0; i<256; i++){
sineTable[i] = (char)(127.0 * sin(6.283*((float)i)/256.0)) ;
// the following table needs
// rampTable[0]=0 and rampTable[255]=127
rampTable[i] = i>>1 ;
}
// init the time counter
time=0;
UCSRB = 0x98;
UBRRL = 207 ; //4800 baud rate with 16MHz clk
DDRD= DDRD ^ 0b00000100; // PORT D.1,2 is an ouput
PORTD = PORTD ^ 0b00000100; //toggle LED
r_ready=0;
r_index=0;
string_ready=0;
waitState = beginWait;
//initialize variables
dir_ready=0;
k=0;
lat1=0;
lon1=0;
lat1_deg=0;
lon1_deg=0;
dlon=0;
target_dir=0;
lat2 = 0;
lon2= 0;
compass_done=0;
sound_dir=0;
rx_transmission_flag=0;
//reference:
//ithaca = 42deg 26' 26"N, 76deg 29' 48"W = 42.4406N, 76.4967W
//setup LCD
init_lcd();
CopyStringtoLCD(locations[0], 0, 0);//start at char=0 line=0
LCDGotoXY(0, 1);
CopyStringtoLCD(locations[1], 0, 1);//start at char=0 line=0
while(PINB!=167){
checkButtons();
}
degreesToSound((int)input);
// turn on all ISRs
sei() ;
}
//sets parameters of PWMs
void degreesToSound(int degrees){
if(degrees>=270){
amplitudeL = calculateAmplitude(0);
amplitudeR = calculateAmplitude(degrees);
offsetL = calculateDelay(0);
offsetR = calculateDelay(degrees);
}
else if(degrees>=180){
amplitudeL = calculateAmplitude(0);
amplitudeR = calculateAmplitude(degrees);
offsetL = calculateDelay(0);
//this should be min
offsetR = calculateDelay(degrees);
}
else if(degrees>=90){
amplitudeL = calculateAmplitude(degrees);
amplitudeR = calculateAmplitude(0);
//this should be min
offsetL = calculateDelay(degrees);
offsetR = calculateDelay(0);
}
else{
amplitudeL = calculateAmplitude(degrees);
amplitudeR = calculateAmplitude(0);
offsetL = calculateDelay(degrees);
offsetR = calculateDelay(0);
}
}
char calculateAmplitude(int degree){
//min
if(degree < 70)
return amplitudeIncrementTable[degree];
else if(degree>290)
return amplitudeIncrementTable[360-degree];
//min amp values
else if(degree>=270)
return amplitudeIncrementTable[70];
else if(degree >= 70)
return amplitudeIncrementTable[70];
else
return 255;
}
int calculateDelay(int degree){
if(degree<=90)
return delayIncrementTable[degree];
else if(degree>=270)
return delayIncrementTable[360-degree];
else
return MAXOFFSET;
}
// LCD setup
void init_lcd(void)
{
LCDinit(); //initialize the display
LCDcursorOFF();
LCDclr(); //clear the display
LCDGotoXY(0,0);
// CopyStringtoLCD(LCD_initialize, 0, 0);
}
void checkButtons(){
if((PINB==151)&&locationIndex>0){
locationIndex--;
printLocation();
}
else if((PINB==247)&&locationIndex<4){
locationIndex++;
printLocation();
}
_delay_ms(60);
}
void printLocation(){
switch(locationIndex){
case 0://Eng Quad
LCDclr();
CopyStringtoLCD(locations[0], 0, 0);//start at char=0 line=0
LCDGotoXY(0, 1);
CopyStringtoLCD(locations[1], 0, 1);//start at char=0 line=0
lat2 = 42.444617;
lon2= 76.4834;
break;
case 1://Brb
LCDclr();
CopyStringtoLCD(locations[1], 0, 0);//start at char=0 line=0
LCDGotoXY(0, 1);
CopyStringtoLCD(locations[2], 0, 1);//start at char=0 line=0
lat2 = 42.448533;
lon2= 76.481217;
break;
case 2://Clock tower
LCDclr();
CopyStringtoLCD(locations[2], 0, 0);//start at char=0 line=0
LCDGotoXY(0, 1);
CopyStringtoLCD(locations[3], 0, 1);//start at char=0 line=0
lat2 = 42.44746;
lon2= 76.48505;
break;
case 3://CTB
LCDclr();
CopyStringtoLCD(locations[3], 0, 0);//start at char=0 line=0
LCDGotoXY(0, 1);
CopyStringtoLCD(locations[4], 0, 1);//start at char=0 line=0
lat2 = 42.442417;
lon2= 76.48527;
break;
case 4://Apartment
LCDclr();
CopyStringtoLCD(locations[4], 0, 0);//start at char=0 line=0
LCDGotoXY(0, 1);
CopyStringtoLCD(locations[5], 0, 1);//start at char=0 line=0
lat2 = 42.442417;
lon2= 76.48527;
break;
case 5:
LCDclr();
CopyStringtoLCD(locations[5], 0, 0);//start at char=0 line=0
LCDGotoXY(0, 1);
CopyStringtoLCD(locations[6], 0, 1);//start at char=0 line=0
break;
default:
break;
}
}