Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I need urgent help

hassanmahmoud

New Member
I have designed a program for the microcontroller and it works well, and I only need to put a welcome message at the start of the operation, which disappears for 2 seconds forever, and does not appear until I restart the microcontroller, and the program after that completes its functions well, and I used the( IF) function in the program.
I am using a 16f877a microcontroller, 2*16 LCD, and micro c software for microchip.
thanks for all
 
Last edited:
My solution is posted below

I found no need of an "IF" statement

C:
// simple program to display welcome message to LCD for 2 seconds, then move on
// PIC16F877A with 8 MHz external 2-pin crystal
// EasyPIC V7 board
// be sure to select LCD library by putting a tick in the box for Lcd in "system libraries"

// defines for LCD library
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main(){
    PORTB = 0;                                                  // all low at start
    TRISB = 0;                                                  // all output
    PORTC = 0;                                                  // all low at start
    TRISC = 0;                                                  // all output
    PORTD = 0;                                                  // all low at start
    TRISD = 0;                                                  // all output

    Delay_ms(500);                                              // allow time for LCD to power up
    Lcd_Init();
    Lcd_Out(1,2,"Welcome 2s");
    Delay_ms(2000);                                             // show welcome message for 2 seconds
    Lcd_Cmd(_LCD_CLEAR);

    Lcd_Out(1,2,"Done");                                        // optional second message

    while(1)
    {
        PORTC = ~PORTC;                                         // toggle all LEDs on port C
        Delay_ms(500);                                          // expect 1Hz square wave if all is well
    }
}
 
Last edited:
Your program :

1) Writes "Welcome 2s"
2) Then waits 2 sec
3) Then writes "Done" to LCD
4) Then loops forever toggling LEDs with a 500 mS period

Based on your posted code. I do not see any IF statment you mentioned.

Regards, Dana.
 
Based on your posted code. I do not see any IF statment you mentioned.

Regards, Dana.
I failed to be clear that this is my attempt at a solution, not the OPs

I have updated my post to hopefully be clearer

It is up to the OP to give more information on his/her ACTUAL requirement, which is probably different to the stated requirement

We are all playing guessing games right now
 
My solution is posted below

I found no need of an "IF" statement

C:
// simple program to display welcome message to LCD for 2 seconds, then move on
// PIC16F877A with 8 MHz external 2-pin crystal
// EasyPIC V7 board
// be sure to select LCD library by putting a tick in the box for Lcd in "system libraries"

// defines for LCD library
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main(){
    PORTB = 0;                                                  // all low at start
    TRISB = 0;                                                  // all output
    PORTC = 0;                                                  // all low at start
    TRISC = 0;                                                  // all output
    PORTD = 0;                                                  // all low at start
    TRISD = 0;                                                  // all output

    Delay_ms(500);                                              // allow time for LCD to power up
    Lcd_Init();
    Lcd_Out(1,2,"Welcome 2s");
    Delay_ms(2000);                                             // show welcome message for 2 seconds
    Lcd_Cmd(_LCD_CLEAR);

    Lcd_Out(1,2,"Done");                                        // optional second message

    while(1)
    {
        PORTC = ~PORTC;                                         // toggle all LEDs on port C
        Delay_ms(500);                                          // expect 1Hz square wave if all is well
    }
}
thanks
 
My solution is posted below

I found no need of an "IF" statement

C:
// simple program to display welcome message to LCD for 2 seconds, then move on
// PIC16F877A with 8 MHz external 2-pin crystal
// EasyPIC V7 board
// be sure to select LCD library by putting a tick in the box for Lcd in "system libraries"

// defines for LCD library
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main(){
    PORTB = 0;                                                  // all low at start
    TRISB = 0;                                                  // all output
    PORTC = 0;                                                  // all low at start
    TRISC = 0;                                                  // all output
    PORTD = 0;                                                  // all low at start
    TRISD = 0;                                                  // all output

    Delay_ms(500);                                              // allow time for LCD to power up
    Lcd_Init();
    Lcd_Out(1,2,"Welcome 2s");
    Delay_ms(2000);                                             // show welcome message for 2 seconds
    Lcd_Cmd(_LCD_CLEAR);

    Lcd_Out(1,2,"Done");                                        // optional second message

    while(1)
    {
        PORTC = ~PORTC;                                         // toggle all LEDs on port C
        Delay_ms(500);                                          // expect 1Hz square wave if all is well
    }
}
The program that I created must have an IF function because I have several choices. If the first choice is not fulfilled, it moves to the second until the last choice. If it does not check the conditions of all the choices, it gives me an error message and the program works very well, but I just wanted the welcome phrase to appear at the beginning. .. Thank you, my friend, for your help. I will try and tell you the result
 
C:
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "first choice";
char txt2[] = "second choice";
char txt3[] = "Third choice";
char txt4[] = "Fourth chec";
char txto[] = "ok";
char txtr[] = "Ready";
char txtt[] = "test";
char txte[] = "error";
char txtn[] = "Not good to use";


void main() {
 trisc.f0=1;
 trisc.f1=1;
 trisc.f2=1;
 trisc.f3=1;
 trisc.f4=1;
 portc=0;

 
  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 
                                     // I want "welcome" here for 2 seconds

      Lcd_Out(1,6,txtr);
     Lcd_Out(2,4,txtt);
     delay_ms(3000);
     Lcd_Cmd(_LCD_CLEAR);


   if(portc==0b00000001)


   {
    Lcd_Out(1,6,txto);
    Lcd_Out(2,6,txt1);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);

     }
if(portc==0b00000011)
{
     Lcd_Out(1,6,txto);
     Lcd_Out(2,6,txt2);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);
     }
     if(portc==0b00000111)
{
     Lcd_Out(1,6,txto);
     Lcd_Out(2,6,txt2);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);
     }
    
     if(portc==0b000001111)
{
     Lcd_Out(1,6,txto);
     Lcd_Out(2,6,txt2);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);
     }
     else Lcd_Out(1,6,txte);
     Lcd_Out(2,5,txtn);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);

}
 
Last edited by a moderator:
Your program :

1) Writes "Welcome 2s"
2) Then waits 2 sec
3) Then writes "Done" to LCD
4) Then loops forever toggling LEDs with a 500 mS period

Based on your posted code. I do not see any IF statment you mentioned.

Regards, Dana.



My code
C:
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "first choice";
char txt2[] = "second choice";
char txt3[] = "Third choice";
char txt4[] = "Fourth chec";
char txto[] = "ok";
char txtr[] = "Ready";
char txtt[] = "test";
char txte[] = "error";
char txtn[] = "Not good to use";


void main() {
 trisc.f0=1;
 trisc.f1=1;
 trisc.f2=1;
 trisc.f3=1;
 trisc.f4=1;
 portc=0;

 
  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 
                                     // I want "welcome" here for 2 seconds

      Lcd_Out(1,6,txtr);
     Lcd_Out(2,4,txtt);
     delay_ms(3000);
     Lcd_Cmd(_LCD_CLEAR);


   if(portc==0b00000001)


   {
    Lcd_Out(1,6,txto);
    Lcd_Out(2,6,txt1);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);

     }
if(portc==0b00000011)
{
     Lcd_Out(1,6,txto);
     Lcd_Out(2,6,txt2);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);
     }
     if(portc==0b00000111)
{
     Lcd_Out(1,6,txto);
     Lcd_Out(2,6,txt2);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);
     }
    
     if(portc==0b000001111)
{
     Lcd_Out(1,6,txto);
     Lcd_Out(2,6,txt2);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);
     }
     else Lcd_Out(1,6,txte);
     Lcd_Out(2,5,txtn);
     delay_ms(5000);
     Lcd_Cmd(_LCD_CLEAR);

}
 
Last edited by a moderator:
Please use code tags.. Makes you post far more readable.

You'll need a forever loop. Or the the thing will keep resetting..
All you if statements need to wrap round
C:
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "first choice";
char txt2[] = "second choice";
char txt3[] = "Third choice";
char txt4[] = "Fourth chec";
char txto[] = "ok";
char txtr[] = "Ready";
char txtt[] = "test";
char txte[] = "error";
char txtn[] = "Not good to use";


void main()
    {
     trisc.f0=1;
     trisc.f1=1;
     trisc.f2=1;
     trisc.f3=1;
     trisc.f4=1;
     portc=0;
    Lcd_Init();            // Initialize LCD

    Lcd_Cmd(_LCD_CLEAR);        // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);     // Cursor off
 
    Lcd_Out(1,2,"Welcome 2s");
    delay_ms(2000);            // I want "welcome" here for 2 seconds

    Lcd_Out(1,6,txtr);
    Lcd_Out(2,4,txtt);
    delay_ms(3000);
    Lcd_Cmd(_LCD_CLEAR);
    while(1)            // <-- Need a forever loop here
        {
        if(portc==0b00000001)
            {
            Lcd_Out(1,6,txto);
            Lcd_Out(2,6,txt1);
            delay_ms(5000);
            Lcd_Cmd(_LCD_CLEAR);

            }
        if(portc==0b00000011)
            {
            Lcd_Out(1,6,txto);
            Lcd_Out(2,6,txt2);
            delay_ms(5000);
            Lcd_Cmd(_LCD_CLEAR);
            }
        if(portc==0b00000111)
            {
            Lcd_Out(1,6,txto);
            Lcd_Out(2,6,txt2);
            delay_ms(5000);
            Lcd_Cmd(_LCD_CLEAR);
            }
        if(portc==0b000001111)
            {
            Lcd_Out(1,6,txto);
            Lcd_Out(2,6,txt2);
            delay_ms(5000);
            Lcd_Cmd(_LCD_CLEAR);
            }
        else Lcd_Out(1,6,txte);
        Lcd_Out(2,5,txtn);
        delay_ms(5000);
        Lcd_Cmd(_LCD_CLEAR);
        }           
    }
 

Latest threads

New Articles From Microcontroller Tips

Back
Top