/******* T3.c ****************** * * Use Fosc = 4 MHz for Fcpu = Fosc/4 = 1 MHz. * Timer1 and Timer3 are both clocked by the Timer1 crystal oscillator. * Same mainline code as for T2.c. * LoopTime function puts chip to sleep. Timer1 awakens chip every 10 ms * within LoopTime function. CPU adjusts Timer1 content for it to timeout * after another ten milliseconds. * Timer3 generates high-priority interrupts every 4 ms to step motor. * Toggle RC2 output every 10 milliseconds for measuring looptime with scope. * Blink LED on RD4 for 10 ms every four seconds. * Post PRESS PB message on LCD until first pushbutton push. * Thereafter, increment and display LCD's CHAR0:CHAR1 every second * and increment and display LCD's CHAR3:CHAR4 for each pushbutton press. * * Current draw = 31 uA (with LED and LCD switched off but whether * or not the stepper motor is connected.) * ******* Program hierarchy ***** * * main * Initial * Display * BlinkAlive * Time * Pushbutton * UpdateLCD * Display * LoopTime * * LoPriISR * * HiPriISR * ******************************* */ #include // Define PIC18LF4321 registers and bits #include // Used by the LoadLCDSTRING macro /******************************* * Configuration selections ******************************* */ #pragma config OSC = INTIO1 // Use internal osc, RA6=Fosc/4, RA7=I/O #pragma config PWRT = ON // Enable power-up delay #pragma config LVP = OFF // Disable low-voltage programming #pragma config WDT = OFF // Disable watchdog timer initially #pragma config WDTPS = 4 // 16 millisecond WDT timeout period, nominal #pragma config MCLRE = ON // Enable master clear pin #pragma config PBADEN = DIG // PORTB<4:0> = digital #pragma config CCP2MX = RB3 // Connect CCP2 internally to RB3 pin #pragma config BOR = SOFT // Brown-out reset controlled by software #pragma config BORV = 3 // Brown-out voltage set for 2.0V, nominal #pragma config LPT1OSC = OFF // Deselect low-power Timer1 oscillator /******************************* * Global variables ******************************* */ char PBFLAG; // Flag, set after first press of pushbutton char LCDFLAG; // Flag, set to send string to display char NEWPB; // Flag, set if pushbutton is now pressed char OLDPB; // Flag, set if pushbutton was pressed last loop char LPISRFLAG; // Flag, set when LP interrupt has been handled unsigned int ALIVECNT; // Scale-of-400 counter for blinking "Alive" LED unsigned int STEPCNT; // 65536 - number of counts between steps unsigned char TIMECNT; // Scale-of-100 counter of loop times = 1 second unsigned char UNITS; // For display of seconds unsigned char TENS; unsigned char PBUNITS; // For display of pushbutton count unsigned char PBTENS; unsigned char i; // Index into strings unsigned int DELAY; // Sixteen-bit counter for obtaining a delay char LCDSTRING[] = "PRESS PB "; // LCD display string /******************************* * Function prototypes ******************************* */ void Initial(void); void BlinkAlive(void); void Time(void); void Pushbutton(void); void UpdateLCD(void); void Display(void); void LoopTime(void); void HiPriISR(void); void LoPriISR(void); /******************************* * Macros ******************************* */ #define Delay(x) DELAY = x; while(--DELAY){ Nop(); Nop(); } #define LoadLCDSTRING(lit) strcpypgm2ram(LCDSTRING,(const far rom char*)lit) /******************************* * Interrupt vectors ******************************* */ // For high priority interrupts: #pragma code high_vector=0x08 void interrupt_at_high_vector(void) { _asm GOTO HiPriISR _endasm } #pragma code #pragma interrupt HiPriISR // For low priority interrupts: #pragma code low_vector=0x18 void interrupt_at_low_vector(void) { _asm GOTO LoPriISR _endasm } #pragma code #pragma interruptlow LoPriISR /////// Main program ////////////////////////////////////////////////////////// /******************************* * main ******************************* */ void main() { Initial(); // Initialize everything while (1) { PORTCbits.RC2 ^= 1; // Toggle pin, for measuring loop time BlinkAlive(); // Blink "Alive" LED Time(); // Display seconds Pushbutton(); // Display pushbutton count UpdateLCD(); // Update LCD LoopTime(); // Use Timer1 to wakeup and loop again } } /******************************* * HiPriISR * * This high-priority interrupt service routine creates a positive pulse on * RD1 every 4 ms. Four milliseconds = 4000 * 0.032768 = 131 Timer3 counts. * Add STEPCNT = 65536+1-131 = 65406 counts to Timer3. * The +1 in the above equation results because the 0-to-1 transition when * Timer3 is reenabled increments Timer3. * RB0 is toggled to measure step period. ******************************* */ void HiPriISR() { T3CONbits.TMR3ON = 0; // Disable clock input to Timer3 TMR3L += STEPCNT; // Add into lower byte TMR3H = (TMR3H + STATUSbits.C) + (STEPCNT >> 8); // and into upper byte T3CONbits.TMR3ON = 1; // Reenable Timer3 PIR2bits.TMR3IF = 0; // Clear interrupt flag PORTDbits.RD1 = 1; // Create 1 us wide positive pulse PORTDbits.RD1 = 0; // to step motor PORTBbits.RB0 ^= 1; // Toggle pin to measure step period } /******************************* * LoPriISR * * This low-priority interrupt service routine updates Timer1 to interrupt * every 10 ms. Ten ms = 10000 * 0.032768 = 328 to cut out all * but 328 counts. Add 65536+1-328 = 65209 = 0xFEB9 to Timer1 = 0x0000 (or * at most a count or two higher if the HPISR intervenes). ******************************* */ void LoPriISR() { T1CONbits.TMR1ON = 0; // Pause Timer1 counter TMR1L += 0xB9; // Cut out all but 328 counts of Timer1 T1CONbits.TMR1ON = 1; // Resume Timer1 counter TMR1H = 0xFE; // Upper byte of Timer1 will be 0xFE PIR1bits.TMR1IF = 0; // Clear interrupt flag LPISRFLAG = 1; // Set a flag for LoopTime } /******************************* * Initial * * This function performs all initializations of variables and registers. ******************************* */ void Initial() { OSCCON = 0b01100010; // Use Fosc = 4 MHz (Fcpu = 1 MHz) SSPSTAT = 0b00000000; // Set up SPI for output to LCD SSPCON1 = 0b00110000; ADCON1 = 0b00001011; // RA0,RA1,RA2,RA3 pins analog; others digital TRISA = 0b00001111; // Set I/O for PORTA TRISB = 0b01000100; // Set I/O for PORTB TRISC = 0b10000000; // Set I/O for PORTC TRISD = 0b10000000; // Set I/O for PORTD TRISE = 0b00000010; // Set I/O for PORTE PORTA = 0; // Set initial state for all outputs low PORTB = 0; PORTC = 0; PORTD = 0b00100000; // except RD5 that drives LCD interrupt PORTE = 0; SSPBUF = ' '; // Send a blank to initialize state of UART Delay(50000); // Pause for half a second RCONbits.SBOREN = 0; // Now disable brown-out reset PBFLAG = 0; // Clear flag until pushbutton is first pressed LCDFLAG = 0; // Flag to signal LCD update is initially off LPISRFLAG = 0; // Flag to signal that LPISR has been executed TIMECNT = 0; // Reset TIMECNT TENS = '5'; // Initialize to 59 so first display = 00 UNITS = '9'; PBTENS = '0'; // Initialize count of pushbutton presses PBUNITS = '1'; ALIVECNT = 300; // Blink immediately STEPCNT = 65406; // Cut out all but 131 counts of Timer3 OLDPB = 0; // Initialize to unpressed pushbutton state T1CON = 0b01001111; // Timer1 - loop time via low-pri interrupts T3CON = 0b00000111; // Timer3 - step motor via hi-pri interrupts TMR1H = 0xFE; // Set Timer1 to be 10 ms away from TMR1L = 0xB9; // next roll over (65536 + 1 - 328 = 0xFEB9) TMR3H = 0xFF; // Set Timer3 to be 4 ms away from TMR3L = 0xAE; // next roll over (65536 + 1 - 131 = 0xFF7E) PIE1bits.TMR1IE = 1; // Enable local interrupt source PIE2bits.TMR3IE = 1; // Enable local interrupt source IPR1bits.TMR1IP = 0; // Use Timer1 for low-priority interrupts IPR2bits.TMR3IP = 1; // Use Timer3 for hi-priority interrupts RCONbits.IPEN = 1; // Enable high/low priority interrupt feature INTCONbits.GIEL = 1; // Global low-priority interrupt enable INTCONbits.GIEH = 1; // Enable both high and low interrupts Display(); // Display initial "PRESS PB" message LoadLCDSTRING("00 01 "); // Reinitialize LCDSTRING } /******************************* * BlinkAlive * * This function briefly blinks the LED every four seconds. * With a looptime of about 10 ms, count 400 looptimes. ******************************* */ void BlinkAlive() { PORTDbits.RD4 = 0; // Turn off LED if (++ALIVECNT == 400) // Increment counter and return if not 400 { ALIVECNT = 0; // Reset ALIVECNT PORTDbits.RD4 = 1; // Turn on LED for one looptime } } /******************************* * Time * * After pushbutton is first pushed, display seconds. ******************************* */ void Time() { if (PBFLAG) // After pushbutton is first pushed, { if (++TIMECNT == 100) // count TIMECNT to 1 second { TIMECNT = 0; // Reset TIMECNT for next second if (++UNITS > '9') // and increment time { UNITS = '0'; if (++TENS > '5') { TENS = '0'; } } LCDSTRING[0] = TENS; // Update display string LCDSTRING[1] = UNITS; LCDFLAG = 1; // Set flag to display } } } /******************************* * Pushbutton * * After pushbutton is first pressed, display pushbutton count. ******************************* */ void Pushbutton() { PORTEbits.RE0 = 1; // Power up the pushbutton Nop(); // Delay one microsecond before checking it NEWPB = !PORTDbits.RD7; // Set flag if pushbutton is pressed PORTEbits.RE0 = 0; // Power down the pushbutton if (!OLDPB && NEWPB) // Look for last time = 0, now = 1 { if (!PBFLAG) // Take action for very first PB press { PBFLAG = 1; ALIVECNT = 399; // Synchronize LED blinking to counting TIMECNT = 99; // Update display immediately } else // Take action for subsequent PB presses { if (++PBUNITS > '9') // and increment count of PB presses { PBUNITS = '0'; if (++PBTENS > '9') { PBTENS = '0'; } } } LCDSTRING[3] = PBTENS; // Update display string for simulated LCD LCDSTRING[4] = PBUNITS; LCDFLAG = 1; // Set flag to display } OLDPB = NEWPB; // Save present pushbutton state } /******************************* * UpdateLCD * * This function updates the 8-character LCD if Time * or Pushbutton has set LCDFLAG. ******************************* */ void UpdateLCD() { if(PBFLAG && LCDFLAG) { Display(); LCDFLAG = 0; } } /******************************* * Display * * This function sends LCDSTRING to the LCD. ******************************* */ void Display() { PORTDbits.RD5 = 0; // Wake up LCD display for (i = 0; i < 9; i++) { PIR1bits.SSPIF = 0; // Clear SPI flag SSPBUF = LCDSTRING[i]; // Send byte while (!PIR1bits.SSPIF); // Wait for transmission to complete } PORTDbits.RD5 = 1; // Return RB5 high, ready for next string } /******************************* * LoopTime * * This function puts the chip to sleep upon entry. * For a Timer3 interrupt, it executes the HPISR and then returns to sleep. * For a Timer1 interrupt, it executes the LPISR and then exits. ******************************* */ void LoopTime() { while (!LPISRFLAG) // Sleep upon entry and upon exit from HPISR { // Return only if LPISR has been executed, Sleep(); // which sets LPISRFLAG Nop(); } LPISRFLAG = 0; // Sleep upon next entry to LoopTime }