-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
80 lines (67 loc) · 1.97 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "io.h"
#define SET_BIT(p,i) ((p) |= (1 << (i)))
#define CLR_BIT(p,i) ((p) &= ~(1 << (i)))
#define GET_BIT(p,i) ((p) & (1 << (i)))
/*-------------------------------------------------------------------------*/
#define DATA_BUS PORTC // port connected to pins 7-14 of LCD display
#define CONTROL_BUS PORTD // port connected to pins 4 and 6 of LCD disp.
#define RS 6 // pin number of uC connected to pin 4 of LCD disp.
#define E 7 // pin number of uC connected to pin 6 of LCD disp.
/*-------------------------------------------------------------------------*/
void LCD_ClearScreen(void) {
LCD_WriteCommand(0x01);
}
void LCD_init(void) {
//wait for 100 ms.
delay_ms(100);
LCD_WriteCommand(0x38);
LCD_WriteCommand(0x06);
LCD_WriteCommand(0x0f);
LCD_WriteCommand(0x01);
delay_ms(10);
}
void LCD_WriteCommand (unsigned char Command) {
CLR_BIT(CONTROL_BUS,RS);
DATA_BUS = Command;
SET_BIT(CONTROL_BUS,E);
asm("nop");
CLR_BIT(CONTROL_BUS,E);
delay_ms(2); // ClearScreen requires 1.52ms to execute
}
void LCD_WriteData(unsigned char Data) {
SET_BIT(CONTROL_BUS,RS);
DATA_BUS = Data;
SET_BIT(CONTROL_BUS,E);
asm("nop");
CLR_BIT(CONTROL_BUS,E);
delay_ms(1);
}
void LCD_DisplayString( unsigned char column, const unsigned char* string) {
LCD_ClearScreen();
unsigned char c = column;
while(*string) {
LCD_Cursor(c++);
LCD_WriteData(*string++);
}
}
void LCD_Cursor(unsigned char column) {
if ( column < 17 ) { // 16x1 LCD: column < 9
// 16x2 LCD: column < 17
LCD_WriteCommand(0x80 + column - 1);
} else {
LCD_WriteCommand(0xB8 + column - 9); // 16x1 LCD: column - 1
// 16x2 LCD: column - 9
}
}
void delay_ms(int miliSec) //for 8 Mhz crystal
{
int i,j;
for(i=0;i<miliSec;i++)
for(j=0;j<775;j++)
{
asm("nop");
}
}