working with lm4f120h5qr launchpad. want to interface with hcsr04 sensor to launchpad without using "Stellarisware" and other libraries.
my code is as follows :
//This is to avoid doing the math everytime you do a reading const double temp = 1.0/80.0; //Stores the pulse length extern volatile unsigned int pulse; //Tells the main code if the a pulse is being read at the moment extern volatile unsigned char echowait; int main(void) { PLL_Init(); //80mhz SysTick_Init(); Init_Timer(); Init_sensor(); while(1) { //Checks if a pulse read is in progress if(echowait != 1) { //Does the required pulse of 10uS TriggerPulse(); /*This makes the code wait for a reading to finish You can omit this part if you want the code to be non-blocking but reading is only ready when echowait=0.*/ while(echowait != 0); //Converts the counter value to cm. pulse =(unsigned int)(temp * pulse); pulse = pulse / 58; //Prints out the distance measured. send_serial("distance = %2dcm \n" , pulse); } //wait about 10ms until the next reading. delay_ms(10); } } /*********************main end********************/ followinf functions are in different c file :
volatile unsigned int pulse=0;
volatile unsigned char echowait=0;
void Init_sensor(void) { volatile unsigned long delay; SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA; // activate clock for port A delay = SYSCTL_RCGC2_R; GPIO_PORTA_AMSEL_R &= ~(TRIG_PIN | ECHO_PIN); // disable analog functionality on PA5,PA6 GPIO_PORTA_PCTL_R |= 0x00222211;//(GPIO_PORTA_PCTL_R&0x00FFFFFF)+0x33000000; // configure PA5 PA6 as GPIO GPIO_PORTA_DIR_R |= TRIG_PIN; // make PA5 out GPIO_PORTA_DIR_R &= ~ECHO_PIN; //make PA6 In //GPIO_PORTA_DR8R_R |= TRIG_PIN; // enable 8 mA drive on PA5,6 GPIO_PORTA_AFSEL_R &= ~(TRIG_PIN | ECHO_PIN); // disable alt funct on PA5,6 GPIO_PORTA_DEN_R = (TRIG_PIN | ECHO_PIN); // enable digital I/O on PA5,6 GPIO_PORTA_PUR_R |= ECHO_PIN; // enable weak pull-up on PA GPIO_PORTA_IS_R &= ~ECHO_PIN; // PA6 is edge-sensitive GPIO_PORTA_IBE_R |= ECHO_PIN; // PA6 is both edges GPIO_PORTA_ICR_R = ECHO_PIN; // clear flags PA6 GPIO_PORTA_IM_R |= ECHO_PIN; // arm interrupt on PA6 NVIC_PRI7_R = (NVIC_PRI7_R&0xFF00FFFF)|0x00400000; // (g) priority 2 NVIC_EN0_R = NVIC_EN0_INT0; // enable interrupt 30 in NVIC } // Start the timers and interrupt frequency void Init_Timer(void) { unsigned long volatile delay; delay = SYSCTL_RCGC2_R; SYSCTL_RCGCTIMER_R |= 0x04; // 0) activate timer2 delay = SYSCTL_RCGC2_R; TIMER2_CTL_R = 0x00000000; // 1) disable timer2A during setup TIMER2_CFG_R = TIMER_CFG_32_BIT_TIMER; // 2) configure for 32-bit mode TIMER2_TAMR_R = TIMER_TAMR_TAMR_PERIOD; // 3) configure for periodic mode, default down-count settings TIMER2_TAILR_R = 0xFFFFFFFF;//((80000000 / 1000000) * 10); // 4) reload value 10us TIMER2_TAPR_R = 0; // 5) bus clock resolution TIMER2_CTL_R = 0x00000001; // enable timer2A }
void EchoPulseHandler(void) //This ISR executes when it detects the end of the echo pulse { unsigned int status = 0; status = GPIO_PORTA_MIS_R; GPIO_PORTA_ICR_R = 0x01; // acknowledge flag0 /* If it's a rising edge then set he timer to 0 It's in periodic mode so it was in some random value */ //if echo pin high means burst is receiv to echo till echo pin gose low if(( status & ECHO_PIN) == ECHO_PIN ) //if high { TIMER2_TAR_R = 0; //Loads value 0 into the timer. TIMER2_CTL_R = 0x00000001; // enable timer2A echowait=1; } // If it's a falling edge that was detected, then get the value of the counter else { pulse = TIMER2_TAV_R; //record value TIMER2_CTL_R = 0x00000000; // disable timer2A echowait=0; } } //This executes the trigger pulse void TriggerPulse(void) { TRIG_PIN_HIGH //Set Trigger pin HIGH delay_us(10); //10us TRIG_PIN_LOW //Set Trigger LOW delay_us(10); //10us } }
1)
the problem is falling edge interrupt doesn't occurred , the ISR stuck in raising edge.
i think there is some issue while initialization of port A and ECHO PIN
trigger pin is working ok.
2)
the other thing is when raising edge occurred the counter will be reset but its not.
also i tried TAV_R but still counter value not reset .
tested some stellarisware example on the same pins(with PORTA) with sensor , its measure the accurate distance.