#include #include "main.h" #include "display.h" #include "compass.h" #include "analog.h" #include "uart0_TX_Only.h" #include "dsp.h" #include #include #include bit Tilt = 0; short xdata Heading = 0; bit Redraw = 0; short xdata CurrentScreen = SPLASH_SCREEN; short xdata Loop = 0; xdata struct ContactType Contact[10]; // Handler for button presses void ButtonHandler (void) interrupt 0 using 2 { unsigned char tempP3; // temp storage for P3 unsigned char tempP4; // temp storage for P4 delay(100); // read and store P3 and P4 tempP3 = P3; tempP4 = P4; // check value of INT0 to see which button was pressed if ( !(tempP3 & 0x01) ) { moveCursor(CURSOR_UP); } else if ( !(tempP3 & 0x02) ) { moveCursor(CURSOR_DOWN); } else if ( !(tempP3 & 0x04) ) { moveCursor(CURSOR_LEFT); } else if ( !(tempP3 & 0x08) ) { moveCursor(CURSOR_RIGHT); } else if ( !(tempP3 & 0x10) ) { enterPress(); } else if ( !(tempP3 & 0x20) ) { DEBUG("F1 PRESS"); } else if ( !(tempP3 & 0x40) ) { DEBUG("F2 PRESS"); } else if ( !(tempP3 & 0x80) ) { DEBUG("F3 PRESS"); } else if ( !(tempP4 & 0x01) ) { DEBUG("F4 PRESS"); } else { DEBUG("OTHER PRESS"); } } void InitializeInterrupts(void) { // set up the interrupt for external interrupt 0 (buttons) PX0 = 1; // set external interrupt 0 priority to high IT01CF = 0x06; // monitor P0.6 for active low IT0 = 1; // external interrupt 0 is edge triggered // set up the interrupt for external interrupt 1 (DSP) PX1 = 1; // set external interrupt 1 priority to low IT01CF |= 0x70; // monitor P0.7 for active low IT1 = 0; // external interrupt 1 is level triggered // IT0 may need to be set to either 0 or 1, depending on high/low active // or edge/level triggered // I think IE0 gets set to 1 when the interrupt condition is met. // Set IT01CF, bits 0-2 to select which P0.x pin is monitored // set up the interrupt for timer 0 TMOD |= 0x01; // set timer0 for 16 bit mode CKCON |= 0x04; // use the system clock as the timer input TH0 = 0x00; // reload timer 0, 0xFA24 = 64036, which is 1499 less than 0xFFFF TL0 = 0x00; // the timer should count up 1499 cycles then fire the interrupt on // the next cycle. The system clock is 12 MHz / 8, so 1.5MHz IE = 0x87; // enable external interrupt 0, timer 0, external interrupt 1, and enable all timers TR0 = 1; // start timer 0 } void Initialize8051(void) { OSCICN = 0x83; //Internal oscillator enabled, SYSCLK = Internal Oscillator = 12MHz CLKMUL = 0x00; //Select internal oscillator as clock multiplier source clock CLKMUL |= 0x80; //Enable clock multiplier delay(500); CLKMUL |= 0xC0; //Initialize the clock multiplier delay(500); while(!(CLKMUL & 0x20)); //Wit for multiplier to lock //CLKSEL = 0x02; //Change SYSCLK to (4x Multiplier / 2) = 24MHz (24MHz is as fast as F320 will run) CLKSEL = 0x03; //Change SYSCLK to (4x Multiplier ) = 48MHz (48MHz is as fast as F34 will run) RSTSRC = 0x04; //enable missing clock detector //Initialize Port Control Registers P0MDIN = 0xFF; P1MDIN = 0xDF; //P1.5 = VREF P2MDIN = 0x98; //P2.0=TILTZ_AIN, P2.1=TILTY_AIN, P2.2=TILTX_AIN, P2.5=BATT_SENSE_AIN, P2.6=LIGHT_SENSE_AIN P3MDIN = 0xFF; P4MDIN = 0xFF; // P0MDOUT = 0x00; P0MDOUT = 0x1D; P1MDOUT = 0x0F; P2MDOUT = 0x88; // 0x80 P3MDOUT = 0x00; P4MDOUT = 0xA4; P0SKIP = 0xC0; // 0x00 P1SKIP = 0xFF; P2SKIP = 0xE7; // 0xFF P3SKIP = 0xFF; XBR0 = 0x03; // 0x02 XBR1 = 0x40; XBR2 = 0x01; // InitializeSPI_Display_SD(); //Initialize ADC Registers REF0CN = 0x07; //0x0E = use Vcc for VREF //0x07 = Internal VREF Gen used, TempSensor Enabled, Bias Enabled, Reference enabled AMX0P = 0x00; //Analog Mux +: 0x00=P2.0(TiltZ), 0x01=P2.1(TiltY), 0x02=P2.2(TiltX), 0x1E=TempSensor AMX0N = 0x1F; //Analog Mux -: 0x1F = GND ADC0CF = 0xF8; //ADC clock = slowest setting, right justify ADC data ADC0CN = 0x80; //ADC Enabled, Normal Track Mode, AD0BUSY -> H starts conversion //Initial port settings P0 = 0xFF; // init P0 P1 = 0xFC; //3V_EN_GPS=1, 3V_EN_DISP/SD=1, VREF=1 (was COMPASS_RESET=0), COMPASS_DRDY=1, //NSS_COMPASS=1, NSS_DISPLAY=1, DISPLAY_RES = 0, DISPLAY_D/C=0 P2 = 0x7F; //COMPASS_RESET=0, Analog Inputs=1 P4 = 0xDF; //NSS_DSP=1, DSP_INT=1, 3V_EN_MAIN=0, SPARE=1, //ALARM=1, 12V_EN=1, BUTTON9=1, BUTTON8=1 // initialize UART0 for debug UART0_Init(); } // initialize all devices void initialize(void) { // initialize the 8051F340 controller Initialize8051(); // initialize the display InitializeSPI_Display_SD(); InitializeDisplay(); // show the splash screen while everything else is initializing draw(SPLASH_SCREEN); // give the splash screen a chance to be seen delay(40000); // TEST!!!! draw(STATUS_SCREEN); // initialize the internal interrupt(s) (timer0) InitializeInterrupts(); DEBUG("Initialization Complete!"); } // handler for external interrupt (DSP) void DSPHandler(void) interrupt 2 { short pos = 0; // how many characters of data read from DSP int tempint = 0; // temp storage for int data read from DSP float tempfloat = 0.0; // temp storage for float data read from DSP char tempchar[10]; // temp storage for string data read from DSP char dspbyte[2]; // temp storage for the most recent byte from DSP short i = 0; // loop control for initializing tempchar // init the SPI bus to read DSP data Initialize_DSP_SPI(); // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } dspbyte[1] = 0; // poll P0.7, as long as it's low, there's more data to read while ( ((P0 & 0x80) == 0x00) ) { // grab the next byte of date from the DSP dspbyte[0] = Read_DSP_SPI_Byte(); // store the first string from the DSP if ( (pos >= 0) && (pos <= 2)) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 3) && (pos <= 4) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the second string from the DSP if ( (pos >=5) && (pos <= 8) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 9) && (pos <= 10) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the third string from the DSP if ( (pos >=11) && (pos <= 14) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 15) && (pos <= 16) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the fourth string from the DSP if ( (pos >=17) && (pos <= 20) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 21) && (pos <= 22) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the fifth string from the DSP if ( (pos >=23) && (pos <= 26) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 27) && (pos <= 28) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the sixth string from the DSP if ( (pos >=29) && (pos <= 32) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 33) && (pos <= 34) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the seventh string from the DSP if ( (pos >=35) && (pos <= 38) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 39) && (pos <= 40) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the eigth string from the DSP if ( (pos >=41) && (pos <= 44) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if ( (pos >= 45) && (pos <= 46) ) { printf("\r\n"); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the ninth string from the DSP, and convert to a number if ( (pos >=47) && (pos <= 49) ) { // convert the ascii number to the decimal equivale switch (pos) { case 47: tempint = 100*(dspbyte[0] - 48); break; case 48: tempint += 10*(dspbyte[0] - 48); break; case 49: tempint += (dspbyte[0] - 48); break; } // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // read the comma and space if (pos == 51) { Contact[0].id = '5'; Contact[0].bearing = tempint; printf("tempint = %d\r\n", tempint); } // init tempchar for (i = 0; i < 10; i++) { tempchar[i] = 0; } // store the tenth string from the DSP if ( (pos >=52) && (pos <= 55) ) { // DEBUG printf("Reading a char: %c pos = %d\r\n", dspbyte[0], pos); } // delay to give the DSP time to bring the line low, if it's done sending data delay(10); // another character has been read and processed, so increment DSPLoop pos++; // reset pos if we are somehow reading too much data if (pos > 100) { pos = 25; } } // done with DSP, so reset to the screen SPI settings InitializeSPI_Display_SD(); delay(10); printf("Contact[0].bearing right before draw() = %d\r\n", Contact[0].bearing); // now that we've gotten a contact, switch to the contact screen draw(CONTACT_SCREEN); } // handler for the internal interrupt void TimerZeroHandler(void) interrupt 1 using 1 { short hdg = 0; // temp storage for heading TH0 = 0x00; // reload timer 0, 0xFA24 = 64036, which is 1499 less than 0xFFFF TL0 = 0x00; // the timer should count up 1499 cycles then fire the interrupt on // the next cycle. The system clock is 12 MHz / 8, so 1.5MHz // these checks can come slower if (Loop++ == 500) { // reset Loop Loop = 0; // check if we are tilted if (GetTilt() == 0) { // if we were already tilted, signal a redraw if (Tilt == 1) { Redraw = 1; } // change tilt setting Tilt = 0; } else { // if we were non-tilt, signal a redraw if (Tilt == 0) { Redraw = 1; } // change tilt setting Tilt = 1; } // get current heading hdg = GetHeading(); // if heading changed (within a range of +/-3), signal a redraw if ( ((Heading-hdg) > 3) || ((Heading-hdg) < -3) ) { Redraw = 1; // change the heading, only need to change it inside the 'if' // because if you pass this 'if', then they were already equal Heading = hdg; } // if any settings changed, we'll need a redraw if (Redraw == 1) { draw(CurrentScreen); } } } void main(void) { initialize(); while(1) { delay(1000); } } unsigned char _sdcc_external_startup(void) { PCA0MD &= ~0x40; return 0; } void delay(unsigned int delay_ticks) { unsigned int i, j; for(i=0; i