Hallo zusammnen,
da ich ein Fan von kleinen Mikrocontrollern bin, beschäftige ich mich gerade mit dem Attiny13. Um damit mal praktisch was zu steuern, habe ich einfach einen an meinen Avoider III Roboter drangebastelt, damit der mal ein komplexers Verhalten zeigen kann, als immer nur zurückzulaufen, wenn er auf eine Wand trifft.
Der Controller ist über einen Steckverbinder angeschlossen, beim entfernen des Controllers erhält der AVOIDER seine ursprüngleich Funktion zurück.
Code:********************************************************************* AVOIDER III Brain Bug This progam can control a AVOIDER III robot with an Attiny 13 processor. First M1 and M2 are used as inputs. The program detects an IR-Reflection by a change of PortB4. If a change is detected the Ports are switched to output and the Attiny controls the Motors. Processor: Attiny13 Make shure to set the internal fuses to 9.6Mhz systemclock PORTB0: PORTB1: PORTB2: PORTB3: M1 PORTB4: M2 PORTB5: RESET AVOIDER III modifications: 1. cut conducting path from pin 10 of the OP and insert a 100KOHM resistor between 2. cut conducitng path between pin 6 and pin 8 and insert a 100kOhm resistor 3. connect signal M1 to pin 10 4. connect signal M2 to pin 6 The rights of this program are under GNU license. You may copy,modify and redistribute the program with open source code 18.12.2005 stochri **********************************************************************/ #include <avr/interrupt.h> #include <avr/signal.h> typedef unsigned char byte; void init_timer() { TCCR0A=(1<<COM0A0) | 0x02; //CTC mode and toogle OC0A port on compare match TCCR0B=0x01; // internal clock source no prescaling OCR0A=138; // Compare value // calculated frequency at 9.6Mhz for 36.36kHz at toogle pin (*2 toogle )=(9.6e6/((132+1))) // 36kHz->27.77us , 27.77us*36=1ms // counter runs with 72kHz // OCR0A soll: 132 with frequncy counter ajusted value 5%=+6 } #define MILISECOND 72 void delay(unsigned int ticks) { unsigned int n; for (n=0;n<ticks;n++) { while(!(TIFR0<<OCF0A)); // wait for compare flag TIFR0|=1<<OCF0A; // clear compare flag } } void msdelay(unsigned int ms) { unsigned int n; for (n=0;n<ms;n++) { delay(MILISECOND); } } #define R_L_FAST 0x00 #define R_SLOW 0x08 #define MOTORS_OFF 0x10 #define L_BACK 0x18 int main(void) { byte n,flag; init_timer(); cli(); DDRB=0; // All ports as input while(1) { flag=(PINB&(1<<PINB4)); // detect an IR reflection for(n=1;n<10;n++) // make 10 loops to suppress motor disturbances { flag&=(PINB&(1<<PINB4)); // Test PORT bin if IR-Reflektion msdelay(1); } if(flag) // control the motors { DDRB=(1<<PINB3) | (1<<PINB4); // Pins set to OUTPUT PORTB=MOTORS_OFF; msdelay(1000); PORTB=R_L_FAST; msdelay(500); PORTB=MOTORS_OFF; msdelay(500); PORTB=R_L_FAST; msdelay(500); PORTB=MOTORS_OFF; msdelay(500); PORTB=L_BACK; msdelay(2500); PORTB=R_L_FAST; DDRB=0; // All ports as input msdelay(2000); } } return 0; }







Zitieren

Lesezeichen