void shiftout(char n) { //read the SPTEF bit, continue if bit is 1 PTT_PTT4 = 1; //write data to SPI data register while(!SPISR_SPTEF); SPIDR = n; while(!SPISR_SPTEF); //wait for 30 cycles for SPI data to shift out lcdwait(); PTT_PTT4 = 0; lcdwait(); PTT_PTT4 = 1; lcdwait(); }
void lcdwritedata(uint8 da) { lcdwait(); RS=1; RW=0; P0=da; E=1; E=0; }
void lcdwritecmd(uint8 cmd) { lcdwait(); RS=0; RW=0; P0=cmd; E=1; E=0; }
/* *********************************************************************** send_byte: writes character x to the LCD *********************************************************************** */ void send_byte(char x) { shiftout(x); PTT_PTT4=1; PTT_PTT4=0; PTT_PTT4=1; lcdwait(); // shift out character // pulse LCD clock line low->high->low // wait 2 ms for LCD to process data }
void initializations(void) { /* Set the PLL speed (bus clock = 24 MHz) */ CLKSEL = CLKSEL & 0x80; // disengage PLL from system PLLCTL = PLLCTL | 0x40; // turn on PLL SYNR = 0x02; // set PLL multiplier REFDV = 0; // set PLL divider while (!(CRGFLG & 0x08)){ } CLKSEL = CLKSEL | 0x80; // engage PLL /* Disable watchdog timer (COPCTL register) */ COPCTL = 0x40; //COP off, RTI and COP stopped in BDM-mode /* Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts */ SCIBDH = 0x00; //set baud rate to 9600 SCIBDL = 0x9C; //24,000,000 / 16 / 156 = 9600 (approx) SCICR1 = 0x00; //$9C = 156 SCICR2 = 0x0C; //initialize SCI for program-driven operation DDRB = 0x10; //set PB4 for output mode PORTB = 0x10; //assert DTR pin on COM port /* Add additional port pin initializations here */ DDRT = 0xFF; /* Initialize SPI for baud rate of 6 Mbs */ SPIBR = 0b00000001; //SPRx = 1, SPPRx = 0, (0+1)*(2^(1+1)) = 4, 24MHz/4 = 6 Mbps SPICR1 = 0b01010000; //pg156 <- pg159 SPICR2 = 0b00000000; //pg160 <- pg159 /* Initialize digital I/O port pins */ DDRAD = 0; //program port AD for input mode ATDDIEN = 0xC0; //program PAD7 and PAD6 pins as digital inputs /* Initialize the LCD - pull LCDCLK high (idle) - pull R/W' low (write state) - turn on LCD (LCDON instruction) - enable two-line mode (TWOLINE instruction) - clear LCD (LCDCLR instruction) - wait for 2ms so that the LCD can wake up */ PTT_PTT4 = 1; PTT_PTT3 = 0; send_i(LCDON); send_i(TWOLINE); send_i(LCDCLR); lcdwait(2); /* Initialize RTI for 2.048 ms interrupt rate */ CRGINT = CRGINT | 0x80; //enable RTI (pg32) RTICTL = 0x1F; //pg33 /* Initialize TIM Ch 7 (TC7) for periodic interrupts every 1.000 ms - enable timer subsystem - set channel 7 for output compare - set appropriate pre-scale factor and enable counter reset after OC7 - set up channel 7 to generate 1 ms interrupt rate - initially disable TIM Ch 7 interrupts */ TSCR1 = 0b10000000; //enable subsys TIOS = 0b10000000; //chan 7 for output compare TSCR2 = 0b00001100; //set pre-scale factor and enable counter reset TC7 = 1500; //generate 1500clicks = 1 ms interrupt rate from notes TIE = 0b00000000; //interrupt disabled // Add RTI/interrupt initializations here CRGINT = 0b10000000; //enables RTI ATDCTL2 = 0b10000000; //enable atd ATDCTL3 = 0b00010000; // ATDCTL4 = 0b10000110; //set to 10 bit control mode, prescale clock to 2Mhz ATDCTL5 = 0b00010000; //sample across multiple channels ATDCTL2_AFFC = 1; //set to fast flag clear mode }
void main(void) { DisableInterrupts; initializations(); EnableInterrupts; for(;;) { /* write your code here */ // If the left pushbutton ("start reaction test") flag is set, then: // - clear left pushbutton flag // - set the "run/stop" flag // - display message "Ready, Set..." on the first line of the LCD // - turn off the left LED (PT1) // - turn on the right LED (PT0) // Endif if (leftpb ==1 && !runstp){ goteam = 0; react = 0; runstp = 1; leftpb = 0; RLed = 1; //Right LED LLed = 0; //Left LED YELLOW = 0; GREEN = 0; RED = 0; send_i(LCDCLR); lcdwait(); pmsglcd("Ready, Set...", 13, LINE2); } // If the "run/stop" flag is set, then: // - If the "goteam" flag is NOT set, then: // + If "random" = $0000, then: // - set the "goteam" flag // - clear TCNT register (of TIM) // - clear "react" variable (2 bytes) // - enable TIM Ch7 interrupts // - turn on YELLOW LED // - display message "Go Team!" on the second line of the LCD // + Endif // - Endif // Endif if(runstp && !goteam && random==0x00){ goteam = 1; react = 0; YELLOW = 1; //Yellow LED send_i(LCDCLR); lcdwait(); pmsglcd("Go team!", 8, LINE2); TIE = 0x80; } // If the right pushbutton ("stop reaction test") flag is set, then: // - clear right pushbutton flag // - clear the "run/stop" flag // - clear the "goteam" flag // - turn off yellow LED // - disable TIM Ch 7 interrupts // - call "tdisp" to display reaction time message // - turn off right LED (PT0) // - turn on left LED (PT1) // Endif if (rghtpb == 1) { rghtpb = 0; if(runstp && goteam){ runstp = 0; goteam = 0; YELLOW = 0; TIE = 0x0; send_i(LCDCLR); lcdwait(); tdisp(); if(react <= 250){ GREEN = 1; pmsglcd("F**K YEAH!", 10, LINE2); }else{ GREEN =0; pmsglcd("Really?", 7, LINE2); } RLed = 0; //Right LED LLed = 1; //Left LED } } // If "react" = 999 (the maximum 3-digit BCD value), then: // - clear the "run/stop" flag // - turn off yellow LED, turn on red LED // - disable TIM Ch 7 interrupts // - display message "Time = 999 ms" on the first line of the LCD // - display message "Too slow!" on the second line of the LCD // - turn off right LED (PT0) // - turn on left LED (PT1) // Endif if(react >= 999 && runstp){ runstp = 0; react =0; YELLOW = 0; //Yellow LED RED = 1; //Red LED TIE = 0x0; send_i(LCDCLR); lcdwait(); pmsglcd("RT = 999ms", 10, LINE1); pmsglcd("Too slow!", 9, LINE2); RLed = 0; //Right LED LLed = 1; //Left LED } _FEED_COP(); }/* loop forever */ } /* do not leave main */
//*********************************************************************** //Initializations //*********************************************************************** void initializations(void) { //Set the PLL speed (bus clock = 24 MHz) CLKSEL = CLKSEL & 0x80; // disengage PLL from system PLLCTL = PLLCTL | 0x40; // turn on PLL SYNR = 0x02; // set PLL multiplier REFDV = 0; // set PLL divider while (!(CRGFLG & 0x08)){ } CLKSEL = CLKSEL | 0x80; // engage PLL //Disable watchdog timer (COPCTL register) COPCTL = 0x40; //COP off, RTI and COP stopped in BDM-mode //Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts SCIBDH = 0x00; //set baud rate to 9600 SCIBDL = 0x9C; //24,000,000 / 16 / 156 = 9600 (approx) SCICR1 = 0x00; //$9C = 156 SCICR2 = 0x0C; //initialize SCI for program-driven operation DDRB = 0x10; //set PB4 for output mode PORTB = 0x10; //assert DTR pin on COM port //Add additional port pin initializations here ATDDIEN = 0xC0; prevpb = 0; prevpbl = 1; prevpbr = 1; //Initialize the SPI to baud rate of 6 Mbs DDRM = 0xFF; SPICR1 = 0x50; SPICR2 = 0x00; SPIBR = 0x01; //Initialize digital I/O port pins DDRT = 0xFF; //Initialize the LCD // - pull LCDCLK high (idle) PTT_PTT4 = 1; // - pull R/W' low (write state) PTT_PTT3 = 0; // - turn on LCD (LCDON instruction) send_i(LCDON); // - enable two-line mode (TWOLINE instruction) send_i(TWOLINE); // - clear LCD (LCDCLR instruction) send_i(LCDCLR); // - wait for 2ms so that the LCD can wake up lcdwait(); lcdwait(); // //Initialize RTI for 2.048 ms interrupt rate RTICTL = 0x41; CRGINT = CRGINT | 0x80; //Initialize TIM Ch 7 (TC7) for periodic interrupts every 1.000 ms // - enable timer subsystem TSCR1 = 0x80; // - set channel 7 for output compare TSCR2 = 0x0C; // - set appropriate pre-scale factor and enable counter reset after OC7 TIOS = 0x80; // - set up channel 7 to generate 1 ms interrupt rate TIE = 0x00; // - initially disable TIM Ch 7 interrupts TC7 = 1500; }
/* *********************************************************************** Initializations *********************************************************************** */ void initializations(void) { /* Set the PLL speed (bus clock = 24 MHz) */ CLKSEL = CLKSEL & 0x80; // disengage PLL from system PLLCTL = PLLCTL | 0x40; // turn on PLL SYNR = 0x02; // set PLL multiplier REFDV = 0; // set PLL divider while (!(CRGFLG & 0x08)){ } CLKSEL = CLKSEL | 0x80; // engage PLL /* Disable watchdog timer (COPCTL register) */ COPCTL = 0x40; //COP off, RTI and COP stopped in BDM-mode /* Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts */ SCIBDH = 0x00; //set baud rate to 9600 SCIBDL = 0x9C; //24,000,000 / 16 / 156 = 9600 (approx) SCICR1 = 0x00; //$9C = 156 SCICR2 = 0x0C; //initialize SCI for program-driven operation DDRB = 0x10; //set PB4 for output mode PORTB = 0x10; //assert DTR pin on COM port /* Add additional port pin initializations here */ DDRAD = 0; //program port AD for input mode ATDDIEN = 0xf0; //program PAD7 and PAD6 pins as digital inputs DDRM_DDRM5=1; DDRM_DDRM4=1; DDRT=0xff; ATDCTL2 = 0x80; ATDCTL3 = 0x10; ATDCTL4 = 0x85; //ATDCTL5 = 0x85; /* Initialize SPI for baud rate of 6 Mbs */ SPIBR=0x01; SPICR1=0x50; SPICR2=0x08; /* Initialize digital I/O port pins */ //Initialising PWM /*MODRR=0x02;//pt3 as ch3 0000 1000 PWME=0x02; //enable pwm ch3 PWMPOL=0x00;//active low polarity PWMCTL=0;//no concatenate (8) PWMCAE=0;// left-aligned output mode PWMPER1=0xff; //period ff PWMDTY1=0x00; //duty cycle PWMCLK=0x80; //clock a for cho 0 PWMSCLA=0x00; PWMPRCLK=0x07; //clock a =12m */ PTT_PTT6=1; PTT_PTT7=0; send_i(LCDON); send_i(TWOLINE); send_i(LCDCLR); lcdwait(); /* Initialize RTI for 2.048 ms interrupt rate */ RTICTL=0x50 ; CRGINT_RTIE=1; /* Initialize TIM Ch 7 (TC7) for periodic interrupts every 1.000 ms - enable timer subsystem - set channel 7 for output compare - set appropriate pre-scale factor and enable counter reset after OC7 - set up channel 7 to generate 0.01 ms interrupt rate - initially disable TIM Ch 7 interrupts */ TSCR1=0x80; TIOS = 0x80; TSCR2=0x0c; TC7=15; TSCR2_TCRE=1; //TIE=0xff; TIE_C7I = 0; }