Jump to content
  • 0

PIC16F1826 output voltage unstable



Hey guys,

I am developing a project with the PIC16F1826 and one of my outputs is flotoating like 1.9V, 2.9V and I can´t find why. I already check the ANSEL registers and initialize the PORTs at 0. It start´s when I add more if statements in my checkmodule() function...

Here is the code:

int c=0;

int c1_m1 = 0;

int c1_m2 = 0;

int c1_m3 = 0;

void main(){

OSCCON = 0b01110000;

ANSELA = 0b00000000;

ANSELB = 0b00000000;


PORTA = 0b00000000;


PORTB = 0b00000000;






void checkmodules(){







if(PORTAbits.RA1 == 0){

if(c1_m1 == 1){


c1_m1 = 0;


// When I add mores of this if´s, I start to have the problem with my PORTB outputs...

} // end checkmodules()


void module_status(){

if(c>0 && c<=3){

PORTBbits.RB4 = 1;



PORTBbits.RB4 = 0;



PORTBbits.RB5 = 1;



PORTBbits.RB5 = 0;


}// end module_status()

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0


I'm not sure that's happening, maybe we can find this together. First, check out this thread about PORT and LAT:

If you like to set an output, use LAT registers instead of PORT.

Second thing is your code: Is it your intention that variable c is going negative?

if (PORTAbits.RA1 == 0) {
    if (c1_m1 == 1) {
        c1_m1 = 0;

You used an "int" type, which means c is a signed 16 bit integer. You could use more explicit types like "int16_t" and check if your code regards the negative case properly.

Third thing I noticed: Your while() loop is running with maximum speed. It may be better to have a timing in your main sequence to get a more predictable runtime. For example, just add Timer0 to your main loop:

// configure TMR0 according to datasheet, section 20.2
OPTION_REG = 0b11010011;
while(1) {
    // wait here for next cycle
    while(INTCONbits.TMR0IF == 0);
    INTCONbits.TMR0IF = 0;
    // execute cyclic code
    // ...

About your floating output: How did you measure this? If you have used a multimeter, you likely are measuring the RMS value of a rapidly changing digital output. The TMR0 helps you getting a deterministic behavior, a scope helps you seeing what is really going on...

And did you halt the code at certain points and observed the behavior of your pins? With a PICKit or any other debugger attached to your controller, just run a debug session and set a breakpoint where you expect bad things to happen 🙂






Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Create New...