main.c


1
//this is just for testing the display on the tiny 2313 board. I am doing it manually, like it is written in the datasheet of the controller
2
3
#include<stdio.h>
4
#include<avr/io.h>
5
#include<util/delay.h>
6
7
#define L4 0
8
#define L5 1
9
#define L6 2
10
#define L7 3
11
12
#define LCDE PB6
13
#define LCDRS PB7
14
15
//
16
static void lcd_nibble(uint8_t);
17
static void init(void);
18
static void lcd_command(uint8_t);
19
static void lcd_data(uint8_t);
20
static void beep(void);
21
22
int main(void){
23
  DDRB |= (1<<4);
24
  init();
25
  _delay_ms(500);
26
  
27
  lcd_nibble( 0x30 );      //8 Bit mode
28
  _delay_ms( 50 );      // wait >4.1ms
29
30
  lcd_nibble( 0x30 );      // 8 bit mode
31
  _delay_ms( 100 );      // wait >100us
32
33
  lcd_nibble( 0x30 );      // 8 bit mode
34
  _delay_ms( 100 );      // wait >100us
35
36
  lcd_nibble( 0x20 );      // 4 bit mode
37
  _delay_ms( 100 );      // wait >100us
38
  
39
  lcd_command( 0x28 );      // 2 lines 5*7  =0b101000
40
  lcd_command( 0x0F );      // Display and cursor on and blinking
41
  lcd_command( 0x06 );      //cursor increment =0b110
42
  lcd_command( 0x02 );      //reset cursor to home
43
  
44
  _delay_ms(1000);
45
  beep();
46
  lcd_data( 0x48 );        //write an H
47
  _delay_ms(1000);
48
  beep();
49
  lcd_data( 0x48 );        //write an H
50
  _delay_ms(1000);
51
  beep();
52
  lcd_data( 0x48 );        //write an H
53
  while(1){
54
    
55
  }
56
  
57
  return 0;
58
}
59
60
static void init(void){  
61
  //set the ports as output
62
  DDRB |= (1<<L4) | (1<<L5) | (1<<L6) | (1<<L7);  //The Data lines
63
  DDRB |= (1<<LCDE) | (1<<LCDRS);  //the 
64
  //set on Commands
65
  PORTB &=~ (1<<LCDRS);
66
  PORTB &=~ (1<<LCDE);
67
}
68
69
static void lcd_nibble( uint8_t d )
70
{
71
  PORTB &=~(1<<L4); if( d & (1<<4) ) PORTB |= (1<<L4);
72
  PORTB &=~(1<<L5); if( d & (1<<5) ) PORTB |= (1<<L5);
73
  PORTB &=~(1<<L6); if( d & (1<<6) ) PORTB |= (1<<L6);
74
  PORTB &=~(1<<L7); if( d & (1<<7) ) PORTB |= (1<<L7);
75
76
  PORTB |= (1<<LCDE);
77
  _delay_ms( 10 );      // wait > 1us
78
  PORTB &=~ (1<<LCDE);
79
}
80
81
static void lcd_command(uint8_t d){
82
  PORTB &=~ (1<<LCDRS);  //command mode
83
  lcd_nibble(d);    //send the higher nibble
84
  lcd_nibble(d<<4);  //send the lower nibble
85
  _delay_ms(10);
86
}
87
88
static void lcd_data(uint8_t d){
89
  PORTB |= (1<<LCDRS);  //data mode
90
  lcd_nibble(d);    //send the higher nibble
91
  lcd_nibble(d<<4);  //send the lower nibble
92
  _delay_ms(10);
93
}
94
95
//funktion for beeping
96
static void beep(void){
97
  for(int i = 0; i < 100; i++){
98
    PORTB ^= (1<<4);
99
    _delay_ms(1);
100
  }
101
}