Today I will teach you some of the basics in hardware-close C programming.
To start with you will need some sort of programming environment. Since I use an Arduino, I chose to use the environment you can get from HERE. It's the Arduino user- and programmer environment, making you able to both code end program your Arduino unit. It comes loaded with features making your life easier when it comes to the Arduino. For those of you that are more hardcore i recommend ATMELs own AVR studio 5.0. But in that case you will have to program your unit through the SPI-port and that's nothing i'll take up here. Not today anyways =)
The above picture is what you see when you first start the Arduino environment. A clean, small window with a textbox and a console (the black area). The console will give you every information you need and if something went wrong during for eg. the compilation.
I'll not go through the IDE thoroughly. You will have to find your own way by foe example hovering over the buttons (the ones above sketch_dec16a). When and if you do so, it'll appear a small text to the right indicating what that button does.
The IDE comes preloaded with a large pack of examples written in the, let's call it "Arduino-language" =) It's a crossbreed between C and C++. You can use pure C to write your programs and that is what I'm gonna do. And of course, that's what I'll teach you too!
Feel free to look at the code examples the IDE gives you. You find them under File->Examples->...
The code I'm going to show today is code to get a bluetooth module (like BlueSmirf Gold) up and running.
So lets start!
At first you need a pretty hefty knowledge about the ATmega processor (since that's what's on the Arduino). I recommend you to read the manual here.
The code then. I'll just post the code here at the moment since I don't have time to thoroughly go though it.
The comments should be rather self explaining!
And by the way, the code does only(?) fit on an ATmega16 at the moment, though the only changes you'll have to do is change the names on the registers (i've made some changes but not tested them) and the constant F_CPU to "16000000L". By then you'll have a baud rate on 115200 BAUD (though with a effective loss of data on around 3%).
The code below is also interrupt driven. Meaning that if the Arduino receives anything, it'll immediately break that and serve the data that it receives.
The only thing this does is that the Arduino returns whatever you send to it! It's also supposed to light up the led on pin13 on the UNO.
/*
* test.c
*
* Created: 2011-11-24 13:28:08
* Author: Alexander Widerberg
*/
#include<avr/io.h>
#include<stdio.h>
#include<avr/interrupt.h>
#include "util/delay.h"
#define F_CPU 16000000UL // The CPU speed in Hz
#define USART_BAUDRATE 115200 // Baud Rate value
#define MYUBRR (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
uint8_t toggle = 0;
void USART_Init()
{
UCSR0B |= (1<<RXCIE0) | (1 << RXEN0) | (1 << TXEN0); // Turn on the transmission and reception circuitry and
// enables recieve interrupt flag
UCSR0C =
/* no parity bit */
(0 << UPM01) |
(0 << UPM00) |
/* asyncrounous USART */
(0 << UMSEL01) |
(0 << UMSEL00) |
/* one stop bit */
(0 << USBS0) |
/* 8-bits of data */
(1 << UCSZ01) |
(1 << UCSZ00);
UBRR0L = MYUBRR; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRR0H = (MYUBRR >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}
void USART_Transmit( unsigned char data )
{
// Wait for empty transmit buffer
while ( !( UCSR0A & (1<<UDRE0)) )
;
// Put data into buffer, sends the data
UDR0 = data;
}
void USART_Flush( void )
{
unsigned char dummy;
while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}
ISR (USART_RX_vect)
{
unsigned char value;
unsigned char temp;
temp = UDR0; // Fetch the received byte value into the variable "value"
value = temp;
toggle = 1; // Toggle that we have received data!
USART_Transmit(value);
USART_Flush();
}
int
main(void)
{
uint8_t state = 0;
USART_Init();
DDRD = 0x00; // Set all ports on port D to inputs except PD1 (0x01 == 000000010)
DDRB = 0x20; // Set pins PB4 and PB5 to outputs (0x20 == 00110000)
PORTB = 0; // Nulls all data (if there is any on port b)
sei(); // Enables interrupts!
for (;;) { // loop forever! A MUST HAVE!
if (toggle) {
state = state ? 0 : 1;
}
PORTB = state ? 0x20 : 0; // Light up the onboard LED if we have received data!
toggle = 0;
}
cli(); // Disables interrupts
return 0;
}















