Subversion Repositories HomeAutomation

Rev

Rev 191 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 191 Rev 389
1
#include <ks0108.h>
1
#include <ks0108.h>
2
#include <avr/io.h>
2
#include <avr/io.h>
3
#include <font.h>
3
#include <font.h>
4
#include <avr\pgmspace.h>
4
#include <avr\pgmspace.h>
5
 
5
 
6
GrLcdStateType GrLcdState;
6
GrLcdStateType GrLcdState;
7
 
7
 
8
 
8
 
9
void SetControls(uint8_t RS, uint8_t RW){
9
void SetControls(uint8_t RS, uint8_t RW){
10
    if(RS) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RS);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RS);};
10
    if(RS) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RS);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RS);};
11
    if(RW) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RW);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RW);};
11
    if(RW) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RW);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RW);};
12
}
12
}
13
 
13
 
14
void SetData(uint8_t Data){
14
void SetData(uint8_t Data){
15
    if(Data&0b00000001) {LCD_DATA_PORT_DB0 |= (1<<LCD_DATA_PIN_DB0);} else { LCD_DATA_PORT_DB0 &= ~(1<<LCD_DATA_PIN_DB0);};
15
    if(Data&0b00000001) {LCD_DATA_PORT_DB0 |= (1<<LCD_DATA_PIN_DB0);} else { LCD_DATA_PORT_DB0 &= ~(1<<LCD_DATA_PIN_DB0);};
16
    if(Data&0b00000010) {LCD_DATA_PORT_DB1 |= (1<<LCD_DATA_PIN_DB1);} else { LCD_DATA_PORT_DB1 &= ~(1<<LCD_DATA_PIN_DB1);};
16
    if(Data&0b00000010) {LCD_DATA_PORT_DB1 |= (1<<LCD_DATA_PIN_DB1);} else { LCD_DATA_PORT_DB1 &= ~(1<<LCD_DATA_PIN_DB1);};
17
    if(Data&0b00000100) {LCD_DATA_PORT_DB2 |= (1<<LCD_DATA_PIN_DB2);} else { LCD_DATA_PORT_DB2 &= ~(1<<LCD_DATA_PIN_DB2);};
17
    if(Data&0b00000100) {LCD_DATA_PORT_DB2 |= (1<<LCD_DATA_PIN_DB2);} else { LCD_DATA_PORT_DB2 &= ~(1<<LCD_DATA_PIN_DB2);};
18
    if(Data&0b00001000) {LCD_DATA_PORT_DB3 |= (1<<LCD_DATA_PIN_DB3);} else { LCD_DATA_PORT_DB3 &= ~(1<<LCD_DATA_PIN_DB3);};
18
    if(Data&0b00001000) {LCD_DATA_PORT_DB3 |= (1<<LCD_DATA_PIN_DB3);} else { LCD_DATA_PORT_DB3 &= ~(1<<LCD_DATA_PIN_DB3);};
19
    if(Data&0b00010000) {LCD_DATA_PORT_DB4 |= (1<<LCD_DATA_PIN_DB4);} else { LCD_DATA_PORT_DB4 &= ~(1<<LCD_DATA_PIN_DB4);};
19
    if(Data&0b00010000) {LCD_DATA_PORT_DB4 |= (1<<LCD_DATA_PIN_DB4);} else { LCD_DATA_PORT_DB4 &= ~(1<<LCD_DATA_PIN_DB4);};
20
    if(Data&0b00100000) {LCD_DATA_PORT_DB5 |= (1<<LCD_DATA_PIN_DB5);} else { LCD_DATA_PORT_DB5 &= ~(1<<LCD_DATA_PIN_DB5);};
20
    if(Data&0b00100000) {LCD_DATA_PORT_DB5 |= (1<<LCD_DATA_PIN_DB5);} else { LCD_DATA_PORT_DB5 &= ~(1<<LCD_DATA_PIN_DB5);};
21
    if(Data&0b01000000) {LCD_DATA_PORT_DB6 |= (1<<LCD_DATA_PIN_DB6);} else { LCD_DATA_PORT_DB6 &= ~(1<<LCD_DATA_PIN_DB6);};
21
    if(Data&0b01000000) {LCD_DATA_PORT_DB6 |= (1<<LCD_DATA_PIN_DB6);} else { LCD_DATA_PORT_DB6 &= ~(1<<LCD_DATA_PIN_DB6);};
22
    if(Data&0b10000000) {LCD_DATA_PORT_DB7 |= (1<<LCD_DATA_PIN_DB7);} else { LCD_DATA_PORT_DB7 &= ~(1<<LCD_DATA_PIN_DB7);};
22
    if(Data&0b10000000) {LCD_DATA_PORT_DB7 |= (1<<LCD_DATA_PIN_DB7);} else { LCD_DATA_PORT_DB7 &= ~(1<<LCD_DATA_PIN_DB7);};
23
}
23
}
24
 
