Hello,
I am learning basic PIC programming and am trying to control a seven segment display to turn on segements A,B,C,D,E,F in that order to make the display look like it rotating. The LED's turn on in 100ms increments and keep looping.
I am using a PIC16F1823 controller and a LTS4801JR common anode display.
I am also using a push button to control the direction of the rotation.
I am using MPLAB IDE to program the controller and have it hooked up according to the datasheet.
To get the 100ms timing, I am using the internal frequency set to 500kHz, and have prescaled it to 256. I loaded the prescaler to 207 to get the 100ms timing.
To change to led's, I used a case/switch for each segment.
When I build and then program, there are no errors but segment A-F are all on and unchanging. I believe the controller is locked up due to an error. Can someone look over my code and point me in the right direction. Thanks!
I am learning basic PIC programming and am trying to control a seven segment display to turn on segements A,B,C,D,E,F in that order to make the display look like it rotating. The LED's turn on in 100ms increments and keep looping.
I am using a PIC16F1823 controller and a LTS4801JR common anode display.
I am also using a push button to control the direction of the rotation.
I am using MPLAB IDE to program the controller and have it hooked up according to the datasheet.
To get the 100ms timing, I am using the internal frequency set to 500kHz, and have prescaled it to 256. I loaded the prescaler to 207 to get the 100ms timing.
To change to led's, I used a case/switch for each segment.
When I build and then program, there are no errors but segment A-F are all on and unchanging. I believe the controller is locked up due to an error. Can someone look over my code and point me in the right direction. Thanks!
Rich (BB code):
#include <htc.h>
__CONFIG(0x09A4); // Set Config1 register
__CONFIG(0x1CFF); // Set Config2 register
void main (void)
{
int flag; //100ms flag to count
int Pb; //Pushbutton input
PORTA=0x00; //Clear Port A
LATA=0x00; //Clear Latch A
//BANKSEL PORTC;
PORTC=0x00; //Clear Port C
LATC=0x00; //Clear Latch C
ANSELA=0x00; //all I/O
ANSELC=0x00; //all I/O
TRISA=0x08; //RA 3 as input. RA0,RA1,RA2,RA4,RA5 as Outputs
TRISC=0x00; //RC ports outputs
OSCCON=0x38; //set internal oscillator to 500kHz
OPTION_REG=0xC7; //set prescalar to 1:256
TMR0=0xCF; //load Timer0 to 207 for 100ms timing
T0IF=0; //clear flag
flag = 0;
while(1)
{
while (T0IF==0); //wait for flag to change
flag++; //Increase flag by one
T0IF=0; //Reset Interrupt flag to 0
if ((RA3 == 1) && (flag==!0)) //look for high on push button and flag not 0
{
switch(flag)
{
case '1':
LATA0=0; //turn on segment A
LATA1=1;
LATA2=1;
LATA4=1;
LATA5=1;
LATC0=1;
break;
case '2':
LATA0=1;
LATA1=0; //turn on segment B
LATA2=1;
LATA4=1;
LATA5=1;
LATC0=1;
break;
case '3':
LATA0=1;
LATA1=1;
LATA2=0; //turn on segment C
LATA4=1;
LATA5=1;
LATC0=1;
break;
case '4':
LATA0=1;
LATA1=1;
LATA2=1;
LATA4=0; //turn on segment D
LATA5=1;
LATC0=1;
break;
case '5':
LATA0=1;
LATA1=1;
LATA2=1;
LATA4=1;
LATA5=0; //turn on segment E
LATC0=1;
break;
case '6':
LATA0=1;
LATA1=1;
LATA2=1;
LATA4=1;
LATA5=1;
LATC0=0; //turn on segment F
break;
}
if (flag==7)
{
T0IF=0; //set flag back to 0
flag = 0; //reset flag to 0
TMR0=0xCF; //load Timer0 to 207CF
}
}
if ((RA3 == 0) && (flag==!0)) //if pushbutton is pressed, active low
{
switch(flag)
{
case '1':
LATA0=1;
LATA1=1;
LATA2=1;
LATA4=1;
LATA5=1;
LATC0=0; //turn on segment F
break;
case '2':
LATA0=1;
LATA1=1;
LATA2=1;
LATA4=1;
LATA5=0; //turn on segment E
LATC0=1;
break;
case '3':
LATA0=1;
LATA1=1;
LATA2=1;
LATA4=0; //turn on segment D
LATA5=1;
LATC0=1;
break;
case '4':
LATA0=1;
LATA1=1;
LATA2=0; //turn on segment C
LATA4=1;
LATA5=1;
LATC0=1;
break;
case '5':
LATA0=1;
LATA1=0; //turn on segment B
LATA2=1;
LATA4=1;
LATA5=1;
LATC0=1;
break;
case '6':
LATA0=0; //turn on segment A
LATA1=1;
LATA2=1;
LATA4=1;
LATA5=1;
LATC0=1;
break;
}
if (flag==7)
{
T0IF=0; //set flag back to 0
flag = 0; //reset flag to 0
TMR0=0xCF; //load Timer0 to 201CF
}
}
}
}
Last edited by a moderator: