Well, it is Thursday, so here’s some code. You’ll want to wire the Baby Orangutan to the transmitter just like in the walk-through document. They should share the same ground line, and you will want to connect the Baby O’s TX line (PD1) to the transmitter’s data-in line. Assuming you connect the IR-Beacon to the same input pins as before (PC0-PC3), the Baby-O code should look like this:
#define F_CPU 20000000//CPU clock
#define BAUD 2400//baud rate for UART
#define MYUBRR (F_CPU/16/BAUD-1)//baud rate variable for UART hardware
#include <util/delay.h>
#include <avr/io.h>
void USART_Init(unsigned int ubrr){//Initialize USART hardware & settings for Serial Radio
UBRR0H=(unsigned char)(ubrr>>8);//set buad rate
UBRR0L=(unsigned char) ubrr;
UCSR0B=(1<<TXEN0);//enable transmitter
UCSR0C=(3<<UCSZ00);//Set frame format for 8bit with 1 stop
}
void USART_Trans (unsigned char data){//Transmit a byte of data over USART
while(!(UCSR0A&(1<<UDRE0)));//wait for transmition to complete
UDR0=data;
}
int main(){
unsigned char i;
DDRC&=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3));//Configure PortC IO
USART_Init(MYUBRR);//Initialize serial USART
while(1){
USART_Trans(PINC);//Transmit state of PINC
for(i=0;i<5;i++){
_delay_ms(10);
}
}
return 0;
}
Basically this code sets the first four pins in PortC to inputs (for good housekeeping) and initializes the USART (Universal Serial Asynchronous Receive Transmit) module on the ATMega48, sets the serial protocol and baud-rate (I’m assuming you bought the 2400BPS version of the serial radio). Then the while loop transmits the state of all 8 pins in PortC, and delays for 50ms. This code doesn’t need on any of the libraries of from Orangutan-lib.
The receiver should share your Orangutan’s ground, and have it’s data-out line connected to RX (PD0) on the Orangutan. The Orangutan code should look like this:
#define F_CPU 8000000//CPU clock
#define BAUD 2400//baud rate for UART
#define MYUBRR (F_CPU/16/BAUD-1)//baud rate variable for UART hardware
#include <avr/io.h>
#include <avr/interrupt.h>
#include "device.h"
#include "lcd.h"
unsigned char newBeaconState=0;
ISR(USART_RX_vect){//USART Byte reieved
newBeaconState=1;
}
void USART_Init(unsigned int ubrr){//Initialize USART hardware & settings for Serial Radio
UBRR0H=(unsigned char)(ubrr>>8);//set buad rate
UBRR0L=(unsigned char) ubrr;
UCSR0B=(1<<RXEN0);//enable reciever
UCSR0B|=(1<<RXCIE0);//enable recieve complete interrupt
UCSR0C=(3<<UCSZ00);//Set frame format for 8bit with 1 stop
}
int main(){
lcd_init(); // this function must be called before any other LCD command
USART_Init(MYUBRR);//Initialize serial USART
lcd_gotoxy(0,0);// go to the start of LCD line 1
lcd_string("Beacon");
sei();//enable global interrupts
while(1){
if(newBeaconState){
newBeaconState=0;
lcd_gotoxy(4, 1); // go to the start of LCD line 2
if(!(UDR0&(1<<PC0))){//north detected, do something
lcd_string("N"); // write a character
}else{
lcd_string(" "); // write a space
}
if(!(UDR0&(1<<PC1))){//east detected, do something
lcd_string("E"); // write a character
}else{
lcd_string(" "); // write a space
}
if(!(UDR0&(1<<PC2))){//south detected, do something
lcd_string("S"); // write a character
}else{
lcd_string(" "); // write a space
}
if(!(UDR0&(1<<PC3))){//west detected, do something
lcd_string("W"); // write a character
}else{
lcd_string(" "); // write a space
}
}
}
return 0;
}
This code uses the Orangutan-lib libraries to run the LCD, so you’ll need to import them like before (or just paste this into your old project and recompile it). Actually this is very similar to the previous code from when the Orangutan was hooked directly to the IR beacon. The new parts are the serial USART configuration and the interrupt function. Whenever a new byte is received, the interrupt function ISR(USART_RX_vect) is called automatically, which sets the variable newBeaconState to 1. The main while loop checks this variable, and if there is a new beacon state it looks at the received serial bytes (the states of the PortC pins on the Baby O) and displays the same characters on the LCD as before.
I tested the code with an ATMega168 Baby O wired (no radios) to my Orangutan, and it works for me. Let me know how it turns out!
-Adam
