unsigned char number[2] = "3";
int temp = 0;
void display1(void) {
write_string(1, 0, number);
delay_msec(100);
}
int write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char *str) {
if (row)
{
col |= 0x40;
}
col |= 0x80;
lcd_cout(col);
while (*str)
temp = lcd_dout(*str++);
return temp;
}
void incrementbutton(void) {
if (button == 1) {
temp = temp + 1;
button = 0;
}
}
int write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char *str) {
if (row)
{
col |= 0x40;
}
col |= 0x80;
lcd_cout(col);
while (*str)
lcd_dout(*str++);
}
void display1(void) {
temp = write_string(1, 0, number);
delay_msec(100);
}
void incrementbutton(void) {
if (button == 1) { //button is pressed
temp = temp + 1;
button = 0;
}
}
It's related to the last post.
@KlausST till the code and relevant information for the problem is all I have provided.Hi,
it´s you who causes the delays and makes it unable to help.
We like to help, we repeatedly ask for complete informations ...
Klaus
@FvM I want to increment the two or more than 2 digits in the LCD when the button is pressed and usage of sprintf() and.....how it works...A feature of C language is that we can use libraries and don't need to write anything, e.g. formatted number output, from the scratch. Unless specified otherwise in your exercise problem, I would expect usage of sprintf() or itoa() for multi digit number to string conversion.
void incrementbutton(void) {
if (button == 1) { //button is pressed
if (temp >= 48 && temp < 57) { //to check the counter value between 0-9
temp = temp+1;
if (temp >57) { //to make count value to 0 when count is more than 9
temp = 48;
}
button = 0;
}
}
Hi,till the code and relevant information for the problem is all I have provided.
@KlausST till the code and relevant information for the problem is all I have provided.
The button press followed by release (you should debounce it, in and out), should unconditionally@KlausST till the code and relevant information for the problem is all I have provided.
--- Updated ---
@FvM I want to increment the two or more than 2 digits in the LCD when the button is pressed and usage of sprintf() and.....how it works...
--- Updated ---
@KlausST how can i make count value to zero after it reach 9, at present its incrementing between 0 to 9.
Code:void incrementbutton(void) { if (button == 1) { //button is pressed if (temp >= 48 && temp < 57) { //to check the counter value between 0-9 temp = temp+1; if (temp >57) { //to make count value to 0 when count is more than 9 temp = 48; } button = 0; } }
#include <stdio.h>
#include <string.h>
#include <stdint.h>
extern unsigned char number[4] = "3 ";
extern unsigned char number1[4] = "2 ";
int temp = 0;
int temp1 = 0;
int place1 = 1;
int place2 = 0;
unsigned char button = 0;
unsigned char button1 = 0;
void Countdisplay(void);
void lcd_init(void);
void lcd_cout(unsigned char ccod);
void lcd_dout(unsigned char dcod);
void display1(void);
void set_cursor_position (uint_fast8_t row, uint_fast8_t col);
void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char *str);
void incrementbutton(void);
void op_switich_in(void) ;
unsigned char op_swin_1d(void);
void move_cursor(void);
void CountdisplayToLCD(void);
void main()
{
lcd_init();
display1();
while (1)
{
incrementbutton();
move_cursor();
}
}
void lcd_init(void)
{
P7 = 0x00; //E=0,RS=0
P7 = 0x03;
P7 = 0x23; //E=1,RS=0
delay_micro(100);
P7 = 0x03; //E=0,RS=0
delay_msec(5);
P7 = 0x23; //E=1,RS=0
delay_micro(100);
P7 = 0x03; //E=0,RS=0
delay_msec(5);
P7 = 0x23; //E=1,RS=0
delay_micro(100);
P7 = 0x03; //E=0,RS=0
lcd_cout(0x02);
delay_msec(5);
lcd_cout(0x28);
delay_msec(5);
lcd_cout(0x0C);
delay_msec(5);
}
void lcd_cout(unsigned char ccod) //for Lcd command
{
unsigned char ccod_msb;
unsigned char ccod_lsb;
ccod_msb = ccod / 0x10;
ccod_lsb = ccod & 0x0F;
P7 = ccod_msb;
P7 = ccod_msb | 0x20;
delay_micro(2);
P7 = ccod_msb;
delay_micro(2);
P7 = ccod_lsb;
P7 = ccod_lsb | 0x20;
delay_micro(2);
P7 = ccod_lsb;
delay_micro(50);
}
void lcd_dout(unsigned char dcod) //for lcd_Data
{
unsigned char dcod_msb;
unsigned char dcod_lsb;
dcod_msb = dcod / 0x10;
dcod_lsb = dcod & 0x0F;
P7 = dcod_msb | 0x10;
P7 = dcod_msb | 0x30;
delay_micro(2);
P7 = dcod_msb | 0x10;
delay_micro(2);
P7 = dcod_lsb | 0x10;
P7 = dcod_lsb | 0x30;
delay_micro(2);
P7 = dcod_lsb | 0x10;
delay_micro(50);
}
void display1(void)
{
temp = write_string(1, 0, number);
delay_msec(100);
temp1 = write_string(1, 1, number1);
delay_msec(100);
}
void set_cursor_position (uint_fast8_t row, uint_fast8_t col) //for moving cursor
{
if (row)
{
col |= 0x40;
}
col |= 0x80;
lcd_cout (col);
}
void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char *str) {
if (row)
{
col |= 0x40;
}
col |= 0x80;
lcd_cout (col);
while (*str)
lcd_dout(*str++);
}
Code for the button:
void op_switich_in(void) //reading the button
{
unsigned char opsw_id;
opsw_id = op_swin_1d();
if (opsw_id == before_opsw_id){
if ((opsw_id & 0x01) == 0) button = 1; //increment button
if ((opsw_id & 0x01) == 0) button1 = 1; //cursor moving button
}
before_opsw_id = opsw_id;
}
unsigned char op_swin_1d(void)
{
unsigned char swid;
P0 = P0 | 0x20;
delay_micro(100);
swid = P2 & 0x3F;
P0 = P0 & 0xDF;
return swid;
}
void incrementbutton(void) {
if (button == 1) { //button pressed
if (temp >= 48 && temp < 57) { //to check if the counter value between 0-9
temp = temp+1;
if (temp >57) { //to roll count value to 0 when count is more than 9
temp = 48; //But not rolling
}
set_cursor_position (1, 0); //sets the cursor to this position when incrementing count
button = 0;
}
}
void move_cursor(void)
{
if (button1 == 1) {
set_cursor_position (place1, place2);
place1 = 1;
place2 = place1+1;
button1 = 0;
}
}
}
void CountdisplayToLCD(void) {
lcd_dout(temp);
}
void op_switich_upf(void){
if (upf == 1) {
set_cursor_position (1, 2);
if (temp >= 47 && temp < 57) {
temp = temp+1;
lcd_dout(temp);
set_cursor_position (1, 2);
upf = 0;
}
else {
set_cursor_position(1,3);
if (upf ==1) {
if (temp1 >= 47 && temp1 < 57) {
temp1 = temp1 +1;
lcd_dout(temp1);
set_cursor_position(1,3);
upf = 0;
}
}
}
}
}
Notice in code above you never process temp1.....section because youvoid op_switich_upf(void) {
if (upf == 1) {
set_cursor_position (1, 2);
if (temp >= 47 && temp < 57) {
temp = temp+1;
lcd_dout(temp);
set_cursor_position (1, 2);
upf = 0;
} else {
set_cursor_position(1,3);
if (upf == 1) {
if (temp1 >= 47 && temp1 < 57) {
temp1 = temp1 +1;
lcd_dout(temp1);
set_cursor_position(1,3);
upf = 0;
}
}
}
}
}
void op_switich_upf(void) {
if (upf == 1) {
set_cursor_position (1, 2);
if (temp >= 47 && temp < 57) {
temp = temp+1;
lcd_dout(temp);
set_cursor_position (1, 2);
upf = 0;
}
if (upf == 0 ) {
set_cursor_position(1,3);
if (temp1 >= 47 && temp1 < 57) {
temp1 = temp1 +1;
lcd_dout(temp1);
set_cursor_position(1,3);
upf = 0;
}
}
}
Regards, Dana.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
extern unsigned char number[4] = "3 ";
extern unsigned char number1[4] = "2 ";
int temp = 0;
int temp1 = 0;
int place1 = 1;
int place2 = 0;
unsigned char button = 0;
unsigned char button1 = 0;
void Countdisplay(void);
void lcd_init(void);
void lcd_cout(unsigned char ccod);
void lcd_dout(unsigned char dcod);
void display1(void);
void set_cursor_position (uint_fast8_t row, uint_fast8_t col);
void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char *str);
void incrementbutton(void);
void op_switich_in(void) ;
unsigned char op_swin_1d(void);
void move_cursor(void);
void CountdisplayToLCD(void);
void main()
{
lcd_init();
display1();
while (1)
{
incrementbutton();
move_cursor();
}
}
void lcd_init(void)
{
P7 = 0x00; //E=0,RS=0
P7 = 0x03;
P7 = 0x23; //E=1,RS=0
delay_micro(100);
P7 = 0x03; //E=0,RS=0
delay_msec(5);
P7 = 0x23; //E=1,RS=0
delay_micro(100);
P7 = 0x03; //E=0,RS=0
delay_msec(5);
P7 = 0x23; //E=1,RS=0
delay_micro(100);
P7 = 0x03; //E=0,RS=0
lcd_cout(0x02);
delay_msec(5);
lcd_cout(0x28);
delay_msec(5);
lcd_cout(0x0C);
delay_msec(5);
}
void lcd_cout(unsigned char ccod) //for Lcd command
{
unsigned char ccod_msb;
unsigned char ccod_lsb;
ccod_msb = ccod / 0x10;
ccod_lsb = ccod & 0x0F;
P7 = ccod_msb;
P7 = ccod_msb | 0x20;
delay_micro(2);
P7 = ccod_msb;
delay_micro(2);
P7 = ccod_lsb;
P7 = ccod_lsb | 0x20;
delay_micro(2);
P7 = ccod_lsb;
delay_micro(50);
}
void lcd_dout(unsigned char dcod) //for lcd_Data
{
unsigned char dcod_msb;
unsigned char dcod_lsb;
dcod_msb = dcod / 0x10;
dcod_lsb = dcod & 0x0F;
P7 = dcod_msb | 0x10;
P7 = dcod_msb | 0x30;
delay_micro(2);
P7 = dcod_msb | 0x10;
delay_micro(2);
P7 = dcod_lsb | 0x10;
P7 = dcod_lsb | 0x30;
delay_micro(2);
P7 = dcod_lsb | 0x10;
delay_micro(50);
}
void display1(void)
{
temp = write_string(1, 0, number);
delay_msec(100);
temp1 = write_string(1, 1, number1);
delay_msec(100);
}
void set_cursor_position (uint_fast8_t row, uint_fast8_t col) //for moving cursor
{
if (row)
{
col |= 0x40;
}
col |= 0x80;
lcd_cout (col);
}
void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char *str) {
if (row)
{
col |= 0x40;
}
col |= 0x80;
lcd_cout (col);
while (*str)
lcd_dout(*str++);
}
Code for the button:
void op_switich_in(void) //reading the button
{
unsigned char opsw_id;
opsw_id = op_swin_1d();
if (opsw_id == before_opsw_id){
if ((opsw_id & 0x01) == 0) button = 1; //increment button
if ((opsw_id & 0x01) == 0) button1 = 1; //cursor moving button
}
before_opsw_id = opsw_id;
}
unsigned char op_swin_1d(void)
{
unsigned char swid;
P0 = P0 | 0x20;
delay_micro(100);
swid = P2 & 0x3F;
P0 = P0 & 0xDF;
return swid;
}
void incrementbutton(void) {
if (button == 1) {
set_cursor_position (1, 2);
if (temp >= 48 && temp < 57) {
temp = temp+1;
if(temp > 57) temp = 48;
set_cursor_position (1, 2);
upf = 0;
}
if(button == 1) {
set_cursor_position(1,3);
if (temp1 >= 48 && temp1 < 57) {
temp1 = temp1 +1;
set_cursor_position(1,3);
upf = 0;
}
}
}
}
void move_cursor(void)
{
if (button1 == 1) {
set_cursor_position (place1, place2);
place1 = 1;
place2 = place1+1;
button1 = 0;
}
}
}
void CountdisplayToLCD(void) {
lcd_dout(temp);
delay_msec(5);
lcd_dout(temp1);
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?