/** * @brief Increment boot_time_ms variable and decrement timer array. * @param None * @retval None */ void timer_update_ms(void) { boot_time_ms++; /* each timer decrements every millisecond if > 0 */ for (unsigned i = 0; i < NTIMERS; i++) if (timer[i] > 0) timer[i]--; if (timer[TIMER_LED] == 0) { /* blink activitiy */ LEDToggle(LED_ACT); timer[TIMER_LED] = LED_TIMER_COUNT; } if (timer[TIMER_SONAR] == 0) { sonar_trigger(); timer[TIMER_SONAR] = SONAR_TIMER_COUNT; } if (timer[TIMER_SYSTEM_STATE] == 0) { send_system_state_now = true; timer[TIMER_SYSTEM_STATE] = SYSTEM_STATE_COUNT; } if (timer[TIMER_RECEIVE] == 0) { receive_now = true; timer[TIMER_RECEIVE] = SYSTEM_STATE_COUNT; } if (timer[TIMER_PARAMS] == 0) { send_params_now = true; timer[TIMER_PARAMS] = PARAMS_COUNT; } if (timer[TIMER_IMAGE] == 0) { send_image_now = true; timer[TIMER_IMAGE] = global_data.param[PARAM_VIDEO_RATE]; } if (timer[TIMER_LPOS] == 0) { send_lpos_now = true; timer[TIMER_LPOS] = LPOS_TIMER_COUNT; } }
void sonar_init() { sonar1_port.DIRCLR = echo1_mask; // set echo pin as input sonar2_port.DIRCLR = echo2_mask; sonar1_port.DIRSET = trigger_mask; sonar_timer.CTRLA = TC_CLKSEL_DIV64_gc; // set divider to 64: 32 MHz / 64 = 500 kHz sonar_timer.PER = 40000; // 500 kHz / 40000 = 12.5 Hz, 1/12.5 Hz = 80 ms to overflow sonar_timer.INTCTRLA = TC_OVFINTLVL_LO_gc; // set overflow interrupt to low level sonar1_port.PIN4CTRL = PORT_ISC_BOTHEDGES_gc; //Set pin interrupts to occur on the both edges sonar2_port.PIN4CTRL = PORT_ISC_BOTHEDGES_gc; sonar1_port.INT0MASK = echo1_mask; //Set echo pins in port to be part of an interrupt sonar2_port.INT0MASK = echo2_mask; sonar1_port.INTCTRL = TC_OVFINTLVL_LO_gc; // echo set to Medium Priority sonar2_port.INTCTRL = TC_OVFINTLVL_LO_gc; sonar_trigger(); }
int main() { // string buffer for printing to UART uint8_t output[128]; // disable the clock prescaler clock8MHz(); // disable interrupts during setup cli(); uart_init(UART_38400); sonar_init(); // enable interrupts sei(); for (;;) { sonar_trigger(); _delay_ms(36); if (sonar_echo_received() == 1) { distance = 0.0171 * sonar_get_distance() - 0.8192; snprintf((char*)output, 128, "distance %d\n\r", (int)distance); print(output); }else { snprintf((char*)output, 128, "sonar failed\n\r"); print(output); } _delay_ms(1000); } return 0; }
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); }
void sonar_update_fn(void) { sonar_trigger(); }