想做一个用片内温度传感器采集的温度信号去控制呼吸灯的程序

2019-03-24 10:36发布

软件ccsv5,板子用的f28x  28027,里面内置有个温度传感,我先写了一个温度采集的程序,然后能够正常跑起来,又写了个呼吸灯的程序,也没问题,,,现在想用温度采集程序的温度变化值去控制呼吸灯的明暗,请问大家这该怎么写啊。。。。
(1)温度采集程序代码:
#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File
//包含的文件
#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/wdog.h"
#include "f2802x_common/include/spi.h"
// Prototype statements for functions found within this file.
interrupt void adc_isr(void);
void spi_fifo_init(void);
void spi_init(void);
// Global variables used in this example:
uint16_t ConversionCount=0;
uint32_t TempSensorVoltage;
uint16_t TV;
uint16_t sdata0,sdata1;
int16_t degC; //temperature in deg. C
int16_t degK; //temperature in deg. K
ADC_Handle myAdc;
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
PWM_Handle myPwm;
SPI_Handle mySpi;
//数码管字库:0~9,0.~9. FSEt-yno
Uint16 Led_lib[] = {0x42, 0xf3, 0x86, 0xa2, 0x33, 0x2a, 0x0a, 0xf2, 0x02, 0x22,
                                        0x40, 0xf1, 0x84, 0xa0, 0x31, 0x28, 0x08, 0xf0, 0x00, 0x20,
                                        0x36, 0x4E, 0x0f, 0xbf, 0x23, 0x9b, 0x8b}; //小LED字库
void main()
{

    CPU_Handle myCpu;
    PLL_Handle myPll;
    WDOG_Handle myWDog;

    // Initialize all the handles needed for this application
    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));
    myPwm = PWM_init((void *)PWM_ePWM1_BASE_ADDR, sizeof(PWM_Obj));
    myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));
    mySpi = SPI_init((void *)SPIA_BASE_ADDR, sizeof(SPI_Obj));
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the F2802x_SysCtrl.c file.
//   InitSysCtrl();

    // Perform basic system initialization
    WDOG_disable(myWDog);
    CLK_enableAdcClock(myClk);
    (*Device_cal)();

    //Select the internal oscillator 1 as the clock source
    CLK_setOscSrc(myClk, CLK_OscSrc_Internal);

    // Setup the PLL for x10 /2 which will yield 50Mhz = 10Mhz * 10 / 2
    PLL_setup(myPll, PLL_Multiplier_10, PLL_DivideSelect_ClkIn_by_2);

    // Disable the PIE and all interrupts
    PIE_disable(myPie);
    PIE_disableAllInts(myPie);
    CPU_disableGlobalInts(myCpu);
    CPU_clearIntFlags(myCpu);

// If running from flash copy RAM only functions to RAM
#ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in F2802x_DefaultIsr.c.
// This function is found in F2802x_PieVect.c.
//   InitPieVectTable();
    PIE_setDebugIntVectorTable(myPie);
    PIE_enable(myPie);

// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
    PIE_registerPieIntHandler(myPie, PIE_GroupNumber_10, PIE_SubGroupNumber_1, (intVec_t)&adc_isr);

// Step 4. Initialize the ADC:
// This function is found in F2802x_Adc.c
//   InitAdc();  // For this example, init the ADC
    ADC_enableBandGap(myAdc);
    ADC_enableRefBuffers(myAdc);
    ADC_powerUp(myAdc);
    ADC_enable(myAdc);
    ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);
    // Step 7. User specific code, enable interrupts:

    // Enable ADCINT1 in PIE
    //   PieCtrlRegs.PIEIER1.bit.INTx1 = 1;    // Enable INT 1.1 in the PIE
        PIE_enableAdcInt(myPie, ADC_IntNumber_1);
    //    IER |= M_INT1;                         // Enable CPU Interrupt 1
        CPU_enableInt(myCpu, CPU_IntNumber_10);
    //    EINT;                                  // Enable Global interrupt INTM
        CPU_enableGlobalInts(myCpu);
    //    ERTM;                                  // Enable Global realtime interrupt DBGM
        CPU_enableDebugInt(myCpu);

