0
\$\begingroup\$

Hi I am trying to blink LED with 18f4550, and I am getting no success.

I have defined the PORTD as output , TRISD=0;

I am trying to blink led with Some delay of 50 ms on mplabx and xc8,

I don’t want to use the LATDbits to set the led to High. Instead I am trying

PORTD=0b00000000; format.

Sample

#include<pic18f4550>
#define _XTAL_FREQ 2000000

void main(void)
{
 TRISD=0; //output
    while(1)
      {
         PORTD = 0b10000000; // Dont want to use LATDbits.RD0=1;
         __delay_ms(50);
         PORTD = 0b01000000;
        __delay_ms(50);
   } 
} 

If am using LATDbits.RD0=1; then its working fine the way its supposed to be. But I want to use PORTD =0b00000000; format because it is little convenient . Am I doing something wrong ?

\$\endgroup\$
  • 1
    \$\begingroup\$ You write to LAT and read from PORT. What is wrong with using LATDbits.RD0=1? If you really don't want to use it, substitute LATD for PORTD, but I'll be if you look at the disassembly, it will be identical. \$\endgroup\$ – Matt Young Feb 5 '14 at 14:37
  • 1
    \$\begingroup\$ If you are using RD0 for the LED, the order of your PORTD bit assignments is reversed. Change to PORTD = 0b00000001; etc. \$\endgroup\$ – Tut Feb 5 '14 at 14:50
  • \$\begingroup\$ Thank you tut, It just didnt come to my mind :D .. yes the bit assignment got reversed. thanks man :) Problem Solved. :D \$\endgroup\$ – Sanju Feb 5 '14 at 14:54
3
\$\begingroup\$

If you are using RD0 for the LED, the order of your PORTD bit assignments is reversed. Change to PORTD = 0b00000001; etc.

As Matt mentioned, the use of LATD is preferred for outputs.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ thanks :) , Got one more doubt , is it the same for PIC18F2550 also ? I wish to blink RB0 and RB1 in PortB of 18f2550 then it should be PORTD=0b11000000; ? \$\endgroup\$ – Sanju Feb 11 '14 at 13:24
  • \$\begingroup\$ I'm not sure I understand your comment. Binary notation is independent of the processor, although it may be compiler dependent as I don't think it is a part of the C standards (I could be wrong). That said, the least-significant bit is on the right and the most-significant bit is on the left. I have never seen it otherwise. Your comment is incorrect in two ways (wrong port and wrong bit order). It should be something like PORTB=0b00000011; if you wish to set RB0 and RB1. \$\endgroup\$ – Tut Feb 11 '14 at 13:57
  • \$\begingroup\$ ... Even better: LATB |= 0b00000011; and then after the delay: LATB &= 0bffffff00; ... This will toggle RB0 and RB1 without affecting the other bits in PORTB. \$\endgroup\$ – Tut Feb 11 '14 at 13:57
  • \$\begingroup\$ Oh lord... yes u are right, I was actually about to say same... that, if i want RB0 then should it be PORTB=0b00000011 . I am sooo sorry. Dont know what i was thinking... Thanks for helping me out. :D :D Alright i would go with LATB \$\endgroup\$ – Sanju Feb 11 '14 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.