Tool/software:
Hello,
I'm quite new to the C2000 platform and although trying to read through the examples I didnt quite manage to get a simple ADC readout to work. Basically I want to turn on the LED11 on my board which is GPIO15 if the ADC readout is greater than 2048. The ADC readout is the potentiometer on my board which according to the handbook is ADC-B0. But the readouts Im getting are quite weird and never change although Im turning on the potentiometer. Right now Im really out of ideas. Following my code:
//
// Included Files
//
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#include "DSP2833x_Device.h" // General device header for F2833x
#include "DSP2833x_Examples.h" // Header that includes various peripheral definitions
//
// Function Prototypes
//
void InitAdc(void);
Uint16 ReadAdcResult(void);
//
// Main
//
void main(void)
{
//
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
//
InitSysCtrl();
//
// Step 2. Initialize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to its default state.
//
//InitGpio();
InitAdc();
//
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
//
DINT;
//
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
//
InitPieCtrl();
//
// Disable CPU interrupts and clear all CPU interrupt flags:
//
IER = 0x0000;
IFR = 0x0000;
//
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
//
InitPieVectTable();
//
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2833x_InitPeripherals.c
//
// InitPeripherals(); // Not required for this example
//
// Step 5. User specific code
//
// Configure GPIO9 as an input for the Start button
GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0; // Enable pull-up on GPIO7
GpioCtrlRegs.GPADIR.bit.GPIO7 = 0; // GPIO7 = input (Stop button)
// Configure GPIO9 as an input for the Start button
GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0; // Enable pull-up on GPIO9
GpioCtrlRegs.GPADIR.bit.GPIO9 = 0; // GPIO9 = input (Start button)
EALLOW; // Allow protected register access
GpioCtrlRegs.GPAMUX1.bit.GPIO15 = 0; // Set GPIO15 as a general-purpose I/O pin
GpioCtrlRegs.GPADIR.bit.GPIO15 = 1; // Set GPIO15 as an output
GpioDataRegs.GPACLEAR.bit.GPIO15 = 1; // Start with LED off
EDIS; // Disable protected register access
//
// Enable global Interrupts and higher priority real-time debug events
//
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
//
// Main loop
//
while(1)
{
Uint16 adcResult;
// Read ADC result from channel B0
adcResult = ReadAdcResult();
/*
if (GpioDataRegs.GPADAT.bit.GPIO9 == 0) // Start button pressed
{
DELAY_US(50000); // Debouncing delay
if (GpioDataRegs.GPADAT.bit.GPIO9 == 0) // Confirm button press after debounce
{
GpioDataRegs.GPASET.bit.GPIO15 = 1; // Turn on LED
}
}
else if (GpioDataRegs.GPADAT.bit.GPIO7 == 0) // Stop button pressed
{
DELAY_US(50000); // Debouncing delay
if (GpioDataRegs.GPADAT.bit.GPIO7 == 0) // Confirm button press after debounce
{
GpioDataRegs.GPACLEAR.bit.GPIO15 = 1; // Turn off LED
}
}
*/
// Optional: Process ADC result (for debugging or logic based on ADC)
if (adcResult > 2048) // Example threshold check
{
GpioDataRegs.GPASET.bit.GPIO15 = 1; // Turn on LED based on ADC threshold
}
}
}
//
// Function to initialize ADC for channel B0
//
void InitAdc(void)
{
EALLOW;
SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1; // Enable ADC clock
// Reset ADC to default state
AdcRegs.ADCTRL1.all = 0x00E0; // Default settings
AdcRegs.ADCTRL2.all = 0x0000; // Default settings
AdcRegs.ADCTRL3.all = 0x0000; // Default settings
// Configure ADC
AdcRegs.ADCTRL1.bit.ACQ_PS = 6; // Set acquisition window size
AdcRegs.ADCTRL1.bit.SEQ_CASC = 0; // Disable sequence cascading (single conversion mode)
AdcRegs.ADCTRL1.bit.CONT_RUN = 0; // Stop after one sequence
// Select ADC-B0 for SOC0 (Start-of-Conversion)
AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 8; // SOC0 -> ADC-B0
// Set ADC clock prescaler
AdcRegs.ADCTRL3.bit.ADCCLKPS = 2; // Set ADC clock prescaler
EDIS;
}
//
// Function to read ADC result from SOC0
//
Uint16 ReadAdcResult(void)
{
// Reset SEQ1 to ensure the sequence starts from SOC0
AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1; // Reset SEQ1 for fresh conversion
// Start conversion on SOC0 (which is mapped to ADC-B0)
AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1; // Trigger SOC0
// Wait for conversion to complete
while (AdcRegs.ADCST.bit.INT_SEQ1 == 0); // Wait until conversion is done
// Clear interrupt flag for the next conversion
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;
// Read the result from ADCRESULT8 (corresponding to ADC-B0)
return AdcRegs.ADCRESULT8;
}