Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Toggle screen back and forth

Status
Not open for further replies.

Mimuwhen

Junior Member level 3
Joined
Oct 7, 2014
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
284
Hi,

I'm trying to use an if else statement in microC to toggle an LCD screen between two states.

Unfortunately I cannot get this to work,

The code under the if statement cycles between 3 different messages and when the button tied to RB7 makes RB7 go low, it's supposed to make the screen display the string "Screen Toggled".

What is happening instead is that the 3 different messages keep cycling as if the button isn't being pushed and the code never goes into the else part of the code.

Attached is the mikroC project as well as the proteous project.

I'd appreciate any help, thank you.View attachment LCDtoggle.rar

This is what the code looks like,

Code:
// Lcd module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End Lcd module connections


void main(){

      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b10000000;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;
      
      Lcd_Init();                        // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off


      if(PORTB.F7=1)                     // If button is not pushed
      {
      while(1){                          // Repeat sequence
      Lcd_Out(1,6,"Message 1");
      Delay_ms(500);
      Lcd_Out(1,6,"Message 2");
      Delay_ms(500);
      Lcd_Out(1,6,"Message 3");
      Delay_ms(500);
      }
      }
      
      else{                              // If button is pressed
      Delay_ms(100);                     // Debounce
      if(PORTB.F7=0){                    // If button is still pressed
      Lcd_Out(1,2,"Screen Toggled");
      }
      }


}
 

You're having the while(1) loop in wrong place. Now it never goes out of PORTB.F7. Move while like this:
Code:
void main(){

      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b10000000;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;
      
      Lcd_Init();                        // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
	  
while(1){

      if(PORTB.F7=1)                     // If button is not pushed
      {
                                // Repeat sequence
      Lcd_Out(1,6,"Message 1");
      Delay_ms(500);
      Lcd_Out(1,6,"Message 2");
      Delay_ms(500);
      Lcd_Out(1,6,"Message 3");
      Delay_ms(500);
      }
      
      
      else{                              // If button is pressed
      Delay_ms(100);                     // Debounce
      if(PORTB.F7=0){                    // If button is still pressed
      Lcd_Out(1,2,"Screen Toggled");
      }
      }
}

}
 

Okay thanks.

milan.rajik, the idea is to have the screen cycle through the three messages and when RB7 is low, to display a completley different message for as long as the button is held down. Once RB7 goes high again, the program returns to cycling the three first messages again.

wade_hassler, thanks I made the change.

Veketti, your suggestion got it to work finally. I also had to add a delay to the else statement to get it to show on screen.

I've updated the code to be a little bit more intricate and I'm trying to achieve a couple more things but I'm having a new problem.

Let me explain what the new code does.

I have added unsigned int var1, var2, var3, and var4.

If RB7 is high, button not pushed:

The screen shows text "Total01:" followed by the value of var1.
500ms Delay
The screen shows text "Total02:" followed by the value of var2.
500ms Delay
The screen shows text "Total03:" followed by the value of var3.
500ms Delay
Repeat

If RB7 button is pushed and as long as it is kept pushed

var1,var2,var3 are added together and result stored in var4.
The screen shows text "Gran Total:" followed by the value of var4.
500ms delay
Lcd Clear
Repeat for as long as RB7 is low, if not return to first part of the code.

The problem is that the values of the variables var1, var2, var3, and var4 do not show up on screen at all even though the text strings do. Here is a screen shot.

TotalsMissing.pngGrantotalMissing.png

You can see that the variables aren't printed on the screen at all. What could be causing this? I tried using both Lcd_Out and Lcd_Chr with no success.

Here is the code and hex. View attachment LCDtoggle.hex.tar.gz

