I have 2 different screens on LCD connected with a single 8051 microcontroller. I want to increment the count on both screens present in the same position (1,0) on LCD when the button is pressed. Three buttons are used one for moving the cursor to that position and another for incrementing count and another for changing the screen. But when I pressed the button on the second screen it gets the temp
value incremented, but I want the temp1
value to be incremented.
How can I increment the second screen count with the same button?
Here, I have incremented the first screen count as:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
void display_screen1(void);
void display_screen2(void);
void change_screen(void);
void move_cursor(void);
void increment(void);
int set_cursor_position(uint_fast8_t row, uint_fast8_t col);
int temp = '2';
int temp1 = '5';
int first = 1;
int second = 0;
int count = 0;
unsigned char button0 = 0;
unsigned char button1 = 0;
unsigned char button2 = 0;
int set_cursor_position(uint_fast8_t row, uint_fast8_t col)
{
if (row)
{
col |= 0x40;
}
col |= 0x80;
lcd_cout(col);
return 0;
}
void display_screen1(void)
{
lcd_l2(0x80); //position on LCD (1,0)
lcd_dout(temp); //displaying on LCD
}
void display_screen2(void)
{
lcd_l2(0x80);
lcd_dout(temp1);
}
void change_screen(void)
{
if (button0 == 1)
{
if(count == 0) {
count++;
delay_msec(100);
display_screen1();
button0 = 0;
}
else if (count == 1) {
display_screen2();
count = 0;
button0 = 0;
}
}
void move_cursor(void)
{
if (button1 == 1)
{
if (second > 1 && second <= 6)
{
first = 1;
second = second + 1;
if (second > 6)
second = 2;
set_cursor_position(first, second);
}
button1 = 0;
}
}
void increment(void)
{
if (button2 == 1) //if button is pressed
{
temp++;
if (temp > 0x39)
temp = 0x30;
lcd_dout(temp);
}
set_cursor_position(1, 0);
button2 = 0;
}
First
You should need a variable to record what currently used LCD.
Otherwise, 8051
can't increase the count on the correct LCD.
So you maybe try to add a variable in void change_screen(void)
.
Second
Before increasing the count, 8051
needs to judge which LCD you are using.
So you need to add the judgment in void increment(void)
The concept is like below:
enum{
LCD1,
LCD2
}LCD_NUM;
bool SwitchLCD_flag=false;
void change_screen(void)
{
if (ChangeLCD_button == 1)
{
....
// When pressed the button, switch the LCD_NUM.
SwitchLCD_flag = !SwitchLCD_flag;
switch(SwitchLCD_flag) {
case 0:
LCD_NUM = LCD1;
...
break;
case 1:
LCD_NUM = LCD2;
...
break;
}
}
}
Third
For now, 8051
knows which LCD is in use.
So you can write the increase function in the judge function.
The concept is like below:
void increment(void)
{
if (Increase_button == 1)
{
switch(LCD_NUM){
case LCD1:
LCD1_count++;
break;
case LCD2:
LCD2_count++;
break;
...
}
}
...
}
And suggest you before programming, you can draw a simple flowchart to clear your code's structure and the relationship between each function.