AVRly - AVR Development Resources
am_radio.c
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file am_radio.c
25 * @ingroup am_radio
26 * @author Jason Duffy
27 * @date 10th September 2022
28 * @brief Driver for a small, low power AM radio transmitter, to broadcast
29 * monophonic square wave tones over a short distance.
30 */
31
32#include <avr/power.h>
33#include <avr/interrupt.h>
34#include <util/delay.h>
35
36#include "am_radio.h"
37#include "pin_defines.h"
38
39
40// Set default values based on 120BPM tempo.
41static uint16_t duration[6] = {2000,1000,500,250,125,62};
42
43
44/*
45 * Initialises timer/counter for radio transmission at a specified carrier frequency.
46 */
48{
49 cli();
50 // ----------------- TIMER 0 setup ------------------ //
51 TCCR0A |= (1 << WGM01); // Timer 0 in CTC mode.
52 TCCR0A |= (1 << COM0B0); // Toggle OC0B on compare match.
53 TCCR0B |= (1 << CS00); // Prescaler of 1.
54
55 // F_CPU / (2 * 1 * (1 + 3)) = Carrier_freq
56 // 8,000,000 / (2 * 1 * (1 + 3)) = 1,000,000
57 uint8_t count_value = (((F_CPU / carrier_freq) / 2) - 1); // calculate count value.
58 OCR0A = count_value; // Set the carrier frequency.
59
60 // ----------------- TIMER 1 setup ------------------ //
61
62 // TODO: Make this setup dependant on F_CPU and carrier_freq_khz.
63
64 TCCR1B |= (1 << WGM12); // Timer 1 in CTC mode.
65 TCCR1B |= (1 << CS11); // Prescaler of 8.
66 TIMSK1 |= (1 << OCIE1A); // Enable output compare match interrupt.
67 sei();
68}
69
70
71ISR(TIMER1_COMPA_vect)
72{
73 ANTENNA_DDR ^= (1 << ANTENNA); // Toggle carrier on and off.
74}
75
76
77/*
78 * Sets tempo for musical tones to be transmitted (in BPM).
79 */
80void am_radio_set_tempo(uint8_t bpm)
81{
82 duration[SEMIBREVE] = (60000 / bpm) * 4;
83 duration[MINIM] = (duration[SEMIBREVE] >> 1); // Div by 2
84 duration[CROTCHET] = (duration[MINIM] >> 1); // Div by 2
85 duration[QUAVER] = (duration[CROTCHET] >> 1); // Div by 2
86 duration[SEMIQUAVER] = (duration[QUAVER] >> 1); // Div by 2
87 duration[DEMISEMIQUAVER] = (duration[SEMIQUAVER] >> 1); // Div by 2
88}
89
90
91/*
92 * Transmits a square wave, monophonic note for the specified duration.
93 */
95{
96 // Set pitch.
97 OCR1A = pitch;
98
99 // Enable interrupts.
100 sei();
101
102 // Wait for specified time.
103 for (uint16_t count = 0; count < duration[note_length]; ++count)
104 {
105 _delay_ms(1);
106 }
107
108 // Disable interrupts.
109 cli();
110
111 // Back to full carrier freq.
112 ANTENNA_DDR |= (1 << ANTENNA);
113}
114
115
116/*
117 * Waits a specified duration before playing the next note.
118 */
120{
121 // Wait for specified time.
122 for (uint16_t count = 0; count < duration[rest_length]; ++count)
123 {
124 _delay_ms(1);
125 }
126}
127
128
129/*
130 * Test sequence, transmits an arpeggio scale in C major.
132{
133 am_radio_transmit_note(C2, CROTCHET);
134 am_radio_rest(QUAVER);
135 am_radio_transmit_note(E2, CROTCHET);
136 am_radio_rest(QUAVER);
137 am_radio_transmit_note(G2, CROTCHET);
138 am_radio_rest(QUAVER);
139 am_radio_transmit_note(C3, CROTCHET);
140 am_radio_rest(QUAVER);
141 am_radio_transmit_note(G2, CROTCHET);
142 am_radio_rest(QUAVER);
143 am_radio_transmit_note(E2, CROTCHET);
144 am_radio_rest(QUAVER);
145}
146
147
148
149/*** end of file ***/
150
void am_radio_transmit_note(note_pitch_t pitch, note_duration_t note_length)
Transmits a square wave, monophonic note for the specified duration.
Definition: am_radio.c:94
void init_am_radio(frequency_khz_t carrier_freq)
Initialises timer/counter for radio transmission at a specified carrier frequency.
Definition: am_radio.c:47
void arpeggio_in_c_test(void)
Test sequence, transmits an arpeggio scale in C major.
Definition: am_radio.c:131
void am_radio_rest(note_duration_t rest_length)
Waits a specified duration before playing the next note.
Definition: am_radio.c:119
void am_radio_set_tempo(uint8_t bpm)
Sets tempo for musical tones to be transmitted (in BPM).
Definition: am_radio.c:80
Driver for a small, low power AM radio transmitter, to broadcast monophonic square wave tones over a ...
frequency_khz_t
Enum to expose the predefined carrier frequency options.
Definition: am_radio.h:49
note_duration_t
Enum to expose the predefined duration options.
Definition: am_radio.h:63
ISR(TIMER0_COMPA_vect)
Interrupt Service Routine.
Definition: digital_clock.c:57
Definitions for pin mapping (for CCS81 gas sensor)
note_pitch_t
Enum to expose the count values required to generate the pitches.
Definition: scale.h:39