Table of Contents
Overview
Hi guys, today we are going to talk about another interesting and important module that interacts with the TMS320. We’ll just deal with the joystick first. Then learn about the joystick interface with the TMS320 in this article.
What is a joystick, and how does it work?
The joystick is essentially an input device used to control robotics and is also used in games. A picture of the joystick design is shown in the figure above. The joystick has different types and uses for different purposes. The image above shows the contacts that will be used to interact with the equipment. We know the first two contacts, which are the main contacts used in each piece of equipment. This is +5 V, and another grounded. The other three contacts are incoming and outgoing. VRx and VRy are the input and SW is the output. The pins will briefly include an interface joystick from the TMS320.
Basically, all the work of the joystick is based on the movement of the axis shown in the figure below.
In this image, we saw that VRx and VRy generated different values in different positions, so this value will be important for our hardware for the next command.
Components Required
- TMS320 UNO
- USB Cable for Uploading the Code
- Joystick Module
- LED
- 220-ohm resistor
- Jumper Wires
- Breadboard
Controlling LED’S using a joystick/TMS320 joystick controller
- Connection to TMS320 and TMS320joystick.
- Connect the Vcc of the joystick pin to the 5v of TMS320.
- Connect the Gnd of the joystick pin to the Gnd of TMS320.
- Connect the I Rx of the joystick pin to the ADC_CH_ADCIN0 of TMS320.
- Connect I RY Vcc of joystick pin to the ADC_CH_ADCIN2 of TMS320.
Final code
/*
* mian.c
*
* Created on: 26-Nov-2021
* Author: Admin
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "F2837xD_device.h"
#include "F28x_Project.h"
#include "F2837xD_Examples.h"
#include "driverlib.h"
#include "device.h"
#define EX_ADC_RESOLUTION 12
void ConfigADC(uint32_t ADC_BASE);
void initADC_SOC(void);
void gpio_init();
uint16_t Adc_Result_1,Adc_Result_2;
void main(void)
{
Device_init();
Device_initGPIO();
Interrupt_initModule();
Interrupt_initVectorTable();
ConfigADC(ADCA_BASE);
initADC_SOC();
gpio_init();
EINT;
ERTM;
while(1)
{
// Convert, wait for completion, and store results
ADC_forceSOC(ADCA_BASE, ADC_SOC_NUMBER0);
ADC_forceSOC(ADCA_BASE, ADC_SOC_NUMBER2);
while(ADC_getInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1) == false)
{
}
ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);
//// // Store results
Adc_Result_1= ADC_readResult(ADCARESULT_BASE, ADC_SOC_NUMBER0);
Adc_Result_2 = ADC_readResult(ADCARESULT_BASE, ADC_SOC_NUMBER2);
if( (Adc_Result_1 <2000))
{
GpioDataRegs.GPCTOGGLE.bit.GPIO67=1;
DEVICE_DELAY_US(50000);
}
else if(Adc_Result_1>4000)
{
GpioDataRegs.GPDTOGGLE.bit.GPIO111 =1;
DEVICE_DELAY_US(50000);
}
else if( (Adc_Result_2 <2000))
{
GpioDataRegs.GPBTOGGLE.bit.GPIO60=1;
DEVICE_DELAY_US(50000);
}
else if(Adc_Result_2>4090)
{
GpioDataRegs.GPATOGGLE.bit.GPIO22=1;
DEVICE_DELAY_US(50000);
}
else {
GpioDataRegs.GPCSET.bit.GPIO67=1;
GpioDataRegs.GPDSET.bit.GPIO111=1;
GpioDataRegs.GPBSET.bit.GPIO60=1;
GpioDataRegs.GPASET.bit.GPIO22=1;
}
}
}
void ConfigADC(uint32_t ADC_BASE)
{
EALLOW;
ADC_setPrescaler(ADCA_BASE, ADC_CLK_DIV_4_0);
#if(EX_ADC_RESOLUTION == 12)
{
ADC_setMode(ADC_BASE, ADC_RESOLUTION_12BIT, ADC_MODE_SINGLE_ENDED);
}
#elif(EX_ADC_RESOLUTION == 16)
{
ADC_setMode(ADCA_BASE, ADC_RESOLUTION_16BIT, ADC_MODE_DIFFERENTIAL);
}
#endif
ADC_setInterruptPulseMode(ADC_BASE, ADC_PULSE_END_OF_CONV);
ADC_enableConverter(ADC_BASE);
DEVICE_DELAY_US(1000);
EDIS;
}
void initADC_SOC(void)
{
#if(EX_ADC_RESOLUTION == 12)
{
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_SW_ONLY, ADC_CH_ADCIN0, 15);
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER2, ADC_TRIGGER_SW_ONLY, ADC_CH_ADCIN2, 15);
}
#elif(EX_ADC_RESOLUTION == 16)
{
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_SW_ONLY, ADC_CH_ADCIN0,64);
}
#endif
ADC_setInterruptSource(ADCA_BASE, ADC_INT_NUMBER1, ADC_SOC_NUMBER0);
ADC_setInterruptSource(ADCA_BASE, ADC_INT_NUMBER1, ADC_SOC_NUMBER2);
ADC_enableInterrupt(ADCA_BASE, ADC_INT_NUMBER1);
ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);
}
void gpio_init()
{
EALLOW;
GpioCtrlRegs.GPCMUX1.bit.GPIO67=0; // 0=GPIO, 1=EPWM1A, 2=Resv, 3=Resv
GpioCtrlRegs.GPCDIR.bit.GPIO67=1; // 1=OUTput, 0=INput
GpioCtrlRegs.GPCPUD.bit.GPIO67=0; // 0: Enables the Pull-Up. 1: Disables the Pull-Up.
GpioCtrlRegs.GPCCSEL1.bit.GPIO67=0; //xx00: CPU1 selected xx01: CPU1.CLA1 selected
//xx10: CPU2 selected xx11: CPU2.CLA1 selected
GpioCtrlRegs.GPDMUX1.bit.GPIO111=0; // 0=GPIO, 1=EPWM1A, 2=Resv, 3=Resv
GpioCtrlRegs.GPDDIR.bit.GPIO111=1; // 1=OUTput, 0=INput
GpioCtrlRegs.GPDPUD.bit.GPIO111=0; // 0: Enables the Pull-Up. 1: Disables the Pull-Up.
GpioCtrlRegs.GPDCSEL2.bit.GPIO111=0; //xx00: CPU1 selected xx01: CPU1.CLA1 selected
GpioCtrlRegs.GPBMUX2.bit.GPIO60=0; // 0=GPIO, 1=EPWM1A, 2=Resv, 3=Resv
GpioCtrlRegs.GPBDIR.bit.GPIO60=1; // 1=OUTput, 0=INput
GpioCtrlRegs.GPBPUD.bit.GPIO60=0; // 0: Enables the Pull-Up. 1: Disables the Pull-Up.
GpioCtrlRegs.GPBCSEL4.bit.GPIO60=0; //xx00: CPU1 selected xx01: CPU1.CLA1 selected
GpioCtrlRegs.GPAMUX2.bit.GPIO22=0; // 0=GPIO, 1=EPWM1A, 2=Resv, 3=Resv
GpioCtrlRegs.GPADIR.bit.GPIO22=1; // 1=OUTput, 0=INput
GpioCtrlRegs.GPAPUD.bit.GPIO22=0; // 0: Enables the Pull-Up. 1: Disables the Pull-Up.
GpioCtrlRegs.GPACSEL3.bit.GPIO22=0; //xx00: CPU1 selected xx01: CPU1.CLA1 selected
EDIS;
}