Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <inttypes.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdbool.h>
- /*
- UART-Init:
- Berechnung des Wertes für das Baudratenregister
- aus Taktrate und gewünschter Baudrate
- */
- #ifndef F_CPU
- #define F_CPU 16000000UL // Systemtakt in Hz - Definition als unsigned long beachten
- // Ohne ergeben sich unten Fehler in der Berechnung
- #endif
- #define BAUD 9600UL // Baudrate
- void uart_init(); //initialisieren
- uint8_t uart_putc(unsigned char c); //Char senden
- void uart_puts(char *s); //String senden
- uint8_t uart_getc(); //Char empfangen
- // Berechnungen
- #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // clever runden
- #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Reale Baudrate
- #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
- #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
- #error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!
- #endif
- int main(void)
- {
- uart_init();
- uart_puts("Hallo.");
- bool For = false;
- bool Back = false;
- bool Left = false;
- bool Right = false;
- DDRA = 0xFF;
- DDRD = 0xFF;
- while (1)
- {
- uint8_t c;
- c = uart_getc();
- switch(c)
- {
- case 'F':For=true;
- break;
- case '3':For=false;
- break;
- case 'B':Back = true;
- break;
- case '4':Back = false;
- break;
- case 'R':Right = true;
- break;
- case '1':Right = false;
- break;
- case 'L':Left = true;
- break;
- case '2':Left = false;
- break;
- }
- if((For == true && Back == false)) //FORWARD (L293D - Enable 1 High, Input 1 High, Input 2 Low, Enable 2 High, Input 3 High, Input 4 Low)
- {
- PORTA = (1<<PA0);
- uart_puts("OK");
- PORTD = (1<<PD6) | (1<<PD5); //Motor 1 - Output 1 High
- _delay_us(50);
- }
- if(Back == true && For == false) //BACKWARDS (L293D - Enable 1 High, Input 1 Low, Input 2 High, Enable 2 High, Input 3 Low, Input 4 High)
- {
- PORTA = (1<<PA1);
- PORTD = 0x0;
- PORTD = (1<<PD6) | (1<<PD4); //Motor 2 - Output 4 High
- _delay_us(50);
- }
- /*
- if(Right == true && Left == false) //Right (L293D - Enable 1 High, Input 1 High, Input 2 Low, Enable 2 High, Input 3 Low, Input 4 High)
- {
- PORTA = (1<<PA1);
- PORTD = 0x0;
- uart_puts("OK - Right");
- }
- if(Left == true && Right == false) //Left (L293D - Enable 1 High, Input 1 Low, Input 2 High, Enable 2 High, Input 3 High, Input 4 Low)
- {
- PORTA = (1<<PA3);
- PORTD = 0x0;
- PORTD = (1<<PD2) | (1<<PD4); //Motor 1 - Output 2 High
- PORTD = (1<<PD5) | (1<<PD6); //Motor 2 - Output 4 High
- uart_puts("OK - Left");
- }
- if (For == false && Back == false && Left== false && Right ==false)
- {
- PORTA = (1<<PA1);;
- PORTD = 0x0;
- _delay_us(50);
- }
- */
- }
- }
- void uart_init(void)
- {
- UBRRH = UBRR_VAL >> 8;
- UBRRL = UBRR_VAL & 0xFF;
- UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // Asynchron 8N1
- UCSRB |= (1<<RXEN); // UART RX einschalten
- UCSRB |= (1<<TXEN); // UART TX einschalten
- }
- /* Zeichen empfangen */
- uint8_t uart_getc(void)
- {
- while (!(UCSRA & (1<<RXC))) // warten bis Zeichen verfuegbar
- ;
- return UDR; // Zeichen aus UDR an Aufrufer zurueckgeben
- }
- //Char senden
- uint8_t uart_putc(unsigned char c)
- {
- while (!(UCSRA & (1<<UDRE))) // warten bis Senden moeglich
- {
- }
- UDR = c; // sende Zeichen
- return 0;
- }
- //====================================
- //String senden
- void uart_puts(char *s)
- {
- while (*s)
- { // so lange *s != '\0' also ungleich dem "String-Endezeichen"
- uart_putc(*s);
- s++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment