#include<htc.h>
#define _XTAL_FREQ 4000000
#define abs(n) ((n) >= 0 ? (n) : -(n))
char A,B,tog; ; Couple of variables
void main()
{
OPTION_REG = 0; ; prescaler off
TMR0IE = 1; ; Timer Interrupt on
TRISB2 = 0; ; Output on RB2
A = B = 34; ;
TMR0 = A; ; initial PWM 50%
GIE = 1; ; Global Interrupt on
while(1)
{
if(RA0 && (A < 62)) ; Button one will increase PWM
{ ; 95% max
A++;
B--;
__delay_ms(50); ; debounce
}
if(RA1 && (A > 6)) ; Button two will decrease PWM
{ ; 5% min
A--;
B++;
__delay_ms(50); ; debounce
}
}
}
void interrupt ISR()
{
if(TMR0IF) ; If timer 0 spilled over
{
tog = ~tog; ; toggle duty cycle
if(tog)
TMR0 = 0xff - A; ; preset timer for on
else
TMR0 = 0xff - B; ; preset timer for off
RB2 = tog;
TMR0IF = 0; ; clear Interrupt flag.
}
}