GENGE
Newbie level 2
- Joined
- Dec 13, 2012
- Messages
- 2
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,304
I have my code and already implemented on hardware that scan 4x4 matrix keypad and display a number in seven segment display, now I wan't to connect a DC motor so that number 1 is pressed on keyboard motor should rotate clockwise and when 2 is pressed motor rotate ant clockwise.,
the code for keypad and proteus simulation are down here,
kindly help me to add that subroutine to rotate motor as described above,
regards,
char findKey(unsigned short a, unsigned short b)
{
if(b == 0)
{
if(a == 3)
return '0';
else if(a == 2)
return '1';
else if(a == 1)
return '2';
else if(a == 0)
return '3';
}
else if(b == 1)
{
if(a == 3)
return '4';
else if(a == 2)
return '5';
else if(a == 1)
return '6';
else if(a == 0)
return '7';
}
else if(b == 2)
{
if(a == 3)
return '8';
else if(a == 2)
return '9';
else if(a == 1)
return 'A';
else if(a == 0)
return '-';
}
else if(b == 3)
{
if(a == 3)
return 'C';
else if(a == 2)
return 'U';
else if(a == 1)
return 'E';
else if(a == 0)
return 'F';
}
}
char readKeyboard()
{
unsigned int i = 0;
for(i=0;i<4;i++)
{
if(i == 0)
PORTB = 1;
else if(i == 1)
PORTB = 2;
else if(i == 2)
PORTB = 4;
else if(i == 3)
PORTB = 8;
if(PORTB.F4)
return findKey(i,0);
if(PORTB.F5)
return findKey(i,1);
if(PORTB.F6)
return findKey(i,2);
if(PORTB.F7)
return findKey(i,3);
}
return ' ';
}
unsigned int sevenSegmentDecoder(char a)
{
switch(a)
{
case '0': return 0x3F;
case '1': return 0x06;
case '2': return 0x5B;
case '3': return 0x4F;
case '4': return 0x66;
case '5': return 0x6D;
case '6': return 0x7D;
case '7': return 0x07;
case '8': return 0x7F;
case '9': return 0x6F;
case '0': return 0x3F;
case 'A': return 0x77;
case '-': return 0x40;
case 'C': return 0x39;
case 'U': return 0x3E;
case 'E': return 0x79;
case 'F': return 0x71;
case ' ': return 0;
}
}
void main()
{
char r;
TRISB = 0xF0;
TRISD = 0x00;
do
{ r = readKeyboard();
if(r != ' ')
PORTD = sevenSegmentDecoder(r);
Delay_ms(100);
}
while(1);
}
the code for keypad and proteus simulation are down here,
kindly help me to add that subroutine to rotate motor as described above,
regards,
char findKey(unsigned short a, unsigned short b)
{
if(b == 0)
{
if(a == 3)
return '0';
else if(a == 2)
return '1';
else if(a == 1)
return '2';
else if(a == 0)
return '3';
}
else if(b == 1)
{
if(a == 3)
return '4';
else if(a == 2)
return '5';
else if(a == 1)
return '6';
else if(a == 0)
return '7';
}
else if(b == 2)
{
if(a == 3)
return '8';
else if(a == 2)
return '9';
else if(a == 1)
return 'A';
else if(a == 0)
return '-';
}
else if(b == 3)
{
if(a == 3)
return 'C';
else if(a == 2)
return 'U';
else if(a == 1)
return 'E';
else if(a == 0)
return 'F';
}
}
char readKeyboard()
{
unsigned int i = 0;
for(i=0;i<4;i++)
{
if(i == 0)
PORTB = 1;
else if(i == 1)
PORTB = 2;
else if(i == 2)
PORTB = 4;
else if(i == 3)
PORTB = 8;
if(PORTB.F4)
return findKey(i,0);
if(PORTB.F5)
return findKey(i,1);
if(PORTB.F6)
return findKey(i,2);
if(PORTB.F7)
return findKey(i,3);
}
return ' ';
}
unsigned int sevenSegmentDecoder(char a)
{
switch(a)
{
case '0': return 0x3F;
case '1': return 0x06;
case '2': return 0x5B;
case '3': return 0x4F;
case '4': return 0x66;
case '5': return 0x6D;
case '6': return 0x7D;
case '7': return 0x07;
case '8': return 0x7F;
case '9': return 0x6F;
case '0': return 0x3F;
case 'A': return 0x77;
case '-': return 0x40;
case 'C': return 0x39;
case 'U': return 0x3E;
case 'E': return 0x79;
case 'F': return 0x71;
case ' ': return 0;
}
}
void main()
{
char r;
TRISB = 0xF0;
TRISD = 0x00;
do
{ r = readKeyboard();
if(r != ' ')
PORTD = sevenSegmentDecoder(r);
Delay_ms(100);
}
while(1);
}