Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Unable to set specific bits in PORTs

Status
Not open for further replies.

jnz

New Member
[SOLVED] Unable to set specific bits in PORTs

I'm using a PIC18F4550 and it seems that I can't set any bits higher than RB0 and RB1. If I set all 8 on at the same time it seems to work fine, just not if I want 7 for example.

Here is some test code:

Code:
#include "p18f4550.h"
#include "delays.h"
#include "timers.h"

#pragma config PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2, FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF, PWRT = ON, BOR = OFF, BORV = 0, VREGEN = OFF, WDT = OFF, CCP2MX = OFF, PBADEN = OFF, LPT1OSC = ON, MCLRE = ON, STVREN = ON, LVP = OFF, ICPRT = OFF, XINST = OFF, DEBUG = OFF

#define delay() Delay10KTCYx(150)

void main()
{
    unsigned int delay = 1;
    volatile unsigned char x = 4;
    ADCON0 = 0;
    INTCON = 0;
    SPPCON = 0;
    UCON = 0;
    TRISB = 0;
    PORTB = 0xFF;

    TRISD = 0;
    PORTD = 0;
    while(1)
    {
        PORTB = PORTB + 1;
        PORTD = PORTD + 1;
        delay();
    }
    
}



All I see with this code are my LEDs counting in binary 0, 1, 2, 3, 0, 1, 2, 3. What's even more strange is that PORTB (don't know about the others) is actually resetting to 0 after 3.


Could any of your gurus please shed some light on this. I've never come across this project before. My hardware setup is extremely simple and as 0xFF seems to work fine I guess it's not the LEDs themselves that are bust.



Please note that i've tried quite a few diffrent ways of changing PORTB, including setting the value manually.
 
Last edited:
You have the port set to analogue. Put 0x0f in ADCON1 to fix it.

Mike.
 
Hi,
Thanks very much for the input, I've made the relevent changes however I'm still not having any joy.

Code:
#include "p18f4550.h"
#include "delays.h"
#include "timers.h"

#pragma config PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2, FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF, PWRT = ON, BOR = OFF, BORV = 0, VREGEN = OFF, WDT = OFF, CCP2MX = OFF, PBADEN = OFF, LPT1OSC = ON, MCLRE = ON, STVREN = ON, LVP = OFF, ICPRT = OFF, XINST = OFF, DEBUG = OFF

#define delay() Delay10KTCYx(150)

void main()
{
    ADCON0 = 0x00;
    ADCON1 = 0x0F;
    INTCON = 0;
    INTCON2 = 0;
    INTCON3 = 0;
    SPPCON = 0;
    UCON = 0;
    TRISB = 0;
    PORTB = 0xFF;

    TRISD = 0;
    PORTD = 0;
    while(1)
    {
        delay();
        PORTB = PORTB + 1;
        PORTD = PORTD + 1;
        
    }
    
}

I've changed the delay so now I can see all 8 LEDs flash on before the program starts. They do flash but it still only counts to 3 :/
 
Update:
I can set bits 3 and 4 on together IE:

PORTB = 0x4 | ox8; //works
PORTB = 0x4; //nope
PORTB = 0x8; //nope

Also the last 4 bits can be set together but no other combination.
PORTB = 0xF0; //works
 
Sorry for the triple post, found the problem. Many of the LEDs were shorted together. Common newbie mistake, can't believe I did that! Surprised the chip is still alive, actually.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top