Hi!
I'm currently working on a small 5x6 LED matrix, driven by a PIC 16F505. The problem is it won't enter sleep mode when (I think) it should. The program displays 3 messages.
In theory:
If the button is pressed the PIC wakes up and displays message#1 and then enters sleep mode, unless the button is pressed again. If so message#2 is displayed, and after it finishes scrolling, PIC is going to sleep. If the button is pressed one more time (while displaying m#2) message#3 appears and finally PIC can enter sleep mode (this time for good).
What happens is:
After it finishes scrolling through message#1 it immediately repeats it, on and on.
If I press the button while displaying m#1 for the first time, message#2 appears. When m#2 is done, m#1 comes back again. Pressing the button causes m#1 to reset and start over.
If I somehow manage to get to message#3 it finally ends and the PIC enters sleep mode (I think).
I bet there's some extremely stupid newbie mistake, but I can't figure it out. Can You please help me? Thank you in advance.
MPLab8.92, XC8 v1.33, PICKit3
Config bits set in MPLab
#include <pic16f505.h>
#include <xc.h>
#define _XTAL_FREQ 4000000
void txt(const char array[], char size);
const char m1[74] = {hex numbers, 1 byte = 1 column};
const char m2[57] = {...};
const char m3[107] = {...};
char t=0;
char dummy;
void main(void)
{
OPTION=0b01010111; //wake-up on pin change enabled, T0 source is internal osc
TRISC=0;
TRISB=0b00001000; //all output except RB3
t=0; //this variable changes the displayed messages
if(t==0) txt(m1, 74);
if(t==1) txt(m2, 57);
if(t==2) txt(m3, 107);
PORTB=0;
PORTC=0; //clear all before sleep
dummy=PORTB; //read the port, required for wake-up on pin change
asm("sleep"); //goodnight, sleep tight
}
void txt(const char array[], char size)
{
char i=0; //column pointer
while(i<size) //work 'till we run out of bytes in array
{
for(char k=0; k<5; k++) //display each frame 5 times
{
for(char j=0; j<6; j++) //displaying 1 frame - multiplexing 6 columns
{
PORTB=0;
PORTC=0;
PORTC=array[i+j]; //choose proper byte
if(j==0) RC5=1; //enable column0
if(j==1) RB4=1; //enable column1
if(j==2) RB5=1; //etc...
if(j==3) RB0=1;
if(j==4) RB1=1;
if(j==5) RB2=1;
__delay_ms(2);
}
}
i++; //shift everything left
if(RB3==0) //if button is pressed...
{
t++; //...move on to next message...
i=0xff; //...but first exit this 'while' loop, since 255 is always greater than 'size'
}
}
}