Skip to content

Instantly share code, notes, and snippets.

@ibanezmatt13
Last active August 29, 2015 13:59
Show Gist options
  • Save ibanezmatt13/10498980 to your computer and use it in GitHub Desktop.
Save ibanezmatt13/10498980 to your computer and use it in GitHub Desktop.
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdint.h>
#define RADIOPIN 9
volatile char *ptr = NULL; // pointer to datastring buffer
volatile boolean sentence_needed = true;
char send_datastring[100] = "";
void setup() {
pinMode(RADIOPIN, OUTPUT);
setPwmFrequency(RADIOPIN, 1);
initialise_interrupt();
}
void initialise_interrupt()
{
// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
OCR1A = F_CPU/16000-1; // DOMINOEX16
TCCR1B |= (1 << WGM12); // turn on CTC mode:
// Set CS10 and CS12 bits for:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
sei(); // enable global interrupts
}
ISR(TIMER1_COMPA_vect)
{
static uint8_t current_symbol = 0;
static uint8_t current_char = 0x00;
static uint8_t symbol_index = 0;
uint8_t next_symbol;
next_symbol = varicode[current_char][symbol_index++]; // get next symbol from varicode table
current_symbol = (current_symbol + next_symbol + 2) % 18; // update for next symbol transmission
analogWrite(RADIOPIN, current_symbol);
// if reached end of either 2 or 3 symbol character, set index to 3 for executing next code
if(symbol_index < 3 && !(varicode[current_char][symbol_index] & 0x08)) {
symbol_index = 3;
}
// if we still have more symbols to send
if(symbol_index != 3){
return;
}
symbol_index = 0; // reset symbol index for next character
if(sentence_needed == false && ptr){
current_char = *ptr;
ptr++;
if(current_char){
sentence_needed = false;
}
else{
sentence_needed = true;
}
}
else{
sentence_needed = true;
}
}
// the loop routine runs over and over again forever:
void loop() {
if (sentence_needed){
sprintf(send_datastring, "%s", "Hello World!");
sentence_needed = true;
ptr = send_datastring;
sentence_needed = false;
}
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1:
mode = 0x01;
break;
case 8:
mode = 0x02;
break;
case 64:
mode = 0x03;
break;
case 256:
mode = 0x04;
break;
case 1024:
mode = 0x05;
break;
default:
return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
}
else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
}
else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1:
mode = 0x01;
break;
case 8:
mode = 0x02;
break;
case 32:
mode = 0x03;
break;
case 64:
mode = 0x04;
break;
case 128:
mode = 0x05;
break;
case 256:
mode = 0x06;
break;
case 1024:
mode = 0x7;
break;
default:
return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment