PIC micro FLASHER code

The topic on number crunching

PIC micro FLASHER code

Postby pcbenthusiast » Mon May 15, 2017 2:18 am

hello,

i need help with the code i wrote below for a turn signal/brake signal flasher module. i use PIC 16F1503. Currently, when left is pressed, light leds come on flashing,m when right is pressed, right leds come on and when brake is pressed all leds turn on. but when left/brake and right/brake is pressed, the leds act weird. whats happening is when left/brake is pressed, the Brake portion of leds stay solid while the left leds are bliking very subtly-can hardly see them and blinking fast. its although two signals are working at the same time-one asking it to blink, another asking it to turn off.... sometimes ALL THE LIGHTS stay solid, no blinking.... same with right/brake..any input is appreciated.

#include <xc.h>


void main(void)
{
ANSELC = 0; //analog select register takes analog input only by default. making the bits 0 connects it to digital/port registers. See page 103 of datasheet.
TRISC = 0b00000111; // set port C-RC0 (left sw in),RC1 (right sw in),RC2 (brake sw in) as inputs and RC3 (right led out), RC4 (left led out)
OSCCON = 0b01100000; //Oscillator set to 2MHZ
OPTION_REG = 0b00000111; //prescalar 256
int counter1;
int CountLimit = 3;
TMR0IF = 0;


while(1)
{

if (INTCONbits.TMR0IF == 1) //check to see if timer rolled over
{
counter1++; //increment counter
INTCONbits.TMR0IF = 0; // clear timer 0 flag
if (counter1 >= CountLimit) //counts 1- timer roll-overs
{

//ALL OFF

if (PORTC == 0)
{
PORTCbits.RC3 = 0;
PORTCbits.RC4 = 0;
}

//LEFT TURN SIGNAL

if (PORTCbits.RC0 == 1)
{
PORTCbits.RC3 = 0;
counter1 = 0; //reset counter
if (PORTCbits.RC4 == 1) // if LED is on
{
PORTCbits.RC4 = 0; // turn LED off
}
else
{
PORTCbits.RC4 = 1; // otherwise turn it on
}}
else
{
PORTCbits.RC4 = 0; // This will set C4 low if the input C0 is not high
}

//RIGHT TURN SIGNAL


if (PORTCbits.RC1 == 1) //check to see if there is input voltage at RC0
{
PORTCbits.RC4 = 0;
counter1 = 0; //reset counter
if (PORTCbits.RC3 == 1) // if LED is on
{
PORTCbits.RC3 = 0; // turn LED off
}
else
{
PORTCbits.RC3 = 1; // otherwise turn it on
}

}
else
{
PORTCbits.RC3 = 0; // This will set C3 low if the input C0 is not high
}

//LEFT TURN & BRAKE SIGNAL

if (PORTCbits.RC0 == 1 & PORTCbits.RC2 == 1) //check to see if there is input voltage at RC0
{
PORTCbits.RC3 = 1;
counter1 = 0; //reset counter
if (PORTCbits.RC4 == 1) // if LED is on
{
PORTCbits.RC4 = 0; // turn LED off
}
else
{
PORTCbits.RC4 = 1; // otherwise turn it on
}
}

//RIGHT TURN & BRAKE SIGNAL

if (PORTCbits.RC1 == 1 & PORTCbits.RC2 == 1) //check to see if there is input voltage at RC0
{
PORTCbits.RC4 = 1;
counter1 = 0;
if (PORTCbits.RC3 == 1) // if LED is on
{
PORTCbits.RC3 = 0; // turn LED off
}
else
{
PORTCbits.RC3 = 1; // otherwise turn it on
}
}

//BRAKE SIGNAL

if (PORTCbits.RC2 == 1 & PORTCbits.RC0 == 0) //check to see if there is input voltage at RC2
{
if (PORTCbits.RC2 == 1 & PORTCbits.RC1 == 0)
{
PORTCbits.RC3 = 1; // otherwise turn RIGHT LED on
PORTCbits.RC4 = 1; // otherwise turn LEFT LED on

} }

}}}}
pcbenthusiast
 
Posts: 2
Joined: Mon May 15, 2017 2:13 am

Re: PIC micro FLASHER code

