]>
cloudbase.mooo.com Git - irmp.git/blob - irmp-main-avr-uart.c
1 /*---------------------------------------------------------------------------------------------------------------------------------------------------
2 * irmp-main-avr-uart.c - demo main module to test IRMP decoder on AVR with UART
4 * Copyright (c) 2009-2016 Frank Meyer - frank(at)fli4l.de
6 * $Id: irmp-main-avr-uart.c,v 1.4 2016/11/18 11:48:47 fm Exp $
8 * This demo module is runnable on AVRs with UART
10 * ATMEGA88 @ 8 MHz internal RC Osc with BODLEVEL 4.3V: lfuse: 0xE2 hfuse: 0xDC efuse: 0xF9
11 * ATMEGA88 @ 8 MHz external Crystal Osc with BODLEVEL 4.3V: lfuse: 0xFF hfuse: 0xDC efuse: 0xF9
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *---------------------------------------------------------------------------------------------------------------------------------------------------
26 /*---------------------------------------------------------------------------------------------------------------------------------------------------
28 *---------------------------------------------------------------------------------------------------------------------------------------------------
31 #include <util/setbaud.h>
35 #define UART0_UBRRH UBRR0H
36 #define UART0_UBRRL UBRR0L
37 #define UART0_UCSRA UCSR0A
38 #define UART0_UCSRB UCSR0B
39 #define UART0_UCSRC UCSR0C
40 #define UART0_UDRE_BIT_VALUE (1<<UDRE0)
41 #define UART0_UCSZ1_BIT_VALUE (1<<UCSZ01)
42 #define UART0_UCSZ0_BIT_VALUE (1<<UCSZ00)
44 #define UART0_URSEL_BIT_VALUE (1<<URSEL0)
46 #define UART0_URSEL_BIT_VALUE (0)
48 #define UART0_TXEN_BIT_VALUE (1<<TXEN0)
49 #define UART0_UDR UDR0
50 #define UART0_U2X U2X0
54 #define UART0_UBRRH UBRRH
55 #define UART0_UBRRL UBRRL
56 #define UART0_UCSRA UCSRA
57 #define UART0_UCSRB UCSRB
58 #define UART0_UCSRC UCSRC
59 #define UART0_UDRE_BIT_VALUE (1<<UDRE)
60 #define UART0_UCSZ1_BIT_VALUE (1<<UCSZ1)
61 #define UART0_UCSZ0_BIT_VALUE (1<<UCSZ0)
63 #define UART0_URSEL_BIT_VALUE (1<<URSEL)
65 #define UART0_URSEL_BIT_VALUE (0)
67 #define UART0_TXEN_BIT_VALUE (1<<TXEN)
76 UART0_UBRRH
= UBRRH_VALUE
; // set baud rate
77 UART0_UBRRL
= UBRRL_VALUE
;
80 UART0_UCSRA
|= (1<<UART0_U2X
);
82 UART0_UCSRA
&= ~(1<<UART0_U2X
);
85 UART0_UCSRC
= UART0_UCSZ1_BIT_VALUE
| UART0_UCSZ0_BIT_VALUE
| UART0_URSEL_BIT_VALUE
;
86 UART0_UCSRB
|= UART0_TXEN_BIT_VALUE
; // enable UART TX
90 uart_putc (unsigned char ch
)
92 while (!(UART0_UCSRA
& UART0_UDRE_BIT_VALUE
))
111 uart_puts_P (PGM_P s
)
115 while ((ch
= pgm_read_byte(s
)) != '\0')
123 itoh (char * buf
, uint8_t digits
, uint16_t number
)
125 for (buf
[digits
] = 0; digits
--; number
>>= 4)
127 buf
[digits
] = "0123456789ABCDEF"[number
& 0x0F];
135 #if defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__) // ATtiny45 / ATtiny85:
137 #if F_CPU >= 16000000L
138 OCR1C
= (F_CPU
/ F_INTERRUPTS
/ 8) - 1; // compare value: 1/15000 of CPU frequency, presc = 8
139 TCCR1
= (1 << CTC1
) | (1 << CS12
); // switch CTC Mode on, set prescaler to 8
141 OCR1C
= (F_CPU
/ F_INTERRUPTS
/ 4) - 1; // compare value: 1/15000 of CPU frequency, presc = 4
142 TCCR1
= (1 << CTC1
) | (1 << CS11
) | (1 << CS10
); // switch CTC Mode on, set prescaler to 4
146 OCR1A
= (F_CPU
/ F_INTERRUPTS
) - 1; // compare value: 1/15000 of CPU frequency
147 TCCR1B
= (1 << WGM12
) | (1 << CS10
); // switch CTC Mode on, set prescaler to 1
151 TIMSK1
= 1 << OCIE1A
; // OCIE1A: Interrupt by timer compare
153 TIMSK
= 1 << OCIE1A
; // OCIE1A: Interrupt by timer compare
157 #ifdef TIM1_COMPA_vect // ATtiny84
158 #define COMPA_VECT TIM1_COMPA_vect
160 #define COMPA_VECT TIMER1_COMPA_vect // ATmega
163 ISR(COMPA_VECT
) // Timer1 output compare A interrupt service routine, called every 1/15000 sec
165 (void) irmp_ISR(); // call irmp ISR
166 // call other timer interrupt routines...
175 irmp_init(); // initialize irmp
176 timer1_init(); // initialize timer1
177 uart_init(); // initialize uart
179 sei (); // enable interrupts
183 if (irmp_get_data (&irmp_data
))
185 uart_puts_P (PSTR("protocol: 0x"));
186 itoh (buf
, 2, irmp_data
.protocol
);
189 #if IRMP_PROTOCOL_NAMES == 1
190 uart_puts_P (PSTR(" "));
191 uart_puts_P (pgm_read_word (&(irmp_protocol_names
[irmp_data
.protocol
])));
194 uart_puts_P (PSTR(" address: 0x"));
195 itoh (buf
, 4, irmp_data
.address
);
198 uart_puts_P (PSTR(" command: 0x"));
199 itoh (buf
, 4, irmp_data
.command
);
202 uart_puts_P (PSTR(" flags: 0x"));
203 itoh (buf
, 2, irmp_data
.flags
);
206 uart_puts_P (PSTR("\r\n"));