Guest User

l239D

a guest
Nov 15th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. #include <inttypes.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdbool.h>
  5.  
  6.  /*
  7.   UART-Init:
  8. Berechnung des Wertes für das Baudratenregister
  9. aus Taktrate und gewünschter Baudrate
  10. */
  11.  
  12. #ifndef F_CPU
  13. #define F_CPU 16000000UL  // Systemtakt in Hz - Definition als unsigned long beachten
  14.                          // Ohne ergeben sich unten Fehler in der Berechnung
  15. #endif
  16.  
  17. #define BAUD 9600UL      // Baudrate
  18.  
  19. void uart_init();               //initialisieren
  20. uint8_t uart_putc(unsigned char c); //Char senden
  21. void uart_puts(char *s);        //String senden
  22. uint8_t uart_getc();               //Char empfangen
  23.  
  24. // Berechnungen
  25. #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   // clever runden
  26. #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))     // Reale Baudrate
  27. #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
  28.  
  29. #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
  30.   #error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!
  31. #endif
  32.  
  33.     int main(void)
  34. {
  35.   uart_init();
  36.   uart_puts("Hallo.");
  37.   bool For = false;
  38.   bool Back = false;
  39.   bool Left = false;
  40.   bool Right = false;
  41.   DDRA = 0xFF;
  42.   DDRD = 0xFF;
  43.   while (1)
  44.   {  
  45.      uint8_t c;
  46.     c = uart_getc();
  47. switch(c)
  48.         {
  49.     case 'F':For=true;
  50.     break;
  51.    
  52.     case '3':For=false;
  53.     break;
  54.    
  55.     case 'B':Back = true;
  56.     break;
  57.    
  58.     case '4':Back = false;
  59.     break;
  60.    
  61.     case 'R':Right = true;
  62.     break;
  63.    
  64.     case '1':Right = false;
  65.     break;
  66.    
  67.     case 'L':Left = true;
  68.     break;
  69.    
  70.     case '2':Left = false;
  71.     break;
  72.         }
  73.        
  74.        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)
  75.        {
  76.            PORTA = (1<<PA0);
  77.            uart_puts("OK");
  78.            PORTD = (1<<PD6) | (1<<PD5); //Motor 1 - Output 1 High
  79.            _delay_us(50);          
  80.        }
  81.        
  82.        
  83.        
  84.        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)
  85.        {
  86.            PORTA = (1<<PA1);
  87.            PORTD = 0x0;
  88.            PORTD = (1<<PD6) | (1<<PD4);  //Motor 2 - Output 4 High
  89.            _delay_us(50);
  90.        }
  91.        
  92.        
  93.        /*
  94.          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)
  95.        {
  96.            PORTA = (1<<PA1);
  97.            PORTD = 0x0;
  98.  
  99.            uart_puts("OK - Right");
  100.        }
  101.        
  102.        
  103.          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)
  104.        {
  105.            PORTA = (1<<PA3);
  106.            PORTD = 0x0;
  107.            PORTD = (1<<PD2) | (1<<PD4); //Motor 1 - Output 2 High
  108.            PORTD = (1<<PD5) | (1<<PD6);  //Motor 2 - Output 4 High
  109.            uart_puts("OK - Left");
  110.        }
  111.        
  112.        
  113.        if (For == false && Back == false && Left== false && Right ==false)
  114.        {
  115.            PORTA = (1<<PA1);;
  116.            PORTD = 0x0;
  117.             _delay_us(50);
  118.        }
  119.        */
  120.        
  121.     }
  122. }
  123.  
  124. void uart_init(void)
  125. {
  126.     UBRRH = UBRR_VAL >> 8;
  127.     UBRRL = UBRR_VAL & 0xFF;
  128.    
  129.     UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);  // Asynchron 8N1
  130.     UCSRB |= (1<<RXEN);                        // UART RX einschalten
  131.     UCSRB |= (1<<TXEN);                        // UART TX einschalten
  132. }
  133.  
  134. /* Zeichen empfangen */
  135. uint8_t uart_getc(void)
  136. {
  137.     while (!(UCSRA & (1<<RXC)))   // warten bis Zeichen verfuegbar
  138.     ;
  139.     return UDR;                   // Zeichen aus UDR an Aufrufer zurueckgeben
  140. }
  141.  
  142. //Char senden
  143. uint8_t uart_putc(unsigned char c)
  144. {
  145.     while (!(UCSRA & (1<<UDRE)))  // warten bis Senden moeglich
  146.     {
  147.     }
  148.    
  149.     UDR = c;                      // sende Zeichen
  150.     return 0;
  151. }
  152. //====================================
  153. //String senden
  154. void uart_puts(char *s)
  155. {
  156.     while (*s)
  157.     {   // so lange *s != '\0' also ungleich dem "String-Endezeichen"
  158.         uart_putc(*s);
  159.         s++;
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment