You should make a new question for every other question you have... But well. The code below (Might be a bit arduino styled, but it might help the questioner).
The Setup
code runs only once, at the start of your progam. Great to initialize any peripherals and/or send a hello message / startup routine.
The Loop
code runs continiously, usually this contains a program like blinking leds... or the security system of a nuclear powerplant. However, it's not designed to be returned from, it's designed to run forever. Though my code will catch you in a while(forever); if you were to return from it.
I haven't tested the code, I'm at.. "work".. oops. - (I'm actually a student on internship, before anyone starts hating, this has educational value.)
#include <avr/io.h>
#define F_CPU 16000000
#define BUAD 9600
#define BUAD_RATE_CALC ((F_CPU/16/BUAD) - 1)
//Function definitions
void Setup(void);
void Loop(void);
void serialSetup(void);
void serialSend(char* sendString);
//Global variables.
char ar[]= "hello";
void Setup(){
//Code to be run once (At start).
serialSetup();
serialSend(ar);
}
void Loop(){
//Code to be run continiously.
}
void serialSetup(){
//Register settings
//High and low bits
UBRR0H = (BUAD_RATE_CALC >> 8);
UBRR0L = BUAD_RATE_CALC;
//transimit and recieve enable
UCSR0B = (1 << TXEN0)| (1 << TXCIE0) | (1 << RXEN0) | (1 << RXCIE0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); //8 bit data format
}
void serialSend(char* sendString){
for (int i = 0; i < strlen(sendString); i++){
while (( UCSR0A & (1<<UDRE0)) == 0){};
UDR0 = sendString[i];
}
}
int main(void){
Setup();
while(1){
Loop();
}
while(1){}//If you ever return from Loop(); This while will catch you, so it won't continue doing randomness.
}