You are here: Forum Home → ANT Developers Forums → ANT General Questions → Thread
/*
* AP1_master.c
*
* Created: 5/15/2011 4:09:22 PM
* Author: DC
*/
/*
07-07-08
Copyright Spark Fun Electronics© 2008
Aaron Weiss
aaron at sparkfun.com
Master AP1 Intitialization @ 4800bps
*/
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
//#define FOSC 8000000 // 8MHz //ORIGINAL
#define F_CPU 8000000 // 8MHz //I added
#define BAUD 4800
#define MYUBRR 103 // Calculated from http://www.wormfood.net/avrbaudcalc.php
#define sbi(var, mask) ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask) ((var) &= (uint8_t)~(1 << mask))
#define STATUS_LED 5 // Pin 28 (PC5)
// Define functions
//=======================
void ioinit(void); // initializes IO
static int uart_putchar(char c, FILE *stream); // sends char out of UART
uint8_t uart_getchar(void); // receives char from UART
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void delay_ms(uint16_t x); // general purpose delay
void config(void); // runs config functions
//=======================
// Configuration functions
//=======================
void reset(void);
void assignch(void);
void setchid(void);
void opench(void);
void setchperiod(void);
void send(void);
//=======================
int main (void)
{
ioinit();
sbi(PORTC, STATUS_LED); //Just to inform that everything is powered up
config();
while(1)
{
sbi(PORTC, STATUS_LED);
delay_ms(500); //was 500
cbi(PORTC, STATUS_LED);
delay_ms(500); //was 500
}
return(0);
}
void config (void)
{
reset();
delay_ms(1000);
assignch();
delay_ms(1000);
setchid();
delay_ms(1000);
setchperiod();
delay_ms(1000);
opench();
delay_ms(1000);
send();
}
// Resets module
void reset (void)
{
uint8_t i;
uint8_t setup[7];
//start test code
//uint8_t test;
//test = 0xA;
//putchar('U');
//putchar('A');
//end test code
setup[0] = 0xa4; // SYNC Byte
setup[1] = 0x01; // LENGTH Byte
setup[2] = 0x4a; // ID Byte
setup[3] = 0x00; // Data Byte N (N=LENGTH)
setup[4] = 0xef; // Checksum
setup[5] = 0x00; // zero pad
setup[6] = 0x00; // zero pad
for(i = 0 ; i < 7 ; i++)
{
putchar(setup[i]);
}
}
// Assigns CH=0, CH Type=10(TX), Net#=0
void assignch (void)
{
uint8_t i;
uint8_t setup[9];
setup[0] = 0xa4; //Sync
setup[1] = 0x03; //Length = 3 bytes
setup[2] = 0x42; //Message ID = assign channel
setup[3] = 0x00; //ch # 0
setup[4] = 0x10; //ch type=10
setup[5] = 0x00; //Network # = 0
setup[6] = 0xf5; //Checksum
setup[7] = 0x00; // zero pad
setup[8] = 0x00; // zero pad
for(i = 0 ; i < 9 ; i++)
{
putchar(setup[i]);
}
}
// Assigns Device#=3100 (actually 31 - little endian), Device Type ID=01, Trans Type=01
void setchid (void)
{
uint8_t i;
uint8_t setup[11];
setup[0] = 0xa4; //Sync byte
setup[1] = 0x05; //Length = 5 bytes
setup[2] = 0x51; //message ID = set channel ID
setup[3] = 0x00; //ch # 0
setup[4] = 0x31; //Device number = 0031 (little endian), so 31 here
setup[5] = 0x00; //Device number = 0031 (little endian), so 00 here
setup[6] = 0x01; //Device type ID = 01
setup[7] = 0x01; //WAS 0x05. This is transmission type, and I changed to 0x01 (no shared address) as in example on p16 of ANT guide
setup[8] = 0xc1; //checksum
setup[9] = 0x00; // zero pad
setup[10] = 0x00; // zero pad
for(i = 0 ; i < 11 ; i++)
{
putchar(setup[i]);
}
}
// Opens CH 0
void opench (void)
{
uint8_t i;
uint8_t setup[7];
setup[0] = 0xa4; //sync
setup[1] = 0x01; //length = 1
setup[2] = 0x4b; //message ID = open channel
setup[3] = 0x00; //ch # 0
setup[4] = 0xee; //checksum
setup[5] = 0x00; // zero pad
setup[6] = 0x00; // zero pad
for(i = 0 ; i < 7 ; i++)
{
putchar(setup[i]);
}
}
// Sets channel period to 4 Hz (8192)
void setchperiod (void)
{
uint8_t i;
uint8_t setup[9];
setup[0] = 0xa4; //sync
setup[1] = 0x03; //Length = 3 (short needed, little endian)
setup[2] = 0x43; //message ID = set channel period
setup[3] = 0x00; //ch # 0
setup[4] = 0x92; //Least significant byte of 8192
setup[5] = 0x81; //Most significant byte of 8192
setup[6] = 0xf7; //checksum
setup[7] = 0x00; // zero pad
setup[8] = 0x00; // zero pad
for(i = 0 ; i < 9 ; i++)
{
putchar(setup[i]);
}
}
// Sends Data=AAAAAAAAAAAAAAAA
void send (void)
{
uint8_t i;
uint8_t setup[13];
setup[0] = 0xa4; //sync
setup[1] = 0x09; //length = 9 bytes
setup[2] = 0x4e; //message id = broadcast data
setup[3] = 0x00; //ch # 0
setup[4] = 0xaa;
setup[5] = 0xaa;
setup[6] = 0xaa;
setup[7] = 0xaa;
setup[8] = 0xaa;
setup[9] = 0xaa;
setup[10] = 0xaa;
setup[11] = 0xaa;
setup[12] = 0xe3;
for(i = 0 ; i < 13 ; i++)
{
putchar(setup[i]);
}
}
void ioinit (void)
{
//1 = output, 0 = input
DDRB = 0b11101111; //PB4 = MISO
DDRC = 0b11111111; //all outputs
DDRD = 0b11111110; //PORTD (RX on PD0)
//USART Baud rate: 4800
UBRR0H = (MYUBRR >> 8);
UBRR0L = MYUBRR;
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
stdout = &mystdout; //Required for printf init
}
static int uart_putchar(char c, FILE *stream)
{
if (c == '\\n') uart_putchar('\\r', stream);
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
uint8_t uart_getchar(void)
{
while( !(UCSR0A & (1<<RXC0)) );
return(UDR0);
}
//General short delays
void delay_ms(uint16_t x)
{
uint8_t y, z, w;
for ( ; x > 0 ; x--){
for ( y = 0 ; y < 90; y++){ //was 90, 11 gives delayms(500) actually 500ms at 8MHz
for ( w = 0 ; w < 8; w++){ //New - put in to compensate for clock divide by 8 error in original
for ( z = 0 ; z < 6 ; z++){
asm volatile ("nop");
}
}
}
}
}