// Step 5. Configure ADC to sample the temperature sensor on ADCIN5:
// The output of Piccolo temperature sensor can be internally connected to the ADC through ADCINA5
// via the TEMPCONV bit in the ADCCTL1 register. When this bit is set, any voltage applied to the external
// ADCIN5 pin is ignored.
//    EALLOW;
//    AdcRegs.ADCCTL1.bit.TEMPCONV     = 1;    //Connect internal temp sensor to channel ADCINA5.
    ADC_enableTempSensor(myAdc);
//    EDIS;

// Step 6. Continue configuring ADC to sample the temperature sensor on ADCIN5:
// Since the temperature sensor is connected to ADCIN5, configure the ADC to sample channel ADCIN5
// as well as the ADC SOC trigger and ADCINTs preferred. This example uses EPWM1A to trigger the ADC
// to start a conversion and trips ADCINT1 at the end of the conversion.

//Note: The temperature sensor will be double sampled to apply the workaround for rev0 silicon errata for the ADC 1st sample issue
//    EALLOW;
//    AdcRegs.ADCCTL1.bit.INTPULSEPOS    = 1;    //ADCINT1 trips after AdcResults latch
    ADC_setIntPulseGenMode(myAdc, ADC_IntPulseGenMode_Prior);
//    AdcRegs.INTSEL1N2.bit.INT1E     = 1;    //Enabled ADCINT1
    ADC_enableInt(myAdc, ADC_IntNumber_1);
//    AdcRegs.INTSEL1N2.bit.INT1CONT  = 0;    //Disable ADCINT1 Continuous mode
    ADC_setIntMode(myAdc, ADC_IntNumber_1, ADC_IntMode_ClearFlag);
//    AdcRegs.INTSEL1N2.bit.INT1SEL    = 1;    //setup EOC1 to trigger ADCINT1 to fire
    ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC1);
//    AdcRegs.ADCSOC0CTL.bit.CHSEL     = 5;    //set SOC0 channel select to ADCINA5 (which is internally connected to the temperature sensor)
    ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A5);
//    AdcRegs.ADCSOC1CTL.bit.CHSEL     = 5;    //set SOC1 channel select to ADCINA5 (which is internally connected to the temperature sensor)  errata workaround
    ADC_setSocChanNumber (myAdc, ADC_SocNumber_1, ADC_SocChanNumber_A5);
//    AdcRegs.ADCSOC0CTL.bit.TRIGSEL     = 5;    //set SOC0 start trigger on EPWM1A
    ADC_setSocTrigSrc(myAdc, ADC_SocNumber_0, ADC_SocTrigSrc_EPWM1_ADCSOCA);
//    AdcRegs.ADCSOC1CTL.bit.TRIGSEL     = 5;    //set SOC1 start trigger on EPWM1A errata workaround
    ADC_setSocTrigSrc(myAdc, ADC_SocNumber_1, ADC_SocTrigSrc_EPWM1_ADCSOCA);
//    AdcRegs.ADCSOC0CTL.bit.ACQPS     = 6;    //set SOC0 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1)
    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_7_cycles);
//    AdcRegs.ADCSOC1CTL.bit.ACQPS     = 6;    //set SOC1 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1) errata workaround
    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_1, ADC_SocSampleWindow_7_cycles);
//    EDIS;





    CLK_enablePwmClock(myClk, PWM_Number_1);
// Assumes ePWM1 clock is already enabled in InitSysCtrl();
//   EPwm1Regs.ETSEL.bit.SOCAEN    = 1;        // Enable SOC on A group
    PWM_enableSocAPulse(myPwm);
