Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
timerAttach(TIMER2OVERFLOW_INT, &sample_sensors );
void sample_sensors(void) // this exists to call in the timers, no input allowed { // track time time = (time + 1) % TICSPERSECOND; // get next tic and scale to 1 second increments if (time == 0) seconds_elapsed = seconds_elapsed + 1; if (seconds_elapsed == 60) { minutes_elapsed = minutes_elapsed + 1; seconds_elapsed = 0; } if (minutes_elapsed == 60) { hours_elapsed = hours_elapsed + 1; minutes_elapsed = 0; } sample_sensors_report(time, FALSE); }
Well I have some snippets that might help, it's part of a larger more complex program but I have an interrupt routine that checks a bunch of my sensors:In an init routine I set up the timer: Code: [Select]timerAttach(TIMER2OVERFLOW_INT, &sample_sensors );Then in the routine I update my real time clock and check my sensors:Code: [Select]void sample_sensors(void) // this exists to call in the timers, no input allowed { // track time time = (time + 1) % TICSPERSECOND; // get next tic and scale to 1 second increments if (time == 0) seconds_elapsed = seconds_elapsed + 1; if (seconds_elapsed == 60) { minutes_elapsed = minutes_elapsed + 1; seconds_elapsed = 0; } if (minutes_elapsed == 60) { hours_elapsed = hours_elapsed + 1; minutes_elapsed = 0; } sample_sensors_report(time, FALSE); }Does this give you something to start with? What do you want to do in your interrupt?Doug
//declare these global in control.c#define TICSPERSECOND 1000unsigned int time;unsigned int seconds_elapsed;unsigned int minutes_elapsed;unsigned int hours_elapsed;void int_routine(void); // interrupt routine void int_routine(void) { time = (time + 1) % TICSPERSECOND; // get next tic and scale to 1 second increments if (time == 0) seconds_elapsed = seconds_elapsed + 1; if (seconds_elapsed == 60) { minutes_elapsed = minutes_elapsed + 1; seconds_elapsed = 0; } if (minutes_elapsed == 60) { hours_elapsed = hours_elapsed + 1; minutes_elapsed = 0; } } void control(void)// replace your control function with this { int seconds; time = hours_elapsed = minutes_elapsed = seconds_elapsed = 0; seconds = 0; // not completely sure if both of these are necessary but it's working for me timer2Init(); reset_timer_2(); delay_cycles(6000); // let system stabilize // once you do this the interrupt will be called // this timer is already scaled to 1000/second (TICSPERSECOND) above timerAttach(TIMER2OVERFLOW_INT, &int_routine ); while(1) { if(seconds_elapsed != seconds) // if timer ticks over then another second has gone by print time { //this will only be reached if interrupt routine is updating the global variables, i.e. seconds_elapsed, outside of this while loop seconds = seconds_elapsed; rprintf("H:%d M:%d S:%d ",hours_elapsed, minutes_elapsed, seconds_elapsed); } } }