Code:
void main(){

     unsigned int var1=2;
     unsigned int var2=3;
     unsigned int var3=2;
     unsigned int var4=0;

      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b10000000;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;
      
      Lcd_Init();                        // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
      


      while(1){
      if(PORTB.F7==1)                     // If button is not pushed
      {                                   // Repeat sequence
      Lcd_Out(1,4,"Total01:");
      Lcd_Chr(1,14,var1);                 // Print value of Var1 on screen
      Delay_ms(500);
      Lcd_Out(1,4,"Total02:");
      Lcd_Chr(1,14,var2);                 // Print value of Var2 on screen
      Delay_ms(500);
      Lcd_Out(1,4,"Total03:");
      Lcd_Chr(1,14,var3);                 // Print value of Var3 on screen
      Delay_ms(500);
      }
      
      else{        			 // If button is pressed
      var4=var1+var2+var3;               // Add var1-3 store result in var 4                         
      Lcd_Out(1,3,"Gran Total:");
      Lcd_Chr(2,6,var4);                 // Print value of var4 on screen
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);
      }
      }


}
 

Try this.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unsigned int var1, var2, var3, var4;
char lcdData[17];
 
while(1) {
       
       IntToStr(var1, lcdData);
       Lcd_Chr(1,14,var1);
 
       IntToStr(var2, lcdData);
       Lcd_Chr(1,14,var2);
 
       IntToStr(var3, lcdData);
       Lcd_Chr(1,14,var3);
 
       IntToStr(var4, lcdData);
       Lcd_Chr(2,6,var4);
}

 

Okay I tried that with no success, still the same problem, the variables do not appear on screen.

Here's what the code looks like after adding what you suggested,

Code:
void main(){

     unsigned int var1=0;
     unsigned int var2=0;
     unsigned int var3=0;
     unsigned int var4=0;
     char lcdData[17];

      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b10000000;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;
      
      Lcd_Init();                                       // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);                   // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
      


      while(1){
      if(PORTB.F7==1)                        // If button is not pushed
      {                                                 
      Lcd_Out(1,4,"Total-1:");
      IntToStr(var1, lcdData);
      Lcd_Chr(1,14, var1);                 // Print value of Var1 on screen
      Delay_ms(500);                 
      Lcd_Out(1,4,"Total-2:");
      IntToStr(var2, lcdData);
      Lcd_Chr(1,14,var2);                 // Print value of Var2 on screen
      Delay_ms(500);
      Lcd_Out(1,4,"Total-3:");
      IntToStr(var3, lcdData);
      Lcd_Chr(1,14,var3);                 // Print value of Var3 on screen
      Delay_ms(500);
      }
      
      else{                                       // If button is pressed
      var4=var1+var2+var3;
      Lcd_Out(1,3,"Gran Total:");
      IntToStr(var4, lcdData);
      Lcd_Chr(2,6,var4);
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);
      }
      }


}
 

Sorry my mistake. I copied and pasted your code and modified it earlier but forgot to change LCD_Chr() to LCD_Out(). Here is the working code. To print strings you have to use LCD_Out().


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
unsigned int var1=0;
unsigned int var2=0;
unsigned int var3=0;
nsigned int var4=0;
char lcdData[17];
 
void main(){
    
      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0x80;
      PORTB = 0x00;
      TRISD = 0x00;
      PORTD = 0x00;
      
      Lcd_Init();                                       // Initialize Lcd
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
      Lcd_Cmd(_LCD_CLEAR);                   // Clear display
      
      while(1){
      if(PORTB.F7==1)                        // If button is not pushed
      {
    Delay_ms(100);
     if(PORTB.F7==1)                        // If button is not pushed
      {
                                               
      Lcd_Out(1,4,"Total-1:");
      IntToStr(var1, lcdData);
      Lcd_Out(1,14, var1);                 // Print value of Var1 on screen
      Delay_ms(500);                 
      Lcd_Out(1,4,"Total-2:");
      IntToStr(var2, lcdData);
      Lcd_Out(1,14,var2);                 // Print value of Var2 on screen
      Delay_ms(500);
      Lcd_Out(1,4,"Total-3:");
      IntToStr(var3, lcdData);
      Lcd_Out(1,14,var3);                 // Print value of Var3 on screen
      Delay_ms(500);
    }
      }
      
      else{                                       // If button is pressed
      var4=var1+var2+var3;
      Lcd_Out(1,3,"Gran Total:");
      IntToStr(var4, lcdData);
      Lcd_Out(2,6,var4);
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);
      }
      }
}

 

