TI中文支持网
TI专业的中文技术问题搜集分享网站

TMS320F280049: ADC采样,采集3.3V电压,寄存器中的值为2048,采样其他的电压值,寄存器中的值都只有4096下的一半

Part Number:TMS320F280049

//
#include "driverlib.h"
#include "device.h"
#include "math.h"
//
// Defines
//
#define RESULTS_BUFFER_SIZE 256

//
// Globals
//
uint16_t adcAResultsA0[RESULTS_BUFFER_SIZE]; // Buffer for results
uint16_t adcAResultsA1[RESULTS_BUFFER_SIZE];
uint16_t adcAResultsB4[RESULTS_BUFFER_SIZE];
uint16_t index; // Index into result buffer
uint16_t indexa1; // Index into result buffer
uint16_t indexb4; // Index into result buffer

volatile uint16_t bufferFull; // Flag to indicate buffer is full

//
// Function Prototypes
//
void initADC(void);
void initEPWM(void);
void initADCSOC(void);
__interrupt void adcA1ISR(void);

int duty=0;
int y=0;
double z=0;
double i=0;
int j=0;
double x=0;
double sin_value=0;

//
// Main
//
void main(void)
{
// Initialize device clock and peripherals
Device_init();
// Disable pin locks and enable internal pullups.
Device_initGPIO();
// Initialize PIE and clear PIE registers. Disables CPU interrupts.
Interrupt_initModule();
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
Interrupt_initVectorTable();
// Interrupts that are used in this example are re-mapped to ISR functions
// found within this file.
Interrupt_register(INT_ADCA1, &adcA1ISR);
// Set up the ADC and the ePWM and initialize the SOC
initADC();
initEPWM();
initADCSOC();
// Initialize results buffer
for(index = 0; index < RESULTS_BUFFER_SIZE; index++)
{
adcAResultsA0[index] = 0;
}
index = 0;
bufferFull = 0;
// Enable ADC interrupt
Interrupt_enable(INT_ADCA1);
// Enable Global Interrupt (INTM) and realtime interrupt (DBGM)
EINT;
ERTM;
// Loop indefinitely
GPIO_writePin(23U,1);
while(1)
{
// Start ePWM1, enabling SOCA and putting the counter in up-count mode

//PWM
x=0;
for(j=0;j<=333;j++)
{
sin_value=sinf(i);
x=x+0.005988;
i=x*M_PI;
z=(0.3*sin_value+0.5)*3330;
y=ceil(z);
duty=3340-y;//加到正以上 多加12 duty越小 pwm越大 duty值应该增大
if(duty>=3250)
{
duty=duty-40;
}
EPWM_setCounterCompareValue(EPWM1_BASE, EPWM_COUNTER_COMPARE_A,duty);//设置比较值
EPWM_setCounterCompareValue(EPWM2_BASE, EPWM_COUNTER_COMPARE_A,duty);
EPWM_setCounterCompareValue(EPWM1_BASE, EPWM_COUNTER_COMPARE_B,duty);
EPWM_setCounterCompareValue(EPWM2_BASE, EPWM_COUNTER_COMPARE_B,duty);

EPWM_enableADCTrigger(EPWM1_BASE, EPWM_SOC_A);
// EPWM_setTimeBaseCounterMode(EPWM1_BASE, EPWM_COUNTER_MODE_UP_DOWN);
// Wait while ePWM1 causes ADC conversions which then cause interrupts.
// When the results buffer is filled, the bufferFull flag will be set.
while(bufferFull == 0)
{
}
bufferFull = 0; // Clear the buffer full flag
// Stop ePWM1, disabling SOCA and freezing the counter
// EPWM_disableADCTrigger(EPWM1_BASE, EPWM_SOC_A);
// EPWM_setTimeBaseCounterMode(EPWM1_BASE, EPWM_COUNTER_MODE_STOP_FREEZE);
// Software breakpoint. At this point, conversion results are stored in
// adcAResults.
// Hit run again to get updated conversions.
//
// ESTOP0;
}
}
}
//
// Function to configure and power up ADCA.
void initADC(void)
{
// Enable internal reference on ADCs
ADC_setVREF(ADCA_BASE, ADC_REFERENCE_INTERNAL, ADC_REFERENCE_3_3V);
ADC_setVREF(ADCB_BASE, ADC_REFERENCE_INTERNAL, ADC_REFERENCE_3_3V);
ADC_setVREF(ADCC_BASE, ADC_REFERENCE_INTERNAL, ADC_REFERENCE_3_3V);
// Set ADCCLK divider to /2
ADC_setPrescaler(ADCA_BASE, ADC_CLK_DIV_2_0);
ADC_setPrescaler(ADCB_BASE, ADC_CLK_DIV_2_0);
// Set pulse positions to late
ADC_setInterruptPulseMode(ADCA_BASE, ADC_PULSE_END_OF_CONV);
// Power up the ADC and then delay for 1 ms
ADC_enableConverter(ADCA_BASE);
ADC_enableConverter(ADCB_BASE);
DEVICE_DELAY_US(1000);
}

//
// Function to configure ePWM1 to generate the SOC.
//
void initEPWM(void)
{
GPIO_setPadConfig(4U, GPIO_PIN_TYPE_PULLUP);//fault
GPIO_setDirectionMode(4U, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(23U, GPIO_PIN_TYPE_STD);//推挽输出 继电器
GPIO_setDirectionMode(23U, GPIO_DIR_MODE_OUT);

GPIO_setPadConfig(0U, GPIO_PIN_TYPE_STD);//推挽输出
GPIO_setPinConfig(GPIO_0_EPWM1A);
GPIO_setPadConfig(1U, GPIO_PIN_TYPE_STD);
GPIO_setPinConfig(GPIO_1_EPWM1B);
GPIO_setPadConfig(2U, GPIO_PIN_TYPE_STD);//推挽输出
GPIO_setPinConfig(GPIO_2_EPWM2A);
GPIO_setPadConfig(3U, GPIO_PIN_TYPE_STD);
GPIO_setPinConfig(GPIO_3_EPWM2B);
//EPWM1
// Disable SOCA
EPWM_disableADCTrigger(EPWM1_BASE, EPWM_SOC_A);
// Configure the SOC to occur on the first up-count event
EPWM_setADCTriggerSource(EPWM1_BASE, EPWM_SOC_A, EPWM_SOC_TBCTR_PERIOD);
EPWM_setADCTriggerEventPrescale(EPWM1_BASE, EPWM_SOC_A, 1);
// Set the compare A value to 1000 and the period to 1999
// Assuming ePWM clock is 100MHz, this would give 50kHz sampling
// 50MHz ePWM clock would give 25kHz sampling, etc. // The sample rate can also be modulated by changing the ePWM period
// directly (ensure that the compare A value is less than the period).
//
// EPWM_setCounterCompareValue(EPWM1_BASE, EPWM_COUNTER_COMPARE_A, 1000);

EPWM_setTimeBasePeriod(EPWM1_BASE, 3330);
// Set the local ePWM module clock divider to /1
EPWM_setClockPrescaler(EPWM1_BASE,EPWM_CLOCK_DIVIDER_1,EPWM_HSCLOCK_DIVIDER_1);
// Freeze the counter
EPWM_setTimeBaseCounterMode(EPWM1_BASE, EPWM_COUNTER_MODE_UP_DOWN);//EPWM_COUNTER_MODE_STOP_FREEZE
/*
EPMW1和 EPMW2初始相位相同
*/
EPWM_setPhaseShift(EPWM1_BASE, 0U);
EPWM_setTimeBaseCounter(EPWM1_BASE, 0U);
EPWM_disablePhaseShiftLoad(EPWM1_BASE);
EPWM_setSyncOutPulseMode(EPWM1_BASE,EPWM_SYNC_OUT_PULSE_ON_COUNTER_ZERO);
/*
设置死区
*/
EPWM_setDeadBandCounterClock(EPWM1_BASE,EPWM_DB_COUNTER_CLOCK_FULL_CYCLE);//Dead band counter runs at TBCLK rate
EPWM_setRisingEdgeDelayCount(EPWM1_BASE,50U);//500ns死区
EPWM_setFallingEdgeDelayCount(EPWM1_BASE,50U);//
EPWM_setDeadBandDelayMode(EPWM1_BASE,EPWM_DB_RED,true);
EPWM_setDeadBandDelayMode(EPWM1_BASE,EPWM_DB_FED,true);
EPWM_setRisingEdgeDeadBandDelayInput(EPWM1_BASE,EPWM_DB_INPUT_EPWMA);
EPWM_setFallingEdgeDeadBandDelayInput(EPWM1_BASE,EPWM_DB_INPUT_EPWMA);
EPWM_setDeadBandDelayPolarity(EPWM1_BASE,EPWM_DB_FED,
EPWM_DB_POLARITY_ACTIVE_LOW);
EPWM_setDeadBandDelayPolarity(EPWM1_BASE,EPWM_DB_RED,EPWM_DB_POLARITY_ACTIVE_HIGH);
/*
设置动作方向
*/
EPWM_setActionQualifierAction(EPWM1_BASE,
EPWM_AQ_OUTPUT_A,
EPWM_AQ_OUTPUT_HIGH,
EPWM_AQ_OUTPUT_ON_TIMEBASE_UP_CMPA);
EPWM_setActionQualifierAction(EPWM1_BASE,
EPWM_AQ_OUTPUT_A,
EPWM_AQ_OUTPUT_LOW,
EPWM_AQ_OUTPUT_ON_TIMEBASE_DOWN_CMPA);
EPWM_setActionQualifierAction(EPWM1_BASE,
EPWM_AQ_OUTPUT_B,
EPWM_AQ_OUTPUT_HIGH,
EPWM_AQ_OUTPUT_ON_TIMEBASE_UP_CMPB);
EPWM_setActionQualifierAction(EPWM1_BASE,
EPWM_AQ_OUTPUT_B,
EPWM_AQ_OUTPUT_LOW,
EPWM_AQ_OUTPUT_ON_TIMEBASE_DOWN_CMPB);
/////////////////EPWM2
EPWM_setTimeBasePeriod(EPWM2_BASE,3300U);
EPWM_setPhaseShift(EPWM2_BASE,0);//设置初始移相值
EPWM_setTimeBaseCounter(EPWM2_BASE, 0U);
EPWM_setTimeBaseCounterMode(EPWM2_BASE,EPWM_COUNTER_MODE_UP_DOWN);//
EPWM_enablePhaseShiftLoad(EPWM2_BASE);
EPWM_setSyncOutPulseMode(EPWM2_BASE,EPWM_SYNC_OUT_PULSE_ON_EPWMxSYNCIN);
EPWM_setClockPrescaler(EPWM2_BASE,EPWM_CLOCK_DIVIDER_1,EPWM_HSCLOCK_DIVIDER_1);

EPWM_setDeadBandCounterClock(EPWM2_BASE,EPWM_DB_COUNTER_CLOCK_FULL_CYCLE);//Dead band counter runs at TBCLK rate
EPWM_setRisingEdgeDelayCount(EPWM2_BASE,50U);//500ns死区
EPWM_setFallingEdgeDelayCount(EPWM2_BASE,50U);//
EPWM_setDeadBandDelayMode(EPWM2_BASE,EPWM_DB_RED,true);
EPWM_setDeadBandDelayMode(EPWM2_BASE,EPWM_DB_FED,true);
EPWM_setRisingEdgeDeadBandDelayInput(EPWM2_BASE,EPWM_DB_INPUT_EPWMA);
EPWM_setFallingEdgeDeadBandDelayInput(EPWM2_BASE,EPWM_DB_INPUT_EPWMA);
EPWM_setDeadBandDelayPolarity(EPWM2_BASE,EPWM_DB_FED,
EPWM_DB_POLARITY_ACTIVE_LOW);
EPWM_setDeadBandDelayPolarity(EPWM2_BASE,EPWM_DB_RED,
EPWM_DB_POLARITY_ACTIVE_HIGH);
EPWM_setActionQualifierAction(EPWM2_BASE,
EPWM_AQ_OUTPUT_A,
EPWM_AQ_OUTPUT_HIGH,
EPWM_AQ_OUTPUT_ON_TIMEBASE_UP_CMPA);
EPWM_setActionQualifierAction(EPWM2_BASE,
EPWM_AQ_OUTPUT_A,
EPWM_AQ_OUTPUT_LOW,
EPWM_AQ_OUTPUT_ON_TIMEBASE_DOWN_CMPA);
EPWM_setActionQualifierAction(EPWM2_BASE,
EPWM_AQ_OUTPUT_B,
EPWM_AQ_OUTPUT_HIGH,
EPWM_AQ_OUTPUT_ON_TIMEBASE_UP_CMPB);
EPWM_setActionQualifierAction(EPWM2_BASE,
EPWM_AQ_OUTPUT_B,
EPWM_AQ_OUTPUT_LOW,
EPWM_AQ_OUTPUT_ON_TIMEBASE_DOWN_CMPB);

}

// Function to configure ADCA's SOC0 to be triggered by ePWM1.
void initADCSOC(void)
{
// Configure SOC0 of ADCA to convert pin A0. The EPWM1SOCA signal will be
// the trigger.
// – For 12-bit resolution, a sampling window of 7 (80 ns at a 100MHz
// SYSCLK rate) will be used. // – NOTE: A longer sampling window will be required if the ADC driving
// source is less than ideal (an ideal source would be a high bandwidth
// op-amp with a small series resistance). See TI application report
// SPRACT6 for guidance on ADC driver design.
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_EPWM1_SOCA,
ADC_CH_ADCIN0, 8);
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER1, ADC_TRIGGER_EPWM1_SOCA,
ADC_CH_ADCIN1, 8);
ADC_setupSOC(ADCB_BASE, ADC_SOC_NUMBER4, ADC_TRIGGER_EPWM1_SOCA,
ADC_CH_ADCIN4, 8);
// Set SOC0 to set the interrupt 1 flag. Enable the interrupt and make
// sure its flag is cleared.
ADC_setInterruptSource(ADCA_BASE, ADC_INT_NUMBER1, ADC_SOC_NUMBER0);
ADC_enableInterrupt(ADCA_BASE, ADC_INT_NUMBER1);
ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);
}

