El ejemplo parece estar bien, no ser por que no andaria... utilizas el LVP ???
#pragma config LVP = ON // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
Es lo unico en las palabras de configuracion que me hace "ruido" (pero deberia andar si es que lo programas de esa forma, al menos yo siempre programe con LVP=OFF), y deberia andar """bien""" y no causarte que no veas nada en la salida.
Una de las cosas es que estas intentando hacer es un PWM por software a pesar que tenes un PWM en hardware.
Y Digo """bien""" por que hay cosas que estan mal en el concepto.
1-En tu rutina de interrupcion:
if (INTCONbits.T0IF && INTCONbits.T0IE){
Eso esta de mas, ya que solo entraria ahi si fuera si o si ese caso (es decir habilitadas y ese flag en alto). Deberias desactivar la interrupcion el T0IE apenas entra a la interrupcion y limpiar el flag cuando entra o cuando sale ANTES DE HABILITAR LA INTERRUPCION. Al final de la rutina habilitas la interrupcion nuevamente.
interrupcion() {
INTCONbits.T0IE = 0
INTCONbits.T0IF = 0
// aca la rutina de tu interrupcion
INTCONbits.T0IE = 1
}
2-La idea general del sistema es mantener un periodo de 20ms, pero tu programa cuando presionas un boton se encuentra con: __delay_ms(100);, es decir 100ms mas (si entra una sola ves, sino suponete la señal de 20mS y luego una de 220mS), haciendo que el periodo sea muchisimo mayor y luego volves a 20ms. Por la forma que funciona es decir asignando un valor y no un incremento no vale la pena hacer un antirebote, de forma que si entra 100 veces no va a importar que lo asigne muchas veces, distinto seria un incremeneto donde incrementaria una cantidad aleatoria segun la cantidad de rebotes que detecte. Tendrias una salida mas estable justamente quitando ese delay.
3-Si quisieras mantener tus 8Mhz en el oscilador podrias usar un divisor por 32 y no por 16 ( como en el ejemplo de 4Mhz) del TMR0 asi mantenes tus 20ms de periodo (Si es que mantuviste la misma cantidad de interrupciones, si lo cambiaste entonces olvidate de esto). Aunque si lo cambiaste el pulso en alto deberia ser el doble.
4-Deberias poner a 0 el TMR0 cuando lo terminas de configurar.
5-Por que crear un __delay_ms(), cuando XC8 ya lo tiene en el xc.h ? y tambien lei:
The _delay (x) inline function is limited slightly over 3 * 256 * 256
Lo que es igual a 196.6k y vos lo utilizas con 200k con tu macro. De todas formas no lo necesitarias para este caso como dije en el punto 2.
Es lo unico que veo mal, realmente otra cosa no se me ocurre. Y lo mas seguro es que es por la forma de programacion, el LVP. Y no estes programando nada.