This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28335: control the motor according to the temperature change, timer interrupt problem

Part Number: TMS320F28335

Tool/software:

Hi, team.

I want to control the motor according to the temperature change.

The code we are currently working on is as follows.(delay function)

I am using Delay function now, but when I use the timer interrupt function, there is no error, but it does not work.

-------

#include "DSP2833x_Device.h" // DSP2833x header file

// function
void Gpio_select(void); // GPIO initialization function
void InitSystem(void); // System initialization function
void InitPWM(void); // PWM initialization function
void SetMotorSpeed(float duty); // motor speed setting function
void delay_ms(unsigned long ms); // delay function
void setupADC(void); // ADC setup function
float readTemperature(void); // temperature measurement function
float lowPassFilter(float newValue); // low-pass filter function

// defining a constant
#define AQ_SET 2
#define AQ_CLEAR 1
#define PWM_PIN 0
#define IN1_PIN 1
#define IN2_PIN 2
#define VREF 3.3
#define ADC_MAX_VALUE 4096
#define TEMP_THRESHOLD_LOW 40.0
#define TEMP_THRESHOLD_HIGH 80.0
#define FILTER_SIZE 10

// a global variablech
float filterBuffer[FILTER_SIZE] = {0};
int filterIndex = 0;
float temperature; // Global variables that store temperature values

void main(void)
{
InitSystem(); // system initialization
Gpio_select(); // GPIO initialization
InitPWM(); // PWM initialization
setupADC(); // ADC setting

// Initial settings
SetMotorSpeed(0.1); // motor speed 50%
GpioDataRegs.GPASET.bit.GPIO1 = 1; // Forward rotation
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
GpioDataRegs.GPASET.bit.GPIO0 = 1; // Enable pin

while (1)
{
// Temperature measurement and filter application
temperature = readTemperature(); // Store values in global variables

// LED status control and motor control depending on temperature
if (temperature <= TEMP_THRESHOLD_LOW)
{
// Turn on the green LED and rotate the motor at 50% speed in the forward directionGpioDataRegs.GPASET.bit.GPIO29 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO30 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO31 = 1;
SetMotorSpeed(0.1);
GpioDataRegs.GPASET.bit.GPIO1 = 1; // forward rotation
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
}
else if (temperature > TEMP_THRESHOLD_LOW && temperature <= TEMP_THRESHOLD_HIGH)
{
// Turn on the yellow LED and rotate the motor at low speed

GpioDataRegs.GPACLEAR.bit.GPIO29 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO30 = 1;
GpioDataRegs.GPASET.bit.GPIO31 = 1;
SetMotorSpeed(0.1); // 속도 30%
GpioDataRegs.GPASET.bit.GPIO1 = 1; // forward rotation
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
}
else
{
// turn on the red LED and stop motor
GpioDataRegs.GPACLEAR.bit.GPIO29 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO31 = 1;
GpioDataRegs.GPASET.bit.GPIO30 = 1;
SetMotorSpeed(0.0); // 모터 정지
GpioDataRegs.GPACLEAR.bit.GPIO1 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
}

delay_ms(1000); // wait 1 second
}
}

// GPIO initialization function
void Gpio_select(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX1.all = 0;
GpioCtrlRegs.GPAMUX2.all = 0;
GpioCtrlRegs.GPADIR.all = 0;
GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; // control signal 1

GpioCtrlRegs.GPADIR.bit.GPIO2 = 1; // control signal 2
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // PWM pin
GpioCtrlRegs.GPADIR.bit.GPIO29 = 1; // green LED
GpioCtrlRegs.GPADIR.bit.GPIO30 = 1; // red LED
GpioCtrlRegs.GPADIR.bit.GPIO31 = 1; // yellow LED
EDIS;
}

// system initialization function
void InitSystem(void)
{
EALLOW;
SysCtrlRegs.WDCR = 0x0068;
SysCtrlRegs.PLLSTS.bit.DIVSEL = 2;
SysCtrlRegs.PLLCR.bit.DIV = 10;
SysCtrlRegs.HISPCP.all = 0x0001;
SysCtrlRegs.LOSPCP.all = 0x0002;
SysCtrlRegs.PCLKCR0.all = 0x0000;
SysCtrlRegs.PCLKCR1.all = 0x0000;
SysCtrlRegs.PCLKCR3.all = 0x0000;
SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;
EDIS;
}

