[Moved]Interface keypad and motor with p18f4520

Status
Not open for further replies.

mystickatara

Newbie level 3
Joined
Feb 18, 2015
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
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.

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;
	}
}
 

Code:
if(KBD_DA==1)
Code:
#define KBD_DA  PORTCbits.RC6
Where PORTCbits.RC6 = PORTC & (1<<6)
So, the result can be 0x00 or 0x40 and never be 0x01
You better use simpler:
if(KBD_DA) ...
this is just a non-zero comparsion
 

Don't mind telling me where to add the first one? I'm still a novice in programming.
 

For read port state use PORTx register. For write LATx. But I recomend you to start from led blinking if you do not understand what we are talking about.
 

Don't use delays for the motor code. If a delay is executing then at that time the keypad will not respond. Use Timer interrupts.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…