00001 #ifndef F_CPU
00002 #warning F_CPU is undefined! Defaulting to 10 MHz.
00003 #define F_CPU 10000000UL
00004 #endif
00005 #include <avr/io.h>
00006 #include <avr/interrupt.h>
00007 #include <util/delay.h>
00008 #include "usart.h"
00009 #include "common.h"
00010 #include "lcdproxima.h"
00011
00012
00013
00014
00015
00016
00017 #ifdef DEBUG
00018 #ifndef ENABLE_USART
00019 #define ENABLE_USART
00020 #endif
00021 #endif
00022
00023 #define TOSTR(s) XSTR(s)
00024 #define XSTR(s) #s
00025
00026 #define LED_DDR (DDRC)
00027 #define LED_PORT (PORTC)
00028 #define LED_PIN (PC0)
00029
00030 #define LED_ON LED_PORT |= (1 << LED_PIN)
00031 #define LED_OFF LED_PORT &= ~(1 << LED_PIN)
00032 #define LED_TOGGLE LED_PORT ^= (1 << LED_PIN)
00033
00034
00035 #define SONAR_INIT_DDR (DDRC)
00036 #define SONAR_INIT_PORT (PORTC)
00037 #define SONAR_INIT_PIN (PC1)
00038
00039 #define SONAR_POWER_DDR (DDRD)
00040 #define SONAR_POWER_PORT (PORTD)
00041 #define SONAR_POWER_PIN (PD6)
00042
00043 #define SONAR_ECHO_DDR (DDRB)
00044 #define SONAR_ECHO_PORT (PINB)
00045 #define SONAR_ECHO_PIN (PB0)
00046
00047 #define SONAR_SET_INIT SONAR_INIT_PORT |= (1 << SONAR_INIT_PIN)
00048 #define SONAR_CLEAR_INIT SONAR_INIT_PORT &= ~(1 << SONAR_INIT_PIN)
00049 #define SONAR_POWER_ON SONAR_POWER_PORT &= ~(1 << SONAR_POWER_PIN)
00050 #define SONAR_POWER_OFF SONAR_POWER_PORT |= (1 << SONAR_POWER_PIN)
00051 #define SONAR_GET_ECHO (SONAR_ECHO_PORT & (1 << SONAR_ECHO_PIN))
00052
00053
00054 #define MAX_DELAY (100000)
00055
00056 volatile uint16_t ov_counter = 0;
00057 volatile uint16_t icr1 = 0;
00058 volatile bool_t captured = FALSE;
00059
00060
00061
00062
00063 ISR(TIMER1_OVF_vect)
00064 {
00065 ov_counter++;
00066 }
00067
00068
00069
00070
00071
00072 ISR(TIMER1_CAPT_vect)
00073 {
00074 icr1 = ICR1;
00075 captured = TRUE;
00076 }
00077
00078
00079
00080
00081 int main (void)
00082 {
00083 uint8_t c = 0;
00084 uint32_t i = 0;
00085 uint8_t mcusr = 0;
00086
00087 mcusr = MCUSR;
00088 MCUSR = 0;
00089
00090 LED_DDR |= 1 << LED_PIN;
00091 SONAR_INIT_DDR |= 1 << SONAR_INIT_PIN;
00092 SONAR_POWER_DDR |= 1 << SONAR_POWER_PIN;
00093
00094 SONAR_ECHO_PORT |= 1 << SONAR_ECHO_PIN;
00095 SONAR_ECHO_DDR &= 1 << SONAR_ECHO_PIN;
00096
00097 SONAR_POWER_OFF;
00098 SONAR_CLEAR_INIT;
00099 LED_ON;
00100
00101 #ifdef ENABLE_USART
00102 USART0_init ();
00103 USART0_puts (
00104 "\r\n"
00105 "\r\n"
00106 "AVR sonar started\r\n"
00107
00108 "Compiled on " __DATE__ " " __TIME__ "\r\n"
00109 );
00110 #endif
00111 LCDP_init ();
00112 LCDP_all ();
00113 _delay_ms (500);
00114 LCDP_clear ();
00115
00116 #ifdef ENABLE_USART
00117 if (mcusr & JTRF)
00118 {
00119 USART0_puts ("JTAG reset detected\r\n");
00120 }
00121 if (mcusr & WDRF)
00122 {
00123 USART0_puts ("Watchdog reset detected\r\n");
00124 }
00125 if (mcusr & BORF)
00126 {
00127 USART0_puts ("Brown-out reset detected\r\n");
00128 }
00129 if (mcusr & EXTRF)
00130 {
00131 USART0_puts ("External reset detected\r\n");
00132 }
00133 if (mcusr & PORF)
00134 {
00135 USART0_puts ("Power-on reset detected\r\n");
00136 }
00137 if (mcusr)
00138 {
00139 USART0_puts ("\r\n");
00140 }
00141 #endif
00142
00143
00144
00145 TIMSK1 = (1 << ICIE1) | (1 << TOIE1);
00146
00147
00148 TCCR1B = (1 << ICNC1) | (1 << ICES1) | (1 << CS10);
00149
00150
00151
00152 while (1)
00153 {
00154
00155 LED_ON;
00156 #ifdef DEBUG
00157 USART0_printf ("\r\n"
00158 "Power on sonar\r\n");
00159 #endif
00160 SONAR_POWER_ON;
00161
00162 _delay_ms (10);
00163 #ifdef DEBUG
00164 USART0_printf ("Initialize\r\n");
00165 #endif
00166
00167 captured = 0;
00168 icr1 = 0;
00169 ov_counter = 0;
00170 ICR1 = 0;
00171 TCNT1 = 0;
00172 sei ();
00173 SONAR_SET_INIT;
00174
00175 for (i = 0; (i < MAX_DELAY) && !captured; i++)
00176 {
00177 asm ("nop");
00178 }
00179 cli ();
00180 if (captured)
00181 {
00182 float distance = 0.0;
00183 float time = 0.0;
00184
00185
00186 const float speedOfSound = 346.0;
00187 const float distanceMax = 7.51;
00188 const float distanceMin = 0.44;
00189
00190
00191 #ifdef DEBUG
00192 USART0_printf ("======> Echo received! i: %lu, icr1: %u, ov_counter: %u\r\n", i, icr1, ov_counter);
00193 #endif
00194
00195 time = (float) 1.0 / (float) F_CPU * (float) ((uint32_t) icr1 + (((uint32_t) ov_counter) << 16));
00196 time -= 2.38e-3;
00197 #ifdef ENABLE_USART
00198 USART0_printf ("Time: %lu us\r\n", (uint32_t) (time * 1e6));
00199 #endif
00200
00201 distance = speedOfSound * time / (float) 2.0;
00202 distance -= 0.31;
00203 #ifdef ENABLE_USART
00204 USART0_printf ("Distance: %u cm\r\n", (uint16_t) (distance * 100.0));
00205 #endif
00206
00207 if (distance > distanceMax)
00208 {
00209 latches |= LCDP_SEGMENT_MAX;
00210 }
00211 else
00212 {
00213 latches &= ~LCDP_SEGMENT_MAX;
00214 }
00215 if (distance < distanceMin)
00216 {
00217 latches |= LCDP_SEGMENT_ARROW0;
00218 }
00219 else
00220 {
00221 latches &= ~LCDP_SEGMENT_ARROW0;
00222 }
00223 latches |= LCDP_SEGMENT_2DP;
00224 LCDP_printNumber (distance * 100.0, FALSE);
00225 }
00226 else
00227 {
00228 LCDP_printNumberHex (0xE);
00229 #ifdef ENABLE_USART
00230 USART0_printf ("------> No echo received!\r\n");
00231 #endif
00232 }
00233 LED_OFF;
00234
00235 _delay_ms (10);
00236 SONAR_CLEAR_INIT;
00237 _delay_ms (10);
00238 #ifdef DEBUG
00239 USART0_printf ("Power off sonar\r\n");
00240 #endif
00241 SONAR_POWER_OFF;
00242 #ifdef DEBUG
00243 USART0_printf ("Wait\r\n");
00244 #endif
00245 _delay_ms (350);
00246 }
00247 return 0;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268