// PWM initialization function
void InitPWM(void)
{
EALLOW;
EPwm1Regs.TBPRD = 1500;
EPwm1Regs.TBCTL.bit.CTRMODE = 0;
EPwm1Regs.AQCTLA.bit.ZRO = AQ_CLEAR;
EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;
EDIS;
}

// moror speed setup function
void SetMotorSpeed(float duty)
{
if (duty < 0.0) duty = 0.0;
if (duty > 1.0) duty = 1.0;
EPwm1Regs.CMPA.half.CMPA = (Uint16)(1500 * duty);
}

// delay function
void delay_ms(unsigned long ms)
{
unsigned long i;
for (i = 0; i < ms * 1500; i++)
{
asm(" NOP");
}
}

// ADC setup function
void setupADC(void)
{
extern void InitAdc(void);
InitAdc();
EALLOW;
AdcRegs.ADCTRL1.bit.ACQ_PS = 6;
AdcRegs.ADCTRL3.bit.ADCCLKPS = 11;
AdcRegs.ADCTRL1.bit.SEQ_CASC = 1;
AdcRegs.ADCTRL1.bit.CONT_RUN = 1;
EDIS;
}

// Temperature measurement function

float readTemperature(void)
{
Uint16 adcValue;
float voltage;

AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 7;
AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1;
while (AdcRegs.ADCST.bit.INT_SEQ1 == 0);
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;
adcValue = AdcRegs.ADCRESULT0 >> 4;

voltage = (adcValue * VREF) / ADC_MAX_VALUE;

return lowPassFilter((voltage - 0.5) * 100);
}

// low-pass filter function
float lowPassFilter(float newValue)
{
filterBuffer[filterIndex] = newValue;
filterIndex = (filterIndex + 1) % FILTER_SIZE;

float sum = 0.0;
int i;
for (i = 0; i < FILTER_SIZE; i++)
{
sum += filterBuffer[i];
}

return sum / FILTER_SIZE;
}

------------

and, The following code is applied with timer interrupt.

-------------

#include "DSP2833x_Device.h" // DSP2833x header file
#include "DSP2833x_Examples.h" // DSP2833x examples header file

// function
void Gpio_select(void); // GPIO initialization function
void InitSystem(void); // system initialization function
void InitPWM(void); // PWM initialization function
void SetMotorSpeed(float duty); // motor speed setup function
void setupADC(void); // ADC setup function
float readTemperature(void); // temperature function
float lowPassFilter(float newValue); // low-pass filter function
void InitTimer1(void); // timer 1 initialization function
interrupt void cpu_timer1_isr(void); // Timer1 interrupt handler function

// constant
#define AQ_SET 2
#define AQ_CLEAR 1
#define PWM_PIN 0
#define IN1_PIN 1
#define IN2_PIN 2
#define VREF 3.3
#define ADC_MAX_VALUE 4096
#define TEMP_THRESHOLD_LOW 40.0
#define TEMP_THRESHOLD_HIGH 80.0
#define FILTER_SIZE 10

// 
float filterBuffer[FILTER_SIZE] = {0};
int filterIndex = 0;
float temperature; // temperature

void main(void)
{
InitSystem(); // system initialization function
Gpio_select(); // GPIO initialization
InitPWM(); // PWM initailization
setupADC(); // ADC setup
InitTimer1(); // timer 1 initialization

// initial setup
SetMotorSpeed(0.1); // motor speed 10%
GpioDataRegs.GPASET.bit.GPIO1 = 1; // forward rotation
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
GpioDataRegs.GPASET.bit.GPIO0 = 1; // Enable pin

while (1)
{
// 
}
}

// GPIO initialization function
void Gpio_select(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX1.all = 0;
GpioCtrlRegs.GPAMUX2.all = 0;
GpioCtrlRegs.GPADIR.all = 0;
GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; // control signal 1
GpioCtrlRegs.GPADIR.bit.GPIO2 = 1; // control signal 2
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // PWM pin
GpioCtrlRegs.GPADIR.bit.GPIO29 = 1; // green LED
GpioCtrlRegs.GPADIR.bit.GPIO30 = 1; // red LED
GpioCtrlRegs.GPADIR.bit.GPIO31 = 1; // yellow LED
EDIS;
}

