// UART file descriptor c
// putchar and getchar are in uart.c
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
// semaphore to protect shared variable
#define SEM_SHARED 3
int args[3] ;
volatile char timer0_overflows;
volatile char mic_state;
volatile char mic_order [3];
volatile unsigned int sound_time1;
volatile unsigned int sound_time2;
volatile char vector_update_flag;
char servo_update_flag;
float sound_vector;
int servo_min_ticks;
int servo_max_ticks;
#define servo_min 0.535 //period in ms
#define servo_max 2.425 //period in ms
#define speed_sound 34029.0 //speed in cm/sec
#define mic_distance 14.0 //distance in cm
int cur_deg;
int avg_deg;
int num_samples;
int next_deg;
//MICROPHONE #1 - B0
ISR (PCINT1_vect) {
if (!(PINB & 0x01)) {
switch (mic_state) {
case 0:
TCNT2= 0; //restart timer
mic_state= 1; //update state
mic_order[0]= 1; //update mic order
break;
code
case 1:
sound_time1= TCNT2; //save timer value1
mic_state= 2; //update state
mic_order[1]= 1; //update mic order
break;
case 2:
sound_time2= TCNT2; //save timer value2
mic_state= 3;
mic_order[2]= 1; //update mic order
vector_update_flag=1; //UPDATE VECTOR!!
PORTD &= ~(1<<PIND2);
break;
default:
mic_state= -1;
break;
}
}
else { //Mic 1 off => reset
PORTD |= (1<<PIND2);
mic_state= 0;
}
}
//MICROPHONE #2 - C0
ISR (PCINT2_vect) {
if (!(PINC & 0x01)) {
switch (mic_state) {
case 0:
TCNT2= 0; //restart timer
mic_state= 1; //update state
mic_order[0]= 2; //update mic order
break;
code
case 1:
sound_time1= TCNT2; //save timer value1
mic_state= 2; //update state
mic_order[1]= 2; //update mic order
break;
case 2:
sound_time2= TCNT2; //save timer value2
mic_state= 3;
mic_order[2]= 2; //update mic order
vector_update_flag=1; //UPDATE VECTOR!!
PORTD &= ~(1<<PIND2);
break;
default:
mic_state= -1;
break;
}
}
else { //Mic 2 off => reset
PORTD |= (1<<PIND2);
mic_state= 0;
}
}
//MICROPHONE #3 - D0
ISR (PCINT3_vect) {
if (!(PIND & 0x01)) {
switch (mic_state) {
case 0:
TCNT2= 0; //restart timer
mic_state= 1; //update state
mic_order[0]= 3; //update mic order
break;
code
case 1:
sound_time1= TCNT2; //save timer value1
mic_state= 2; //update state
mic_order[1]= 3; //update mic order;
break;
case 2:
sound_time2= TCNT2; //save timer value2
mic_state= 3;
mic_order[2]= 3; //update mic order
vector_update_flag=1; //UPDATE VECTOR!!
PORTD &= ~(1<<PIND2);
break;
default:
mic_state= -1;
break;
}
}
else { //Mic 3 off => reset
PORTD |= (1<<PIND2);
mic_state= 0;
}
// --- TASK2: Servo Control ----------------------------------------
void servo_control(void* args) {
uint32_t rel, dead ;
int deg_window = 15;
int update_window = 7;
while (1) {
trtWait(SEM_SHARED);
if (servo_update_flag == 1){ //flag to update servo position
servo_update_flag = 0;
//sleep, run at 50Hz
rel = trtCurrentTime() + SECONDS2TICKS(0.02);
dead = trtCurrentTime() + SECONDS2TICKS(0.02);
trtSleepUntil(rel, dead);
}
}
// --- MAIN -----------------------------------------------------------
int main(void) {
initialize();
code
//init the UART -- trt_uart_init() is in trtUart.c
trt_uart_init();
stdout = stdin = stderr = &uart_str;
fprintf(stdout,"\n\r TRT 9feb2009\n\r\n\r");
// start TRT
trtInitKernel(80); // 80 bytes for the idle task stack
// --- create semaphores ----------
// You must creat the first two semaphores if you use the uart
trtCreateSemaphore(SEM_RX_ISR_SIGNAL, 0) ; // uart receive ISR semaphore
trtCreateSemaphore(SEM_STRING_DONE,0) ; // user typed <enter>
// variable protection
trtCreateSemaphore(SEM_SHARED, 1) ; // protect shared variables
// --- creat tasks ----------------
trtCreateTask(sound_calculation, 400, SECONDS2TICKS(0.1), SECONDS2TICKS(0.25), &(args[0]));
trtCreateTask(servo_control, 200, SECONDS2TICKS(0.1), SECONDS2TICKS(0.1), &(args[1]));
// Idle task
// just sleeps the cpu to save power
// every time it executes
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
while (1) {
sleep_cpu();
}
}
// --- SERVO CONTROL -------------------------------------------
void servo(float degree) {
int cmpMatch;
degree= degree/2.0;
// fprintf(stdout,"/n/r %i \n\r",(int)degree);
It looks like the provided code is for a microcontroller program that uses the Timer/Counter and Interrupt Service Routines (ISRs) to control a servo motor and process sound signals from multiple microphones. The program seems to be using the Two-Rate Tasking (TRT) real-time operating system.
Here's a brief overview of the main components and functionality in the code:
ISRs: The code defines several Interrupt Service Routines (ISRs) for handling external interrupts, timer overflows, and controlling the servo motor. These ISRs are responsible for capturing sound timings, updating mic states, and controlling the servo motor.
Tasks: The code also defines two tasks - sound_calculation and servo_control that run concurrently using the TRT real-time operating system. The sound_calculation task calculates the sound vector based on the timings and mic orders, and the servo_control task updates the servo position based on the calculated sound vector.
Initialization: The initialize function is used to set up the microcontroller's timers, external interrupts, and other necessary configurations.
Main Function: The main function initializes the system, creates semaphores, creates tasks, and then enters an idle state.
Servo Control: The servo function is used to control the servo motor based on the desired degree.
Variable Protection: The code uses semaphores to protect shared variables and ensure mutual exclusion in accessing critical sections of the code.
It seems like the code is designed to capture sound timings from multiple microphones, calculate the sound vector, and use this information to control the position of a servo motor. The use of TRT suggests that the system is designed to be real-time responsive.
If you have any specific questions or need further assistance with understanding or modifying this code, feel free to ask!