Advertisement
Einstein_R

Untitled

Aug 3rd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <compat/twi.h>
  5. #include <inttypes.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8.  
  9. #define F_CPU 8000000UL  // 8 MHz
  10. #define USART_BAUDRATE 9600
  11. #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
  12. #define ENG_SIG_TAB_SIZE 16
  13.  
  14.  
  15. volatile uint16_t TDelay = 100; //Zmienna obsługiwna w przerwaniach
  16. char * komunikat = "Test!";
  17. volatile char  wartosc[4];
  18. volatile uint8_t i = 0;
  19. volatile uint8_t ii = 0;
  20. volatile uint8_t pozycja[ENG_SIG_TAB_SIZE] = {5,0,45,0,40,0,46,0,6,0,54,0,48,0,53,0};
  21. //volatile uint8_t pozycja[ENG_SIG_TAB_SIZE] = {0,0};
  22. //volatile uint8_t pozycja[8] = {5,45,40,46,6,54,48,53};
  23. //volatile uint8_t pozycja[8] = {5,48,6,40,5,48,6,40};
  24. //volatile uint8_t pozycja[8] = {1,9,8,10,2,18,16,17};
  25. //volatile uint8_t pozycja[16] = {1,0,9,0,8,0,10,0,2,0,18,0,16,0,17,0};
  26. //volatile uint8_t pozycja[8] = {1,8,2,16,1,8,2,16};
  27. //volatile uint8_t pozycja[8] = {5,40,6,48,5,40,6,48};
  28. volatile uint16_t time = 1;
  29. volatile uint16_t speed = 100;
  30. volatile bool kierunek =0;
  31. void USART_Init(void)
  32. {
  33.     UCSRA &= ~(1<<U2X); //Rejest ustawienia preskalera do obliczania BAUD
  34.     UBRRL = (unsigned char)BAUD_PRESCALE; //współczynnik do okreslenia predkosci transmisji 9600 kb/s (UBBR=f_sys/(16*BAUD)-1)
  35.     UBRRH = (BAUD_PRESCALE >> 8);
  36.     UCSRC &= ~(1<<UMSEL); //Draca asynchroniczna
  37.     //Odkomentowanie tego nie dzi
  38.     //UCSRC |= (1<<URSEL); //Mozliwosc konfigurowania UCSRC
  39.     //UCSRC |= ((1<<UCSZ1) | (1<<UCSZ0)); //Bity okreslajace wielkosc wysyłanych danych (nie ramki)
  40.     UCSRB |= ((1<<RXEN) | (1<<TXEN)); //wlaczone moduly nadawcze i odbiorcze
  41.     UCSRB |= (1<<RXCIE); //Umozliwia wyzwolenia przerwan przy odbiorze
  42. }
  43.  
  44. void Timer_Init(void)
  45. {
  46.     TCCR1B |= (1<<CS12)|(1<<WGM12);
  47.     TIMSK |= (1<<OCIE1A);
  48.     OCR1A = speed;
  49. }
  50. void strSend(char* msg)
  51. {
  52.     uint8_t j=0;
  53.         while(*(msg + j) != 0)
  54.         {
  55.             uartSend(*(msg + j));
  56.             j++;
  57.         }
  58.         uartSend('\r');
  59.         uartSend('\n');
  60. }
  61.  
  62. void uartSend(uint8_t u8Data)
  63. {
  64.     while((UCSRA &(1<<UDRE)) == 0);
  65.     UDR = u8Data;
  66. }
  67.  
  68. ISR(USART_RXC_vect) //Odczyt z UART
  69. {
  70.     //przyjmowanie jednej liczby
  71.     uint8_t dane = UDR;
  72.     if(dane >= '0' && dane <= '9')
  73.             time = dane - 48;
  74.     if (dane == 'A')
  75.         speed = speed - 5;
  76.     if (dane == 'B')
  77.         speed = speed + 1;
  78.     if (dane == 'C')
  79.         kierunek = 0;
  80.     if (dane == 'D')
  81.             kierunek = 1;
  82.         OCR1A = speed;
  83.         itoa(speed, wartosc, 10);
  84.         strSend(wartosc);
  85.         _delay_ms(10);
  86. }
  87.  
  88. ISR(INT0_vect) // Interrupt PD2
  89. {
  90. i=0;
  91.     while(*(komunikat + i) != 0)
  92.     {
  93.         uartSend(*(komunikat + i));
  94.         i++;
  95.     }
  96. uartSend('\r');
  97. uartSend('\n');
  98. }
  99.  
  100. ISR(TIMER1_COMPA_vect)
  101. {
  102.     (kierunek)?ii++:ii--;
  103.     ii=ii % ENG_SIG_TAB_SIZE;
  104.     PORTC = pozycja[ii];
  105. }
  106.  
  107.  
  108. int main(void){
  109.     DDRD &=~(1<<PD2);// Set INT0 pin
  110.     PORTD |=(1<<PD2);// Set pin INT0 high
  111.     DDRD |= (1 <<PD7)|(1<<PD6); //Set OU0T
  112.     //DDRD &= ~(1<<PD6); //Set IN
  113.     PORTD |= (1<<PD6);
  114.     DDRC = 0b00111111;
  115.  
  116.     cli(); // blokowanie przerwań CLEAR INTERRUPTS
  117.     GICR |=(1<<INT0); //uaktywnienie INT0 w rejestrz GICR s.46 datasheet
  118.     MCUCR |=(1<<ISC01); //ustawienie INT0 na zbocze opadajace s.65 datasheet
  119.     USART_Init(); //Inicjalizacja komunikacji USART
  120.     Timer_Init(); //Ustawienia Timera
  121.     sei(); // odblokowanie przerwan globalnych SET INTERRUPTS
  122.  
  123.  
  124.  
  125.  
  126.     while(1){
  127.         //_delay_ms(100*time);
  128.         //PORTD ^=(1<<PD7);
  129.     }
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement