Other Parts Discussed in Thread: TMS320F28027
Hello TI Community.
I recently started to work with C2000 Launchpad which have the uC TMS320F28027, in CCS v7.2.0, compiler: TI v6.4.12.
I'm trying to get a distance from an object in front of the sensor HC SR-04.
In my code i'm configuring GPIO34 as the TRIGGER for the sensor, GPIO7 as the external interrupt pin, and timer0 which i use as a counter in free run mode.
In my interrupt routine i'm trying to calculate the difference between when timer started and when timer stopped to get the distance in centimeters.
Also i'm trying to see if the distance is calculated correctly by lighting one of the four LEDs from the board, depending in which interval of values is measured distance.
For some reason, when i use the API functions to configure the timer0, the timer is running and nothing happens, that's why i changed to bits manipulation.
Nonetheless, i keep getting odd values from the sensor. (something like 70K or 0, even if the interrupt routine is called correctly)
I used a voltage divider from 5V ECHO pin to 3.3V GPIO7, because the sensor uses 5V.
Also i'm using the 5V pins and GND from the Launchpad board.
If someone could help me, that would be great.
Thank you for your time.
Here is the code:
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#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/pwr.h"
#include "f2802x_common/include/timer.h"
#include "f2802x_common/include/wdog.h"
// Prototype statements for functions found within this file.
interrupt void xint1_isr(void);
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
TIMER_Handle myTimer0;
// Global variables for this example
volatile uint32_t Xint1Count = 0;
volatile uint32_t distance = 0; //storing distance in cm
volatile uint32_t temp_var = 0; // storing the value from substraction
volatile int obstacol = 0;
volatile uint32_t start_val = 0; // storing start value of timer0
volatile uint32_t stop_val = 0; //storing stop value of timer0
#define DELAY (CPU_RATE/1000*6*510) //Qual period at 6 samples
#define US_LIMIT 1200000U //limit of about 24 ms for ultrasonic sensor
void main(void)
{
CPU_Handle myCpu;
PLL_Handle myPll;
PWR_Handle myPwr;
WDOG_Handle myWDog;
// Initialize all the handles needed for this application
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));
myTimer0 = TIMER_init((void *)TIMER0_BASE_ADDR, sizeof(TIMER_Obj));
myPwr = PWR_init((void *)PWR_BASE_ADDR, sizeof(PWR_Obj));
myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));
// 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 60Mhz = 10Mhz * 10 / 2
PLL_setup(myPll, PLL_Multiplier_12, 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
// Setup a debug vector table and enable the PIE
PIE_setDebugIntVectorTable(myPie);
PIE_enable(myPie);
// Register interrupt handlers in the PIE vector table
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_1, PIE_SubGroupNumber_4, (intVec_t)&xint1_isr);
// Clear the counters
Xint1Count = 0; // Count XINT1 interrupts
// Enable XINT1 and XINT2 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);
// Enable Global Interrupts
CPU_enableGlobalInts(myCpu);
// GPIO28 & GPIO29 are outputs, start GPIO28 high and GPIO29 low
//GPIO_setHigh(myGpio, GPIO_Number_28);
GPIO_setMode(myGpio, GPIO_Number_34, GPIO_28_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_34, GPIO_Direction_Output);
// GPIO0 and GPIO1 are inputs
GPIO_setMode(myGpio, GPIO_Number_7, GPIO_0_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_7, GPIO_Direction_Input);
GPIO_setQualification(myGpio, GPIO_Number_7, GPIO_Qual_Sync);
// Configure GPIO 0-3 as outputs
GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_GeneralPurpose);
GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_GeneralPurpose);
GPIO_setMode(myGpio, GPIO_Number_2, GPIO_2_Mode_GeneralPurpose);
GPIO_setMode(myGpio, GPIO_Number_3, GPIO_3_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_0, GPIO_Direction_Output);
GPIO_setDirection(myGpio, GPIO_Number_1, GPIO_Direction_Output);
GPIO_setDirection(myGpio, GPIO_Number_2, GPIO_Direction_Output);
GPIO_setDirection(myGpio, GPIO_Number_3, GPIO_Direction_Output);
GPIO_setHigh(myGpio, GPIO_Number_0);
GPIO_setHigh(myGpio, GPIO_Number_1);
GPIO_setHigh(myGpio, GPIO_Number_2);
GPIO_setHigh(myGpio, GPIO_Number_3);
// GPIO0 is XINT1, GPIO1 is XINT2
GPIO_setExtInt(myGpio, GPIO_Number_7, CPU_ExtIntNumber_1);
// Configure XINT1
PIE_setExtIntPolarity(myPie, CPU_ExtIntNumber_1, PIE_ExtIntPolarity_FallingEdge);
// Enable XINT1 and XINT2
PIE_enableExtInt(myPie, CPU_ExtIntNumber_1);
// TIMER_stop(myTimer0);
//// Configure CPU-Timer 0, 1, and 2 to interrupt every second:
//// 50MHz CPU Freq, 1 second Period (in uSeconds)
//
// //ConfigCpuTimer(&CpuTimer0, 50, 1000000);
// TIMER_setPeriod(myTimer0, 0xFFFFFFFF);
// TIMER_setPreScaler(myTimer0, 1);
// TIMER_reload(myTimer0);
// TIMER_setEmulationMode(myTimer0, TIMER_EmulationMode_RunFree);
// TIMER_disableInt(myTimer0);
CpuTimer0Regs.TCR.bit.TSS = 1; // 1 = Stop timer, 0 = Start/Restart Timer
CpuTimer0Regs.PRD.all = 0xFFFFFFFF; // Initialize timer period to maximum
CpuTimer0Regs.TPR.all = 0x01;
CpuTimer0Regs.TCR.bit.TRB = 1; // 1 = reload timer now
CpuTimer0Regs.TCR.bit.FREE = 1;
CpuTimer0Regs.TCR.bit.SOFT = 0;
CpuTimer0Regs.TCR.bit.TIE = 0; // 0 = Disable/ 1 = Enable Timer Interrupt
//CpuTimer0Regs.TCR.all = 0; // TSS = 0 = Start/Restart Timer
for(;;) {
GPIO_setHigh(myGpio, GPIO_Number_34); //trigg
DELAY_US(10);
GPIO_setLow(myGpio, GPIO_Number_34);
//TIMER_start(myTimer0);
CpuTimer0Regs.TCR.bit.TRB = 1;
start_val = CpuTimer0Regs.TIM.all;
CpuTimer0Regs.TCR.bit.TSS = 0;
distance = 500;
DELAY_US(60000); // the time in which sensor has to give an echo back
if(distance < 50)
{
GPIO_setLow(myGpio, GPIO_Number_0);
GPIO_setHigh(myGpio, GPIO_Number_1);
GPIO_setHigh(myGpio, GPIO_Number_2);
GPIO_setHigh(myGpio, GPIO_Number_3);
}
else if(distance < 100)
{
GPIO_setHigh(myGpio, GPIO_Number_0);
GPIO_setLow(myGpio, GPIO_Number_1);
GPIO_setHigh(myGpio, GPIO_Number_2);
GPIO_setHigh(myGpio, GPIO_Number_3);
}
else if(distance < 150 )
{
GPIO_setHigh(myGpio, GPIO_Number_0);
GPIO_setHigh(myGpio, GPIO_Number_1);
GPIO_setLow(myGpio, GPIO_Number_2);
GPIO_setHigh(myGpio, GPIO_Number_3);
}
else if(distance < 200)
{
GPIO_setHigh(myGpio, GPIO_Number_0);
GPIO_setHigh(myGpio, GPIO_Number_1);
GPIO_setHigh(myGpio, GPIO_Number_2);
GPIO_setLow(myGpio, GPIO_Number_3);
}
else
{
GPIO_setHigh(myGpio, GPIO_Number_0);
GPIO_setHigh(myGpio, GPIO_Number_1);
GPIO_setHigh(myGpio, GPIO_Number_2);
GPIO_setHigh(myGpio, GPIO_Number_3);
}
}
}
interrupt void xint1_isr(void)
{
//temp_var = TIMER_getCount(myTimer0);
stop_val = CpuTimer0Regs.TIM.all;
CpuTimer0Regs.TCR.bit.TSS = 1;
//TIMER_stop(myTimer0);
temp_var = start_val - stop_val;
distance = (temp_var / 5882 ); // (17 * 0.000001) //Converts Time to Distance
//TIMER_reload(myTimer0);
//CpuTimer0Regs.TCR.bit.TRB = 1;
// while(PIE_getIntFlags(myPie, PIE_GroupNumber_1) != 1); //Waiting for Echo
// TIMER_start(myTimer0);
// //Timer Starts
// while(PIE_getIntFlags(myPie, PIE_GroupNumber_1) == 1); //Waiting for Echo goes LOW
// //TIMER_stop(myTimer0); //Timer Stops
Xint1Count++;
// Acknowledge this interrupt to get more from group 1
PIE_clearInt(myPie, PIE_GroupNumber_1);
}
//===========================================================================
// No more.
//===========================================================================