// Includes
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Definitions
#define MAX_LENGTH_RX 32
#define MAX_LENGTH_TX_PKT 32
#define MAX_LENGTH_TX_DATA 16
#define synch_char 0b10101010 //170
#define start_char 0b11001010 //202
#define end_char 0b11010100 //212
#define PORTLT PORTD
#define PORTLR PORTD
#define tx_l 0x80
#define rx_l 0x80
//declarations - general
volatile unsigned char txrx_i;
//declarations - RX
volatile unsigned char rx_, decoded_byte, rx_byte, rx_done, rx_length, buffer, rx_started, max_rx_length, rx_led;
volatile char rx_data[MAX_LENGTH_RX];
//declarations - TX
volatile unsigned char tx_data_length, tx_pkt_length, tx_id, tx_byte, tx_ing, tx_pkt_byte, tx_data_byte, tx_led;
volatile char tx_data[MAX_LENGTH_TX_PKT];
volatile char in_data[MAX_LENGTH_TX_DATA];
//the encoding...
volatile char code[16] = {0b10001011,0b10001101,0b10010011,0b10010101,0b10010110,
0b10011001,0b10011010,0b10011100,0b10100011,0b10100101,0b10100110,0b10101001,
0b10101100,0b10110001,0b10110010,0b10110100};
/****************************************************************************************
GENERAL FUNCTIONS ********************************************************************
****************************************************************************************/
//**********************************************************
// Used to initialize the baud rate and and the UART0 connection
void txrx_init(int tx, int rx, int baud_num, char led) //00-none, 10-tx only, 01-rx only, 11-both
{
tx_byte=MAX_LENGTH_TX_PKT;
tx_ing=0;
tx_led=led;
rx_done=1;
rx_led=led;
UCSRB = ((rx<<7)|(tx<<5)|(rx<<4)|(tx<<3));
UBRRH = baud_num;
}
//**********************************************************
char decodeOne(char msbyte)
{
txrx_i=0;
buffer=start_char;
while(txrx_i<16)
{
if(msbyte==code[txrx_i])
{
buffer=txrx_i;
}
txrx_i++;
}
return buffer;
}
//**********************************************************
char decode(char msbyte, char lsbyte)
{
txrx_i=0;
while(txrx_i<16)
{
if(msbyte==code[txrx_i])
{
buffer=txrx_i;
}
txrx_i++;
}
txrx_i=0;
while(txrx_i<16)
{
if(lsbyte==code[txrx_i])
{
buffer = (buffer<<4);
buffer = buffer | txrx_i;
}
txrx_i++;
}
return buffer;
}
/****************************************************************************************
RECEIVE SIDE FUNCTIONS ***************************************************************
****************************************************************************************/
//RX Complete ISR
ISR(USART0_RX_vect)+-
{
if(rx_done==0)
{
if (rx_byte==0)
{
rx_data[rx_byte]=UDR;
if(rx_data[0]==synch_char)
{
rx_started=1;
}
else
{
rx_started=0;
}
if((rx_data[0]==synch_char) && rx_started)
{
rx_byte++;
if(rx_led == 1)
{
PORTLR &= ~(rx_l);
}
}
}
else if (rx_byte == 1)
{
rx_data[rx_byte]=UDR;
if(rx_data[0]==synch_char && rx_data[1]==start_char)
{
rx_byte++;
}
}
else
{
rx_data[rx_byte]=UDR;
if (rx_byte == 4)
{
rx_length = decode(rx_data[3],rx_data[4]);
}
else if(rx_byte>=max_rx_length || rx_data[rx_byte]==212)
{
if(rx_led==1)
{
PORTLR |= (rx_l);
}
rx_done=1;
}
rx_byte++;
}
}
}
//**********************************************************
// Resets the receiver to receive another packet
void rx_reset(char max_rx)
{
rx_done=0;
rx_started=0;
rx_length=0;
rx_byte=0;
max_rx_length=max_rx;
}
//**********************************************************
// Returns a 1 if the RF is done receiving a packet
char rxdone(void)
{
return rx_done;
}
//**********************************************************
// Initializes the read index to 0
void init_getrx(void)
{
rx_ = 0;
}
// Returns the next byte that was received over RF
char get_next_rx_data(void)
{
return rx_data[rx_++];
}
// Returns a 1 if there are still empty bytes left in the RX buffer
char rx_empty(void)
{
return (rx_ < max_rx_length) ? 0 : 1;
}
/****************************************************************************************
TRANSMIT SIDE FUNCTIONS **************************************************************
****************************************************************************************/
//UDR Empty ISR
ISR(USART0_UDRE_vect)
{
if(tx_byte< tx_pkt_length)
{
UDR=tx_data[tx_byte];
tx_byte++;
if(tx_led==1)
{
PORTLT &= ~(tx_l);
}
}
if(tx_byte==tx_pkt_length)
{
if(tx_led==1)
{
PORTLT |= (tx_l);
}
tx_ing=0;
}
}
//**********************************************************
// Encodes some of the bytes that need to be sent over RF during transmission
void encode(void)
{
//sunchronization.
tx_data[0] = synch_char; //calibration
tx_data[1] = synch_char; //calibration
tx_data[2] = synch_char; //synchronization
tx_data[3] = synch_char; //synchronization
//start character
tx_data[4] = start_char;
//1-byte of ID
tx_data[5] = code[tx_id]; //Channel Id, static 0 for this unit
//2-bytes of length
tx_data[6] = code[tx_data_length>>4]; //length of data to expect.
tx_data[7] = code[(tx_data_length<<4)>>4]; //length of data to expect.
//Payload
tx_pkt_byte=8;
tx_data_byte=0;
while(tx_data_byte<tx_data_length)
{
tx_data[tx_pkt_byte] = in_data[tx_data_byte];
tx_pkt_byte++;
tx_data_byte++;
}
//End character
tx_data[tx_pkt_length-1] = end_char;
}
//**********************************************************
// Function used to pass a data array from the main program that needs to be sent from transmitter over RF
void tx_me(char tx_data[], int length, int id)
{
if(tx_ing==0)
{
txrx_i=0;
while(txrx_i < length)
{
in_data[txrx_i]=tx_data[txrx_i];
txrx_i++;
}
tx_id=id;
tx_data_length=length;
tx_pkt_length=length+9;
encode();
tx_byte=0;
tx_ing=1;
}
}
//**********************************************************
// Returns a 1 if done transmitting
char txdone(void)
{
return tx_ing;
}
errors
Line No.
Error 4 first defined here 1 1 uhvtransmitter
Error 3 multiple definition of `code' 1 1 uhvtransmitter
Error 1 multiple definition of `txrx_init' 48 1 uhvtransmitter
Error 2 multiple definition of 61 1 uhvtransmitter
Error 5 multiple definition of `decode' 80 1 uhvtransmitter
Error 6 multiple definition of `USART0_RX_vect' 115 1 uhvtransmitter
Error 7 multiple definition of `rx_reset' 180 1 uhvtransmitter
Error 8 multiple definition of `rxdone' 191 1 uhvtransmitter
Error 9 multiple definition of `init_getrx' 198 1 uhvtransmitter
Error 10 multiple definition of `get_next_rx_data' 204 1 uhvtransmitter
[quote name='pradnesh' date='07 March 2013 - 03:56 PM' timestamp='1362671761' post='1815792']
// Includes
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Definitions
#define MAX_LENGTH_RX 32
#define MAX_LENGTH_TX_PKT 32
#define MAX_LENGTH_TX_DATA 16
#define synch_char 0b10101010 //170
#define start_char 0b11001010 //202
#define end_char 0b11010100 //212
#define PORTLT PORTD
#define PORTLR PORTD
#define tx_l 0x80
#define rx_l 0x80
//declarations - general
volatile unsigned char txrx_i;
//declarations - RX
volatile unsigned char rx_, decoded_byte, rx_byte, rx_done, rx_length, buffer, rx_started, max_rx_length, rx_led;
volatile char rx_data[MAX_LENGTH_RX];
//declarations - TX
volatile unsigned char tx_data_length, tx_pkt_length, tx_id, tx_byte, tx_ing, tx_pkt_byte, tx_data_byte, tx_led;
volatile char tx_data[MAX_LENGTH_TX_PKT];
volatile char in_data[MAX_LENGTH_TX_DATA];
//the encoding...
volatile char code[16] = {0b10001011,0b10001101,0b10010011,0b10010101,0b10010110,
0b10011001,0b10011010,0b10011100,0b10100011,0b10100101,0b10100110,0b10101001,
0b10101100,0b10110001,0b10110010,0b10110100};
/****************************************************************************************
GENERAL FUNCTIONS ********************************************************************
****************************************************************************************/
//**********************************************************
// Used to initialize the baud rate and and the UART0 connection
void txrx_init(int tx, int rx, int baud_num, char led) //00-none, 10-tx only, 01-rx only, 11-both
{
tx_byte=MAX_LENGTH_TX_PKT;
tx_ing=0;
tx_led=led;
rx_done=1;
rx_led=led;
UCSRB = ((rx<<7)|(tx<<5)|(rx<<4)|(tx<<3));
UBRRH = baud_num;
}
//**********************************************************
char decodeOne(char msbyte)
{
txrx_i=0;
buffer=start_char;
while(txrx_i<16)
{
if(msbyte==code[txrx_i])
{
buffer=txrx_i;
}
txrx_i++;
}
return buffer;
}
//**********************************************************
char decode(char msbyte, char lsbyte)
{
txrx_i=0;
while(txrx_i<16)
{
if(msbyte==code[txrx_i])
{
buffer=txrx_i;
}
txrx_i++;
}
txrx_i=0;
while(txrx_i<16)
{
if(lsbyte==code[txrx_i])
{
buffer = (buffer<<4);
buffer = buffer | txrx_i;
}
txrx_i++;
}
return buffer;
}
/****************************************************************************************
RECEIVE SIDE FUNCTIONS ***************************************************************
****************************************************************************************/
//RX Complete ISR
ISR(USART0_RX_vect)+-
{
if(rx_done==0)
{
if (rx_byte==0)
{
rx_data[rx_byte]=UDR;
if(rx_data[0]==synch_char)
{
rx_started=1;
}
else
{
rx_started=0;
}
if((rx_data[0]==synch_char) && rx_started)
{
rx_byte++;
if(rx_led == 1)
{
PORTLR &= ~(rx_l);
}
}
}
else if (rx_byte == 1)
{
rx_data[rx_byte]=UDR;
if(rx_data[0]==synch_char && rx_data[1]==start_char)
{
rx_byte++;
}
}
else
{
rx_data[rx_byte]=UDR;
if (rx_byte == 4)
{
rx_length = decode(rx_data[3],rx_data[4]);
}
else if(rx_byte>=max_rx_length || rx_data[rx_byte]==212)
{
if(rx_led==1)
{
PORTLR |= (rx_l);
}
rx_done=1;
}
rx_byte++;
}
}
}
//**********************************************************
// Resets the receiver to receive another packet
void rx_reset(char max_rx)
{
rx_done=0;
rx_started=0;
rx_length=0;
rx_byte=0;
max_rx_length=max_rx;
}
//**********************************************************
// Returns a 1 if the RF is done receiving a packet
char rxdone(void)
{
return rx_done;
}
//**********************************************************
// Initializes the read index to 0
void init_getrx(void)
{
rx_ = 0;
}
// Returns the next byte that was received over RF
char get_next_rx_data(void)
{
return rx_data[rx_++];
}
// Returns a 1 if there are still empty bytes left in the RX buffer
char rx_empty(void)
{
return (rx_ < max_rx_length) ? 0 : 1;
}
/****************************************************************************************
TRANSMIT SIDE FUNCTIONS **************************************************************
****************************************************************************************/
//UDR Empty ISR
ISR(USART0_UDRE_vect)
{
if(tx_byte< tx_pkt_length)
{
UDR=tx_data[tx_byte];
tx_byte++;
if(tx_led==1)
{
PORTLT &= ~(tx_l);
}
}
if(tx_byte==tx_pkt_length)
{
if(tx_led==1)
{
PORTLT |= (tx_l);
}
tx_ing=0;
}
}
//**********************************************************
// Encodes some of the bytes that need to be sent over RF during transmission
void encode(void)
{
//sunchronization.
tx_data[0] = synch_char; //calibration
tx_data[1] = synch_char; //calibration
tx_data[2] = synch_char; //synchronization
tx_data[3] = synch_char; //synchronization
//start character
tx_data[4] = start_char;
//1-byte of ID
tx_data[5] = code[tx_id]; //Channel Id, static 0 for this unit
//2-bytes of length
tx_data[6] = code[tx_data_length>>4]; //length of data to expect.
tx_data[7] = code[(tx_data_length<<4)>>4]; //length of data to expect.
//Payload
tx_pkt_byte=8;
tx_data_byte=0;
while(tx_data_byte<tx_data_length)
{
tx_data[tx_pkt_byte] = in_data[tx_data_byte];
tx_pkt_byte++;
tx_data_byte++;
}
//End character
tx_data[tx_pkt_length-1] = end_char;
}
//**********************************************************
// Function used to pass a data array from the main program that needs to be sent from transmitter over RF
void tx_me(char tx_data[], int length, int id)
{
if(tx_ing==0)
{
txrx_i=0;
while(txrx_i < length)
{
in_data[txrx_i]=tx_data[txrx_i];
txrx_i++;
}
tx_id=id;
tx_data_length=length;
tx_pkt_length=length+9;
encode();
tx_byte=0;
tx_ing=1;
}
}
//**********************************************************
// Returns a 1 if done transmitting
char txdone(void)
{
return tx_ing;
}
errors
Line No.
Error 4 first defined here 1 1 uhvtransmitter
Error 3 multiple definition of `code' 1 1 uhvtransmitter
Error 1 multiple definition of `txrx_init' 48 1 uhvtransmitter
Error 2 multiple definition of 61 1 uhvtransmitter
Error 5 multiple definition of `decode' 80 1 uhvtransmitter
Error 6 multiple definition of `USART0_RX_vect' 115 1 uhvtransmitter
Error 7 multiple definition of `rx_reset' 180 1 uhvtransmitter
Error 8 multiple definition of `rxdone' 191 1 uhvtransmitter
Error 9 multiple definition of `init_getrx' 198 1 uhvtransmitter
Error 10 multiple definition of `get_next_rx_data' 204 1 uhvtransmitter

New Topic/Question
Reply


MultiQuote




|