Debian Codegraph @

doc/examples/asmdemo/asmdemo.c

raw
1 /*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * Joerg Wunsch wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
7 * ----------------------------------------------------------------------------
8 *
9 * Demo combining C and assembly source files.
10 *
11 * This demo implements an RC model type PWM decoder. The incoming
12 * PWM signal consists of a pulse sequence with a pulse width of 920
13 * microseconds up to 2120 microseconds (1520 microseconds being the
14 * neutral point). Depending on the value of the decoded incoming
15 * PWM, an outgoing PWM is controlled between 0 and 100 %.
16 *
17 * The project is intented to be run on an ATtiny13 that has only one
18 * timer channel (timer 0), so both the incoming signal discrimination
19 * as well as the outgoing PWM need to run on the same timer.
20 *
21 * For verification purposes, the same project can also be run on an
22 * ATtiny25/45/85, where timer 1 can be used to evaluate the incoming
23 * PWM signal, and timer 0 to generate the outgoing PWM. In that
24 * case, no additional assembly code is needed.
25 *
26 * $Id$
27 */
28
29 /*
30 * This is the main C source file for the demo.
31 */
32 #include <avr/interrupt.h>
33 #include <avr/io.h>
34 #include <avr/sleep.h>
35
36 #include "project.h"
37
38 volatile uint16_t pwm_incoming;
39 volatile struct
40 {
41 uint8_t pwm_received: 1;
42 }
43 intbits;
44
45 void
46 ioinit(void)
47 {
48 counter_hi = 0;
49 flags = 0;
50
51 /*
52 * Timer 0 runs as phase-correct PWM at full clock, OC0B connects to
53 * the PWM engine.
54 */
55 TCCR0A = (1 << COM0B1) | (1 << WGM00);
56 TCCR0B = (1 << CS00);
57 OCR0A = 255;
58
59 #if defined(__AVR_ATtiny13__)
60 TIMSK0 = (1 << TOIE0) | (1 << OCIE0A);
61
62 # define F_CPU 1200000ul
63 /* Minimal PWM pulse width is 920 us. */
64 # define MIN_PWM_VAL ((920ul * F_CPU) / 1000000ul)
65 /* Maximal PWM pulse width is 2120 us */
66 # define MAX_PWM_VAL ((2120ul * F_CPU) / 1000000ul)
67
68 #elif defined(__AVR_ATtiny25__) ||\
69 defined(__AVR_ATtiny45__) ||\
70 defined(__AVR_ATtiny85__)
71
72 # define F_CPU 1000000ul
73 /*
74 * We use a prescaler of 16 here to avoid the 32-bit calculations
75 * below.
76 */
77 /* Minimal PWM pulse width is 920 us. */
78 # define MIN_PWM_VAL ((920ul * F_CPU) / 16 / 1000000ul)
79 /* Maximal PWM pulse width is 2120 us */
80 # define MAX_PWM_VAL ((2120ul * F_CPU) / 16 / 1000000ul)
81
82 #else
83 # error "Don't know how to run on your MCU_TYPE."
84 #endif
85
86 PCMSK = (1 << 4);
87 GIFR = (1 << PCIF);
88 GIMSK = (1 << PCIE);
89
90 DDRB = (1 << PB1);
91 PORTB = 0;
92
93 sei();
94 }
95
96 #if defined(__AVR_ATtiny25__) ||\
97 defined(__AVR_ATtiny45__) ||\
98 defined(__AVR_ATtiny85__)
99 ISR(PCINT0_vect)
100 {
101 uint8_t tcnt1;
102
103 if (PINB & (1 << 4))
104 {
105 /* Start timer 1 with a prescaler of 16. */
106 TCNT1 = 0;
107 TCCR1 = (1 << CS12) | (1 << CS10);
108 return;
109 }
110
111 /* Stop timer 1, current value is pulse width. */
112 tcnt1 = TCNT1;
113 TCCR1 = 0;
114 GIMSK &= ~(1 << PCIE);
115
116 pwm_incoming = tcnt1;
117 intbits.pwm_received = 1;
118 }
119 #endif /* ATtinyX5 */
120
121 int
122 main(void)
123 {
124
125 ioinit();
126
127 for (;;)
128 {
129 if (intbits.pwm_received)
130 {
131 intbits.pwm_received = 0;
132 #if defined(__AVR_ATtiny13__)
133 if (pwm_incoming < MIN_PWM_VAL)
134 pwm_incoming = MIN_PWM_VAL;
135 else if (pwm_incoming > MAX_PWM_VAL)
136 pwm_incoming = MAX_PWM_VAL;
137 OCR0B = (pwm_incoming - MIN_PWM_VAL) * 255ul / (MAX_PWM_VAL - MIN_PWM_VAL);
138 #else
139 OCR0B = (pwm_incoming - MIN_PWM_VAL) * 255u / (MAX_PWM_VAL - MIN_PWM_VAL);
140 #endif
141 GIFR = (1 << PCIF);
142 GIMSK |= (1 << PCIE);
143 }
144 sleep_mode();
145 }
146 }