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 ?

asked Feb 5, 2014 at 14:25
\$\endgroup\$
3
  • 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\$ Commented Feb 5, 2014 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\$ Commented Feb 5, 2014 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\$ Commented Feb 5, 2014 at 14:54

1 Answer 1

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.

answered Feb 5, 2014 at 14:57
\$\endgroup\$
4
  • \$\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\$ Commented Feb 11, 2014 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\$ Commented Feb 11, 2014 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\$ Commented Feb 11, 2014 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\$ Commented Feb 11, 2014 at 16:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.