Increment PORTDbits.RD0?

Thread Starter

Ben_C

Joined Oct 19, 2010
65
Hi all,

Does anyone have any code ideas on how I can increment through specific ports in a while loop? This is the code:
Code:
while (p<8)
    {

        int j = 0;
        int k = 200;
   
            while (j < 500 && k > 0)
            {
                for(i=0;i<j;i++)
                {
                PORTDbits.RD0 = 1;        // THIS IS THE PORT I WANT TO INCREMENT WITH EACH LOOP, RD0, RD1, RD2 etc
                __delay_us(100);
                }
                for(i=0;i<k;i++)
                {
                PORTDbits.RD0 = 0;        // THIS IS THE PORT I WANT TO INCREMENT WITH EACH LOOP, RD0, RD1, RD2 etc
                __delay_us(100);
                }
                j++;
                k--;
            }
        p++;
       
    }
I have tried an array:

Code:
int array[8];
   
    array[0] = PORTDbits.RD0;
    array[1] = PORTDbits.RD1;
    array[2] = PORTDbits.RD2;
    array[3] = PORTDbits.RD3;
    array[4] = PORTDbits.RD4;
    array[5] = PORTDbits.RD5;
    array[6] = PORTDbits.RD6;
    array[7] = PORTDbits.RD7;
   
    while (p<8)
    {

        int j = 0;
        int k = 200;
   
            while (j < 500 && k > 0)
            {
                for(i=0;i<j;i++)
                {
                array[m] = 1;
                __delay_us(100);
                }
                for(i=0;i<k;i++)
                {
                array[m] = 0;
                __delay_us(100);
                }
                j++;
                k--;
            }
        m++;
        p++;
       
    }
but still no luck. I could hard code this but surely there's a way to implement it into a loop?? I just want to control specific PORT bits so I can have control over the others..


Ben.
 

JohnInTX

Joined Jun 26, 2012
3,900
I would not increment PORTD directly. Use an unsigned char variable, bump that then write to the PORT. If your PIC has LATx registers, use those for all output operations including read-modify-write.

Your array approach should at least use unsigned char, int is 16 bits. You might have to use explicit casts or logical operations to extract the bit from the byte(s).

How have you set up the PORT? Tell us which PIC, compiler and IDE you are using. Also, show the code that initializes the PORT (pins set to output, analog functions turned off etc. with the #pragmas that set the CONFIG fuzes.

I'm not sure what your code is supposed to do in the first example. It sets or clears the port bit (not toggles it) in the 'for' loops with delays in between setting/clearing the bit to the same value.

If you just want to see something on the port,

Code:
#pragma ... ALL config fuzes set for your configuration

unsigned char c;
c = 0;
TRISD = 0;
// anything else to assign PORTD pins to digital - turning off analog functions etc.

while(1){
  PORTD = c;  // LATD if your chip has it
  c++;
  delay..
}
Good luck.
 
Last edited:

dannyf

Joined Sep 13, 2015
2,197
how I can increment through specific ports in a while loop?
the specifics will depend on what you meant by "incrementing".

For example, this could be one solution

Code:
  uint8_t mask = 1<<0; //port msk, lsb first
  do {
    OUT_PORT |= mask; //set pins by mask
    //alternatively if you just want to set one pin
    //OUT_PORT = mask; //set a pin by mask
    mask = mask << 1; //shift to the next pin
  } while (mask);
there are literally gazillion ways of doing it, based on what you want to do.
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
Thanks you 2, I have it working as it is, basically just a slow glow led increment whilst keeping the previous LED's on. The code I have is kinda scruffy as it is, I'm sure there's a simpler way to put a variable for RD0, RD1, RD2 etc..


Code:
#include<pic.h>
#include<stdio.h>
#include <pic16f877a.h>
#include <hitech.h>


#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 8000000

    unsigned int numbers[] = {0, 1, 3, 7, 15, 31, 63, 127, 255};
    int i = 0;
    int p = 0;
   
   
   
    time (){
    __delay_us (50);
    }
   

void main()
{
    TRISC = 0x00;
    PORTC = 0x00;
    TRISD = 0x00;
    PORTD = 0x00;

int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD0 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD0 = 0;
        time();
        }
        j++;
        k--;
    }
PORTD =  numbers[1];
///////////////////////////////////////////////////////////////////////////////      
int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD1 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD1 = 0;
        time();
        }
        j++;
        k--;
    } 
PORTD =  numbers[2];
//////////////////////////////////////////////////////////////////////////////////       
int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD2 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD2 = 0;
        time();
        }
        j++;
        k--;
    }
    PORTD =  numbers[3];
//////////////////////////////////////////////////////////////////////////////////
int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD3 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD3 = 0;
        time();
        }
        j++;
        k--;
    }
PORTD =  numbers[4];
///////////////////////////////////////////////////////////////////////////////      
int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD4 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD4 = 0;
        time();
        }
        j++;
        k--;
    } 
PORTD =  numbers[5];
//////////////////////////////////////////////////////////////////////////////////       
int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD5 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD5 = 0;
        time();
        }
        j++;
        k--;
    }
PORTD =  numbers[6];
//////////////////////////////////////////////////////////////////////////////////
int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD6 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD6 = 0;
        time();
        }
        j++;
        k--;
    }
PORTD =  numbers[7];
//////////////////////////////////////////////////////////////////////////////////
int j = 0;
int k = 200;
    while (j < 300 && k > 0)
    {
        for(i=0;i<j;i++)
        {
         RD7 = 1;
        time();
        }
        for(i=0;i<k;i++)
        {
         RD7 = 0;
        time();
        }
        j++;
        k--;
    }
PORTD =  numbers[8];
 
}
and this is the effect:


 

nsaspook

Joined Aug 27, 2009
6,580
I wrote a quick something in an existing project keeping in the spirit of your code.
Code:
/*
* bit set/reset
*/
void bitmap_e(uint8_t slot, uint8_t state)
{
    if (state)
        LATE |= 0x01 << slot;
    else
        LATE &= ~(0x01 << slot);
}

/*
* LEDS are ON with zero, ON=0, OFF=1
*/
void fade_up_leds(void)
{
    uint8_t numbers[] = {0, 1, 3, 7, 15, 31, 63, 127, 255}, l;
    int16_t i, j, k;


    for (l = 0; l <= 7; l++) {
        k = 200;
        j = 0;
        while (j < 300 && k > 0) {
            for (i = 0; i < j; i++) {
                bitmap_e(l, ON);
                wdtdelay(2); // short delay function
            }
            for (i = 0; i < k; i++) {
                bitmap_e(l, OFF);
                wdtdelay(2);
            }
            j++;
            k--;
        }
        LATE = ~numbers[l + 1]; // flip the bits for my setup
    }
}
 
Last edited:
Top