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 ?
-
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– Matt Young2014年02月05日 14:37:33 +00:00Commented 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\$Tut– Tut2014年02月05日 14:50:44 +00:00Commented 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\$Sanju– Sanju2014年02月05日 14:54:58 +00:00Commented Feb 5, 2014 at 14:54
1 Answer 1
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.
-
\$\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– Sanju2014年02月11日 13:24:16 +00:00Commented 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\$Tut– Tut2014年02月11日 13:57:20 +00:00Commented 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\$Tut– Tut2014年02月11日 13:57:36 +00:00Commented 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\$Sanju– Sanju2014年02月11日 16:33:42 +00:00Commented Feb 11, 2014 at 16:33