i am trying to make an three phase inverter with pwm. but i am facing a problen to control the frecuency of de sine.
the problem is that the ADC i configure isn't working properly. i am using a potentiometer entering values from 0 to 3.3V and the ADC isn't showing me the rigth numbers(0 to 4096, since is 12 bits), instead is showing me number only between 2000 and 2100.
i really don't get it
this is my complete code. everething is working, except the interpretation of the adc reading(lectura() function).
i can't find my mistake.
#include "driverlib/pin_map.h"
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_gpio.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/adc.h"
void delayMS(unsigned long ms) {
SysCtlDelay( (SysCtlClockGet()/(27*10000))*ms );//lista tiene 90, son 90 delays por ciclo, asi contrarestar y time está en mS
}
uint32_t ADCValues[1];
int time = 200;
}
void lectura(){
ADCProcessorTrigger(ADC0_BASE, 3);// Trigger the ADC conversion.
while(!ADCIntStatus(ADC0_BASE, 3, false)){}// Wait for conversion to be completed.
ADCIntClear(ADC0_BASE, 3);// Clear the ADC interrupt flag.
ADCSequenceDataGet(ADC0_BASE, 3, ADCValues);// Read ADC Value.
//time = (float)ADCValues[0];
if(ADCValues[0]<2000){time = 200;}
else{time = 1000;}
}
int main(void)
{
int sn[90] = {0,8,17,26,35,44,53,61,70,78,87,95,103,111,119,127,135,142,149,156,163,170,177,183,189,195,200,206,211,216,220,225,229,232,236,239,242,245,247,249,251,252,253,254,254,255,254,254,253,252,251,249,247,245,242,239,236,232,229,225,220,216,211,206,200,195,189,183,177,170,163,156,149,142,135,127,119,111,103,95,87,78,70,61,53,44,35,26,17,8};
int a = 0;
int b = 30;
int c = 60;
//Set the clock
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
//Configure PWM Clock to match system
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
// Enable the peripherals used by this program.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); //The Tiva Launchpad has two modules (0 and 1). Module 1 covers the LED pins
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4); // make F4 an input
GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // enable F4's pullup, the drive strength won't affect the input
//Configure PF1,PF2,PF3 Pins as PWM
GPIOPinConfigure(GPIO_PF1_M1PWM5);
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
//ADC
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);
ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 3);
ADCIntClear(ADC0_BASE, 3);
//Configure PWM Options
//PWM_GEN_2 Covers M1PWM4 and M1PWM5
//PWM_GEN_3 Covers M1PWM6 and M1PWM7 See page 207 4/11/13 DriverLib doc
PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
//Set the Period (expressed in clock ticks)
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, 255);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, 255);
//Set PWM duty-50% (Period /2)
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,sn[a]);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,sn[b]);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,sn[c]);
// Enable the PWM generator
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
//PWMGenIntClear(PWM1_BASE,PWM_GEN_2,PWM_INT_CNT_ZERO);
//PWMGenIntClear(PWM1_BASE,PWM_GEN_3,PWM_INT_CNT_ZERO);
// Turn on the Output pins
PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT | PWM_OUT_6_BIT | PWM_OUT_7_BIT, true);
while(1)
{
lectura();
uint32_t sentido=0; // variable to hold the pinRead
sentido= GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4); // read F4
if((sentido & GPIO_PIN_4)==0)
{
delayMS(time);
if(a==90)a = 0;
if(b==90)b = 0;
if(c==90)c = 0;
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,sn[a]);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,sn[b]);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,sn[c]);
a = a+1;
b = b+1;
c = c+1;
}
else{
delayMS(time);
if(a==90)a = 0;
if(b==90)b = 0;
if(c==90)c = 0;
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,sn[a]);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,sn[c]);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,sn[b]);
a = a+1;
b = b+1;
c = c+1;
}
}
thanks,
Pedro A