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

HC-SRF04 Interfaceing LPC1768

Hello !

HC-SRF04 interfaceing LPC1768 with Timer2

//****************************************
#include "lpc17xx.h"
#include "type.h"
#include <stdio.h>

#define LCD_LPC1768
#define LCD_PORT_1
#define LCD_RS 4
#define LCD_RW 8
#define LCD_E 9
#define LCD_DB4 10
#define LCD_DB5 14
#define LCD_DB6 15
#define LCD_DB7 16
#include <lcd.h>

#define CPU 72000000 // 72MHZ
#define ECHO PORT0.1
#define TRIG PORT0.0

uint32_t S;

void Delay (uint32_t Time)
{ uint32_t i;

i = 0; while (Time--) { for (i = 0; i < 5000; i++); }
}

volatile int T_Counter;
float T= 0.0;
float d= 0.0;
char str[32];

/******************************************************************************
** Main Function main() vv
******************************************************************************/
int main (void)
{

SystemInit();; lcd_init(); lcd_clear(); lcd_gotoxy(1,1); lcd_putsf("HC-SRF04."); //***************************************************** // timer //***************************************************** LPC_SC->PCONP |= 1<<22; LPC_TIM2->TCR = 0x02; LPC_TIM2->CTCR = 0; LPC_TIM2->MR0 = 10000000; LPC_TIM2->MCR = 2; LPC_TIM2->TC = 0x0; LPC_TIM2->PR = 0; //LPC_TIM2->TCR = 0x01; NVIC_EnableIRQ(TIMER2_IRQn);

LPC_GPIO0->FIODIR = 1<<0;

while ( 1 ) { Delay(500); T_Counter = 0; LPC_GPIO0->FIOSET = 1<<0; Delay(10); LPC_GPIO0->FIOCLR = 1<<0;

while(((LPC_GPIO0->FIOPIN>>1)&1) == 0);

LPC_TIM2->TC = 0x0; LPC_TIM2->TCR = 0x01;

while(((LPC_GPIO0->FIOPIN>>1)&1) == 1); S = LPC_TIM2->TC; LPC_TIM2->TCR = 0x02; T = 0; d = 0;

T = (float)T_Counter*(100000.0/18000.0) + (float)S/18000.0; d = (float)T*340.0/2.0;

lcd_gotoxy(1,1); sprintf(str,"T = %2.3f ms ",T); lcd_putsf(str); lcd_gotoxy(2,1); sprintf(str,"d = %2.3f mm ",d); lcd_putsf(str);

}
}

void __irq TIMER2_IRQHandler(void)
{ LPC_TIM2->IR = 0x02; T_Counter++; return;

}

/******************************************************************************
** End Of File
******************************************************************************/