//   EPwm1Regs.ETSEL.bit.SOCASEL    = 4;        // Select SOC from from CPMA on upcount
    PWM_setSocAPulseSrc(myPwm, PWM_SocPulseSrc_CounterEqualCmpAIncr);
//   EPwm1Regs.ETPS.bit.SOCAPRD     = 1;        // Generate pulse on 1st event
    PWM_setSocAPeriod(myPwm, PWM_SocPeriod_FirstEvent);
//   EPwm1Regs.CMPA               = 0x0080;    // Set compare A value
    PWM_setCmpA(myPwm, 0x0080);
//   EPwm1Regs.TBPRD                 = 0xFFFF;    // Set period for ePWM1
    PWM_setPeriod(myPwm, 0xFFFF);
//   EPwm1Regs.TBCTL.bit.CTRMODE     = 0;        // count up and start
    PWM_setCounterMode(myPwm, PWM_CounterMode_Up);
    CLK_enableTbClockSync(myClk);
    //初始化SPI通讯的GPIO口
    GPIO_setPullUp(myGpio, GPIO_Number_16, GPIO_PullUp_Enable);
    GPIO_setPullUp(myGpio, GPIO_Number_17, GPIO_PullUp_Enable);
    GPIO_setPullUp(myGpio, GPIO_Number_18, GPIO_PullUp_Enable);
    GPIO_setPullUp(myGpio, GPIO_Number_19, GPIO_PullUp_Enable);
    GPIO_setQualification(myGpio, GPIO_Number_16, GPIO_Qual_ASync);
    GPIO_setQualification(myGpio, GPIO_Number_17, GPIO_Qual_ASync);
    GPIO_setQualification(myGpio, GPIO_Number_18, GPIO_Qual_ASync);
    GPIO_setQualification(myGpio, GPIO_Number_19, GPIO_Qual_ASync);
    GPIO_setMode(myGpio, GPIO_Number_16, GPIO_16_Mode_SPISIMOA);
    GPIO_setMode(myGpio, GPIO_Number_17, GPIO_17_Mode_SPISOMIA);
    GPIO_setMode(myGpio, GPIO_Number_18, GPIO_18_Mode_SPICLKA);
    GPIO_setMode(myGpio, GPIO_Number_19, GPIO_19_Mode_SPISTEA_NOT);
    spi_init();         // Initialize SPI
    spi_fifo_init();    // Initialize the SPI FIFOs
// Wait for ADC interrupt
    for(;;)
    {
    }

}


