问题描述
我正在使用键盘选择数学运算和数字,然后在 LCD 上显示带有数字的运算和答案。答案和选择的数字显然是整数,我可以显示一个简单的字符串,例如 LCD_String("HELLO") 给我你好。但是如果我选择加法运算,并选择3和4作为数字,我如何在LCD上显示3+4=7?
我当前的代码是:
#include "ECE532_includes_v00.h"
//For LCD
#define RS LATD0 //PORTD pin 0 is used for register slect
#define EN LATD1 //PORTD pin 1 is used for enable
#define Idata LATB //PORTB is used for transmitting data to LCD
#define LCD_PORT TRISB
#define LCD_Control TRISD
void LCD_Init();
void LCD_Command(char );
void LCD_String(const char *);
//For connecting keypad to PORTC
#define X_1 RC0
#define X_2 RC1
#define X_3 RC2 //Connect keypad to PortC
#define X_4 RC3
#define Y_1 RC4
#define Y_2 RC5
#define Y_3 RC6
#define Y_4 RC7
#define Keypad_PORT PORTC
#define Keypad_PORT_Direction TRISC
char keypad_scanner (void);
char digit[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40}; //array for hex values of numbers 0-9
void main() {
int y = 0; //value for sum and product and input numbers
int num1 = 0;
int num2 = 0;
char key = 'n';
Keypad_PORT_Direction = 0xF0; //Last 4 pins are inputs and the first 4 are outputs
Keypad_PORT = 0x00; //Set Keypad port pin values to zero
while(1){
//Math Operations
key = keypad_scanner();
if(key == 'A'){
key = keypad_scanner(); //Addition
num1 = key - '0';
key = keypad_scanner();
num2 = key - '0';
y = num1 + num2;
LCD_Init();
}
//when results is a negative display "Negative" on LCD
// if(y < 0){
// LCD_Init();
// LCD_String("NEGATIVE");
// }
}
}
void LCD_Init()
{
__delay_ms(15); //15ms,16x2 LCD Power delay
LCD_PORT = 0x00; //set PORT as output PORT for LCD data(DO-D7) pins
LCD_Control = 0X00; //set port as output port LCD control(RS,EN)pins
LCD_Command(0x38); // uses 2 line and initialize 5*8 matrix of LCD
LCD_Command(0x01); // clear display screen
LCD_Command(0x0E); // display on cursor off
LCD_Command(0x06); // increment cursor (shift cursor to right)
}
void
LCD_Command (char cmd)
{
Idata = cmd ; /* Send data to PORT as a command for LCD */
RS = 0; /* Command Register is selected */
EN = 1; /* High to Low pulse on Enable pin to latch data */
EN = 0;
__delay_ms (3);
}
void LCD_Char (char dat)
{
Idata = dat ; /* Send data to LCD */
RS = 1; /* Data Register is selected */
EN=1; /* High to Low pulse on Enable pin to latch data */
EN=0;
__delay_ms(1);
}
void LCD_String(const char *msg)
{
while((*msg)!=0)
{
LCD_Char(*msg);
msg++;
}
}
char keypad_scanner (void)
{
//Reading the keypad input value
X_1 = 0; X_2 = 1; X_3 = 1; X_4 = 1;
if (Y_1 == 0) { __delay_ms (100); while (Y_1==0); return '1';} //Row 1
if (Y_2 == 0) { __delay_ms (100); while (Y_2==0); return '2';}
if (Y_3 == 0) { __delay_ms (100); while (Y_3==0); return '3';}
if (Y_4 == 0) { __delay_ms (100); while (Y_4==0); return 'A';}
X_1 = 1; X_2 = 0; X_3 = 1; X_4 = 1;
if (Y_1 == 0) { __delay_ms (100); while (Y_1==0); return '4';} //Row 2
if (Y_2 == 0) { __delay_ms (100); while (Y_2==0); return '5'; }
if (Y_3 == 0) { __delay_ms (100); while (Y_3==0); return '6';}
if (Y_4 == 0) { __delay_ms (100); while (Y_4==0); return 'B';}
X_1 = 1; X_2 = 1; X_3 = 0; X_4 = 1;
if (Y_1 == 0) { __delay_ms (100); while (Y_1==0); return '7';} //Row 3
if (Y_2 == 0) { __delay_ms (100); while (Y_2==0); return '8';}
if (Y_3 == 0) { __delay_ms (100); while (Y_3==0); return '9';}
if (Y_4 == 0) { __delay_ms (100); while (Y_4==0); return 'C';}
X_1 = 1; X_2 = 1; X_3 = 1; X_4 = 0;
if (Y_1 == 0) { __delay_ms (100); while (Y_1==0); return '*';} //Row 4
if (Y_2 == 0) { __delay_ms (100); while (Y_2==0); return '0';}
if (Y_3 == 0) { __delay_ms (100); while (Y_3==0); return '#';}
if (Y_4 == 0) { __delay_ms (100); while (Y_4==0); return 'D';}
return 'n';
}
简而言之,我通过数学运算为一个整数 (y
) 分配了一个变量。我想知道如何显示整数的值或如何将其转换为字符串以便在 LCD 上显示。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)