I have a question about the coding for something in C for use with a microcontroller:
one of the examples in the text book is for interfacing a 4x4 key pad. my problem is that the text boook splits up the columns and rows from the key pad on two ports. I would like to split the rows and columns on one port( im using a P89LPC925 micro its a 8051).
heres the part of the code I would like to change
code from text:
#define COL P2
#define ROW P1
I think that in assembly there is some sort of DPL and DPH for using the high byte adn low byte. I would like to use Port 0 for the whole keypad.
So is there a way to say something like
#define COL DPH P2 // to get COL to be the high byte
#define ROW DPL P2 // ROW to be the low byte
Thanks
Yeti
If it helps any this is the whole code:
#include <reg51.h>
#include <stdio.h>
sfr p0m1 = 0x84; // this is no tin the text, but is used to make the ports
sfr p0m2 = 0x85; // bidirectional
//sfr p1m1 = 0x91; //
//sfr p1m2 = 0x92; //
#define COL P0
#define ROW P0
//#define TX P1
void MSDelay(unsigned int value);
void SerTX(unsigned char);
unsigned char keypad[4][4] = {'1', '2', '3', '+',
'4', '5', '6', '-',
'8', '9', '+', '*',
'.', '0', '=', '/'};
void main()
{
unsigned char colloc, rowloc;
PCON &= 0x7F; //
TMOD = 0x20; //
TH1 = 0xF4; // these values are different than the test b/c of the uC
TL1 = 0xF4; // i am using.
SCON = 0x50; //
TR1 = 1; //
COL = 0xF0; //to set the COL high and ROW low
ROW = 0xF0;
//TX = 0x7F; // the TX pin is P1.0, so setting it low for ouputting
while(1)
{
do
{
colloc = COL>>4;
colloc&= 0x0F;
}while(colloc != 0x0F);
do
{
do
{
MSDelay(20);
colloc = COL>>4;
colloc &= 0x0F;
}while(colloc == 0x0F);
MSDelay(20);
colloc = COL>>4;
colloc &= 0x0F;
}while(colloc == 0x0F);
while(1)
{
ROW = 0xFE;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 0;
break;
}
ROW = 0xFD;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 1;
break;
}
ROW = 0xFB;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 2;
break;
}
ROW = 0xF7;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 3;
break;
}
}
if(colloc == 0x0E)
SerTX(keypad[rowloc][0]);
else if(colloc == 0x0D)
SerTX(keypad[rowloc][1]);
else if(colloc == 0x0B)
SerTX(keypad[rowloc][2]);
else
SerTX(keypad[rowloc][3]);
}
}
void SerTX(unsigned char x)
{
SBUF = x;
while(TI == 0);
TI = 0;
}
void MSDelay(unsigned int value)
{
unsigned int x, y;
for(x=0; x<1275; x++)
for(y=0; y<value; y++);
}
one of the examples in the text book is for interfacing a 4x4 key pad. my problem is that the text boook splits up the columns and rows from the key pad on two ports. I would like to split the rows and columns on one port( im using a P89LPC925 micro its a 8051).
heres the part of the code I would like to change
code from text:
#define COL P2
#define ROW P1
I think that in assembly there is some sort of DPL and DPH for using the high byte adn low byte. I would like to use Port 0 for the whole keypad.
So is there a way to say something like
#define COL DPH P2 // to get COL to be the high byte
#define ROW DPL P2 // ROW to be the low byte
Thanks
Yeti
If it helps any this is the whole code:
#include <reg51.h>
#include <stdio.h>
sfr p0m1 = 0x84; // this is no tin the text, but is used to make the ports
sfr p0m2 = 0x85; // bidirectional
//sfr p1m1 = 0x91; //
//sfr p1m2 = 0x92; //
#define COL P0
#define ROW P0
//#define TX P1
void MSDelay(unsigned int value);
void SerTX(unsigned char);
unsigned char keypad[4][4] = {'1', '2', '3', '+',
'4', '5', '6', '-',
'8', '9', '+', '*',
'.', '0', '=', '/'};
void main()
{
unsigned char colloc, rowloc;
PCON &= 0x7F; //
TMOD = 0x20; //
TH1 = 0xF4; // these values are different than the test b/c of the uC
TL1 = 0xF4; // i am using.
SCON = 0x50; //
TR1 = 1; //
COL = 0xF0; //to set the COL high and ROW low
ROW = 0xF0;
//TX = 0x7F; // the TX pin is P1.0, so setting it low for ouputting
while(1)
{
do
{
colloc = COL>>4;
colloc&= 0x0F;
}while(colloc != 0x0F);
do
{
do
{
MSDelay(20);
colloc = COL>>4;
colloc &= 0x0F;
}while(colloc == 0x0F);
MSDelay(20);
colloc = COL>>4;
colloc &= 0x0F;
}while(colloc == 0x0F);
while(1)
{
ROW = 0xFE;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 0;
break;
}
ROW = 0xFD;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 1;
break;
}
ROW = 0xFB;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 2;
break;
}
ROW = 0xF7;
colloc = COL>>4;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 3;
break;
}
}
if(colloc == 0x0E)
SerTX(keypad[rowloc][0]);
else if(colloc == 0x0D)
SerTX(keypad[rowloc][1]);
else if(colloc == 0x0B)
SerTX(keypad[rowloc][2]);
else
SerTX(keypad[rowloc][3]);
}
}
void SerTX(unsigned char x)
{
SBUF = x;
while(TI == 0);
TI = 0;
}
void MSDelay(unsigned int value)
{
unsigned int x, y;
for(x=0; x<1275; x++)
for(y=0; y<value; y++);
}