// system initialization function
void InitSystem(void)
{
EALLOW;
SysCtrlRegs.WDCR = 0x0068;
SysCtrlRegs.PLLSTS.bit.DIVSEL = 2;
SysCtrlRegs.PLLCR.bit.DIV = 10;
SysCtrlRegs.HISPCP.all = 0x0001;
SysCtrlRegs.LOSPCP.all = 0x0002;
SysCtrlRegs.PCLKCR0.all = 0x0000;
SysCtrlRegs.PCLKCR1.all = 0x0000;
SysCtrlRegs.PCLKCR3.all = 0x0000;
SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;
EDIS;
}

// PWM initialization function
void InitPWM(void)
{
EALLOW;
EPwm1Regs.TBPRD = 1500;
EPwm1Regs.TBCTL.bit.CTRMODE = 0;
EPwm1Regs.AQCTLA.bit.ZRO = AQ_CLEAR;
EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;
EDIS;
}

// motor speed setup function
void SetMotorSpeed(float duty)
{
if (duty < 0.0) duty = 0.0;
if (duty > 1.0) duty = 1.0;
EPwm1Regs.CMPA.half.CMPA = (Uint16)(1500 * duty);
}

// ADC setup function
void setupADC(void)
{
extern void InitAdc(void);
InitAdc();
EALLOW;
AdcRegs.ADCTRL1.bit.ACQ_PS = 6;
AdcRegs.ADCTRL3.bit.ADCCLKPS = 11;
AdcRegs.ADCTRL1.bit.SEQ_CASC = 1;
AdcRegs.ADCTRL1.bit.CONT_RUN = 1;
EDIS;
}

// temperature
float readTemperature(void)
{
Uint16 adcValue;
float voltage;

AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 7;
AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1;
while (AdcRegs.ADCST.bit.INT_SEQ1 == 0);
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;
adcValue = AdcRegs.ADCRESULT0 >> 4;

voltage = (adcValue * VREF) / ADC_MAX_VALUE;

return lowPassFilter((voltage - 0.5) * 100);
}

// low-pass filter function
float lowPassFilter(float newValue)
{
filterBuffer[filterIndex] = newValue;
filterIndex = (filterIndex + 1) % FILTER_SIZE;

float sum = 0.0;
int i;
for (i = 0; i < FILTER_SIZE; i++)
{
sum += filterBuffer[i];
}

return sum / FILTER_SIZE;
}

// timer 1 initialization function
void InitTimer1(void)
{
EALLOW;
CpuTimer1Regs.PRD.all = 0x927C00; // 1second
CpuTimer1Regs.TCR.bit.TSS = 1; // stop timer
CpuTimer1Regs.TCR.bit.TRB = 1; // reload timer
CpuTimer1Regs.TCR.bit.TIE = 1; // timer interrupt
CpuTimer1Regs.TCR.bit.TSS = 0; // start timer interrupt

EDIS;
PieCtrlRegs.PIEIER3.bit.INTx7 = 1; // timer interrupt1
IER |= M_INT3; // CPU interrupt 3
EINT; // global interrupt
}

// timer 1 interrupt handler
interrupt void cpu_timer1_isr(void)
{
// temperature and filter
temperature = readTemperature();

// LED status control and motor control depending on temperature
if (temperature <= TEMP_THRESHOLD_LOW)
{
// turn on green LED and Turn the motor forward at 10% speed
GpioDataRegs.GPASET.bit.GPIO29 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO30 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO31 = 1;
SetMotorSpeed(0.1);
GpioDataRegs.GPASET.bit.GPIO1 = 1; // forward rotation
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
}
else if (temperature > TEMP_THRESHOLD_LOW && temperature <= TEMP_THRESHOLD_HIGH)
{
// turn on yellow LED and rotate the motor at low speed
GpioDataRegs.GPACLEAR.bit.GPIO29 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO30 = 1;
GpioDataRegs.GPASET.bit.GPIO31 = 1;
SetMotorSpeed(0.1); // speed 10%
GpioDataRegs.GPASET.bit.GPIO1 = 1; // forward rotation
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
}
else
{
// turn on red LED and stop motor
GpioDataRegs.GPACLEAR.bit.GPIO29 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO31 = 1;
GpioDataRegs.GPASET.bit.GPIO30 = 1;
SetMotorSpeed(0.0); // stop motor
GpioDataRegs.GPACLEAR.bit.GPIO1 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;
}

// timer interrupt clear
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}


-------------

when I debug this code, there is no error but is wasn't work.
can I get advise?
Thank you.