Skip to content

Instantly share code, notes, and snippets.

@kala13x
Last active December 17, 2015 18:19
Show Gist options
  • Save kala13x/5652647 to your computer and use it in GitHub Desktop.
Save kala13x/5652647 to your computer and use it in GitHub Desktop.
Driver for Ultrasonic HC–SR04 Sensor.
//#########################################################################
//
// This is free software driver for Ultrasonic HC–SR04 Sensor. software
// is written for uniHack Vision Plus Team. Driver has two methods to
// transmit information from sensor: Equalizer on board display and USART.
//
// Copyright (c) 2014 uniHack Vision Plus TeaM
// Web: http://off-sec.com/ ; E-Mail: [email protected]
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3, or
// (at your option) any later version; incorporated herein by reference.
//
//#########################################################################
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <stdio.h>
#include <stdint.h>
#define DD_HCRS04_Trig_Out DDRD |= (1 << 4); // Test
#define DD_HCRS04_Trig_In DDRD &= ~(1 << 4); // Test
#define DD_HCRS04_Echo_Out DDRD |= (1 << 5); // Test
#define DD_HCRS04_Echo_In DDRD &= ~(1 << 5); // Test
#define HCRS04_Trig_1 PORTD |= (1 << 4); // Test
#define HCRS04_Trig_0 PORTD &= ~(1 << 4); // Test
#define USART_Wait_For_Free while(USART_Busy){};
#define BAUDRATE 38400 // Set desired baud rate
#define UBRRVAL ((8000000/(BAUDRATE*16UL))-1) // Calculate UBRR value
char USART_buffer[128];
unsigned char USART_Positone=0;
unsigned char USART_Busy=0;
void USART_Init();
void USART_Transmit();
//#########################################################################
union {
unsigned long _long;
unsigned short _short;
unsigned char _char[4];
} MyVel;
union {
unsigned long _a1;
unsigned char _a4[4];
} All;
int main(void) {
DDRA = 0xff;
DDRB = 0xff;
DDRC = 0xff;
DD_HCRS04_Trig_Out;
DD_HCRS04_Echo_In;
char x1 = 0;
USART_Init();
while (1) {
HCRS04_Trig_1;
_delay_ms(200);
HCRS04_Trig_0;
x1 = PIND & 0b00100000;
if (x1==0) {
while (1) {
x1 = PIND & 0b00100000;
if (x1!=0) break;
}
MyVel._long = 0; PORTA = 0; PORTB = 0; PORTC = 0;
while (1) {
MyVel._long++;
x1 = PIND & 0b00100000;
if (x1==0) break;
}
USART_Wait_For_Free;
sprintf(USART_buffer,"Distance is: %lu\n\r", MyVel._long);
USART_Transmit();
unsigned char i;
unsigned long st = 325;
All._a1 = 1;
for (i = 0; i <= 24; i++) {
if (st < MyVel._long) {
st += 325;
All._a1 = All._a1 << 1;
All._a1+=1;
}
}
PORTA = All._a4[0];
PORTB = All._a4[1];
PORTC = All._a4[2];
_delay_ms(8);
} else {
_delay_ms(25);
}
}
}
//#########################################################################
void USART_Init()
{
UBRR0L= UBRRVAL; // Set baud rate, low byte
UBRR0H=(UBRRVAL>>8); // Set baud rate, high byte
UCSR0C = (0<<UMSEL0)| //
(0<<UPM01) | //
(0<<UPM00) | //
(0<<USBS0) | //
(1<<UCSZ01)| //
(1<<UCSZ00)| //
(0<<UCPOL0); //
UCSR0B = (0<<RXEN)| // Enable USART Receiver
(1<<TXEN) | // Enable USART Transmitter
(0<<RXCIE) | // Enable USART Interrupt on receive complete
(1<<TXCIE);
sei();
}
void USART_Transmit() {
USART_Positone = 0 ;
if (USART_buffer[USART_Positone]!=0) {
USART_Busy = 1 ;
UDR0=USART_buffer[USART_Positone];
} else {
USART_Busy = 0 ;
}
}
ISR(USART0_TX_vect) {
USART_Positone ++;
if (USART_buffer[USART_Positone]!=0) {
UDR0=USART_buffer[USART_Positone];
} else {
USART_Busy = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment