Koden till Displayapplikationen.

 

 

Main function. 1

LCD functions. 1

Button functions. 3

Serial communication functions. 4

 

Main function

 

#include <avr/io.h>

#include <avr/delay.h>

 

char message[2][16];

 

int main(void) {

       /* INITIALIZE */

       LCD_init();

       delay(1);

       initUART();

       delay(1);

      

       /* wait for data */

       while(1) {

              receiveUART(message);

              for(int i = 0;i<2;i++)

                     LCD_putLine(i,message[i]);

              for(int i = 0;i<2;i++)

                     for(int j = 0;j<16;j++)

                           message[i][j] = ' ';

       }

       return(0);

}

 

 

LCD functions

 

#include <avr/io.h>

#include <avr/delay.h>

#define LCD_RS 5

#define LCD_RW 7

#define LCD_E 6

 

char rstring[16];

 

void delay(int nbrDelays){

       for(int i = 0; i < 200*nbrDelays; ++i);

}

 

//LCD_putchar writes a character to the LCD at the current address,

//no busy flag check is done before or after

//the character is written!

//usage: LCD_putchar('A'); or LCD_putchar(0x55);

void LCD_putchar(char data)

{

       //PORTA is output

       DDRA = 0xFF;

       //put data on bus

       PORTA = data;

       //RW low, E low

       PORTD &= ~((1<<LCD_RW)|(1<<LCD_E));

       //RS high, strobe E

       PORTD |= ((1<<LCD_RS)|(1<<LCD_E));

       //the number of nops required varies with your clock frequency, try it out!

       delay(4);

       //RS low again, E low (belongs to strobe)

       PORTD &= ~((1<<LCD_RS)|(1<<LCD_E));

       //release bus

       DDRA = 0;

}

 

//LCD_getaddress reads the address counter and busy flag. For the address only,

//mask off bit7 of the return value.

char LCD_getaddr(void)

{

       //make var for the return value

       char address;

       //PORTA is input

       DDRA = 0;

       //RW high, strobe enable

       PORTD |= ((1<<LCD_RW)|(1<<LCD_E));

       delay(2);

       //while E is high, get data from LCD

       address = PINA;

       //reset RW to low, E low (for strobe)

       PORTD &= ~((1<<LCD_RW)|(1<<LCD_E));

       //return address and busy flag

       return address;

}

 

//LCD_reads the address counter (which contains the busy flag) and loops until

//the busy flag is cleared.

void LCD_wait(void)

{

       //get address and busy flag

       //and loop until busy flag cleared

       while((LCD_getaddr() & 0x80) == 0x80);

}

 

//LCD_command works EXACTLY like LCD_putchar, but takes RS low for accessing the command reg

//see LCD_putchar for details on the code

void LCD_command(char command)

{

       //set DDRA to output

       DDRA = 0xFF;

       //output command on PORTA

       PORTA = command;

       //set RS, RW and E low

       PORTD &= ~((1<<LCD_RS)|(1<<LCD_RW)|(1<<LCD_E));

       //set E high

       PORTD |= (1<<LCD_E);

       delay(4);

       //set E low

       PORTD &= ~(1<<LCD_E);

       //set DDRA to input

       DDRA = 0;

}

 

/*LCD_init initialises the LCD with the following paramters:

8 bit mode, 5*7 font, 2 lines (also for 4 lines)

auto-inc cursor after write and read

cursor and didsplay on, cursor blinking.

*/

void LCD_init(void)

{

       //setup the LCD control signals on PORTD

       DDRD |= ((1<<LCD_RS)|(1<<LCD_RW)|(1<<LCD_E));

       PORTD = 0x00;

       //if called right after power-up, we'll have to wait a while (fine-tune for faster execution)

       delay(5);

       //tell the LCD that it's used in 8-bit mode 3 times, each with a delay inbetween.

       LCD_command(0x30);

       delay(3);

       LCD_command(0x30);

       delay(3);

       LCD_command(0x30);

       delay(3);

       //now: 8 bit interface, 5*7 font, 2 lines.

       LCD_command(0x38);

       //wait until command finished

       LCD_wait();

       //display on, cursor on (blinking)

       LCD_command(0x0F);

       LCD_wait();

       //now clear the display, cursor home

       LCD_command(0x01);                               

       LCD_wait();

       delay(3);

       //cursor auto-inc

       LCD_command(0x06);

}

 

void LCD_clear() {

       //now clear the display, cursor home

       LCD_command(0x01);

       delay(3);

}

 

void LCD_putLine(int i,char* dstring) {

       int r = 0;

       while(r < 16) {

              if(*dstring)

                     rstring[r] = *dstring;

              else

                     rstring[r] = ' ';

              r++;

              dstring++;

       }

       if(i==0) {

              for(int j = 0; j< 16; j++)

              {

                     LCD_putchar(rstring[j]);

                     LCD_wait();

              }

       }

       else if(i==1) {

              for(int j = 0; j< 24; j++)

              {

                     LCD_putchar(' ');

                     LCD_wait();  

              }

              for(int j = 0; j< 16; j++)

              {

                     LCD_putchar(rstring[j]);

                     LCD_wait();  

              }

       }

}

      

 

Button functions

 

#include <avr/io.h>

#include <avr/signal.h>

 

#define UP 4

#define DOWN 3

#define TOGGLE 7

 

 

void initButtons() {

       DDRD &= ~((1<<UP)|(1<<DOWN));

       DDRB &= ~(1<<TOGGLE);

}

 

char btnReadCommand() {

       char command;

       while(1){

              command = PIND;

              //Check if up button is pushed

              if((command & (1<<UP)) == 0) {

                     return 'u';

              }

              //Check if down button is pushed

              else if((command & (1<<DOWN)) == 0) {

                     return 'd';

              }

              command = PINB;

              if((command & (1<<TOGGLE)) == 0) {

                     return 't';

              }

       }

}

 

 

Serial communication functions

 

#include <avr/io.h>

#include <avr/signal.h>

 

/*sätt bitarnas position i UCR och USR */

#define UCR UCSRB /*control register */

#define USR UCSRA /*status register */

 #define BAUD0H_REG        UBRRH

#define BAUD0L_REG         UBRRL

#define myAddress 0x30 /* '0' */

#define serverAddress 0x31 /* '1' */

 

char* m;

 

void initUART(void) {

       BAUD0L_REG = 0x33;

       BAUD0H_REG = 0x00;

       UCR |= (1<<TXEN);

       UCR |= (1<<RXEN);

       UCSRC = (1 << URSEL) | (3<<UCSZ0);

}

 

void receiveUART(char message[2][16]) {

       char in;

       while(!(USR & (1<<RXC))) {

              ;

       }

       in = UDR;

       LCD_clear();

       while(in != '=') {

              while(!(USR & (1<<RXC))) {

                     ;

              }

              in = UDR;

       }

       /* Receive first char of name*/

       while(!(USR & (1<<RXC))) {

              ;

       }            

       in = UDR;

       int j = 0;

       for(int i = 0;i<2;i++) {

              while(in!= ';') {

                     message[i][j] = in;

                     j++;

                     while(!(USR & (1<<RXC))) {

                           ;

                     }                   

                     in = UDR;

              }

              while(!(USR & (1<<RXC))) {

                           ;

                     }                   

              in = UDR;

              j = 0;

              if(in=='=')

                     break;

       }

}