Postby pcbenthusiast » Mon May 15, 2017 10:27 pm

I was able to work on my code and get the flasher to be flexible! Left, right and brake work well but now, I have a new problem where in left/brake and right/brake mode, the leds are not blinking accurately.

what’s happening is when left/brake is pressed, the Brake portion of leds stay solid WHILE the left leds are blinking very subtly-can hardly see them and blinking fast. It looks like two signals are working at the same time-one asking it to blink, another asking it to turn off.... sometimes ALL THE LIGHTS stay solid, no blinking.... same with right/brake.. hope I didn’t confuse you guys! I'M DEFINITELY glad the flash rate is flexible now! I have kept it at 2MHZ with 256 preclear at counter1=3 giving me approx. 0.8s flash rate- aprox 76 FPM.



#include <xc.h>

void main(void)
{
ANSELC = 0; //analog select register takes analog input only by default. making the bits 0 connects it to digital/port registers. See page 103 of datasheet.
TRISC = 0b00000111; // set port C-RC0 (left sw in),RC1 (right sw in),RC2 (brake sw in) as inputs and RC3 (right led out), RC4 (left led out)
OSCCON = 0b01100000; //Oscillator set to 2MHZ
OPTION_REG = 0b00000111; //prescalar 256
int counter1;
int CountLimit = 3;
TMR0IF = 0;

while(1)
{

if (INTCONbits.TMR0IF == 1) //check to see if timer rolled over
{
counter1++; //increment counter
INTCONbits.TMR0IF = 0; // clear timer 0 flag
if (counter1 >= CountLimit) //counts 1- timer roll-overs
{
counter1 = 0;

///ALL OFF
if (PORTC == 0)
{
PORTCbits.RC3 = 0;
PORTCbits.RC4 = 0;
}

//LEFT TURN SIGNAL

if (PORTCbits.RC0 == 1)
{
PORTCbits.RC3 = 0;
if (PORTCbits.RC4 == 1) // if LED is on
{
PORTCbits.RC4 = 0; // turn LED off
}
else
{
PORTCbits.RC4 = 1; // otherwise turn it on
}
}
else
{
PORTCbits.RC4 = 0; // This will set C4 low if the input C0 is not high
}

//RIGHT TURN SIGNAL

if (PORTCbits.RC1 == 1) //check to see if there is input voltage at RC0
{
PORTCbits.RC4 = 0;
if (PORTCbits.RC3 == 1) // if LED is on
{
PORTCbits.RC3 = 0; // turn LED off
}
else
{
PORTCbits.RC3 = 1; // otherwise turn it on
}
}
else
{
PORTCbits.RC3 = 0; // This will set C3 low if the input C0 is not high
}

//LEFT TURN & BRAKE SIGNAL

if (PORTCbits.RC0 == 1 && PORTCbits.RC2 == 1) //check to see if there is input voltage at RC0
{
PORTCbits.RC3 = 1;
if (PORTCbits.RC4 == 1) // if LED is on
{
PORTCbits.RC4 = 0; // turn LED off
}
else
{
PORTCbits.RC4 = 1; // otherwise turn it on
}
}

//RIGHT TURN & BRAKE SIGNAL

if (PORTCbits.RC1 == 1 && PORTCbits.RC2 == 1) //check to see if there is input voltage at RC0
{
PORTCbits.RC4 = 1;
if (PORTCbits.RC3 == 1) // if LED is on
{
PORTCbits.RC3 = 0;
}
else
{
PORTCbits.RC3 = 1; // otherwise turn it on
}
}

//BRAKE SIGNAL

//check to see if there is input voltage at RC2
if (PORTCbits.RC2 == 1 && PORTCbits.RC0 == 0)
{
if (PORTCbits.RC2 == 1 && PORTCbits.RC1 == 0)
{
PORTCbits.RC3 = 1; // otherwise turn RIGHT LED on
PORTCbits.RC4 = 1; // otherwise turn LEFT LED on

} }

}}}
}
pcbenthusiast
 
Posts: 2
Joined: Mon May 15, 2017 2:13 am


Return to Microcontrollers & Embedded

Who is online

Users browsing this forum: No registered users and 1 guest