Increment PORTDbits.RD0?

Discussion in 'Embedded Systems and Microcontrollers' started by Ben_C, May 14, 2016.

  1. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    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 (Text):
    1.    
    2. while (p<8)
    3.     {
    4.  
    5.         int j = 0;
    6.         int k = 200;
    7.    
    8.             while (j < 500 && k > 0)
    9.             {
    10.                 for(i=0;i<j;i++)
    11.                 {
    12.                 PORTDbits.RD0 = 1;        // THIS IS THE PORT I WANT TO INCREMENT WITH EACH LOOP, RD0, RD1, RD2 etc
    13.                 __delay_us(100);
    14.                 }
    15.                 for(i=0;i<k;i++)
    16.                 {
    17.                 PORTDbits.RD0 = 0;        // THIS IS THE PORT I WANT TO INCREMENT WITH EACH LOOP, RD0, RD1, RD2 etc
    18.                 __delay_us(100);
    19.                 }
    20.                 j++;
    21.                 k--;
    22.             }
    23.         p++;
    24.        
    25.     }
    I have tried an array:

    Code (Text):
    1. int array[8];
    2.    
    3.     array[0] = PORTDbits.RD0;
    4.     array[1] = PORTDbits.RD1;
    5.     array[2] = PORTDbits.RD2;
    6.     array[3] = PORTDbits.RD3;
    7.     array[4] = PORTDbits.RD4;
    8.     array[5] = PORTDbits.RD5;
    9.     array[6] = PORTDbits.RD6;
    10.     array[7] = PORTDbits.RD7;
    11.    
    12.     while (p<8)
    13.     {
    14.  
    15.         int j = 0;
    16.         int k = 200;
    17.    
    18.             while (j < 500 && k > 0)
    19.             {
    20.                 for(i=0;i<j;i++)
    21.                 {
    22.                 array[m] = 1;
    23.                 __delay_us(100);
    24.                 }
    25.                 for(i=0;i<k;i++)
    26.                 {
    27.                 array[m] = 0;
    28.                 __delay_us(100);
    29.                 }
    30.                 j++;
    31.                 k--;
    32.             }
    33.         m++;
    34.         p++;
    35.        
    36.     }
    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.
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    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 (Text):
    1.  
    2. #pragma ... ALL config fuzes set for your configuration
    3.  
    4. unsigned char c;
    5. c = 0;
    6. TRISD = 0;
    7. // anything else to assign PORTD pins to digital - turning off analog functions etc.
    8.  
    9. while(1){
    10.   PORTD = c;  // LATD if your chip has it
    11.   c++;
    12.   delay..
    13. }
    14.  
    Good luck.
     
    Last edited: May 14, 2016
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    the specifics will depend on what you meant by "incrementing".

    For example, this could be one solution

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

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    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 (Text):
    1. #include<pic.h>
    2. #include<stdio.h>
    3. #include <pic16f877a.h>
    4. #include <hitech.h>
    5.  
    6.  
    7. #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    8. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
    9. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    10. #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    11. #pragma config LVP = OFF         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
    12. #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    13. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    14. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
    15.  
    16. #define _XTAL_FREQ 8000000
    17.  
    18.     unsigned int numbers[] = {0, 1, 3, 7, 15, 31, 63, 127, 255};
    19.     int i = 0;
    20.     int p = 0;
    21.    
    22.    
    23.    
    24.     time (){
    25.     __delay_us (50);
    26.     }
    27.    
    28.  
    29. void main()
    30. {
    31.     TRISC = 0x00;
    32.     PORTC = 0x00;
    33.     TRISD = 0x00;
    34.     PORTD = 0x00;
    35.  
    36. int j = 0;
    37. int k = 200;
    38.     while (j < 300 && k > 0)
    39.     {
    40.         for(i=0;i<j;i++)
    41.         {
    42.          RD0 = 1;
    43.         time();
    44.         }
    45.         for(i=0;i<k;i++)
    46.         {
    47.          RD0 = 0;
    48.         time();
    49.         }
    50.         j++;
    51.         k--;
    52.     }
    53. PORTD =  numbers[1];
    54. ///////////////////////////////////////////////////////////////////////////////      
    55. int j = 0;
    56. int k = 200;
    57.     while (j < 300 && k > 0)
    58.     {
    59.         for(i=0;i<j;i++)
    60.         {
    61.          RD1 = 1;
    62.         time();
    63.         }
    64.         for(i=0;i<k;i++)
    65.         {
    66.          RD1 = 0;
    67.         time();
    68.         }
    69.         j++;
    70.         k--;
    71.     }
    72. PORTD =  numbers[2];
    73. //////////////////////////////////////////////////////////////////////////////////      
    74. int j = 0;
    75. int k = 200;
    76.     while (j < 300 && k > 0)
    77.     {
    78.         for(i=0;i<j;i++)
    79.         {
    80.          RD2 = 1;
    81.         time();
    82.         }
    83.         for(i=0;i<k;i++)
    84.         {
    85.          RD2 = 0;
    86.         time();
    87.         }
    88.         j++;
    89.         k--;
    90.     }
    91.     PORTD =  numbers[3];
    92. //////////////////////////////////////////////////////////////////////////////////
    93. int j = 0;
    94. int k = 200;
    95.     while (j < 300 && k > 0)
    96.     {
    97.         for(i=0;i<j;i++)
    98.         {
    99.          RD3 = 1;
    100.         time();
    101.         }
    102.         for(i=0;i<k;i++)
    103.         {
    104.          RD3 = 0;
    105.         time();
    106.         }
    107.         j++;
    108.         k--;
    109.     }
    110. PORTD =  numbers[4];
    111. ///////////////////////////////////////////////////////////////////////////////      
    112. int j = 0;
    113. int k = 200;
    114.     while (j < 300 && k > 0)
    115.     {
    116.         for(i=0;i<j;i++)
    117.         {
    118.          RD4 = 1;
    119.         time();
    120.         }
    121.         for(i=0;i<k;i++)
    122.         {
    123.          RD4 = 0;
    124.         time();
    125.         }
    126.         j++;
    127.         k--;
    128.     }
    129. PORTD =  numbers[5];
    130. //////////////////////////////////////////////////////////////////////////////////      
    131. int j = 0;
    132. int k = 200;
    133.     while (j < 300 && k > 0)
    134.     {
    135.         for(i=0;i<j;i++)
    136.         {
    137.          RD5 = 1;
    138.         time();
    139.         }
    140.         for(i=0;i<k;i++)
    141.         {
    142.          RD5 = 0;
    143.         time();
    144.         }
    145.         j++;
    146.         k--;
    147.     }
    148. PORTD =  numbers[6];
    149. //////////////////////////////////////////////////////////////////////////////////
    150. int j = 0;
    151. int k = 200;
    152.     while (j < 300 && k > 0)
    153.     {
    154.         for(i=0;i<j;i++)
    155.         {
    156.          RD6 = 1;
    157.         time();
    158.         }
    159.         for(i=0;i<k;i++)
    160.         {
    161.          RD6 = 0;
    162.         time();
    163.         }
    164.         j++;
    165.         k--;
    166.     }
    167. PORTD =  numbers[7];
    168. //////////////////////////////////////////////////////////////////////////////////
    169. int j = 0;
    170. int k = 200;
    171.     while (j < 300 && k > 0)
    172.     {
    173.         for(i=0;i<j;i++)
    174.         {
    175.          RD7 = 1;
    176.         time();
    177.         }
    178.         for(i=0;i<k;i++)
    179.         {
    180.          RD7 = 0;
    181.         time();
    182.         }
    183.         j++;
    184.         k--;
    185.     }
    186. PORTD =  numbers[8];
    187.  
    188. }
    and this is the effect:


     
    nsaspook likes this.
  5. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,913
    2,181
    I wrote a quick something in an existing project keeping in the spirit of your code.
    Code (Text):
    1.  
    2. /*
    3. * bit set/reset
    4. */
    5. void bitmap_e(uint8_t slot, uint8_t state)
    6. {
    7.     if (state)
    8.         LATE |= 0x01 << slot;
    9.     else
    10.         LATE &= ~(0x01 << slot);
    11. }
    12.  
    13. /*
    14. * LEDS are ON with zero, ON=0, OFF=1
    15. */
    16. void fade_up_leds(void)
    17. {
    18.     uint8_t numbers[] = {0, 1, 3, 7, 15, 31, 63, 127, 255}, l;
    19.     int16_t i, j, k;
    20.  
    21.  
    22.     for (l = 0; l <= 7; l++) {
    23.         k = 200;
    24.         j = 0;
    25.         while (j < 300 && k > 0) {
    26.             for (i = 0; i < j; i++) {
    27.                 bitmap_e(l, ON);
    28.                 wdtdelay(2); // short delay function
    29.             }
    30.             for (i = 0; i < k; i++) {
    31.                 bitmap_e(l, OFF);
    32.                 wdtdelay(2);
    33.             }
    34.             j++;
    35.             k--;
    36.         }
    37.         LATE = ~numbers[l + 1]; // flip the bits for my setup
    38.     }
    39. }
    40.  
     
    Last edited: May 14, 2016
    Ben_C likes this.
Loading...