Part Number: TM4C1294NCPDT
Dear support,
I'm trying to read 4 ADC signals with a potentiometer and generate a PWM to test it.
I've followed the following code: e2e.ti.com/.../374763
Although I'm connecting different Ports physically, the value is always changed in the ui32adcValues[0]. Maybe I didn't get or understand the code correctly.
//*****************************************************************************
// Libraries
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "driverlib/pwm.h"
#include "drivers/pinout.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ints.h" // Added for the Timer
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_adc.h"
#include "inc/hw_timer.h"
#include "inc/hw_nvic.h"
#include "inc/hw_sysctl.h"
#include "utils/uartstdio.h"
//*****************************************************************************
// Variable Declaration
//*****************************************************************************
// ADC
uint32_t ui32adcValues[4],ui32Count,ui32adc0Values[1],ui32adc1Values[1],ui32adc2Values[1],ui32adc3Values[1];
// PWM
long analogValue = 0, analogAvg = 0, adcToPwmRatio = 0;
uint32_t sumCounter = 0, iSamples = 10000, pwmValue = 0;
uint32_t pwmMax = 24000; // 120 MHz / 5 kHz
// System
uint32_t g_ui32SysClock; // System clock rate in Hz.
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
// ADC Initialization
//*****************************************************************************
void
ConfigureADC(void)
{
SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralReset(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlDelay(10);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3 | GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0 );
ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_EIGHTH, 30);
// Choose Sequencer 2 and set it at the highest Priority.
ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_ALWAYS, 0);
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_ALWAYS, 1);
ADCSequenceConfigure(ADC0_BASE, 2, ADC_TRIGGER_ALWAYS, 2);
ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_ALWAYS, 3);
// Choose Step 0 in Sequencer 2 as Data Buffer, set it as last Step and enable Interrupts
ADCSequenceStepConfigure(ADC0_BASE,0,0, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE,0,1, ADC_CTL_CH1);
ADCSequenceStepConfigure(ADC0_BASE,0,2, ADC_CTL_CH2);
ADCSequenceStepConfigure(ADC0_BASE,0,3, ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 0);
ADCSequenceEnable(ADC0_BASE, 1);
ADCSequenceEnable(ADC0_BASE, 2);
ADCSequenceEnable(ADC0_BASE, 3);
}
//*****************************************************************************
// PWM Initialization - 13.09.2020
//*****************************************************************************
void Init_PWM(){
// 1.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // Enable Port F
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF));
// 2.
SysCtlPWMClockSet(SYSCTL_PWMDIV_16);
SysCtlPeripheralDisable(SYSCTL_PERIPH_PWM0);
SysCtlPeripheralReset(SYSCTL_PERIPH_PWM0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_PWM0)));
// PWM0_BASE: 0 -> 0 from M_0_PWM1
// PWM_OUT_1 and PWM_OUT_1_BIT: 1 from M0PWM_1
// PWM_GEN_0: GEN_0 for PWM0 and PWM1, GEN_1 for PWM2 and PWM3...
SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // 120 MHz / 1 = 120 MHz
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1); // Pin 1 from PortF - PF1
GPIOPinConfigure(GPIO_PF1_M0PWM1);
PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, pwmMax); // 120 MHz / pwmMax = pwmPeriod
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 1);
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true);
PWMIntEnable(PWM0_BASE, PWM_INT_GEN_0);
IntMasterEnable();
PWMGenIntTrigEnable(PWM0_BASE, PWM_GEN_0, PWM_TR_CNT_ZERO);
IntEnable(INT_PWM0_0);
}
//*****************************************************************************
// Wait and Read ADC Functions
//*****************************************************************************
uint32_t
WaitAndReadADC(uint32_t *adcValues)
{
ADCProcessorTrigger(ADC0_BASE,0);
// Wait until the sample sequence has completed.
while(!ADCIntStatus(ADC0_BASE, 0, false)) { }
// Read the value from the ADC.
return(ADCSequenceDataGet(ADC0_BASE, 0, adcValues));
}
uint32_t
WaitAndReadADC1(uint32_t *adcValues)
{
ADCProcessorTrigger(ADC0_BASE,1);
// Wait until the sample sequence has completed.
while(!ADCIntStatus(ADC0_BASE, 1, false)) { }
// Read the value from the ADC.
return(ADCSequenceDataGet(ADC0_BASE, 1, adcValues));
}
uint32_t
WaitAndReadADC2(uint32_t *adcValues)
{
ADCProcessorTrigger(ADC0_BASE,2);
// Wait until the sample sequence has completed.
while(!ADCIntStatus(ADC0_BASE, 2, false)) { }
// Read the value from the ADC.
return(ADCSequenceDataGet(ADC0_BASE, 2, adcValues));
}
uint32_t
WaitAndReadADC3(uint32_t *adcValues)
{
ADCProcessorTrigger(ADC0_BASE,3);
// Wait until the sample sequence has completed.
while(!ADCIntStatus(ADC0_BASE, 3, false)) { }
// Read the value from the ADC.
return(ADCSequenceDataGet(ADC0_BASE, 3, adcValues));
}
int
main(void)
{
//
// Run from the PLL at 120 MHz.
//
g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// Configure the device pins.
//
PinoutSet(false, false);
//
// Enable the GPIO pins for the LED D1 (PN1).
//
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1);
//
// Initialize the UART.
//
ConfigureADC();
Init_PWM();
adcToPwmRatio = pwmMax/4095; // 2^12 bits
// Infinite loop
while(1)
{
//ui32Count = WaitAndReadADC1(ui32adcValues);
//ui32Count = WaitAndReadADC2(ui32adcValues);
//ui32Count = WaitAndReadADC3(ui32adcValues);
ui32Count = WaitAndReadADC(ui32adcValues);
// Sum filter
if (sumCounter < iSamples) {
analogValue = ui32adcValues[0] + analogValue;
sumCounter++;
} else {
analogAvg = analogValue / iSamples;
// PWM
pwmValue = analogAvg*adcToPwmRatio;
// PWM Min Limit
if (pwmValue < 30) {
pwmValue = 1; // pwmValue can never be zero
}
// PWM Max Limit
if (pwmValue > (pwmMax - 30)) {
pwmValue = pwmMax;
}
// Change PWM Value
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, pwmValue);
// Reset Sum Filter Variables
analogValue = 0;
sumCounter = 0;
}
}
}
Could you please provide some help?
Best regards,
Gustavo Wegher
