AnsweredAssumed Answered

Interfacing ultrasonic sensor with ADUC7060 eval board

Question asked by Azrine on May 26, 2013
Latest reply on May 27, 2013 by ABuda

I am trying to interface a sensor(40 KHz operation) to the Eval board and we are using the following code. The code consists of accepting two low to high pulses from the sensor and finding the time difference between the two pulses and output the result in UART. We are unable to send the output on UART. The code is as follows:

 

#define BIT0  0x01

#define BIT1  0x02

#define BIT2  0x04

#define BIT3  0x08

#define BIT4  0x10

#define BIT5  0x20

#define BIT6  0x40

#define BIT7  0x80

#define BIT8  0x100

#define BIT9  0x200

#define BIT10 0x400

#define BIT11 0x800

#define BIT12 0x1000

#define BIT13 0x2000

#define BIT14 0x4000

#define BIT15 0x8000

#define BIT16 0x10000

#define BIT17 0x20000

#define BIT18 0x40000

#define BIT20 0x100000

#define BIT24 0x1000000

#define TIMER_LD 0x10000

void ConfigurePWM(void);

#include<aduc7060.h>

 

 

void SendHexASCII(unsigned long Value, unsigned char Digits);

void SendChar(char Character);

char Hex2Ascii(char toconv);

 

 

unsigned int Timer_R_Count, Timer_F_Count, Timer_Diff;

 

 

 

 

unsigned char bTripDetected = 0;

int main(void)

{

          GP1CON  = 0x00000011;                                                     // Select UART functionality for P1.0/P1.1

          COMCON0 = 0x80;                                                                                // Enable access to COMDIV registers

          COMDIV0 = 0x21;                                                                                // Set baud rate to 9600.

          COMDIV1 = 0x00;

          COMCON0 = 0x07;

 

 

          POWKEY1 = 0x1;

          POWCON0 = 0x78;                       // Set core to max CPU speed of 10.24Mhz

          POWKEY2 = 0xF4;

 

          // Initialize the GPIO pins for PWM mode

          GP0CON0  = 0x00000000;

 

 

          GP1CON  = BIT8 | BIT12 | BIT16 | BIT20;            // Select PWMSYNC and PWMTRIP functions

                                        // Select PWM2, PWM3 output

          GP0DAT   = 0x0F000000;                                                            // Select GP0.0 to GP0.3 as output all 4 OFF

 

          GP2CON   = 0x00000000;                                                            // P2.0 = GPIO & IRQ2

          IRQCONE  = 0x00000022;                                                            // IRQ0 & IRQ2 rising edge

          T0LD     = TIMER_LD;                                                            // Set Timer 0 Value

                                                                                                                        // Value to be selected for Error-Handling

                                                                                                                        // if no pulse is detected

 

 

          T0CON    = 0x00030480;                                                            // Enable Timer 0 count down @ 10.24MHz

                                                                                                                        // with Capture on rising edge from external IRQ2

 

          PWMCON          = BIT0 + BIT10;           // PWM Standard mode, UCLK/2, PWM Trip interrupt enabled

 

          ConfigurePWM();                              // Call function to configure PWM

          bTripDetected = 0;

          IRQEN          = BIT17+BIT18+BIT3+BIT13;                     // Enable PWM Trip interrupt source

 

 

           

             if (bTripDetected == 1)

              {

                                 bTripDetected = 0;

                                 ConfigurePWM();

              }

            

 

 

                                 while (1)

             {

          // Endless Main Loop - to send out data on UART

 

 

 

                    SendChar('\r');

                    SendHexASCII(T0VAL, 8);

                    SendChar(' ');

                    SendHexASCII(Timer_R_Count, 8);

                    SendChar(' ');

                    SendHexASCII(Timer_F_Count, 8);

                    SendChar(' ');

                    SendHexASCII(Timer_Diff, 8);

                    SendChar(' ');

          }

}

 

 

 

 

 

 

// IRQ Handler for Timer Capture of PWM-Pulse

 

 

void IRQ_Handler(void) __irq

{

// no need to buffer IRQSTA - the events can't be lost

 

 

       unsigned long IRQSTATUS = 0;

 

 

          IRQSTATUS = IRQSTA;             // Read off IRQSTA register

 

 

          if ((IRQSTATUS & BIT17) == BIT17)          //If PWM trip interrupt source

          {

                     bTripDetected = 1;                    // Set flag to re configure the PWM block

                     PWMCLRI = 0xFF;                    // Clear the PWM trip interrupt source

          }

 

 

 

 

          if(IRQSTA == 0x00040000)                                                  // IRQ2 == rising edge of signal

          {                                                                                                              // YES !

                    GP0SET = 0x00010000;

                    Timer_R_Count = T0CAP;                                                  // read back captured rising count

                    IRQCLRE = 0x00040000;                                                  // Clear IRQ 2 Interrupt

                    T0CON  = 0x0002B480;                                                  // Enable Timer 0 count down @ 10.24MHz

                                                                                                                        // with Capture on rising edge from external IRQ2

                    GP0CLR = 0x00010000;

          }

          else if(IRQSTA == 0x00002000)                                        // IRQ0 == falling edge of signal (rising edge on IRQ0)

          {                                                                                                               // falling edge triggered !

                    GP0SET = 0x00020000;

                    Timer_F_Count = T0CAP;                                                  // read back captured falling count

                                                                                                                        // calculate pulse length

                                                                                                                        // !!! Timer0 is counting down !!!

                    Timer_Diff = Timer_R_Count - Timer_F_Count;

                    IRQCLRE  = 0x00002000;                                                  // Clear IRQ 0 Interrupt

                    T0LD     = TIMER_LD;                                                  // Set counter to prevent Error underflow

                    T0CON    = 0x00030480;                                                  // Enable Timer 0 count down @ 10.24MHz

                                                                                                                        // with Capture on rising edge from external IRQ0

                    GP0CLR  = 0x00020000;

          }

          else if(IRQSTA == 0x00000008)                                        // Timer 0 overflow - no valid pulse

          {

                    T0CLRI = 0x00;                                                                      // Clear Timer 0 Overflow Interrupt

                    Timer_Diff = Timer_F_Count = Timer_R_Count = 0xEEEEEEEE;

          }

 

 

       

 

 

}

 

 

 

 

 

 

void ConfigurePWM(void)

{

 

 

          //Configure PWM2 for 100khz duty cycle

          // P1.5

          PWM1COM0 = 0x62;                    //          Configure PWM2 output high trigger time

          PWM1COM1 = 0x31;                    //          Configure PWM2 output Low trigger time

          PWM1COM2 = 0x62;                    //          Configure PWM3 output low trigger time

          PWM1LEN = 0x31;                    //          Configure PWM3 output high trigger time

 

 

}

 

 

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

// Send a Integer as HEX-ASCII characters

void SendHexASCII(unsigned long Value, unsigned char Digits)

{

          while(Digits != 0)

          {

                    SendChar(Hex2Ascii((Value >> (--Digits * 4) & 0x0F)));

          }

}

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

// Send a Character to UART

void SendChar(char Character)

{

                    while(!(0x020==(COMSTA0 & 0x020))){}

                      COMTX = Character;

}

 

 

 

 

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

// HEX-ASCII conversion

char Hex2Ascii(char toconv)

{

          if (toconv<0x0A)

          {

                    toconv += 0x30;

          }

          else

          {

                    toconv += 0x37;

          }

 

          return (toconv);

}

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

Outcomes