student92
Newbie level 3
Hello,
I found a program made by https://azimsgarage.blogspot.com and I did some changes.
I am going to control gate of N-MOSFET. I have two ADC values from potentiometers. First to control the duty cycle and second for variable frequency.
The program works really good with constant frequency and I can control the duty cycle from 0-100%.
When I try to change both duty cycle and frequency when I get frequency about 20 kHz the duty cycle can be changed only from 0 to 11% and next it works as 100% duty cycle and I can't even measure the frequency. I was trying with constant duty cycle and variable frequency but it works the same way.
Someone told me that it's not working right way because of CLKDIV and HSPCLKDIV but I was changing it and it doesn't works for me.
Here's the code:
Does anybody know what's wrong with it?
I found a program made by https://azimsgarage.blogspot.com and I did some changes.
I am going to control gate of N-MOSFET. I have two ADC values from potentiometers. First to control the duty cycle and second for variable frequency.
The program works really good with constant frequency and I can control the duty cycle from 0-100%.
When I try to change both duty cycle and frequency when I get frequency about 20 kHz the duty cycle can be changed only from 0 to 11% and next it works as 100% duty cycle and I can't even measure the frequency. I was trying with constant duty cycle and variable frequency but it works the same way.
Someone told me that it's not working right way because of CLKDIV and HSPCLKDIV but I was changing it and it doesn't works for me.
Here's the code:
Code:
#include "DSP28x_Project.h"
#include "f2802x_common/include/adc.h"
#include "f2802x_common/include/clk.h"
#include "f2802x_common/include/flash.h"
#include "f2802x_common/include/gpio.h"
#include "f2802x_common/include/pie.h"
#include "f2802x_common/include/pll.h"
#include "f2802x_common/include/timer.h"
#include "f2802x_common/include/wdog.h"
#include "f2802x_common/include/pwr.h"
#include "f2802x_common/include/pwm.h"
void pwm_Init_();
unsigned int TBPRD = 0;
unsigned int CMPA=0; //32500;
ADC_Handle myAdc;
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
TIMER_Handle myTimer;
CPU_Handle myCpu;
PLL_Handle myPll;
WDOG_Handle myWDog;
PWM_Handle myPwm3;
PWR_Handle myPwr;
uint16_t Digital_Result =0;
uint16_t Digital_Result1 =0;
void globaldisable();
void globalenable();
void ADC_INIT_Fn();
void ADC_SETUP_Fn();
void set_duty(int a);
void set_duty(int b);
int adcresult=2048;
int freqresult=0;
interrupt void adc_isr(void)
{
Digital_Result = ADC_readResult(myAdc, ADC_ResultNumber_0);
Digital_Result1 = ADC_readResult(myAdc, ADC_ResultNumber_2);
adcresult =Digital_Result;
freqresult =Digital_Result1;
set_freq(freqresult);
set_duty(adcresult);
ADC_clearIntFlag(myAdc, ADC_IntNumber_1); // Clear ADCINT1 flag reinitialize for next SOC
PIE_clearInt(myPie, PIE_GroupNumber_10);// Acknowledge interrupt to PIE
return;
}
void main(void)
{
myAdc = ADC_init((void *)ADC_BASE_ADDR, sizeof(ADC_Obj));
myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
myCpu = CPU_init((void *)NULL, sizeof(CPU_Obj));
myFlash = FLASH_init((void *)FLASH_BASE_ADDR, sizeof(FLASH_Obj));
myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));
myPie = PIE_init((void *)PIE_BASE_ADDR, sizeof(PIE_Obj));
myPll = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));
myTimer = TIMER_init((void *)TIMER0_BASE_ADDR, sizeof(TIMER_Obj));
myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));
myPwm3 = PWM_init((void *)PWM_ePWM3_BASE_ADDR, sizeof(PWM_Obj));
myPwr = PWR_init((void *)PWR_BASE_ADDR, sizeof(PWR_Obj));
globaldisable();
// Perform basic system initialization
WDOG_disable(myWDog);
CLK_enableAdcClock(myClk);
CLK_setOscSrc(myClk, CLK_OscSrc_Internal); //Select the internal oscillator 1 as the clock source
PLL_setup(myPll, PLL_Multiplier_10, PLL_DivideSelect_ClkIn_by_2); // Setup the PLL for x10 /2 which will yield 50Mhz = 10Mhz * 10 / 2
globalenable();
ADC_INIT_Fn();
ADC_SETUP_Fn();
GPIO_setMode(myGpio, GPIO_Number_4, GPIO_4_Mode_EPWM3A);
//set_freq(10);
//set_dutycycle(75);
CLK_disableTbClockSync(myClk);
pwm_Init_();
//
CLK_enableTbClockSync(myClk);
while(1)
{
ADC_forceConversion(myAdc, ADC_SocNumber_0);// Wait for ADC interrupt
ADC_forceConversion(myAdc, ADC_SocNumber_2);// Wait for ADC interrupt
}
}
void globaldisable()
{
// Disable the PIE and all interrupts
PIE_disable(myPie);
PIE_disableAllInts(myPie);
CPU_disableGlobalInts(myCpu);
CPU_clearIntFlags(myCpu);
}
void globalenable()
{
PIE_enable(myPie);
// Register interrupt handlers in the PIE vector table
CPU_enableInt(myCpu, CPU_IntNumber_10); // Enable CPU Interrupt 1
CPU_enableGlobalInts(myCpu); // Enable Global interrupt INTM
CPU_enableDebugInt(myCpu); // Enable Global realtime interrupt DBGM
// Enable XINT1 in the PIE: Group 1 interrupt 4 & 5
// Enable INT1 which is connected to WAKEINT
PIE_enableInt(myPie, PIE_GroupNumber_1, PIE_InterruptSource_XINT_1);
CPU_enableInt(myCpu, CPU_IntNumber_1);
// GPIO0 is XINT1, GPIO1 is XINT2
GPIO_setExtInt(myGpio, GPIO_Number_12, CPU_ExtIntNumber_1);
// Configure XINT1
PIE_setExtIntPolarity(myPie, CPU_ExtIntNumber_1, PIE_ExtIntPolarity_RisingEdge);
// Enable XINT1 and XINT2
PIE_enableExtInt(myPie, CPU_ExtIntNumber_1);
}
void ADC_INIT_Fn()
{
ADC_enableBandGap(myAdc);
ADC_enableRefBuffers(myAdc);
ADC_powerUp(myAdc);
ADC_enable(myAdc);
ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);
}
void ADC_SETUP_Fn()
{
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_10, PIE_SubGroupNumber_1, (intVec_t)&adc_isr);
PIE_enableAdcInt(myPie, ADC_IntNumber_1); // Enable ADCINT1 in PIE
//Note: Channel ADCINA1 will be double sampled to workaround the ADC 1st sample issue for rev0 silicon errata
ADC_setIntPulseGenMode(myAdc, ADC_IntPulseGenMode_Prior); //ADCINT1 trips after AdcResults latch
ADC_enableInt(myAdc, ADC_IntNumber_1); //Enabled ADCINT1
ADC_setIntMode(myAdc, ADC_IntNumber_1, ADC_IntMode_ClearFlag); //Disable ADCINT1 Continuous mode
ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC0); //setup EOC0 to trigger ADCINT1 to fire
ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A4); //set SOC0 channel select to ADCINA4
ADC_setSocChanNumber (myAdc, ADC_SocNumber_2, ADC_SocChanNumber_A2);
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_0, ADC_SocTrigSrc_Sw); //set SOC0 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_7_cycles); //set SOC0 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1)
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_2, ADC_SocTrigSrc_Sw); //set SOC0 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_2, ADC_SocSampleWindow_7_cycles); //set SOC0 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1)
}
void pwm_Init_()
{
//EPwm3Regs.TBCTL.bit.CLKDIV = 0;
//EPwm3Regs.TBCTL.bit.HSPCLKDIV = 1;
CLK_enablePwmClock(myClk, PWM_Number_3);
// Setup TBCLK
PWM_setPeriod(myPwm3, TBPRD); // Set timer period 801 TBCLKs
PWM_setPhase(myPwm3, 0x0000); // Phase is 0
PWM_setCount(myPwm3, 0x0000); // Clear counter
// Set Compare values
//CMPA = (TBPRD/100)*adcresult;
// interrupt void adc_isr(void);
// CMPA = adcresult;
//PWM_setCmpA(myPwm3, CMPA); // Set compare A value
// set_duty();
// Setup counter mode
PWM_setCounterMode(myPwm3, PWM_CounterMode_UpDown); // Count up and down
PWM_setIntMode(myPwm3, PWM_IntMode_CounterEqualZero);
PWM_disableCounterLoad(myPwm3); // Disable phase loading
PWM_setHighSpeedClkDiv(myPwm3, PWM_HspClkDiv_by_10); // Clock ratio to SYSCLKOUT
PWM_setClkDiv(myPwm3, PWM_ClkDiv_by_1);
// Setup shadowing
PWM_setShadowMode_CmpA(myPwm3, PWM_ShadowMode_Shadow);
PWM_setLoadMode_CmpA(myPwm3, PWM_LoadMode_Zero);
// Set actions
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm3, PWM_ActionQual_Clear); // Set PWM1A on event A, up count
PWM_setActionQual_CntDown_CmpA_PwmA(myPwm3, PWM_ActionQual_Set); // Clear PWM1A on event A, down count
}
void set_duty( int a)
{
CMPA = a;
PWM_setCmpA(myPwm3, CMPA); // Set compare A value
}
void set_freq( int b)
{
if(freqresult<=100)
{
b=99;
}
TBPRD = b;
PWM_setPeriod(myPwm3, TBPRD);
}