Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4. #include <stdio.h>
  5.  
  6.  
  7. #define BAUD 9600 // baudrate
  8. #define UBRR_VALUE ((F_CPU)/16/(BAUD)-1) // zgodnie ze wzorem
  9.  
  10. // inicjalizacja UART
  11. void uart_init()
  12. {
  13. // ustaw baudrate
  14. UBRR0 = UBRR_VALUE;
  15. // wyczyść rejestr UCSR0A
  16. UCSR0A = 0;
  17. // włącz odbiornik i nadajnik
  18. UCSR0B = _BV(RXEN0) | _BV(TXEN0);
  19. // ustaw format 8n1
  20. UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
  21. }
  22.  
  23. // transmisja jednego znaku
  24. int uart_transmit(char data, FILE *stream)
  25. {
  26. // czekaj aż transmiter gotowy
  27. while(!(UCSR0A & _BV(UDRE0)));
  28. UDR0 = data;
  29. return 0;
  30. }
  31.  
  32. // odczyt jednego znaku
  33. int uart_receive(FILE *stream)
  34. {
  35. // czekaj aż znak dostępny
  36. while (!(UCSR0A & _BV(RXC0)));
  37. return UDR0;
  38. }
  39.  
  40. FILE uart_file;
  41.  
  42. void initTimer(void) {
  43. TCCR1B |= (1 << WGM12); // Ustawienie trybu CT
  44. TCCR1B |= (1 << CS12); // Ustawienie preskalera na wartość 256
  45. TCCR1B |= (1<<ICES1); // reakcja na zbocze narastające
  46. TIMSK1 |= (1<<ICIE1); // przerwania trybu capture
  47. OCR1A = 31249;
  48. DDRB &= ~(1<<PB0); //ICP1
  49.  
  50. }
  51. volatile uint16_t prev = 0;
  52. volatile uint16_t new = 0;
  53.  
  54. ISR (TIMER1_CAPT_vect)
  55. {
  56. new = ICR1-prev;
  57. prev = new;
  58. }
  59.  
  60. int main()
  61. {
  62. // zainicjalizuj UART
  63. uart_init();
  64. // skonfiguruj strumienie wejścia/wyjścia
  65. fdev_setup_stream(&uart_file, uart_transmit, uart_receive, _FDEV_SETUP_RW);
  66. stdin = stdout = stderr = &uart_file;
  67. initTimer();
  68. // odmaskuj przerwania
  69. sei();
  70. // program testowy
  71. while(1)
  72. {
  73. printf("%f\r\n", (float) 31250.0/(float)new); //wypisz czestotliwosc
  74. _delay_ms(500);
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement