Electronic – can’t assign PORTD a variable value on PIC18F

cgpiomicrocontrollerpic

I'm using the following code to test some GPIO while I mess around with the PIC18F45K20 demo board. I realised something was strange when I couldn't increment a value and see it reflected on the LEDs after writing it to the port. Any idea why that is? I'm using the C18 compiler with MPLABX IDE v3.05

Using aPICkit3 with this demo kit: http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=dm164130-4

#pragma config FOSC = INTIO67   
#pragma config WDTEN = OFF, LVP = OFF   

#define TIMER T0CON

#include "p18f45k20.h"   

void delay();   

void delay() {      
   int counter = 0;   
   for (counter = 0; counter<1000; counter++);    
}   
void main(void) {     

   unsigned char d = 0;   

   TRISD = 0; // all bits of portd are set 0 as output      
   PORTD = 0; // deactivate all led's   

   while (1) {    

       d++;

       PORTD = d;

       delay();        

   }       
}   

Best Answer

On PICs, you read from PORT and write to LAT. Replace PORTD with LATD. It's also not a bad idea to declare counter as volatile int counter = 0;. Don't forget {} after for(counter = 0; counter<1000; counter++).