go_away

Author Topic: HC-SR04  (Read 1562 times)

0 Members and 1 Guest are viewing this topic.

Offline FihtarotTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
HC-SR04
« on: February 13, 2015, 05:06:05 AM »
Hi, I do not know English, so I use Google translator.

I use AVR Studio 6 and ATMega128.
I cannot understand why my code doesn't work. I only have a timer 2. Please help me.

Code: [Select]
#define HIGH_P(port, num) ( port |= (1 << num) )
#define LOW_P(port, num) ( port &= ~(1 << num) )
#define OUT_P(port, num) ( port |= (1 << num) )
#define IN_P(port, num) ( port &= ~(1 << num) )
#define SCAN_P(port, num) ( port & (1 << num) )


//=============================
#define HC_Trig_PORT PORTE
#define HC_Trig_DDR DDRE
#define HC_Trig_PIN PINE
#define HC_Trig_PINn 7
//=============================


#define F_CPU 16000000UL

// 1us
#define quant_t2_prescaler (1 << CS20) 
#define quant_t2_start TCNT2 = 0xF0; TCCR2 |= quant_t2_prescaler   
#define quant_t2_stop  TCNT2 = 0xF0; TCCR2 &= ~(quant_t2_prescaler)
//==============

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>


char high_HC_int = 0;
unsigned int HC_time_us = 0;
float HC_dist = 0;


ISR(INT6_vect) {
high_HC_int = ~high_HC_int;
}

ISR(TIMER2_OVF_vect) {
TCNT2 = 0xF0;
HC_time_us++;
}


char HC_time(void) {
unsigned int loopCnt = 0;

HC_time_us = 0;
high_HC_int = 0;

HIGH_P(HC_Trig_PORT, HC_Trig_PINn);

_delay_us(10);

LOW_P(HC_Trig_PORT, HC_Trig_PINn);


while(!high_HC_int) {
if(loopCnt++ == 20000) { // time is up
return 0;
}
}


quant_t2_start;
while(high_HC_int) {
if(HC_time_us > 3000) {
break;
}
}
quant_t2_stop;

return 1;
}


int main(void) {
/*
PE.4 - LED 1
PE.5 - LED 2
PE.6 -  Echo (INT6)
PE.7 -  Trig
*/


//==========================
DDRE = (1 << DDE7) | (0 << DDE6) | (1 << DDE5) | (1 << DDE4) | (0 << DDE3) | (0 << DDE2) | (0 << DDE1) | (0 << DDE0);
//==========================



//==========================

TCCR2 = (0 << WGM20) | (0 << COM21) | (0 << COM20) | (0 << WGM21) | (0 << CS22) | (0 << CS21) | quant_t2_prescaler;
TCNT2 = 0xF0;
OCR2 = 0x00;
quant_t2_stop;


TIMSK = (0 << OCIE2) | (1 << TOIE2) | (0 << TICIE1) | (0 << OCIE1A) | (0 << OCIE1B) | (0 << TOIE1) | (0 << OCIE0) | (0 << TOIE0);
//==========================


//==========================
EICRA = (0 << ISC31) | (0 << ISC30) | (0 << ISC21) | (0 << ISC20) | (0 << ISC11) | (0 << ISC10) | (0 << ISC01) | (0 << ISC00);
EICRB = (0 << ISC71) | (0 << ISC70) | (0 << ISC61) | (1 << ISC60) | (0 << ISC51) | (0 << ISC50) | (0 << ISC41) | (0 << ISC40);
EIMSK = (0 << INT7) | (1 << INT6) | (0 << INT5) | (0 << INT4) | (0 << INT3) | (0 << INT2) | (0 << INT1) | (0 << INT0);
EIFR = (0 << INTF7) | (1 << INTF6) | (0 << INTF5) | (0 << INTF4) | (0 << INTF3) | (0 << INTF2) | (0 << INTF1) | (0 << INTF0);
//==========================

sei();
while(1) {
if(HC_time()) {
HC_dist = (float)HC_time_us / 58.00;
if(HC_dist < 10) {
HIGH_P(PORTE, 5);
} else {
LOW_P(PORTE, 5);
}
}
_delay_ms(50);
}
}

HC_time () returns 0

Offline mklrobo

  • Supreme Robot
  • *****
  • Posts: 516
  • Helpful? 14
  • From Dream to Design at the speed of Imagination!
Re: HC-SR04
« Reply #1 on: February 13, 2015, 07:31:25 AM »
 :) Hello!
I can help analyze the code, but do not have the microcontroller to
begin to apply. What kind of microcontroller are you  using? Axon?
keep me posted......... ;D ;D ;D

Offline FihtarotTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: HC-SR04
« Reply #2 on: February 21, 2015, 07:30:12 AM »
Hello. I've already figured out the problem. Perhaps I can help someone with the same problem You just need to specify the type of a variable as "volatile"

 


Get Your Ad Here