Overview of Analog Joystick
An analog joystick is an input device used to change the position of a cursor based on the movement of the joystick. It outputs analog voltages in the X and Y directions, which correspond to the joystick’s position.
These analog voltages can be processed to determine the cursor’s position or to move the cursor in accordance with the joystick’s position.
For more details on the analog joystick and its usage, refer to the Analog Joystick topic in the sensors and modules section.
To process the analog signals, the ADC (Analog-to-Digital Converter) of the microcontroller is used.
For information on ADC in the PIC18F4550 and how to use it, refer to the ADC in PIC18F4550 topic in the PIC inside section.
Analog Joystick Module
Connection Diagram of Analog Joystick to PIC18F4550
Analog Joystick Module Interfacing with PIC18F4550
Read Analog Joystick using PIC18F4550
In this setup, we will interface an analog joystick with the PIC18F4550 microcontroller and display the digital values corresponding to the joystick’s XY movement on an LCD16x2 display.
To interface the joystick, connect the X and Y output pins of the joystick to the ADC channels of the microcontroller. By reading the ADC values from the X and Y pins, we can determine the joystick’s position.
Additionally, we will display the joystick’s movement on a PC using Matlab. In Matlab, the Serial Port is used to read the values sent by the PIC18F4550 microcontroller, which transmits the joystick data serially.
Analog Joystick Code for PIC18F4550
/*
Analog Joystick Interfacing with PIC18f4550
http://www.electronicwings.com
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <p18f4550.h>
#include "Configuration_Header_File.h" /* Header File for Configuration bits */
#include "LCD_16x2_8-bit_Header_File.h" /* Header File for LCD Functions */
#include "PIC18F4550_ADC_Header_File.h" /* Header File for ADC Functions */
#include "USART_Header_File.h" /* Header File for USART Functions */
void main()
{
char data[10],data1[10];
int x,y;
OSCCON=0x72; /*Set internal Oscillator frequency to 8 MHz*/
LCD_Init(); /*Initialize 16x2 LCD*/
ADC_Init(); /*Initialize 10-bit ADC*/
USART_Init(9600);
while(1)
{
x=ADC_Read(0);
sprintf(data,"%d ",x); /* it is used to convert integer value to ASCII string */
LCD_String_xy(0,0,data); /* send string data for printing */
USART_SendString(data);
MSdelay(90);
y=ADC_Read(1);
sprintf(data1,"%d ",y); /* it is used to convert integer value to ASCII string*/
LCD_String_xy(0,6,data1); /* send string data for printing*/
USART_SendString(data1);
MSdelay(90);
USART_TxChar(0x0A);
USART_TxChar(0x0D);
memset(data,0,10);
memset(data1,0,10);
}
}