24
 
25
 
25
 
26
void Disable(){
26
void Disable(){
27
    LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_E);
27
    LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_E);
28
}
28
}
29
 
29
 
30
void Delay(){ //OBS!! BEHÖVER VEEERKLIGEN FIXAS!
30
void Delay(){ //OBS!! BEHÖVER VEEERKLIGEN FIXAS!
31
           // 1-8us      ...2-13us     ...5-31us
31
           // 1-8us      ...2-13us     ...5-31us
32
                               // 10-60us    ...50-290us
32
                               // 10-60us    ...50-290us
33
unsigned int i;             // 100-580us  ...500-2,9ms
33
unsigned int i;             // 100-580us  ...500-2,9ms
34
unsigned char j;            // 1000-5,8ms ...5000-29ms
34
unsigned char j;            // 1000-5,8ms ...5000-29ms
35
uint8_t p = 10;                             // 10000-56ms ...30000-170ms
35
uint8_t p = 10;                             // 10000-56ms ...30000-170ms
36
                             // 50000-295ms...60000-345ms
36
                             // 50000-295ms...60000-345ms
37
//  for (i = 0; i < p; i++) for (j = 0; j < 10; j++) asm volatile ("nop");
37
//  for (i = 0; i < p; i++) for (j = 0; j < 10; j++) asm volatile ("nop");
38
    for (i = 0; i < p; i++) for (j = 0; j < 10; j++);
38
    for (i = 0; i < p; i++) for (j = 0; j < 10; j++);
39
 
39
 
40
}
40
}
41
void Enable(){
41
void Enable(){
42
    Delay();
42
    Delay();
43
    LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_E);
43
    LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_E);
44
    Delay();
44
    Delay();
45
    Disable();
45
    Disable();
46
    Delay();
46
    Delay();
47
}
47
}
48
 
48
 
49
 
49
 
50
void glcdWriteData(uint8_t data){
50
void glcdWriteData(uint8_t data){
51
    SetControls(1,0);
51
    SetControls(1,0);
52
    SetData(data);
52
    SetData(data);
53
    Enable();
53
    Enable();
54
    GrLcdState.lcdXAddr++;
54
    GrLcdState.lcdXAddr++;
55
 
55
 
56
    if (GrLcdState.lcdXAddr > 63){
56
    if (GrLcdState.lcdXAddr > 63){
57
        glcdSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
57
        glcdSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
58
    }
58
    }
59
    if (GrLcdState.lcdXAddr > 128){
59
    if (GrLcdState.lcdXAddr > 128){
60
        glcdSetXY(0,GrLcdState.lcdYAddr+1);
60
        glcdSetXY(0,GrLcdState.lcdYAddr+1);
61
    }
61
    }
62
}
62
}
63
 
63
 
64
void glcdPowerOn(){
64
void glcdPowerOn(){
65
    LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
65
    LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
66
    Disable();
66
    Disable();
67
    Delay();
67
    Delay();
68
    SetControls(0,0);
68
    SetControls(0,0);
69
    SetData(0b00111111);
69
    SetData(0b00111111);
70
    Enable();
70
    Enable();
71
   
71
   
72
    glcdClear();
72
    glcdClear();
73
}
73
}
74
 