// ADC A Interrupt 1 ISR
__interrupt void adcA1ISR(void)
{
// Add the latest result to the buffer
adcAResultsA0[index++] = ADC_readResult(ADCARESULT_BASE, ADC_SOC_NUMBER0);
adcAResultsA1[indexa1++] = ADC_readResult(ADCARESULT_BASE, ADC_SOC_NUMBER1);
adcAResultsB4[indexb4++] = ADC_readResult(ADCBRESULT_BASE, ADC_SOC_NUMBER4);
// Set the bufferFull flag if the buffer is full

if(RESULTS_BUFFER_SIZE <= index)
{
index = 0;
indexa1=0;
indexb4=0;
bufferFull = 1;
}
// if(RESULTS_BUFFER_SIZE <= indexa1)

// Clear the interrupt flag
ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);
// Check if overflow has occurred
if(true == ADC_getInterruptOverflowStatus(ADCA_BASE, ADC_INT_NUMBER1))
{
ADC_clearInterruptOverflowStatus(ADCA_BASE, ADC_INT_NUMBER1);
ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);
}
// Acknowledge the interrupt
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP1);
}

Green Deng:

你好,你的参考电压是用的内部还是外接的?设置的是多少V?如果是外接的,实际的外接电压是多少伏?

,

ZhouTao Hu:

internal 3.3V

赞(0)
未经允许不得转载:TI中文支持网 » TMS320F280049: ADC采样,采集3.3V电压,寄存器中的值为2048,采样其他的电压值,寄存器中的值都只有4096下的一半
分享到: 更多 (0)