
Zitat von
Lisbeth2010
Hallo, noch Interesse an Nibo und C?
Ich werde versuchen, meine Erkenntnisse mitzuteilen.
Ich benutze das Atmel Studio und die aktuelle Library.
Ich versuche, den Code zu kommentieren!
Viele Grüße auch Workwind, der offensichtlich fleißig mitliest!
Lisbeth
Code:
/* Linie einfachst. Erstmal Bodensensoren */
#include <avr/interrupt.h>
#include <nibo/niboconfig.h>
#include <nibo/iodefs.h>
#include <nibo/bot.h>
#include <nibo/i2cmaster.h>
#include <nibo/leds.h>
#include <nibo/delay.h>
#include <nibo/motco.h>
#include <nibo/irco.h>
#include <nibo/floor.h>
#include <nibo/adc.h>
#include "utiliw.h"
#define WHILE_1_TIME_GRID 20
uint8_t counter_1ms;
uint8_t speed_left = 40; // Ticks/Sekunde
uint8_t speed_right = 40; // Ticks/Sekunde
// ------------------------------------------------------------
// ---------- init timer2 -------------------------------------
// ------------------------------------------------------------
void init_timer2(void) {
TCCR2 = 0x00; // normal port operation
TCNT2 = 0x83; // set count value (131)
}
// ------------------------------------------------------------
// ---------- start timer2 ------------------------------------
// ------------------------------------------------------------
void start_timer2(void) {
cli(); // disable global interrupts
counter_1ms = 0;
TCCR2 |= 0x05; // CS02 = 1; CS01 = 0; CS00 = 1 -> clk/128
TIMSK |= (1<<TOIE2); //0x01; // enable timer overflow interrupt
sei(); // enable global interrupts
}
// ------------------------------------------------------------
// ---------- stop timer2 -------------------------------------
// ------------------------------------------------------------
void stop_timer2(void) {
cli(); // disable global interrupts
TCCR2 &= ~0x05; // stop timer
TIMSK &= ~(1<<TOIE2);//0x01; // disable timer overflow interrupt
sei(); // enable global interrupts
}
// ------------------------------------------------------------
// ---------- timer2 ISR --------------------------------------
// ------------------------------------------------------------
ISR(TIMER2_OVF_vect) { // timer2 overflow
// reload counter value high byte; 0x83 corresponds to 1ms
// clk/128 entspricht 125 kHz entspricht 8 us
TCNT2 = 0x83; // 131 bis 256 also 125 mal 8 us
counter_1ms++;
if (counter_1ms >= WHILE_1_TIME_GRID) {
stop_timer2(); // Zeitdauer unbekannt, deshalb stoppen
// Bodensensoren messen
floor_measure_iw();
//zahlausgabe(floor_mw[2]); // da kein Display!
if (floor_mw[2] < 32) { // schwarz = 0, weiß = 1023
motco_stop();
motco_update();
leds_set_status(LEDS_RED,0);
} else {
leds_set_status(LEDS_GREEN,0);
// delta_motco ms nach vorn fahren
motco_setSpeed(speed_left,speed_right);
motco_update(); // jetzt fährt er erst los!
} // end if floor
counter_1ms = 0;
start_timer2(); // alles abgearbeitet
} // end if counter
} // end ISR
/* die main */
int main() {
sei();
bot_init();
i2c_init();
leds_init();
floor_init();
init_timer2();
delay(2000); // damit er nicht gleich losrast!
motco_setSpeedParameters(5,4,6); // Empfehlung vom "Chef"
start_timer2();
while (1) {
// tu was
delay(10000);
}
return 0;
}
Lesezeichen