Rev 188 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 188 | Rev 191 | ||
|---|---|---|---|
| 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 | ||
| - | 72 | glcdClear(); |
|
| - | 73 | } |
|
| - | 74 | ||
| - | 75 | void glcdClear(){ |
|
| - | 76 | LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1); |
|
| - | 77 | Enable(); |
|
| - | 78 | Delay(); |
|
| - | 79 | Disable(); |
|
| - | 80 | ||
| 71 | 81 | ||
| 72 | uint8_t i; |
82 | uint8_t i; |
| 73 | uint8_t j; |
83 | uint8_t j; |
| 74 | 84 | ||
| 75 | for(j = 0; j < 8; j++){ |
85 | for(j = 0; j < 8; j++){ |
| 76 | SetControls(0,0); |
86 | SetControls(0,0); |
| 77 | SetData(0b10111000 + j); |
87 | SetData(0b10111000 + j); |
| 78 | Enable(); |
88 | Enable(); |
| 79 | 89 | ||
| 80 | SetControls(0,0); |
90 | SetControls(0,0); |
| 81 | SetData(0b01000000); |
91 | SetData(0b01000000); |
| 82 | Enable(); |
92 | Enable(); |
| 83 | for (i = 0; i < 64; i++){ |
93 | for (i = 0; i < 64; i++){ |
| 84 | SetControls(1,0); |
94 | SetControls(1,0); |
| 85 | SetData(0x00); |
95 | SetData(0x00); |
| 86 | Enable(); |
96 | Enable(); |
| 87 | } |
97 | } |
| 88 | } |
98 | } |
| 89 | 99 | ||
| 90 | } |
100 | } |
| 91 | 101 | ||
| 92 | void glcdSetXY(uint8_t x, uint8_t y){ |
102 | void glcdSetXY(uint8_t x, uint8_t y){ |
| 93 | GrLcdState.lcdXAddr = x; |
103 | GrLcdState.lcdXAddr = x; |
| 94 | GrLcdState.lcdYAddr = y; |
104 | GrLcdState.lcdYAddr = y; |
| 95 | 105 | ||
| 96 | //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: |
| 97 | if (x > 63){ |
107 | if (x > 63){ |
| 98 | LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2); |
108 | LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2); |
| 99 | LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS1); |
109 | LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS1); |
| 100 | } else { |
110 | } else { |
| 101 | LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS1); |
111 | LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS1); |
| 102 | LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS2); |
112 | LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS2); |
| 103 | } |
113 | } |
| 104 | 114 | ||
| 105 | //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 |
| 106 | SetControls(0,0); |
116 | SetControls(0,0); |
| 107 | SetData(0b01000000 + x%64); |
117 | SetData(0b01000000 + x%64); |
| 108 | 118 | ||
| 109 | Enable(); |
119 | Enable(); |
| 110 | 120 | ||
| 111 | //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 |
| 112 | SetData(0b10111000 + y); |
122 | SetData(0b10111000 + y); |
| 113 | Enable(); |
123 | Enable(); |
| 114 | } |
124 | } |
| 115 | 125 | ||
| 116 | void glcdWriteChar(unsigned char c) |
126 | void glcdWriteChar(unsigned char c) |
| 117 | { |
127 | { |
| 118 | uint8_t i = 0; |
128 | uint8_t i = 0; |
| 119 | if (GrLcdState.lcdXAddr > (128-5)){ |
129 | if (GrLcdState.lcdXAddr > (128-5)){ |
| 120 | glcdSetXY(0,GrLcdState.lcdYAddr + 1); |
130 | glcdSetXY(0,GrLcdState.lcdYAddr + 1); |
| 121 | } |
131 | } |
| 122 | 132 | ||
| 123 | for(i=0; i<5; i++) |
133 | for(i=0; i<5; i++) |
| 124 | { |
134 | { |
| 125 | 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 |
| 126 | } |
136 | } |
| 127 | glcdWriteData(0x00); |
137 | glcdWriteData(0x00); |
| 128 | 138 | ||
| 129 | } |
139 | } |
| 130 | 140 | ||
| 131 | void glcdPutStr(unsigned char *data){ |
141 | void glcdPutStr(unsigned char *data){ |
| 132 | while (*data){ |
142 | while (*data){ |
| 133 | glcdWriteChar(*data); |
143 | glcdWriteChar(*data); |
| 134 | data++; |
144 | data++; |
| 135 | } |
145 | } |
| 136 | } |
146 | } |
| 137 | 147 | ||
| 138 | 148 | ||