Changeset - c79b24c96a83
[Not reviewed]
default
0 9 2
ethanzonca@CL-SEC241-08.cedarville.edu - 13 years ago 2012-10-29 19:48:57
ethanzonca@CL-SEC241-08.cedarville.edu
Added afsk code, which is tested and works on the micro
11 files changed with 361 insertions and 19 deletions:
0 comments (0 inline, 0 general)
master/master/lib/afsk.c
Show inline comments
 
new file 100644
 
/*
 
 * fsk.c
 
 *
 
 * Created: 10/29/2012 7:40:34 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
/*
 
 * Master Firmware
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 

	
 
#define F_CPU 11059200
 

	
 
#include <stdint.h>
 
#include <avr/io.h>
 
#include <avr/interrupt.h> 
 
#include <util/delay.h>
 
#include <avr/pgmspace.h>
 

	
 
PROGMEM extern const uint8_t afsk_sine_table[512] = {
 
  127, 129, 130, 132, 133, 135, 136, 138, 139, 141, 143, 144, 146, 147, 149, 150, 152, 153, 155, 156, 158, 
 
  159, 161, 163, 164, 166, 167, 168, 170, 171, 173, 174, 176, 177, 179, 180, 182, 183, 184, 186, 187, 188, 
 
  190, 191, 193, 194, 195, 197, 198, 199, 200, 202, 203, 204, 205, 207, 208, 209, 210, 211, 213, 214, 215, 
 
  216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 228, 229, 230, 231, 232, 233, 234, 235, 236, 
 
  236, 237, 238, 239, 239, 240, 241, 242, 242, 243, 244, 244, 245, 245, 246, 247, 247, 248, 248, 249, 249, 
 
  249, 250, 250, 251, 251, 251, 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 
 
  254, 254, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 253, 253, 253, 253, 252, 252, 252, 251, 
 
  251, 251, 250, 250, 249, 249, 249, 248, 248, 247, 247, 246, 245, 245, 244, 244, 243, 242, 242, 241, 240, 
 
  239, 239, 238, 237, 236, 236, 235, 234, 233, 232, 231, 230, 229, 228, 228, 227, 226, 225, 224, 223, 221, 
 
  220, 219, 218, 217, 216, 215, 214, 213, 211, 210, 209, 208, 207, 205, 204, 203, 202, 200, 199, 198, 197, 
 
  195, 194, 193, 191, 190, 188, 187, 186, 184, 183, 182, 180, 179, 177, 176, 174, 173, 171, 170, 168, 167, 
 
  166, 164, 163, 161, 159, 158, 156, 155, 153, 152, 150, 149, 147, 146, 144, 143, 141, 139, 138, 136, 135, 
 
  133, 132, 130, 129, 127, 125, 124, 122, 121, 119, 118, 116, 115, 113, 111, 110, 108, 107, 105, 104, 102, 
 
  101,  99,  98,  96,  95,  93,  91,  90,  88,  87,  86,  84,  83,  81,  80,  78,  77,  75,  74,  72,  71, 
 
   70,  68,  67,  66,  64,  63,  61,  60,  59,  57,  56,  55,  54,  52,  51,  50,  49,  47,  46,  45,  44, 
 
   43,  41,  40,  39,  38,  37,  36,  35,  34,  33,  31,  30,  29,  28,  27,  26,  26,  25,  24,  23,  22, 
 
   21,  20,  19,  18,  18,  17,  16,  15,  15,  14,  13,  12,  12,  11,  10,  10,   9,   9,   8,   7,   7, 
 
    6,   6,   5,   5,   5,   4,   4,   3,   3,   3,   2,   2,   2,   1,   1,   1,   1,   0,   0,   0,   0, 
 
    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1,   1, 
 
    2,   2,   2,   3,   3,   3,   4,   4,   5,   5,   5,   6,   6,   7,   7,   8,   9,   9,  10,  10,  11, 
 
   12,  12,  13,  14,  15,  15,  16,  17,  18,  18,  19,  20,  21,  22,  23,  24,  25,  26,  26,  27,  28, 
 
   29,  30,  31,  33,  34,  35,  36,  37,  38,  39,  40,  41,  43,  44,  45,  46,  47,  49,  50,  51,  52, 
 
   54,  55,  56,  57,  59,  60,  61,  63,  64,  66,  67,  68,  70,  71,  72,  74,  75,  77,  78,  80,  81, 
 
   83,  84,  86,  87,  88,  90,  91,  93,  95,  96,  98,  99, 101, 102, 104, 105, 107, 108, 110, 111, 113, 
 
  115, 116, 118, 119, 121, 122, 124, 125
 
};
 

	
 
inline uint8_t afsk_read_sample(int phase)
 
{
 
  return pgm_read_byte_near(afsk_sine_table + phase);
 
}
 

	
 
volatile uint16_t phase = 10;
 
volatile uint16_t phasedelta = 3300;
 
extern const uint16_t TABLE_SIZE = sizeof(afsk_sine_table);
 

	
 
void afsk_setup() {
 
	
 
  // Source timer2 from clkIO (datasheet p.164)
 
  ASSR &= ~(_BV(EXCLK) | _BV(AS2));
 

	
 
  // Set fast PWM mode with TOP = 0xff: WGM22:0 = 3  (p.150)
 
  TCCR2A |= _BV(WGM21) | _BV(WGM20);
 
  TCCR2B &= ~_BV(WGM22);
 

	
 
  // Do non-inverting PWM on pin OC2B (arduino pin 3) (p.159).
 
  // OC2A (arduino pin 11) stays in normal port operation:
 
  // COM2B1=1, COM2B0=0, COM2A1=0, COM2A0=0
 
  TCCR2A = (TCCR2A | _BV(COM2B1)) & ~(_BV(COM2B0) | _BV(COM2A1) | _BV(COM2A0));
 

	
 
  // No prescaler (p.162)
 
  TCCR2B = (TCCR2B & ~(_BV(CS22) | _BV(CS21))) | _BV(CS20);
 
    
 
  // Enable interrupt when TCNT2 reaches TOP (0xFF) (p.151, 163)
 
	//TIMSK2 |= _BV(TOIE2);
 
	TIMSK2 |= 0b00000001;
 

	
 
	// Initialize PORTA
 
	DDRA = 0xff;
 
	PORTA = 0x00;
 
	
 
	DDRD = 0xff;
 
	PORTD = 0xff;
 
	
 
	sei();
 
}
 

	
 
void afsk_test() {	
 
	afsk_timer_start();
 
	_delay_ms(3);
 
	phasedelta = 1800;
 
	_delay_ms(3);
 
	phasedelta = 3300;
 
	_delay_ms(3);
 
	phasedelta = 1800;
 
	_delay_ms(3);
 
	phasedelta = 3300;
 
	_delay_ms(3);
 
	phasedelta = 1800;
 
	_delay_ms(3);
 
	phasedelta = 3300;
 
	_delay_ms(3);
 
	phasedelta = 1800;
 
	_delay_ms(3);
 
	phasedelta = 3300;
 
	_delay_ms(3);
 
	phasedelta = 1800;
 
	_delay_ms(3);
 
	phasedelta = 3300;
 
	_delay_ms(3);
 
	phasedelta = 1800;
 
	_delay_ms(3);
 
	phasedelta = 3300;
 
	afsk_timer_stop();
 
	_delay_ms(400);
 
}
 

	
 

	
 
// phase offset of 1800 gives ~1900 Hz
 
// phase offset of 3300 gives ~2200 Hz
 
ISR(TIMER2_OVF_vect) {
 
	phase = (phase + phasedelta);
 
	OCR2B = afsk_read_sample((phase >> 7) & (TABLE_SIZE - 1));
 
	return;
 
}
 

	
 
void afsk_timer_start()
 
{
 
	// Clear the overflow flag, so that the interrupt doesn't go off
 
	// immediately and overrun the next one (p.163).
 
	TIFR2 |= _BV(TOV2);       // Yeah, writing a 1 clears the flag.
 

	
 
	// Enable interrupt when TCNT2 reaches TOP (0xFF) (p.151, 163)
 
	TIMSK2 |= _BV(TOIE2);
 
}
 

	
 
void afsk_timer_stop()
 
{
 
	// Output 0v (after DC coupling)
 
	OCR2B = 0x00;
 

	
 
	// Disable playback interrupt
 
	TIMSK2 &= ~_BV(TOIE2);
 
}
master/master/lib/afsk.h
Show inline comments
 
new file 100644
 
/*
 
 * afsk.h
 
 *
 
 * Created: 10/29/2012 7:43:03 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef AFSK_H_
 
#define AFSK_H_
 
 
void afsk_setup();
 
void afsk_test();
 
void afsk_timer_start();
 
void afsk_timer_stop();
 
 
#endif /* AFSK_H_ */
 
