Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #define F_CPU 16000000UL
  2. // (I have also tried disabling the 16 MHz clock and fall back
  3. // to the 8 MHz clock, setting OCR1A to 0x03EF, with no success)
  4.  
  5. void uart1_init() {
  6. uint16_t baudrate = UART_BAUD_SELECT(BAUD485, F_CPU));
  7. UBRR1H = (uint8_t)(baudrate>>8);
  8. UBRR1L = (uint8_t) baudrate;
  9.  
  10. // Clear USART Transmit complete flag, normal USART transmission speed
  11. UCSR1A = (1 << TXC1) | (0 << U2X1);
  12.  
  13. // Enable receiver, transmitter and receive interrupt
  14. UCSR1B = (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1);
  15.  
  16. // Asynchronous mode, no parity, 1 stop bit, character size = 8-bit
  17. UCSR1C = (1 << UCSZ11) | (1 << UCSZ10) | (0 << UCPOL1);
  18. }
  19.  
  20. // somewhere later: sei();
  21.  
  22. volatile uint32_t time = 0;
  23.  
  24. void timer_init(void) {
  25. // This code sets up Timer1 for a 1ms @ 16Mhz Clock (mode 4)
  26. OCR1A = 0x07CF;
  27. // Mode 4, CTC on OCR1A
  28. TCCR1B |= (1 << WGM12);
  29. // Set interrupt on output compare match
  30. TIMSK1 |= (1 << OCIE1A);
  31. // Set prescaler to 8 and start the timer
  32. TCCR1B |= (1 << CS11) ;
  33. }
  34.  
  35. ISR(TIMER1_COMPA_vect) {
  36. // Action to be done every 1 ms
  37. // Problem also occurs with empty interrupt
  38. // What I should have here: time++;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement