// T2 PWM // // Output a PWM signal on PORTC // Global Variables const unsigned char MSG0[21] = "T2 PWM "; unsigned char PWM, N; // Subroutine Declarations #include // Subroutines #include "lcd_portd.c" // High-priority service void interrupt IntServe(void) { if (TMR2IF) { N = (N + 1) % 64; if (N < PWM) PORTC = 0x3F; else PORTC = 0; if(N == 0) RA1 = !RA1; TMR2IF = 0; } } // Main Routine void main(void) { unsigned char i; unsigned int j; TRISA = 0; TRISB = 0xFF; TRISC = 0; TRISD = 0; TRISE = 0; ADCON1 = 0x0F; LCD_Init(); // initialize the LCD LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]); // set up Timer2 for 200 clocks T2CON = 0x04; PR2 = 199; TMR2ON = 1; TMR2IE = 1; TMR2IP = 1; PEIE = 1; PWM = 10; // turn on all interrupts GIE = 1; while(1) { if(RB0) PWM = 0; if(RB1) PWM = 9; if(RB2) PWM = 18; if(RB3) PWM = 27; if(RB4) PWM = 36; if(RB5) PWM = 45; if(RB6) PWM = 54; if(RB7) PWM = 64; LCD_Move(1,0); LCD_Out(PWM, 3, 0); } }