EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Led number display

Led number display

|

Microcontroller › PIC › Led number display

  • This topic has 3 replies, 2 voices, and was last updated 12 years, 11 months ago by AJISH ALFRED.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • May 18, 2013 at 9:17 am #2416
    Usman
    Participant

    Salam , 

    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.RA2
     
    void lcdcmd(unsigned char );
    void lcddata();
    void MsDelay(unsigned int );
    void main() 
    {
     
    // unsigned char message=”Usman Salleem “;
    TRISD=0;       // make an output port
    TRISB=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 #9792
    AJISH ALFRED
    Participant

    Hi 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 #9795
    Usman
    Participant

    Hi  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 #9811
    AJISH ALFRED
    Participant

    Hi 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.
     
  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • AI algorithm for bots April 17, 2026
  • SDCC Array Access In Timer 0 Interrupt Handler April 16, 2026
  • EasyEda program has a major bug April 16, 2026
  • ANOTHER OLD PROJECT REDO April 16, 2026
  • How to set the OSCAL in a PIC 12F675 April 16, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise