AVRly - AVR Development Resources
am_radio.h File Reference

Driver for a small, low power AM radio transmitter, to broadcast monophonic square wave tones over a short distance. This project was inspired by the book "Make: AVR Programming" by Elliot Williams. More...

#include <stdint.h>
#include "scale.h"

Go to the source code of this file.

Enumerations

enum  frequency_khz_t {
  _500_KHZ = 500000 , _570_KHZ = 570000 , _670_KHZ = 670000 , _800_KHZ = 800000 ,
  _1000_KHZ = 1000000 , _1333_KHZ = 1333000
}
 Enum to expose the predefined carrier frequency options. More...
 
enum  note_duration_t {
  SEMIBREVE , MINIM , CROTCHET , QUAVER ,
  SEMIQUAVER , DEMISEMIQUAVER
}
 Enum to expose the predefined duration options. More...
 

Functions

void init_am_radio (frequency_khz_t carrier_freq)
 Initialises timer/counter for radio transmission at a specified carrier frequency. More...
 
void am_radio_set_tempo (uint8_t tempo)
 Sets tempo for musical tones to be transmitted (in BPM). More...
 
void am_radio_transmit_note (note_pitch_t pitch, note_duration_t note_length)
 Transmits a square wave, monophonic note for the specified duration. More...
 
void am_radio_rest (note_duration_t rest_length)
 Waits a specified duration before playing the next note. More...
 
void arpeggio_in_c_test (void)
 Test sequence, transmits an arpeggio scale in C major. More...
 

Detailed Description

Driver for a small, low power AM radio transmitter, to broadcast monophonic square wave tones over a short distance. This project was inspired by the book "Make: AVR Programming" by Elliot Williams.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author
Jason Duffy
Date
10th September 2022

Only tested with F_CPU = 8MHZ, but will build on this later to make it work with other CPU speeds.

Definition in file am_radio.h.

Enumeration Type Documentation

◆ frequency_khz_t

Enum to expose the predefined carrier frequency options.

1000KHZ (1MHZ) is ideal, but may be taken in your locality so try another if there is too much interferance.

Definition at line 48 of file am_radio.h.

49{
50 _500_KHZ = 500000,
51 _570_KHZ = 570000,
52 _670_KHZ = 670000,
53 _800_KHZ = 800000,
54 _1000_KHZ = 1000000,
55 _1333_KHZ = 1333000,
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 at line 62 of file am_radio.h.

63{
64 SEMIBREVE,
65 MINIM,
66 CROTCHET,
67 QUAVER,
68 SEMIQUAVER,
69 DEMISEMIQUAVER,
note_duration_t
Enum to expose the predefined duration options.
Definition: am_radio.h:63

Function Documentation

◆ init_am_radio()

void init_am_radio ( frequency_khz_t  carrier_freq)

Initialises timer/counter for radio transmission at a specified carrier frequency.

Definition at line 47 of file am_radio.c.

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}

◆ am_radio_set_tempo()

void am_radio_set_tempo ( uint8_t  tempo)

Sets tempo for musical tones to be transmitted (in BPM).

Definition at line 80 of file am_radio.c.

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}

◆ am_radio_transmit_note()

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 at line 94 of file am_radio.c.

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}

◆ am_radio_rest()

void am_radio_rest ( note_duration_t  rest_length)

Waits a specified duration before playing the next note.

Definition at line 119 of file am_radio.c.

120{
121 // Wait for specified time.
122 for (uint16_t count = 0; count < duration[rest_length]; ++count)
123 {
124 _delay_ms(1);
125 }
126}

◆ arpeggio_in_c_test()

void arpeggio_in_c_test ( void  )

Test sequence, transmits an arpeggio scale in C major.

Definition at line 131 of file am_radio.c.

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}
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 am_radio_rest(note_duration_t rest_length)
Waits a specified duration before playing the next note.
Definition: am_radio.c:119