This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code for interfacing MCB2140 with HCSR04 ultrasonic sensor

Hi,
I am interfacing MCB2140 with HC-SR04 ultrasonic sensor. My code builds without errors. However, i do not get any value in the capture register. How do i test my code without using a LCD? If i need to use a LCD, can anyone provide the code for it?

#include<LPC214x.h>
#include<stdio.h>

#define PRESCALE 60

void initTimer(void);
void delay(int);


void initTimer()
{
        T0CTCR = 0x0; //select Timer mode
        T1CTCR = 0x0;
        T0PR = PRESCALE -1; // since counter starts from 0
        T0TCR = 0x02; //Reset Timer0
        T1TCR =0x02; //reset Timer1
        T1CCR = 0x0001; //enable CAP0RE for Timer1
}

void delay(int us)
{
        T0TCR = 0x02; // reset timer0
        T0TCR = 0x01; //Enable timer0
        while(T0TC < us) ; //loop for us delay

        T0TCR = 0x00; //disable Timer0
        T0TCR = 0x02;
        T0TC = 0x00;
}



void main()
{

        PINSEL0 &= 0xFFCFFFFF;
        PINSEL0 |= 0x00200000;
        IO0DIR = 0x00000800; //set P0.11(trigger) as o/p and 10(echo) as i/p
        IO1DIR = 0x00FF0000;// set P1.16-23 as outputs

        initTimer();

////    /* initialize the serial interface   */
//      PINSEL0 = 0x00050000;           // Enable RxD1 and TxD1
//      U1LCR = 0x83;                   // 8 bits, no Parity, 1 Stop bit
//      U1DLL = 97;                     // 9600 Baud Rate @ 15MHz VPB Clock
//      U1LCR = 0x03;                   // DLAB = 0

        while(1)
        {
            long time,dist, temp;
                IO1CLR = 0x00FF0000;
        repeat: IO0SET = 0x00000800; //send high on trigger pin
                delay(10); //delay for 10us
                IO0CLR = 0x00000800; // send low after 10us

                T1TCR = 0x01;  //Enable Timer1
                 if(T1CR0 == 0)
                                        {
                                                delay(100);
                                        } //wait for rising edge to occur on echo pin
                delay(3);          //wait for debounce
                if(T1CR0 >0)
                {
                        time = T1CR0; //read value of CR0 reg
                        dist = (time*340)/2; //dist in meters
                        IO1SET = 1<<16;
                        delay(10000);
                        IO1CLR = 1<<16;
                        printf("%d m\n",dist);
                }
                else
                        goto repeat;
                T1TCR = 0x00; //disable Timer1

                delay(1000000);

        }
}

0