Okay, I tried the change and still no luck. Here it is again in case i made any mistake in copying,


Code:
    void main(){

     unsigned int var1=0;
     unsigned int var2=0;
     unsigned int var3=0;
     unsigned int var4=0;
     char lcdData[17];

      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b10000000;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;
      
      Lcd_Init();                        // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
      

      while(1){
      if(PORTB.F7==1)                     // If button is not pushed
      {
      Delay_ms(100);
      if(PORTB.F7==1)
      {                          // Repeat sequence
      Lcd_Out(1,4,"Total-1:");
      IntToStr(var1, lcdData);
      Lcd_Out(1,14, var1);
      Delay_ms(500);                 // Print value of Var1 on screen
      Lcd_Out(1,4,"Total-2:");
      IntToStr(var2, lcdData);
      Lcd_Out(1,14,var2);                 // Print value of Var2 on screen
      Delay_ms(500);
      Lcd_Out(1,4,"Total-3:");
      IntToStr(var3, lcdData);
      Lcd_Out(1,14,var3);                 // Print value of Var3 on screen
      Delay_ms(500);
      }
      }
      
      else{                              // If button is pressed
      var4=var1+var2+var3;
      Lcd_Out(1,3,"Gran Total:");
      IntToStr(var4, lcdData);
      Lcd_Out(2,6,var4);
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);
      }
      }
}
 

LcdData is the string. It has to be sent to LCD_Out().

Oops, I made mistake again.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void main(){
 
     unsigned int var1=0;
     unsigned int var2=0;
     unsigned int var3=0;
     unsigned int var4=0;
     char lcdData[17];
 
      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b10000000;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;
      
      Lcd_Init();                        // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
      
 
      while(1){
      if(PORTB.F7==1)                     // If button is not pushed
      {
      Delay_ms(100);
      if(PORTB.F7==1)
      {                          // Repeat sequence
      Lcd_Out(1,4,"Total-1:");
      IntToStr(var1, lcdData);
      Lcd_Out(1,14, lcdData);
      Delay_ms(500);                 // Print value of Var1 on screen
      Lcd_Out(1,4,"Total-2:");
      IntToStr(var2, lcdData);
      Lcd_Out(1,14,lcdData);                 // Print value of Var2 on screen
      Delay_ms(500);
      Lcd_Out(1,4,"Total-3:");
      IntToStr(var3, lcdData);
      Lcd_Out(1,14,lcdData);                 // Print value of Var3 on screen
      Delay_ms(500);
      }
      }
      
      else{                              // If button is pressed
      var4=var1+var2+var3;
      Lcd_Out(1,3,"Gran Total:");
      IntToStr(var4, lcdData);
      Lcd_Out(2,6,lcdData);
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);
      }
      }
}

 

Okay that finally got it to work. For some strange reason though, the IF part of the code only started to work after i changed the value of var1 and now no matter what value I give these variables it works fine. Sweet, thanks!

I have a couple questions.

Does this mean that the only way to display a variable's numeric vaue is to turn it into a string in mikroC?

Also what does the number in the brackets mean in char lcdData[17]? I've also seem some examples where a text is decared as txt[] without a number in the brackets. What is the purpose of the brackets?
 

lacData[17] is a array of 17 bytes. It is mentioned in mikroC help file that the char array or string should be 17 bytes long if it has to be used for IntToStr() function and 23 bytes long for FloatToStr() function. Each digit of a number is converted to ascii character and stored in the char array and the array is terminated with a null character to make it a string. Only then it can be printed on LCD.
 

Okay thank you for the info.

While playing around with this new trick I've run into a different problem. Now what seems to be happening is that the displayed integer appears in the completley incorrect part of the screen. For example in the new code below I want to display "ledone" on row 2 column 1 of the display but instead it appers in the middle of the screen while ledtwo is no where to be found.