interrupt void  adc_isr(void)
{
//    TempSensorVoltage[ConversionCount] = AdcResult.ADCRESULT1;  //discard ADCRESULT0 as part of the workaround to the 1st sample errata for rev0
    TempSensorVoltage+= ADC_readResult(myAdc, ADC_ResultNumber_1);

    // If 64 conversions have been logged, start over
    if(ConversionCount == 63)
    {
    TV=TempSensorVoltage>> 6;//64次采样后取平均值
    TempSensorVoltage=0;
    ConversionCount=0;
    degC = ADC_getTemperatureC(myAdc, TV);
   // degK = ADC_getTemperatureK(myAdc, TV);
    sdata0 = Led_lib[degC/10]*256 + Led_lib[degC%10];//具体温度值
    sdata1 = Led_lib[20]*256 + Led_lib[21];//℃显示
    SPI_write(mySpi, sdata0);      // Send data
    SPI_write(mySpi, sdata1);      // Send data
    //数据传输结束GPIO19产生一个上升沿使移位寄存器输出显示数据
       GpioDataRegs.GPACLEAR.bit.GPIO19 = 1;
        GpioDataRegs.GPASET.bit.GPIO19 = 1;
    }
    else ConversionCount++;

//  AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;        //Clear ADCINT1 flag reinitialize for next SOC
    ADC_clearIntFlag(myAdc, ADC_IntNumber_1);
//  PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;   // Acknowledge interrupt to PIE
    PIE_clearInt(myPie, PIE_GroupNumber_10);

    return;
}
//*******************************************************************************
//函数名:  void spi_init()
//功 能:   spi通讯初始化
//参 数:   无
//返回值:  无
//注 意:
//******************************************************************************
void spi_init()
{
    CLK_enableSpiaClock(myClk);

    // Reset on, rising edge, 16-bit char bits
    SPI_setCharLength(mySpi, SPI_CharLength_16_Bits);

    // Enable master mode, normal phase,
    // enable talk, and SPI int disabled.
    SPI_setMode(mySpi, SPI_Mode_Master);
    SPI_enableTx(mySpi);

    SPI_setBaudRate(mySpi, SPI_BaudRate_1_MBaud);

    // Relinquish SPI from Reset
    SPI_enableLoopBack(mySpi);
    SPI_enable(mySpi);

    // Set so breakpoints don't disturb xmission
    SPI_setPriority(mySpi, SPI_Priority_FreeRun);

    return;
}
//*******************************************************************************
//函数名:  void spi_fifo_init()
//功 能:   spi快进快出初始化
//参 数:   无
//返回值:  无
//注 意:
//******************************************************************************
void spi_fifo_init()
{

    // Initialize SPI FIFO registers
    SPI_enableChannels(mySpi);
    SPI_enableFifoEnh(mySpi);
    SPI_resetTxFifo(mySpi);
    SPI_clearTxFifoInt(mySpi);
    SPI_resetRxFifo(mySpi);
    SPI_clearRxFifoInt(mySpi);
    SPI_setRxFifoIntLevel(mySpi, SPI_FifoLevel_4_Words);

    return;
}


(2)呼吸灯代码:
#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File
void delay_loop(short n);
void Gpio_select(void);
void Gpio_example1(void);
void Gpio_example2(void);
void Gpio_example3(void);

void main(void)
{

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
//  InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example

// For this example use the following configuration:
   Gpio_select();

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
   DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
//   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
//  InitPieVectTable();
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
Gpio_example1();
}
void delay_loop(short n)
{
    short i;
    for (i = 0; i < n; i++) {}
}

void Gpio_example1(void)
{
short t,Tmax=3000,Tmin=10,T=4000,s,i;
t=Tmin;
   for(;;)
   {
   if(t<Tmax)
   { t++;
       GpioDataRegs.GPADAT.all    =0xfffffffc;
       GpioDataRegs.GPBDAT.all    =0xffffffff;

       delay_loop(t);

       GpioDataRegs.GPADAT.all    =0xffffffff;
       GpioDataRegs.GPBDAT.all    =0xffffffff;

       delay_loop(T-t);

  }
   else{s=t;
           for(i=1;i<t-Tmin;i++)
           { s--;
           GpioDataRegs.GPADAT.all    =0xfffffffc;
       GpioDataRegs.GPBDAT.all    =0xffffffff;

   delay_loop(s);

   GpioDataRegs.GPADAT.all    =0xffffffff;
   GpioDataRegs.GPBDAT.all    =0xffffffff;

   delay_loop(T-s);

}
t=Tmin;
   }
  }
}

void Gpio_select(void)
{


    EALLOW;
        GpioCtrlRegs.GPAMUX1.all = 0x00000000;  // All GPIO
        GpioCtrlRegs.GPAMUX2.all = 0x00000000;  // All GPIO
        GpioCtrlRegs.GPAMUX1.all = 0x00000000;  // All GPIO
    GpioCtrlRegs.GPADIR.all = 0xFFFFFFFF;   // All outputs
    GpioCtrlRegs.GPBDIR.all = 0x0000000F;   // All outputs
    EDIS;

}



此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
ltbytyn
2019-03-24 17:09
 精彩回答 2  元偷偷看……0人看过

一周热门 更多>

相关问题

    相关文章