74
 
75
void glcdClear(){
75
void glcdClear(){
76
    LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
76
    LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
77
    Enable();
77
    Enable();
78
    Delay();
78
    Delay();
79
    Disable();
79
    Disable();
80
 
80
 
81
 
81
 
82
    uint8_t i;
82
    uint8_t i;
83
    uint8_t j;
83
    uint8_t j;
84
 
84
 
85
    for(j = 0; j < 8; j++){
85
    for(j = 0; j < 8; j++){
86
        SetControls(0,0);
86
        SetControls(0,0);
87
        SetData(0b10111000 + j);
87
        SetData(0b10111000 + j);
88
        Enable();
88
        Enable();
89
 
89
 
90
        SetControls(0,0);
90
        SetControls(0,0);
91
        SetData(0b01000000);
91
        SetData(0b01000000);
92
        Enable();
92
        Enable();
93
        for (i = 0; i < 64; i++){
93
        for (i = 0; i < 64; i++){
94
            SetControls(1,0);
94
            SetControls(1,0);
95
            SetData(0x00);
95
            SetData(0x00);
96
            Enable();
96
            Enable();
97
        }
97
        }
98
    }
98
    }
99
 
99
 
100
}
100
}
101
 
101
 
102
void glcdSetXY(uint8_t x, uint8_t y){
102
void glcdSetXY(uint8_t x, uint8_t y){
103
    GrLcdState.lcdXAddr = x;
103
    GrLcdState.lcdXAddr = x;
104
    GrLcdState.lcdYAddr = y;
104
    GrLcdState.lcdYAddr = y;
105
 
105
 
106
    //Vi börjar med X. Steg 1: Välj rätt chip:
106
    //Vi börjar med X. Steg 1: Välj rätt chip:
107
    if (x > 63){
107
    if (x > 63){
108
        LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2);
108
        LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2);
109
        LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS1);
109
        LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS1);
110
    } else {
110
    } else {
111
        LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS1);
111
        LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS1);
112
        LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS2); 
112
        LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS2); 
113
    }
113
    }
114
   
114
   
115
    //Steg 2: Sätt rätt x-adress på det aktiva chippet
115
    //Steg 2: Sätt rätt x-adress på det aktiva chippet
116
    SetControls(0,0);
116
    SetControls(0,0);
117
    SetData(0b01000000 + x%64);
117
    SetData(0b01000000 + x%64);
118
 
118
 
119
    Enable();
119
    Enable();
120
 
120
 
121
    //Steg 3: Sätt rätt y-adress på det aktiva chippet
121
    //Steg 3: Sätt rätt y-adress på det aktiva chippet
122
    SetData(0b10111000 + y);
122
    SetData(0b10111000 + y);
123
    Enable();
123
    Enable();
124
}
124
}
125
 
125
 
126
void glcdWriteChar(unsigned char c)
126
void glcdWriteChar(unsigned char c)
127
{
127
{
128
uint8_t i = 0;
128
uint8_t i = 0;
129
if (GrLcdState.lcdXAddr > (128-5)){
129
if (GrLcdState.lcdXAddr > (128-5)){
130
    glcdSetXY(0,GrLcdState.lcdYAddr + 1);
130
    glcdSetXY(0,GrLcdState.lcdYAddr + 1);
131
}
131
}
132
 
132
 
133
for(i=0; i<5; i++)
133
for(i=0; i<5; i++)
134
{
134
{
135
    glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
135
    glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
136
}
136
}
137
    glcdWriteData(0x00);
137
    glcdWriteData(0x00);
138
 
138
 
139
}
139
}
140
 
140
 
141
void glcdPutStr(unsigned char *data){
141
void glcdPutStr(unsigned char *data){
142
    while (*data){
142
    while (*data){
143
        glcdWriteChar(*data);
143
        glcdWriteChar(*data);
144
        data++;
144
        data++;
145
    }
145
    }
146
}
146
}
147
 
147
 
148
 
148