Analog Joystick interface with AVR ATmega16/ATmega32
Introduction
Analog Joystick
Analog Joystick is used as an input device for changing position of cursor in correspondence to movement of joystick.
It gives analog voltages in X and Y directions corresponding to the position of the joystick.
These voltages can be processed to find the position of cursor or for moving the cursor according to the position of the joystick.
For more information about Analog Joystick and how to use it, refer the topic Analog Joystickin the sensors and modules section.
In order to process the analog signals, we need to use ADC on microcontroller.
For information about ADC in ATmega16 and how to use it, refer the topic ADC in AVR ATmega16/ATmega32 in the ATmega inside section.
Circuit Diagram
Interfacing Analog Joystick With AVR ATmega32
Programming of Joystick
- Let’s make the code to display the Direction of an angular movement.
- Here we also show the voltage at X and Y axis pins at a particular movement.
- The state of Switch, whether it is pressed or not is also shown.
The code for the particular system is mainly divided into three parts:
- X-Axis and Y-Axis Position voltages:
- To measure the voltage at the terminal X-Out and Y-Out, we can use inbuilt 10-bit ADC of Controller.
- To get the voltage at X-OUT pin, we are connecting X-OUT pin to 1st pin of PORTA (PA0). PA0 pin is channel 0 for ADC.
- To get the voltage at Y-OUT pin, we are connecting Y-OUT pin to 2nd pin of PORTA (PA1). PA1 pin is channel 1 for ADC.
- ADC gives output in 10-bit format. It is important to Scale the ADC output in an appropriate format.
- To display the value in form of output voltage, it is required to scale the ADC output in 0 to 5. The formula used for scaling is,
Output Voltage = ((ADC value * 5)/1024);
- If the output voltage is 2.5, The LCD shows C, which indicate the holder is at centre position.
- When we move holder to upward direction, Y-Axis voltage start increase. And LCD shows U, which indicates upward moment. At the same time, the LCD shows the value of voltage on both pins X-axis and Y-axis.
- Similarly, according to movements, LCD shows Downward, Left and Right position of Holder.
2. Switch State:
This pin is connected to Ground when switch is pressed. To detection of switch pressed, it is required to set controller pin as Input configuration and active internal pull-up resistance.
Program
/*
Joystick interface with AVR ATmega16/ATmega32
www.electronicwings.com
*/
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include "LCD_16x2_H_file.h"
#include "ADC_H.h"
int main(void)
{
char buffer[20];
int ADC_Value;
ADC_Init(); /* Initialize ADC */
LCD_Init(); /* Initialize LCD */
while(1)
{
ADC_Value = ADC_Read(0);/* Read the status on X-OUT pin using channel 0 */
sprintf(buffer, "X=%d ", ADC_Value);
LCD_String_xy(1, 0, buffer);
ADC_Value = ADC_Read(1);/* Read the status on Y-OUT pin using channel 0 */
sprintf(buffer, "Y=%d ", ADC_Value);
LCD_String_xy(1, 8, buffer);
ADC_Value = ADC_Read(2);/* Read the status on SWITCH pin using channel 0 */
if(ADC_Value < 600)
LCD_String_xy(2, 0, "Switch pressed ");
else
LCD_String_xy(2, 0, "Switch open ");
}
}
No comments:
Post a Comment