■ FIGURE 8. TxD and RxD.
■ FIGURE 9. Crystal and power.
■ FIGURE 10. Select New Project.
those familiar with the Arduino but not yet comfortable
with regular C programming will feel like they’ve been
backing down a dark hallway in an old house shining a
flashlight in their own eyes when BUMP — they get a look
at this code. Relax guys, this isn’t a slasher film, and we
will — over time — learn what all this means. For now, just
type it into the AVRStudio editor [or get the source from
Workshop21.zip].
/* **********************************************
Breadboarduino_Hello_World Joe Pardue February
10, 2010
********************************************** */
#include <stdio.h>
#include <avr/io.h>
#define FOSC 16000000
#define BAUD 57600
#define MYUBRR FOSC/16/BAUD-1
uint8_t receiveByte( void );
void init();
// From example in avrlibc manual
static int uart_putchar(char c, FILE *stream);
static FILE mystdout = \
FDEV_SETUP_STREAM(uart_putchar, NULL,
_FDEV_SETUP_WRITE);
int main(void)
{
uint8_t b;
uint8_t count = 0;
init();
printf(“BBArduino_Hello_Test v 005\n”);
while(1)
{
b = receiveByte();
printf(“#%d”,count++);
printf(“You sent: %c\n”,b);
}
return 0;
}
static int uart_putchar(char c, FILE *stream)
{
if (c == ‘\n’) uart_putchar
(‘\r’, stream);
// wait for UDR to be clear
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c; //send the character
return 0;
}
void init()
{
//USART Baud rate: 57600
UBRR0H = (MYUBRR) >> 8;
UBRR0L = MYUBRR;
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
//set the output stream
stdout = &mystdout;
}
uint8_t receiveByte( void )
{
// Wait for data to be received
while ( !(UCSR0A & (1<<RXC0)) );
// Get and return received data from buffer
return UDR0;
}
Compiling In AVRStudio
I presume you’ve downloaded and installed the latest
and greatest AVRStudio and WinAVR. If not, get them at:
www.atmel.com/dyn/products/tools_card.asp?
tool_id=2725
http://winavr.sourceforge.net/download.html
Open AVRStudio and click on the Project\New Project
menu item as shown in Figure 10. A window will open as shown
in Figure 11. Highlight ‘AVR GCC,’ browse to a location for your
file, and name the file under ‘Project name.’ Then click ‘Next>>.’
In the next window (shown in Figure 12, select the
‘Debug platform’ AVR Simulator and the Device
‘ATmega168P (unless you got the 328); finally, click finish.
You can now input the program in the edit window as
shown in Figure 13.
Click on the ‘Build Active Configuration’ button as
shown in Figure 14; if all goes well, you’ll get the ‘Build
succeeded with 0 Warnings…’ message.
April 2010 55