timer: Avoid infinite cycle timer on TCNT write
[simavr] / examples / board_timer_64led / atmega168_timer_64led.h
1 /*
2         atmega168_timer_64led.h
3         
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 #ifndef __COMMON_H__
22 #define __COMMON_H__
23
24
25 #include <util/delay.h>
26 #include <avr/io.h>
27 #include <stdint.h>
28 #include <stdio.h>
29
30 static inline void delay_ms(uint16_t millis)
31 {
32  // uint16_t loop;
33   while ( millis-- )
34         _delay_ms(1);
35 }
36
37 #include <avr/pgmspace.h>
38
39 #define printf(format, ...) printf_P(PSTR(format), ## __VA_ARGS__)
40 #define sprintf(wh, format, ...) sprintf_P(wh, PSTR(format), ## __VA_ARGS__)
41
42 /*!
43         Define pin accessors.
44         given a pin name, port, bit number and mask (how many bits it takes) this macro
45         defines a set of inline accessors to set/clear/read the pin
46  */
47 #define PIN_DEFINE(__name, __port, __pin, __mask) \
48         enum { __name##_PIN = (__pin), __name##_MASK = (__mask << __pin) }; \
49         /* toggle pin in PORT */static inline void TOG_##__name() { PIN##__port ^= __mask << __pin; } \
50         /* Clear Pin */                 static inline void CLR_##__name() { PORT##__port &= ~(__mask << __pin); } \
51         /* Set pin to 1 */              static inline void SET_##__name() { PORT##__port |= (__mask << __pin); } \
52         /* Set pin to 0/1 */    static inline void SET_##__name##_V(uint8_t __val) { PORT##__port = (PORT##__port & ~(__mask << __pin)) | (__val << __pin); } \
53         /* Get pin value */             static inline uint8_t GET##__name() { return (PIN##__port >> __pin) & __mask; } \
54         /* Set pin direction */ static inline void DDR_##__name(uint8_t __val) { DDR##__port = (DDR##__port & ~(__mask << __pin)) | (__val << __pin); }
55
56 #if VERBOSE
57 #define V(w) w
58 #else
59 #define V(w)
60 #endif
61
62 #endif // __COMMON_H__