8051 Switch Monitoring Help

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 #include #include

#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>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> 4 ); WriteCommon( value ); } void WriteCommon( unsigned char value ) { clrbit(READ); value = value & 0x0F; value = value

Reply to
Steve
Loading thread data ...

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

Reply to
Paul Burke

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

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

Reply to
Thad Smith

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.