mystickatara
Newbie level 3
- Joined
- Feb 18, 2015
- Messages
- 3
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 22
Hi, I'm having problems with trying to get both my keypad and motor to work together. I am using a MPLAB IDE v8.92.
The theory for this code is when I click on '1' (case 1) on the keypad the motor will run for a short period of time. The codes are also the same for the other numbers '2', '3', and '4' as this is just a basic code I'm trying to get it working before adding more features into this. However it keeps on looping from case 0 to the while-loop and I don't know how to get it to stop looping and getting the keypad to work the way I want it to.
My whole code for keypad and motor is using PORTC. Motor is RC0 while the rest of the pins are using the keypad.
The theory for this code is when I click on '1' (case 1) on the keypad the motor will run for a short period of time. The codes are also the same for the other numbers '2', '3', and '4' as this is just a basic code I'm trying to get it working before adding more features into this. However it keeps on looping from case 0 to the while-loop and I don't know how to get it to stop looping and getting the keypad to work the way I want it to.
My whole code for keypad and motor is using PORTC. Motor is RC0 while the rest of the pins are using the keypad.
Code:
#include <p18f4520.h>
#include <delays.h>
#define KBD_DA PORTCbits.RC6
#define KBD_OE PORTCbits.RC5
#define KBD_DATA PORTC
#define KBD_ENABLE_OUTPUT KBD_OE = 0;
#define KBD_DISABLE_OUTPUT KBD_OE = 0;
unsigned kbd_dec[4] =
{
0x0,
0x1,
0x2,
0x4,
};
void main()
{
unsigned char kbd_data = 0, choice = 0, prev_kbd_data = 0;
unsigned char matches = 0;
int i = 0;
PORTCbits.RC0=0; // Motor off initially.
TRISC=0xff;
KBD_ENABLE_OUTPUT;
while(1)
{
if(KBD_DA==1) //De-bouncing
{
choice = 0;
prev_kbd_data = (KBD_DATA & 0x1E);
matches =0;
while(matches < 3)
{
Delay1KTCYx(50);
kbd_data = (KBD_DATA & 0x1E);
if(prev_kbd_data == kbd_data)
{
matches++;
}
else
{
matches = 0;
}
prev_kbd_data = kbd_data;
}
}
switch(kbd_data)
{
case 0:
PORTCbits.RC0=0; //T 10 PULSE
break;
case 1:
while(i<100)
{
PORTCbits.RC0=0; //T 10 PULSE
Delay100TCYx(90); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay100TCYx(10); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
case 2:
while(i<100)
{
PORTCbits.RC0=0; //T 20 PULSE
Delay10TCYx(100); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay10KTCYx(20); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
case 3:
while(i<100)
{
PORTCbits.RC0=0; //T 20 PULSE
Delay10TCYx(100); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay10KTCYx(20); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
case 4:
while(i<100)
{
PORTCbits.RC0=0; //T 20 PULSE
Delay10TCYx(100); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay10KTCYx(20); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
}
}