8051 Switch Monitoring Help

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hello,

I have to write a program for an 8051 micro-controller using micro-C to
monitor Switch 1 and if the switch in pushed the message "switch 1
pushed" should be displayed in the LCD. Also the microcontroller
should display in the LCD the value of the voltage applied to the input
of the ADC.

The above procedure should only execute once the user has entered
"1234" using a keypad that is attached to the 8051 microprocessor.

I think I can figure out the keypad entry function that will wait until
the user has entered "1234" before proceeding. Here is what I have for
that part so far...

I havent tested it with a micro-controller yet but this code compiled
using the Bipom Micro-Ide compiler.

#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h>

#define MAX_ROWS   4
#define MAX_COLS   4

static char KeyTable[] = {    '1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
};

static unsigned char RowTable[] = { 0xFE, 0xFD, 0xFB, 0xF7 };

char ScanKeypad();

main()
{
char key;

serinit(9600);
for( ;; )
{

key = ScanKeypad();

if( key == "1234" )
{
printf( "\nKey: '%c'", key );
//Proceed with the rest of the program from here
}

printf ("\nIncorrect Code Entered\n");
}
}


char ScanKeypad()
{
char row;
char col;

col = 0;

for( row=0; row<MAX_ROWS; row++ )
{
P2 = RowTable[row];

if( !(P2 & 0x80) )
col = 4;

if( !(P2 & 0x40) )
col = 3;

if( !(P2 & 0x20) )
col = 2;

if( !(P2 & 0x10) )
col = 1;

if( col != 0 )
{
delay(500);
return KeyTable[col-1 + row*MAX_COLS];
}
}
return 0;
}

Now I am not sure how to monitor the switch 1...I tried to make the
program for that specific requirement and here is what I got:

#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h>
#define READ P0.1 /*READ=SET & WRITE=RESET*/
#define STROBE P0.2 /*DATA STROBE (ENABLE)*/
#define CTRL P0.0 /*CONTROL BIT (CONTROL=RESET & DATA=SET)*/

void InitLCD();
void WriteCtrl( unsigned char value );
void WriteData( unsigned char value );
void WriteCommon( unsigned char value );
void WriteLCD( unsigned char* message );
void CursorHome();
void CursorSet( unsigned char pos );

main()
{

/* Set the serial port to 9600 Baud */
serinit(9600);

while(1)
{
// Monitor Switch 1 - Display message on
// LCD if switch is pressed
while(!(P3 & 4))
{
InitLCD();
for(;;)
{
/* Write a simple message to the LCD */
CursorHome();
WriteLCD( "Switch 1 is pressed" );
}

}
}

//Functions to initialize and control the LCD
void InitLCD()
{
/* Wait a bit after power-up */
delay(200);
/* Initialize the LCD to 4-bit mode */
WriteCtrl(3);
delay(50);
WriteCtrl(3);
delay(10);
WriteCtrl(3);
delay(10);
WriteCtrl(2);
delay(10);
/* Function Set */
WriteCtrl(2);
delay(10);
WriteCtrl(8);
delay(10);
/* Display OFF */
WriteCtrl(0);
delay(10);
WriteCtrl(8);
delay(10);
/* Display ON */
WriteCtrl(0);
delay(10);
WriteCtrl(0x0F);
delay(10);
/* Entry mode */
WriteCtrl(0);
delay(10);
WriteCtrl(6);
delay(10);
/* Clear Screen */
WriteCtrl(0);
delay(10);
WriteCtrl(1);
delay(100);
/* Cursor home */
WriteCtrl(0);
delay(10);
WriteCtrl(2);
delay(100);
}
void CursorSet( unsigned char pos )
{
WriteCtrl(8 | ( (pos>>4) & 7 ) );
delay(1);
WriteCtrl(pos & 0x0F);
delay(1);
}
void CursorHome()
{
/* Cursor home */
WriteCtrl(0);
delay(1);
WriteCtrl(2);
delay(1);
}
void WriteLCD( unsigned char* message )
{
unsigned char i;
for( i=0; i<20; i++ )
{
if( !message[i] )
break;
WriteData(message[i]);
}
}
void WriteCtrl( unsigned char value )
{
clrbit(CTRL);
WriteCommon( value );
}
void WriteData( unsigned char value )
{
setbit(CTRL);
WriteCommon( value >> 4 );
WriteCommon( value );
}
void WriteCommon( unsigned char value )
{
clrbit(READ);
value = value & 0x0F;
value = value << 4;
P0 = P0 & 0x0F;
P0 = P0 | value;
setbit(STROBE);
delay(1);
clrbit(STROBE);
setbit(READ);
delay(1);
}

I believe that will work for monitoring the switch and displaying the
message on the LCD but now I really have no idea how to display in the
LCD the value of the voltage applied to the input of the ADC. Could
someone help with that please and verify that the programs posted above
would work considering the stated requirements?

Thank you


Re: 8051 Switch Monitoring Help

Quoted text here. Click to load it
<Bloody great snip...>
Quoted text here. Click to load it

With something like this, there's only ONE sensible way of finding out
if it works- TRY IT OUT. Bit by bit. Start with toggling one output to
make sure main() is executing, then go on from there. If you haven't got
a target, get a simulator. If you haven't got either, do something else
until you have.

Paul Burke


Re: 8051 Switch Monitoring Help

Quoted text here. Click to load it

I am not familiar with that compiler, but if you turn on all the
warnings, you should get some help.

Quoted text here. Click to load it

You should get a warning on the line above that you are comparing a
pointer, "1234", with a char, which indicates a problem with your code:
ScanKeypad() returns a single character, while you are attempting to
compare a pointer to a character string.

Your ScanKeypad() code also does not take new characters faster than one
every 500 something (ms?), also performing an autorepeat any time that a
key is held for more than 500 units.  Neither does it inhibit input when
multiple keys are pressed, nor does it wait for a key to be pressed.

One powerful technique that I use is to place a header comment in front
of each function telling

1.  What is required to call the function (perhaps data has to be
initialized or another function called for setup)
2.  What the effect of calling the function is, including returned
values, side effects, error handling, etc.

If you document your functions in this manner (and pay attention to them
when using the function) you will significantly reduce interface errors.

Thad


Site Timeline