Search Microcontrollers

Tuesday, October 16, 2012

C2000 launchpad - code skeleton

Maybe I am just lazy, maybe laziness is a bit "the geek way".

Each time I start a new project I like to copy from a skeleton, some kind of template instead of typing everything from scratch.
The real reason, I think, is that in most of the cases I cannot recall how to start and with the C2000 Launchpad I always forget something, so I created a file with all the steps and code snippets.

I am posting it here, just in case it might turn useful for others.

First, in CCS start a new project (FIle, new Project, CCS Project )


Then  set the basic project properties (see the highlighted parts)


Some tweaks are then needed in the project properties after the project is created, right click the project name in the project explorer, select properties and add the laiunchpad specific location for the include files.
You may need to adjust that path according to your ControlSuite install dir


Finally add the library


You are now good to go, open your main.c file and copy & paste the following skeleton

Update : Make sure you select the correct linker command file, by mistake I selected 28027 instead of F28027 and spent some time to figure out why the program would not fit in memory according to the linker.



//-------------------------------------------------------

#include <stdio.h>
#include <file.h>

#include "DSP28x_Project.h"     // DSP28x Headerfile

#include "f2802x_common/include/pll.h"
#include "f2802x_common/include/clk.h"
#include "f2802x_common/include/wdog.h"

#include "f2802x_common/include/flash.h"
#include "f2802x_common/include/gpio.h"
#include "f2802x_common/include/pie.h"
#include "f2802x_common/include/adc.h"
#include "f2802x_common/include/sci.h"
#include "f2802x_common/include/sci_io.h"


extern void DSP28x_usDelay(Uint32 Count);

// you probably need these
CPU_Handle myCpu;
PLL_Handle myPll;
WDOG_Handle myWDog;
CLK_Handle myClk;

// these are optional
ADC_Handle myAdc;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
SCI_Handle mySci;




void setup_handles()
{

    myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
    myPll = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));
    myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_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));
    mySci = SCI_init((void *)SCIA_BASE_ADDR, sizeof(SCI_Obj));
    myAdc = ADC_init((void *)ADC_BASE_ADDR, sizeof(ADC_Obj));


}

void init_system()
{
  WDOG_disable(myWDog);
  (*Device_cal)();
  CLK_setOscSrc(myClk, CLK_OscSrc_Internal);
  PLL_setup(myPll, PLL_Multiplier_12, PLL_DivideSelect_ClkIn_by_2);

  PIE_disable(myPie);
  PIE_disableAllInts(myPie);
  CPU_disableGlobalInts(myCpu);
  CPU_clearIntFlags(myCpu);
#ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif
}

void main()
{
  setup_handles();
  init_system();
}
//---------------------------------------------

Note that this is just a skeleton, you may need to remove parts you may not need such as include files, handles etc (unless the linker is smart enough to get rid of them, which I think it's something we might expect).

Also, you will need to add specific peripheral initialization according to your needs and... your program.






4 comments:

Patrick Roberto Almeida said...

Hello guys.

I'm starting at piccolo f28027. I've learned to light some leds with
some didactic and set by example.

At this point, I would like to insert a square wave or sine wave in ADC, and show that the output signal, an LED lighting and extinguishing the GPIO 0.
The input signal has an offset of 1V. Subtract a value from the ADC (4095/3.3) relative to 1V, and compare it to zero.

Already configured the ADC and GPIO, but I can not get the desired result.

Could anyone help me? It seems that the ADC is not reading the entry.

Thank you.

This is the code I used

In expressions, I do not see the value of the variable. Generates an error.

Patrick Roberto Almeida said...

Hello guys.

I'm starting at piccolo f28027. I've learned to light some leds with
some didactic and set by example.

At this point, I would like to insert a square wave or sine wave in ADC, and show that the output signal, an LED lighting and extinguishing the GPIO 0.
The input signal has an offset of 1V. Subtract a value from the ADC (4095/3.3) relative to 1V, and compare it to zero.

Already configured the ADC and GPIO, but I can not get the desired result.

Could anyone help me? It seems that the ADC is not reading the entry.

Thank you.

This is the code I used

In expressions, I do not see the value of the variable. Generates an error.

Patrick Roberto Almeida said...

This my code:

/*
* main.c
*/
#include
#include

#include "DSP28x_Project.h" // DSP28x Headerfile

#include "f2802x_common/include/adc.h"
#include "f2802x_common/include/clk.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"

interrupt void adc_isr(void);

//#ifdef _FLASH
// memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
//#endif


int16 SaidaAdc;
int16 Tensao;

ADC_Handle myAdc;
CLK_Handle myClk;
GPIO_Handle myGpio;

PWM_Handle myPwm;


void main()
{
CPU_Handle myCpu;
WDOG_Handle myWDog;
PLL_Handle myPll;

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));
myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_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));

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

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

// Setup the PLL for x12 /2 which will yield 60Mhz = 10Mhz * 12 / 2
PLL_setup(myPll, PLL_Multiplier_12, PLL_DivideSelect_ClkIn_by_2);

// Disable the PIE and all interrupts

CPU_disableGlobalInts(myCpu);
CPU_clearIntFlags(myCpu);


//INICIALIZAÇÃO E CONFIGURAÇÃO DO GPIO

myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));

GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_0, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_1, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_2, GPIO_2_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_2, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_3, GPIO_3_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_3, GPIO_Direction_Output);


//INICIALIZAÇÃO E CONFIGURAÇÃO DO ADC
ADC_enableBandGap(myAdc);
ADC_enableRefBuffers(myAdc);
ADC_powerUp(myAdc);
ADC_enable(myAdc);
ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);

//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_A1);


ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_7_cycles);

//}
// PROGRAMA

// GPIO_setHigh(myGpio, GPIO_Number_0);
// GPIO_setHigh(myGpio, GPIO_Number_1);
// GPIO_setHigh(myGpio, GPIO_Number_2);
// GPIO_setHigh(myGpio, GPIO_Number_3);

while(1)
{
SaidaAdc = ADC_readResult(myAdc, ADC_ResultNumber_1);
Tensao = SaidaAdc - 1241;

if(Tensao >= 0)
{
GPIO_setHigh(myGpio, GPIO_Number_0);
}
else
GPIO_setLow(myGpio, GPIO_Number_0);

}


}










Francesco Agosti said...

Might be a voltage scaling issue?
Did you try to send back the adc values via uart or to debug them in CCS?