See the screen shot:

wrongposition.png

And here is the code, This is the same project except what is displayed on screen has changed. You can see the command Lcd_Out(2,1, ledone); and Lcd_Out(2,15, ledtwo); setting the positions correctly but it never shows up this way on screen.

Code:
void main(){

     unsigned int led1=5;
     unsigned int led2=3;
     char ledone[17];
     char ledtwo[17];

      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b11111110;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;

      Lcd_Init();                        // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off



      while(1){
      if(PORTB.F7==1)                     // If button is not pushed
      {
      Delay_ms(100);
      if(PORTB.F7==1)
      {                                       
      Lcd_Out(1,1,"LED1:");
      Lcd_Out(1,12,"LED2:");
      IntToStr(led1, ledone);
      IntToStr(led2, ledtwo);
      Lcd_Out(2,1, ledone);
      Lcd_Out(2,15, ledtwo);
      }
      }

      else{                              // If button is pressed
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,5,"ON");
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);
      }
      }


}
 

Before displaying ledone and ledtwo use


Code C - [expand]
1
2
Ltrim(ledone);
Ltrim(ledtwo);

 

Okay thank you that worked perfectly.

I have another question. Is it possible to display two digit numbers under 10 with a zero infront of them? For example to display "02" instead of just "2"? I much prefer the look of this than a single digit. I know this can be done in MPLAB but can it be done with this program?
 

Thank you! Unfortunately there's too many zeroes! haha It seems it displays every single empty space to it's left. Is there a way to specify the exact number of decimal places?
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
char tmp[] = "0";
char tmp2[17];
char ledone[17];
 
memset(tmp2, '\0', sizeof(tmp2));
strcat(tmp2, tmp);
IntToStr(led1, ledone);
strcat(tmp2, ledone);
LCD_Out(2,1,tmp2);

 

Alright, sorry for the delay!

I gave it a try but it's not working out as desired. Instead I get only two digits displyed but I get all the spaces where the zeroes used to be included.


InvisibleZeros.png


Here is the code, I'm not sure if I did it properly. This was a bit confusing for me. Man this is turning out a lot more difficult than I expected!

Code:
void main(){

     unsigned int led1=1;
     unsigned int led2=2;
     
     char tmp[17]="0";
     char tmp2[17];
     char ledone[17];
     
     char tmp3[17]="0";
     char tmp4[17];
     char ledtwo[17];



      TRISA = 0xFF;
      PORTA = 0x00;
      TRISB = 0b11111110;
      PORTB = 0b00000000;
      TRISD = 0X00;
      PORTD = 0X00;

      Lcd_Init();                        // Initialize Lcd
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off



      while(1){
      if(PORTB.F7==1)                     // If button is not pushed
      {
      Delay_ms(100);
      if(PORTB.F7==1)
      {                                       // Repeat sequence
      Lcd_Out(1,1,"LED1");
      Lcd_Out(1,13,"LED2");
      memset(tmp2, '\0', sizeof(tmp2));
      strcat(tmp2, tmp);
      IntToStr(led1, ledone);
      strcat(tmp2, ledone);
      LCD_Out(2,1, tmp2);
      memset(tmp4, '\0', sizeof(tmp4));
      strcat(tmp4, tmp3);
      IntToStr(led2, ledtwo);
      strcat(tmp4, ledtwo);
      LCD_Out(2,15, ledtwo);
      

      }
      }

      else{                              // If button is pressed
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,"((TOGGLE))");
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);
      }
      }


}
 

Try this.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
memset(tmp2, '\0', sizeof(tmp2));
strcat(tmp2, tmp);
IntToStr(led1, ledone);
Ltrim(ledone);
strcat(tmp2, ledone);
LCD_Out(2,1, tmp2);
 
memset(tmp3, '\0', sizeof(tmp4));
strcat(tmp3, tmp);
IntToStr(led2, ledtwo);
Ltrim(ledtwo);
strcat(tmp3, ledtwo);
LCD_Out(2,15, ledtwo);

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top