\ No newline at end of file
master/master/lib/led.c
Show inline comments
 
@@ -7,7 +7,7 @@
 
 
#include <avr/io.h>
 
 
void led_Configure() {
 
void led_setup() {
 
	// Configure ports/pins for LEDs
 
}
 
master/master/lib/led.h
Show inline comments
 
@@ -14,7 +14,7 @@
 
#define STAT 2
 
#define OK 2
 
 
void led_Configure();
 
void led_setup();
 
void led_On(uint8_t led);
 
void led_Off(uint8_t led);
 
void led_Toggle(uint8_t led);
master/master/lib/serial.c
Show inline comments
 
@@ -39,7 +39,7 @@ void serial_SendResponseData(){
 
	
 
}
 
 
void serial_Configure() {
 
void serial_setup() {
 
	// Set registers!	
 
	
 
	// from mnl
master/master/lib/serial.h
Show inline comments
 
@@ -14,6 +14,6 @@
 
void serial_SendChar( char byte );
 
void serial_SendCommand( char moduleID, char measureType, uint8_t dataLength, char* data );
 
void serial_SendResponseData();
 
void serial_Configure();
 
void serial_setup();
 
 
#endif /* SERIAL_H_ */
 
\ No newline at end of file
master/master/lib/serparser.c
Show inline comments
 
@@ -5,9 +5,11 @@
 
 *  Author: ethanzonca
 
 */ 
 
 
#include <inttypes.h>
 
#include <avr/io.h>
 
 
// ************* Macros ***************
 
#define SERIAL_RX_HASBYTES UCSRA & _BV(RXC)
 
#define SERIAL_RX_HASBYTES UCSR0A & _BV(RXC0)
 
#define MAX_CMD_LEN 8
 
#define BROADCAST_ADDR 0
 
 
@@ -39,10 +41,10 @@ uint8_t buffercount = 0;
 
uint8_t uart_getchar(void)
 
{
 
	// Wait for chars
 
	while (!(UCSRA & (1<<RXC))){
 
		idle();
 
	while (!(UCSR0A & (1<<RXC0))){
 
		//idle
 
	}
 
	return UDR;
 
	return UDR0;
 
}
 
 
master/master/master.c
Show inline comments
 
@@ -12,21 +12,24 @@
 
#include <util/delay.h>
 
 
#include "lib/serial.h"
 
#include "lib/afsk.h"
 
 
 
void micro_Configure() {
 
void micro_setup() {
 
	// Generic microcontroller config options
 
}
 
 
int main(void)
 
{
 
	// Initialize
 
	micro_Configure();
 
	led_Configure();
 
	serial_Configure(); // Config serial ports
 
	micro_setup();
 
	led_setup();
 
	serial_setup(); // Config serial ports
 
	afsk_setup();
 
	
 
    while(1)
 
    {
 
        serial_SendCommand('0','A',0,0);
 
		afsk_test();
 
        //serial_SendCommand('0','A',0,0);
 
    }
 
}
 
\ No newline at end of file
master/master/master.cproj
Show inline comments
 
@@ -5,7 +5,7 @@
 
    <ProjectVersion>6.0</ProjectVersion>
 
    <ToolchainName>com.Atmel.AVRGCC8</ToolchainName>
 
    <ProjectGuid>{8579ec2d-5815-40db-84e0-1d14239c7aea}</ProjectGuid>
 
    <avrdevice>ATmega644P</avrdevice>
 
    <avrdevice>ATmega324P</avrdevice>
 
    <avrdeviceseries>none</avrdeviceseries>
 
    <OutputType>Executable</OutputType>
 
    <Language>C</Language>
 
@@ -21,8 +21,8 @@
 
    <OverrideVtorValue />
 
    <eraseonlaunchrule>0</eraseonlaunchrule>
 
    <AsfVersion>3.1.3</AsfVersion>
 
    <avrtool>com.atmel.avrdbg.tool.simulator</avrtool>
 
    <avrtoolinterface />
 
    <avrtool>com.atmel.avrdbg.tool.ispmk2</avrtool>
 
    <avrtoolinterface>ISP</avrtoolinterface>
 
    <com_atmel_avrdbg_tool_simulator>
 
      <ToolType xmlns="">com.atmel.avrdbg.tool.simulator</ToolType>
 
      <ToolName xmlns="">AVR Simulator</ToolName>
 
@@ -38,6 +38,35 @@
 
        <ssl>False</ssl>
 
      </Channel>
 
    </com_atmel_avrdbg_tool_simulator>
 
    <com_atmel_avrdbg_tool_ispmk2>
 
      <ToolType>com.atmel.avrdbg.tool.ispmk2</ToolType>
 
      <ToolName>AVRISP mkII</ToolName>
 
      <ToolNumber>000200131077</ToolNumber>
 
      <KeepTimersRunning>true</KeepTimersRunning>
 
      <OverrideVtor>false</OverrideVtor>
 
      <OverrideVtorValue>
 
      </OverrideVtorValue>
 
      <Channel>
 
        <host>127.0.0.1</host>
 
        <port>61309</port>
 
        <ssl>False</ssl>
 
      </Channel>
 
      <ToolOptions>
 
        <InterfaceName>ISP</InterfaceName>
 
        <InterfaceProperties>
 
          <JtagDbgClock>249000</JtagDbgClock>
 
          <JtagProgClock>1000000</JtagProgClock>
 
          <IspClock>2010000</IspClock>
 
          <JtagInChain>false</JtagInChain>
 
          <JtagEnableExtResetOnStartSession>false</JtagEnableExtResetOnStartSession>
 
          <JtagDevicesBefore>0</JtagDevicesBefore>
 
          <JtagDevicesAfter>0</JtagDevicesAfter>
 
          <JtagInstrBitsBefore>0</JtagInstrBitsBefore>
 
          <JtagInstrBitsAfter>0</JtagInstrBitsAfter>
 
        </InterfaceProperties>
 
      </ToolOptions>
 
    </com_atmel_avrdbg_tool_ispmk2>
 
    <preserveEEPROM>True</preserveEEPROM>
 
  </PropertyGroup>
 
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
 
    <ToolchainSettings>
 
@@ -85,6 +114,12 @@
 
    <Compile Include="config.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\afsk.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\afsk.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\led.c">
 
      <SubType>compile</SubType>
 
    </Compile>
slave/Makefile
Show inline comments
 
@@ -53,7 +53,7 @@ SRC =	$(TARGET).c
 
# MCU name, you MUST set this to match the board you are using
 
# type "make clean" after changing this, so all files will be rebuilt
 
#
 
MCU = atmega328p
 
MCU = atmega324p
 

	
 

	
 
# Processor frequency.
 
@@ -61,7 +61,7 @@ MCU = atmega328p
 
#   so your program will run at the correct speed.  You should also set this
 
#   variable to same clock speed.  The _delay_ms() macro uses this, and many
 
#   examples use this variable to calculate timings.  Do not add a "UL" here.
 
F_CPU = 16000000
 
F_CPU = 11059200
 

	
 

	
 
# Output format. (can be srec, ihex, binary)
slave/main.c
Show inline comments
 
@@ -11,9 +11,142 @@
 
 */
 

	
 
#include <stdint.h>
 
#include <avr/io.h>
 
#include <avr/interrupt.h> 
 
#include <util/delay.h>
 
#include <avr/pgmspace.h>
 

	
 

	
 
PROGMEM extern const prog_uchar afsk_sine_table[512] = {
 
  127, 129, 130, 132, 133, 135, 136, 138, 139, 141, 143, 144, 146, 147, 149, 150, 152, 153, 155, 156, 158, 
 
  159, 161, 163, 164, 166, 167, 168, 170, 171, 173, 174, 176, 177, 179, 180, 182, 183, 184, 186, 187, 188, 
 
  190, 191, 193, 194, 195, 197, 198, 199, 200, 202, 203, 204, 205, 207, 208, 209, 210, 211, 213, 214, 215, 
 
  216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 228, 229, 230, 231, 232, 233, 234, 235, 236, 
 
  236, 237, 238, 239, 239, 240, 241, 242, 242, 243, 244, 244, 245, 245, 246, 247, 247, 248, 248, 249, 249, 
 
  249, 250, 250, 251, 251, 251, 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 
 
  254, 254, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 253, 253, 253, 253, 252, 252, 252, 251, 
 
  251, 251, 250, 250, 249, 249, 249, 248, 248, 247, 247, 246, 245, 245, 244, 244, 243, 242, 242, 241, 240, 
 
  239, 239, 238, 237, 236, 236, 235, 234, 233, 232, 231, 230, 229, 228, 228, 227, 226, 225, 224, 223, 221, 
 
  220, 219, 218, 217, 216, 215, 214, 213, 211, 210, 209, 208, 207, 205, 204, 203, 202, 200, 199, 198, 197, 
 
  195, 194, 193, 191, 190, 188, 187, 186, 184, 183, 182, 180, 179, 177, 176, 174, 173, 171, 170, 168, 167, 
 
  166, 164, 163, 161, 159, 158, 156, 155, 153, 152, 150, 149, 147, 146, 144, 143, 141, 139, 138, 136, 135, 
 
  133, 132, 130, 129, 127, 125, 124, 122, 121, 119, 118, 116, 115, 113, 111, 110, 108, 107, 105, 104, 102, 
 
  101,  99,  98,  96,  95,  93,  91,  90,  88,  87,  86,  84,  83,  81,  80,  78,  77,  75,  74,  72,  71, 
 
   70,  68,  67,  66,  64,  63,  61,  60,  59,  57,  56,  55,  54,  52,  51,  50,  49,  47,  46,  45,  44, 
 
   43,  41,  40,  39,  38,  37,  36,  35,  34,  33,  31,  30,  29,  28,  27,  26,  26,  25,  24,  23,  22, 
 
   21,  20,  19,  18,  18,  17,  16,  15,  15,  14,  13,  12,  12,  11,  10,  10,   9,   9,   8,   7,   7, 
 
    6,   6,   5,   5,   5,   4,   4,   3,   3,   3,   2,   2,   2,   1,   1,   1,   1,   0,   0,   0,   0, 
 
    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1,   1, 
 
    2,   2,   2,   3,   3,   3,   4,   4,   5,   5,   5,   6,   6,   7,   7,   8,   9,   9,  10,  10,  11, 
 
   12,  12,  13,  14,  15,  15,  16,  17,  18,  18,  19,  20,  21,  22,  23,  24,  25,  26,  26,  27,  28, 
 
   29,  30,  31,  33,  34,  35,  36,  37,  38,  39,  40,  41,  43,  44,  45,  46,  47,  49,  50,  51,  52, 
 
   54,  55,  56,  57,  59,  60,  61,  63,  64,  66,  67,  68,  70,  71,  72,  74,  75,  77,  78,  80,  81, 
 
   83,  84,  86,  87,  88,  90,  91,  93,  95,  96,  98,  99, 101, 102, 104, 105, 107, 108, 110, 111, 113, 
 
  115, 116, 118, 119, 121, 122, 124, 125
 
};
 

	
 
inline uint8_t afsk_read_sample(int phase)
 
{
 
  return pgm_read_byte_near(afsk_sine_table + phase);
 
}
 

	
 
volatile uint16_t state = 0;
 
volatile uint8_t up = 1;
 

	
 
int main(void)
 
{
 
  _delay_ms(4);
 

	
 
  // Source timer2 from clkIO (datasheet p.164)
 
  ASSR &= ~(_BV(EXCLK) | _BV(AS2));
 

	
 
  // Set fast PWM mode with TOP = 0xff: WGM22:0 = 3  (p.150)
 
  TCCR2A |= _BV(WGM21) | _BV(WGM20);
 
  TCCR2B &= ~_BV(WGM22);
 

	
 
  // Do non-inverting PWM on pin OC2B (arduino pin 3) (p.159).
 
  // OC2A (arduino pin 11) stays in normal port operation:
 
  // COM2B1=1, COM2B0=0, COM2A1=0, COM2A0=0
 
  TCCR2A = (TCCR2A | _BV(COM2B1)) & ~(_BV(COM2B0) | _BV(COM2A1) | _BV(COM2A0));
 

	
 
  // No prescaler (p.162)
 
  //TCCR2B = (TCCR2B & ~(_BV(CS22) | _BV(CS21))) | _BV(CS20);
 
  TCCR2B = CS22 | CS21 |  CS20;
 
    
 
  // Enable interrupt when TCNT2 reaches TOP (0xFF) (p.151, 163)
 
	//TIMSK2 |= _BV(TOIE2);
 
	TIMSK2 |= 0b00000111;
 

	
 
	
 
	
 
	
 

	
 
	// Initialize PORTA
 
	DDRA = 0xff;
 
	PORTA = 0x00;
 
	
 
	DDRD = 0xff;
 
	PORTD = 0xff;
 
	
 
	sei();
 

	
 
		
 
	while(1) {
 
		//PORTA ++;
 
		/*
 
		if(state < 512) {
 
			OCR2B = afsk_sine_table[state];
 
		}
 
		else {
 
			state = 0;
 
			OCR2B = afsk_sine_table[state];
 
}
 
			*/
 
		/*
 
		OCR2B = afsk_sine_table[state];
 
		
 

	
 
		if(up==1)
 
			state++;
 
		else
 
			state--;
 

	
 
		if(state <= 0)
 
			up = 1;
 
		else if(state >= 254)
 
			up = 0;
 
			*/
 
		_delay_us(10);
 
	}
 
}
 

	
 

	
 
volatile uint8_t meh = 1;
 

	
 
void frick() {
 
	meh = meh + 5;
 
	//OCR2B = 0x09;
 
	OCR2B += meh;// = meh;// = afsk_read_sample(meh);
 
}
 

	
 
ISR(TIMER2_OVF_vect) {
 
/*
 
	PORTA = 0xff;
 
	_delay_ms(500);
 
	PORTA = 0x00;
 
	_delay_ms(500);  */
 
	frick();
 
	//meh++;
 
	//OCR2B = 0x90;
 
	/*
 
  // [...] load the next sample
 
  if(state < 512) {
 
	OCR2B = afsk_sine_table[state];
 
	}
 
	else {
 
		state = 0;
 
	}*/
 

	
 

	
 
			
 
	return;
 
}
0 comments (0 inline, 0 general)