AVRly - AVR Development Resources
am_radio.c File Reference

Driver for a small, low power AM radio transmitter, to broadcast monophonic square wave tones over a short distance. More...

#include <avr/power.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "am_radio.h"
#include "pin_defines.h"

Go to the source code of this file.

Functions

void init_am_radio (frequency_khz_t carrier_freq)
 Initialises timer/counter for radio transmission at a specified carrier frequency. More...
 
 ISR (TIMER1_COMPA_vect)
 
void am_radio_set_tempo (uint8_t bpm)
 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.

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

Definition in file am_radio.c.

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}

◆ ISR()

ISR ( TIMER1_COMPA_vect  )

Definition at line 71 of file am_radio.c.

72{
73 ANTENNA_DDR ^= (1 << ANTENNA); // Toggle carrier on and off.
74}

◆ am_radio_set_tempo()

void am_radio_set_tempo ( uint8_t  bpm)

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