added Sonar -> LCD example

pull/1/head
Ondřej Hruška 10 years ago
parent 0c0feb1482
commit fb4f23bcab
  1. 2
      examples/Makefile
  2. 63
      examples/sonar_to_lcd.c
  3. 3
      lib/lcd.c

@ -11,7 +11,7 @@ EFUSE = 0x05
## === Source files === ## === Source files ===
# Main C file # Main C file
MAIN = uart_stream.c MAIN = sonar_to_lcd.c
# Extra C files in this folder # Extra C files in this folder
LOCAL_SOURCE = LOCAL_SOURCE =

@ -0,0 +1,63 @@
//
// Basic Sonar example
//
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdbool.h>
#include "lib/arduino_pins.h"
#include "lib/sonar.h"
#include "lib/stream.h"
#include "lib/lcd.h"
// Sonar interrupts
ISR(PCINT0_vect)
{
if (sonar_handle_pci()) return;
}
ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
ISR(TIMER1_OVF_vect)
{
if (sonar_handle_t1ovf()) return;
}
void main()
{
lcd_init();
// Init sonar
sonar_t so;
sonar_init(&so, A0, A1);
sei();
while(1) {
// Measure
sonar_start(&so);
while(sonar_busy);
int16_t res = sonar_result;
// Print
lcd_clear();
if (sonar_result < 0) {
put_str(lcd, "---");
} else {
put_i16f(lcd, res, 1); // one decimal place
put_str(lcd, " cm");
}
// a delay...
_delay_ms(200);
}
}

@ -308,6 +308,9 @@ void lcd_home()
void lcd_clear() void lcd_clear()
{ {
lcd_command(LCD_CLEAR); lcd_command(LCD_CLEAR);
_pos.x = 0;
_pos.y = 0;
_addrtype = TEXT;
} }

Loading…
Cancel
Save