PIC18F4685 + Thumb Joystick

Have you got the greatest 48 bit multiplier ever conceived? Prove it - post your code here.

Moderator: phalanx

Post Reply
isr
Posts: 2
Joined: Sun Jan 17, 2010 11:16 am

PIC18F4685 + Thumb Joystick

Post by isr » Sun Jan 17, 2010 11:39 am

I bought a thumb joystick from SF recently. I wired it up on my breadboard and managed to get the PIC to do AD conversions on the potentiometers. I am sending output to a 16x2 red on black 5V LCD, also from SF.

That being said, I am having a problem with the built-in push-button. I tried using several digital I/O pnis on the PIC to read the push-button (both Schmitt Trigger and TTY digital inputs). None of the digital IO pins I tried would read the button. I finally just used another analog input pin and ran an AD conversion on it; that works, but there has to be a better way.

Does anyone have any ideas on what could be causing the digital IO pins to choke on this push-button? Has anyone successfully gotten the thumb joystick push-button working on a digital IO pin?

Code: Select all

/** C O N F I G U R A T I O N   B I T S ******************************/

#pragma config OSC = HSPLL, FCMEN = OFF, IESO = ON, PWRT = OFF, BOREN = OFF
#pragma config BORV = 2, WDT = OFF, WDTPS = 256, MCLRE = OFF, STVREN = ON
#pragma config LVP = OFF, LPT1OSC = OFF, PBADEN = OFF, XINST = OFF

/** I N C L U D E S **************************************************/
#include <p18f4685.h>
#include <usart.h>
#include <delays.h>
#include <stdio.h>

#pragma code 

void main(void)
{
	int i, horizontal, vertical, button;

	// Configure UART
	OpenUSART( USART_TX_INT_OFF &
				USART_RX_INT_OFF &
				USART_ASYNCH_MODE &
				USART_EIGHT_BIT &
				USART_CONT_RX &
				USART_BRGH_LOW,
				64 );						// 9600 Baud
	
	
	// Configure AN1, AN2, and AN3 ports as inputs.
	TRISAbits.TRISA1 = 1;
	TRISAbits.TRISA2 = 1;
	TRISAbits.TRISA3 = 1;
	

	while (1)
	{
		// Read button on AD
		Delay100TCYx(50);
		ADCON2 = 0b00111000;
		ADCON1 = 0b00001011;
		ADCON0 = 0b00000111;
		while( ADCON0bits.GO_DONE );
		if (ADRESH > 100) button = 0;
		else button = 1;
		ADCON2 = 0;
		ADCON1 = 0;
		ADCON0 = 0;
		ADRESH = 0;

		// Read horizontal pot on AD
		Delay100TCYx(50);
		ADCON2 = 0b00111000;
		ADCON1 = 0b00001011;
		ADCON0 = 0b00001011;
		while( ADCON0bits.GO_DONE );
		horizontal = ADRESH;
		ADCON2 = 0;
		ADCON1 = 0;
		ADCON0 = 0;
		ADRESH = 0;

		// Read vertical pot on AD
		Delay100TCYx(50);
		ADCON2 = 0b00111000;
		ADCON1 = 0b00001011;
		ADCON0 = 0b00001111;
		while( ADCON0bits.GO_DONE );
		vertical = ADRESH;
		ADCON2 = 0;
		ADCON1 = 0;
		ADCON0 = 0;
		ADRESH = 0;
		
		Delay100TCYx(50);

		//Clear LCD and output values.
		printf("%c%c", 0xFE, 0x01);
		printf("%.3d, %.3d", horizontal, vertical);
		printf("%c%c", 0xFE, 0xC0);
		printf("%d", button);

		Delay10KTCYx(250); // Delay 2,500,000 cycles, about 250ms
	}
	CloseUSART();
}

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Post by waltr » Sun Jan 17, 2010 3:37 pm

There is too little info on SFE about the button. My guess is that its just a normally open momentary switch. In that case a pull-up resistor would be needed to obtain a logic high on the PIC digital input pin when the button in not depressed. The button should then connect the PIN input pin the ground when depressed for a logic low.

If your button hardware is correct then there is the code. Exactly which IO pins did you try and what was the code used for this. Most of the PICs have the IO pins set for analog input (if xN/Rx) on power up and the pin must be set-up for digital input. This can require writing the several registers. Recheck the data sheet for the pins you are using.

isr
Posts: 2
Joined: Sun Jan 17, 2010 11:16 am

Post by isr » Sun Jan 17, 2010 4:05 pm

I had a pull-up resistor all along. I tried using RB0 and RD7, neither of these worked. Oddly enough, I just tried doing this with RA4 and eureka, it works. When I was using RB0 or RD7 I just did the following:

Code: Select all

TRISBbits.TRISB0 = 1;
...
button = PORTBbits.RB0;
Not sure why that didn't work. I even tried using the internal weak pull-ups to no avail. AN4 works fine though. One other problem I've been noticing is that the voltage read from the vertical pot keeps bouncing very slightly when at center position. I'll have to iron that out later.

capn
Posts: 1
Joined: Wed Feb 23, 2011 8:40 am

Re: PIC18F4685 + Thumb Joystick

Post by capn » Wed Feb 23, 2011 9:22 am

For small momentary switches like that you need a Pull Down resistor to work properly with your digital in. The little push buttons don't have a very high current rating, so that resistor also acts as a current limiting resistor, as well as the pull down. The pull down makes it so your digital port isn't "floating" and will make it read more consistently. Also make sure the voltage you are putting in is compatible with your micro.

You want something like this:
Image

Post Reply