No. no need to return anythings. it check no button are pushed. it sure that all buttons have been released before looking at another one.Does it have to return anything?
What happens at the first test in the while loop?
Do you even need the SwitchNum variable?
#include <REG51.h>
sbit Switch_1 = P2^5;
sbit Switch_2 = P2^7;
sbit Switch_3 = P3^0;
sbit Switch_4 = P3^2;
sbit Register_Select_Pin = P2^0; /* Register Pin of LCD connected to Pin 0 of Port P2 */
sbit Read_Write_Pin = P2^1; /* Read/Write Pin of LCD connected to Pin 1 of Port P2 */
sbit Enable_Pin = P2^2; /* EN pin connected to pin 2 of port P2 */
/* Data pins connected to port P1 of 8051 */
#define Data_Port_Pins (P1)
#define Switch_Not_Pressed (0)
#define Switch_Pressed (1)
/* Function for creating delay in milliseconds */
void Delay(unsigned int wait)
{
unsigned i, j;
for(i = 0; i < wait; i++)
for(j = 0; j < 1200; j++);
}
/* Function to send command instruction to LCD */
void LCD_Command (unsigned char command)
{
Data_Port_Pins = command;
Register_Select_Pin =0;
Read_Write_Pin=0;
Enable_Pin =1;
Delay (2);
Enable_Pin =0;
}
/* Function to send display data to LCD */
void LCD_Data (unsigned char Data)
{
Data_Port_Pins = Data;
Register_Select_Pin=1;
Read_Write_Pin=0;
Enable_Pin =1;
Delay(2);
Enable_Pin =0;
}
/* Function to prepare the LCD and get it ready */
void LCD_Initialization()
{
LCD_Command (0x38);
LCD_Command (0x0e);
LCD_Command (0x01);
LCD_Command (0x81);
}
/* this is delay function */
void Debounce_time (unsigned int Loop)
{
for (Loop = 0; Loop < 40000; Loop++);
{
}
}
bit check_Switch_1(void)
{
bit return_value_1;
return_value_1 = Switch_Not_Pressed;
if(Switch_1 == 0) /* Switch 1 is pressed */
{
Debounce_time(20); /* wait 20 msec */
if(Switch_1 == 0) /* check switch again */
return_value_1 = Switch_Pressed; /* switch is really pressed */
}
return return_value_1;
}
bit check_Switch_2(void)
{
bit return_value_2;
return_value_2 = Switch_Not_Pressed;
if(Switch_2 == 0) /* Switch 2 is pressed */
{
Debounce_time(20); /* wait 20 msec */
if(Switch_2 == 0) /* check switch again */
return_value_2 = Switch_Pressed; /* switch 2 is really pressed */
}
return return_value_2;
}
bit check_Switch_3()
{
bit return_value_3;
return_value_3 = Switch_Not_Pressed;
if(Switch_3 == 0) /* Switch 3 is pressed */
{
Debounce_time(20); /* wait 20 msec */
if(Switch_3 == 0) /* check switch 3 again */
return_value_3 = Switch_Pressed; /* switch is really pressed */
}
return return_value_3;
}
bit check_Switch_4(void)
{
bit return_value_4;
return_value_4 = Switch_Not_Pressed;
if(Switch_4 == 0) /* Switch 4 is pressed */
{
Debounce_time(20); /* wait 20 msec */
if(Switch_4 == 0) /* check switch 4 again */
return_value_4 = Switch_Pressed; /* switch 4 is really pressed */
}
return return_value_4;
}
int CheckSwitches(void)
{
int ReturnValue = 0;
if (check_Switch_1()== Switch_Pressed )
{
ReturnValue = 1 ;
}
else if (check_Switch_2()== Switch_Pressed)
{
ReturnValue = 2;
}
else if (check_Switch_3()== Switch_Pressed)
{
ReturnValue = 3;
}
else if (check_Switch_4()== Switch_Pressed)
{
ReturnValue = 4;
}
return ReturnValue;
}
unsigned int wait_for_a_switch(void)
{
unsigned int SwitchNum = 0; // start with 'no switch'
while (SwitchNum == 0) // loop while 'no switch' - including the first time
{
SwitchNum = CheckSwitches();
}
return SwitchNum; // then return that value
}
unsigned int wait_for_no_switches(void)
{
unsigned int SwitchNum = 0; // start with 'no switch'
while (SwitchNum != 0)
{
SwitchNum = CheckSwitches();
}
return ;
}
I compiled that code before posting there are no errors but three warningsThere is.still a logic problem in wait_for_no_switches.
If that routine doesn't have to return anything, why does it?
I said that it's not ready for the LCD but since it is there:
The imit code won't work without some delays. I posted a link to the datasheet in another thread.
Since you have a DELAY routine in the LCD code calibrated in mused, I would use that for debouncing.
Why have two?
Have you completed the design for the code that gets user input and compares passwords?
You should compile the code and fix any errors before posting. That will save lots of time.
it check no button are pushed. it sure that all buttons have been released before looking at another one. if it return 0 that mean switch has been released no button are pushedThe question is why does it have to return a value at all?
I have tested and it work fine look at this thread https://forum.allaboutcircuits.com/threads/program-to-display-message-on-lcd.137686/#post-1153358It is time to flow out the password routine (or the rest of the program if you can) like I wanted. Issues such as this reveal themselves when you do.
Does the LCD routine work in the sim?
OK there is no return value in flow chart so I will not use any return valueOK.
It's time to flow out that password routine.
My first thought is 2 routines something like this:
GetPassword:
Prompt user
Get and store 4 button values in an array.
Return
CheckPassword:
Compare stored button values with password
Return TRUE or FALSE
Since your display uses ASCII, I would store the button values and password as ASCII, too. So instead of returning 1,2,3 and 4 from the button code, change those return values to their ASCII character equivalents '1', '2', '3', '4'.
What did you decide for the wait for no button routine?
Good question. Given the design methods you have learned, how would you proceed to answer it?How to Get and store 4 button values in an array ?
I have learned how to read the value of switches input but the problem is storing the value in array, I am not getting idea how to store these value in array (structured in C )Good question. Given the design methods you have learned, how would you proceed to answer it?
That would've been my approach; write a routine that reads all buttons at the same time, and then return a boolean array. That way it would let you read not just one button at a time, but a combination of buttons simultaneously. That would allow up to 16 different input scenarios, instead of just 4.Good question. Given the design methods you have learned, how would you proceed to answer it?
*shishhh*That would've been my approach; write a routine that reads all buttons at the same time, and then return a boolean array. That way it would let you read not just one button at a time, but a combination of buttons simultaneously. That would allow up to 16 different input scenarios, instead of just 4.
Aye, captain... promise I'll hold my horse steadier from now on... and won't fire my musket 'till I see the enemy's white of their eyes...*shishhh*
Gracias!Aye, captain... promise I'll hold my horse steadier from now on... and won't fire my musket 'till I see the enemy's white of their eyes...
I understood flow chart. there are four stepGracias!
Tempting though, isn't it..
Parth, how is that flow chart coming along? The natives are getting restless..
Parth, are you familiar with binary numbers? For example, do you know how to write the decimal number 107 in its binary form?I understood flow chart. there are four step
step 1 : check single switch if it return 1 means switch is pressed and if return 0 means switch is not pressed
step 2 :test each switch in sequence when it find one , if switch is pressed return switch value if not return 0
step 3: wait for no switch
step 4: wait for a switch
I spent some time to understand how do we to store the value of inputs button in array but I am stuck
wait for switch ();
check switches ();
wait for no switches ();
check switches ();
Yes I am familiar with conversion decimal to binary conversionParth, are you familiar with binary numbers? For example, do you know how to write the decimal number 107 in its binary form?
128 - 64-32-16-8-4-2-1
0-1-1-0-1-0-1-1
64+32+8+2+1=107
by Duane Benson
by Aaron Carman
by Aaron Carman