Skip to content

Instantly share code, notes, and snippets.

@timvisee
Created April 15, 2015 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timvisee/a8ad139f7fb8c270e489 to your computer and use it in GitHub Desktop.
Save timvisee/a8ad139f7fb8c270e489 to your computer and use it in GitHub Desktop.
#include "mastersettings.h"
#include <avr/io.h>
#include <util/delay.h>
#include "twiMaster.h"
#include "usart_fnct.h"
#include <stdbool.h>
#include <avr/interrupt.h>
#define BAUDRATE 9600
#define UBRRVAL ((F_CPU/(BAUDRATE*16UL))-1)
#define byte char
// Define the packet data length
#define PACKET_DATA_LENGTH 8
#define PACKET_SEPARATION_BYTE 0x00
// Declare the xBee input stream buffers
bool xBeeBuffEnabled = false;
byte xBeeBuffData[8];
int xBeeBuffIndex = 0;
void handleIncomingByte(byte b);
/**
* Called when a malformed packet is received from the Xbee.
*/
void onPacketReceivedMalformedXbee();
/**
* Called when a packet is received from the Xbee.
*
* @param data The received packet data.
*/
void onPacketReceivedXbee(byte data[8]);
void USART_Init(void)
{
//Set baud rate
UBRR0L=(uint8_t)UBRRVAL; //low byte
UBRR0H=(UBRRVAL>>8); //high byte
//Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
UCSR0C=/*(1<<URSEL)|*/(0<<UMSEL00)|(0<<UPM01)|(0<<UPM00)|(0<<USBS0)|(0<<UCSZ02)|(1<<UCSZ01)|(1<<UCSZ00);
//Enable Transmitter and Receiver and Interrupt on receive complete
UCSR0B= (1<<RXEN0)|(1<<RXCIE0);
//enable global interrupts
}
uint8_t USART_vReceiveByte(void)
{
// Wait until a byte has been received
while((UCSR0A & (1 << RXC0)) == 0);
// Return received data
return UDR0;
}
ISR(USART0_RX_vect)
{
UDR0 = 0; //clear UART buffer
byte data = USART_vReceiveByte(); //receive data
handleIncomingByte(data);
}
void handleIncomingByte(byte b) {
// Check whether we're receiving xBeeBuffer data
if(xBeeBuffEnabled) {
// Check whether the buffer is full
if(xBeeBuffIndex >= PACKET_DATA_LENGTH) {
// Make sure this byte is a separation byte
if(b == PACKET_SEPARATION_BYTE)
onPacketReceivedMalformedXbee();
else
// Call the data received function
onPacketReceivedXbee(xBeeBuffData);
// Clear the buffer
for(int i = 0; i < PACKET_DATA_LENGTH; i++)
xBeeBuffData[i] = 0x00;
// Reset the index
xBeeBuffIndex = 0;
// Set the receiving boolean to false
xBeeBuffEnabled = false;
} else {
// Append the received byte to the data buffer
xBeeBuffData[xBeeBuffIndex] = b;
xBeeBuffIndex++;
}
} else {
// Check whether a separation character is received
if(b == PACKET_SEPARATION_BYTE)
// Start the xBee packet listener
xBeeBuffEnabled = true;
}
}
int main(void)
{
DDRB=0xFF;//make PORTC output
PORTB=0x00;//set all outputs to 0
sei();//enable global interrupts
USART_Init();
while(1) {
}
}
void onPacketReceivedMalformedXbee() { }
void onPacketReceivedXbee(byte data[8]) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x00;
_delay_ms(500);
/*// Start the transmission
Wire.beginTransmission(2);
// Send the packet data followed by a separation byte
Wire.write(data, PACKET_DATA_LENGTH);
Wire.write(PACKET_SEPARATION_BYTE);*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment