Microcontroller › PIC › Led number display
- This topic has 3 replies, 2 voices, and was last updated 11 years, 10 months ago by
AJISH ALFRED.
-
AuthorPosts
-
May 18, 2013 at 9:17 am #2416
Usman
ParticipantSalam ,
I am using PIC18f458
i wrote a code to display led number but code is not entering in to the if statement
what is the problem here???
code is here
#include<pic18.h>#define ldata PORTD#define rs PORTBbits.RB0#define rw PORTBbits.RB1#define en PORTBbits.RB2#define led1 PORTAbits.RA0#define led2 PORTAbits.RA1#define led3 PORTAbits.RA2void lcdcmd(unsigned char );void lcddata();void MsDelay(unsigned int );void main(){// unsigned char message=”Usman Salleem “;TRISD=0; // make an output portTRISB=0;TRISAbits.TRISA0=0;TRISAbits.TRISA1=0;TRISAbits.TRISA2=0;en=0;// led1=1;// led2=1;// led3=1;MsDelay(250);lcdcmd(0x38);MsDelay(250);lcdcmd(0x0E);MsDelay(15);lcdcmd(0x01);MsDelay(15);lcdcmd(0x06);MsDelay(15);lcdcmd(0x80);MsDelay(15);lcddata();// MsDelay(1000);// lcddata(‘d’);// MsDelay(1000);// lcddata(‘e’);}void lcdcmd(unsigned char value ){ldata=value ;rs=0;rw=0;en=1;MsDelay(1);en=0;}/*void lcddata(){unsigned char message[]=”Earth is one Land “;unsigned char z ;for(z=0; z<17; z++){ldata=message[z];// MsDelay(100);rs=1;rw=0;en=1;MsDelay(1);en=0;}}*/void lcddata(){unsigned char z ;unsigned char message1[]=”LED 1 is ONN”;unsigned char message2[]=”LED 2 is ONN”;unsigned char message3[]=”LED 3 is ONN”;unsigned char message4[]=”Nothing is ONN”;unsigned char f ;// led1=0;led1=1;led2=0;led3=0;if ((led1==1 || led2 && led3 == 0)==1){for(z=0; z<12; z++){ldata=message1[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}}else if (led2==1){for(z=0; z<12; z++){ldata=message2[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}}else if(led3==1){for(z=0; z<12; z++){ldata=message3[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}}else if(led1==0 && led2==0 && led3==0){for(z=0; z<14; z++){ldata=message4[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}}}void MsDelay(unsigned int itime ){unsigned int i , j ;for(i=0; i<itime; i++)for(j=0; j<135; j++);}May 19, 2013 at 6:03 pm #9792AJISH ALFRED
ParticipantHi Usman,
Using the statement if ((led1==1 || led2 && led3 == 0)==1) you are trying to read port bits 0,1 and 2 of port A which are already set as output. It is always better to avoid reading an output port.
Again you should always careful about using proper brackets like;
if ( ( ( led1==1 ) || ( ( led2 && led3 ) == 0 ) ) ==1 )
May 19, 2013 at 7:08 pm #9795Usman
ParticipantHi Dear,
actually problem is that
when i write a code like that
led1=1;
if(led1==1)
{
display message
}
but problem is compiler is not reading the if and its ignore this statement , even this is a true statement
and goes to else
May 22, 2013 at 5:12 pm #9811AJISH ALFRED
ParticipantHi Usman,
I’ve once faced the same issue with the nested if-else. The problem got fixed by adding ‘else’ for evry ‘if’.
When there is an ‘if’ there should be an ‘else’ eventhough there is nothing in the ‘else’ statement.
Coming to your code, the last ‘else-if’ condition is not terminated with an ‘else’.
Suggest you to add an ‘else’ like the following;
if ((led1==1 || led2 && led3 == 0)==1){for(z=0; z<12; z++){ldata=message1[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}}else if (led2==1){for(z=0; z<12; z++){ldata=message2[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}}else if(led3==1){for(z=0; z<12; z++){ldata=message3[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}}else if(led1==0 && led2==0 && led3==0){for(z=0; z<14; z++){ldata=message4[z];rs=1;rw=0;en=1;MsDelay(1);en=0;}} else; // HERE //Please try and update the result. -
AuthorPosts
- You must be logged in to reply to this topic.