Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <stdio.h>
- #define BAUD 9600 // baudrate
- #define UBRR_VALUE ((F_CPU)/16/(BAUD)-1) // zgodnie ze wzorem
- // inicjalizacja UART
- void uart_init()
- {
- // ustaw baudrate
- UBRR0 = UBRR_VALUE;
- // wyczyść rejestr UCSR0A
- UCSR0A = 0;
- // włącz odbiornik i nadajnik
- UCSR0B = _BV(RXEN0) | _BV(TXEN0);
- // ustaw format 8n1
- UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
- }
- // transmisja jednego znaku
- int uart_transmit(char data, FILE *stream)
- {
- // czekaj aż transmiter gotowy
- while(!(UCSR0A & _BV(UDRE0)));
- UDR0 = data;
- return 0;
- }
- // odczyt jednego znaku
- int uart_receive(FILE *stream)
- {
- // czekaj aż znak dostępny
- while (!(UCSR0A & _BV(RXC0)));
- return UDR0;
- }
- FILE uart_file;
- void initTimer(void) {
- TCCR1B |= (1 << WGM12); // Ustawienie trybu CT
- TCCR1B |= (1 << CS12); // Ustawienie preskalera na wartość 256
- TCCR1B |= (1<<ICES1); // reakcja na zbocze narastające
- TIMSK1 |= (1<<ICIE1); // przerwania trybu capture
- OCR1A = 31249;
- DDRB &= ~(1<<PB0); //ICP1
- }
- volatile uint16_t prev = 0;
- volatile uint16_t new = 0;
- ISR (TIMER1_CAPT_vect)
- {
- new = ICR1-prev;
- prev = new;
- }
- int main()
- {
- // zainicjalizuj UART
- uart_init();
- // skonfiguruj strumienie wejścia/wyjścia
- fdev_setup_stream(&uart_file, uart_transmit, uart_receive, _FDEV_SETUP_RW);
- stdin = stdout = stderr = &uart_file;
- initTimer();
- // odmaskuj przerwania
- sei();
- // program testowy
- while(1)
- {
- printf("%f\r\n", (float) 31250.0/(float)new); //wypisz czestotliwosc
- _delay_ms(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement