void sonar_print(void) { uint8_t i; for (i=0; i<10; i++) { usart->tx0_buffer = utoa(sonar[i], usart->tx0_buffer, 10); usart_printstr(0, NULL); usart_printstr(0, " "); } usart_printstr(0, "\n"); }
void loop() { // cc++; // if (cc>60000) { // status = !status; // if (status) { // SETP(RELAY_PIN); // } else { // CLRP(RELAY_PIN); // } // cc = 0; // } if (usart_chrready()) { char ch = usart_getchr(); switch (ch) { case 'h': { usart_printstr("\n\r" "h - help\n\r" "o - open doors\n\r" "c - close doors\n\r" "s = status\n\r"); break; } case 'o': { SETP(RELAY_PIN); status = 1; break; } case 'c': { CLRP(RELAY_PIN); status = 0; break; } case 's': { if (status) { usart_printstr("1"); } else { usart_printstr("0"); } break; } default: { break; } } usart_printstr("\n\rok\n\r"); } }
int main(void) { uint16_t counter; char c; uint8_t stop = TRUE; usart_init(); sonar_init(); rtc_setup(); counter = 0; usart_resume(0); strcpy_P(usart->tx0_buffer, PSTR("\nTsunami Simulator ")); strcat_P(usart->tx0_buffer, PSTR(GITREL)); strcat_P(usart->tx0_buffer, PSTR("\n\nConnected!\n")); usart_printstr(0, NULL); rtc_start(); while (1) { /* Restart the counter */ rtc_clear(); c = usart_getchar(0, FALSE); switch (c) { case '0': stop = TRUE; break; case '1': stop = FALSE; break; default: break; } if (stop) { /* stop the code */ while(usart_getchar(0, FALSE) != '1'); stop = FALSE; rtc_clear(); } /* send the trigger */ sonar_trigger(); /* clear all the data */ sonar_clear(); /* * Wait 40mS maximum and collect all the * data during the period. */ while (rtc_us < 4000) sonar_set(); /* * speed = ((i * SCALEuS)/1000000) * 340 / 2 * where: * i * SCALEuS is the duration in uS of the signal. * (i * SCALEuS)/1000000 is the same in seconds. * 340 is the speed of the sound and * /2 we need only half of the way. * The simplyfied formula in cm. * 340 mm/msec = 34cm/msec = 0.029 msec/cm = 29 uS/cm * dist (cm) = T (uS) / 29 /2. */ usart->tx0_buffer = utoa(counter, usart->tx0_buffer, 10); usart_printstr(0, NULL); usart_printstr(0, " "); counter++; sonar_print(); /* if the counter has already reach 50mS, * then this cycle takes too long. */ if (rtc_us > 5000) usart_printstr(0, "Warning! Time overrun.\n"); /* Wait up to 50mS before restart */ while (rtc_us < 5000); } return(0); }