-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.c
433 lines (362 loc) · 10 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*==========================================================================*\
| |
| IO.c |
| |
| contains: procedures for display initialisation, output and the keypad |
|----------------------------------------------------------------------------|
| Project: MSP430 ProgD |
| Developed using: Microchip MPLAB IDE v8.66 + HITECHC Toolchain 9.83 |
|----------------------------------------------------------------------------|
| Author: STR |
| Version: 1.0 |
| Initial Version: 08-18-12 |
| Last Change: 08-18-12 |
|----------------------------------------------------------------------------|
| Version history: |
| 1.0 08/12 STR Initial version. |
|----------------------------------------------------------------------------|
| Designed 2012 by Steven Rott Germany |
\*==========================================================================*/
#include "p18f4620.h"
#include "JTAGfunc.h"
#include "LowLevelfunc.h"
#include "IO.h"
const byte keypad[3][16]=
{
'F','B','0','A','E','3','2','1','D','6','5','4','C','9','8','7',
0x70,0x71,0x72,0x73,0xB0,0xB1,0xB2,0xB3,0xD0,0xD1,0xD2,0xD3,0xE0,0xE1,0xE2,0xE3,
0x0F,0x0B,0x00,0x0A,0x0E,0x03,0x02,0x01,0x0D,0x06,0x05,0x04,0x0C,0x09,0x08,0x07
};
word command=0x0010;
byte i=0;
byte *adr;
/*---------------------------------------------------------------------------
Puts a string onto the specified location of the display. Due to the fact
that PIC18F has different data busses for data and program memory and
initialised strings are always declared as rom (program memory), this
function has to distiguish between a rom or ram character to print.
Arguments: pointer to the (rom or ram )string, coordinates on the screen
Result: none
*/
void printToScreen(byte singleChar, byte *stringVar, rom byte *stringConst, byte x, byte y)
{
byte hadr;
byte ladr;
word temp;
//temp=x+(y*40);
temp=y;
temp=(temp*40)+x;
hadr=(temp&0xFF00)>>8;
ladr=temp&0x00FF;
display(2,ladr,hadr,0x24); //set Adress Pointer
display(0,0,0,0xb0);
if(stringConst)
{
while(*stringConst!='\0') //print a whole line
{
display(3,0,0,*stringConst); //print data and inc adr pointer
stringConst++;
}
}
else if(stringVar)
{
while(*stringVar!='\0') //print a whole line
{
display(3,0,0,*stringVar); //print data and inc adr pointer
stringVar++;
}
}
else{
display(3,0,0,singleChar); //print data and inc adr pointer
}
display(0,0,0,0xb2);
}
/*---------------------------------------------------------------------------
initialize T6963 display controller and draws the frame of main menu
Arguments: none
Result: none
*/
void displayInit(void)
{
byte i=20;
byte j=40;
display(0,0,0,0x81); // Mode Set (80=OR Mode)
display(2,0,0,0x40); // Text Home Adress
display(2,0x28,0,0x41); // Text Area Set
// display(2,0x00,0x00,0x42); // Graphic Home Set
// display(2,0x15,0,0x43); // Graphic Area Set
display(2,0,0,0x24); // Adress Pointer
display(2,37,15,0x21); // Cursor Pointer (x,y)
display(2,0,0,0xa7); // Cursor Pattern
display(0,0,0,0xb0); // Data Auto Write ON
while(i--)
{
while(j--)
display(3,0,0,0x20); // Clear Display RAM
}
display(0,0,0,0xb2); // Data Auto Write OFF
display(0,0,0,0x97); // Display Mode (cmd,0
// cmd(94)=text on, graphic off
// cmd(98)=text off, graphic on
// cmd(9C)=text on, graphic on)
display(2,0,0,0x24);
display(0,0,0,0xb0);
// draw a frame
printToScreen(0,0,(rom byte*)"*---------- MSP430 ProgD --------------*",0,0);
for(i=1u;i<=14u;i++)
printToScreen(0,0,(rom byte*)"| |",0,i);
printToScreen(0,0,(rom byte*)"*-v0.01--------------------------------*",0,15);
printToScreen(0,0,(rom byte*)"cmd: ",29,15);
printToScreen(0,0,(rom byte*)">010: applications ",1,1);
printToScreen(0,0,(rom byte*)">020: settings ",1,2);
printToScreen(0,0,(rom byte*)" ",1,3);
printToScreen(0,0,(rom byte*)">030: help ",1,4);
printToScreen(0,0,(rom byte*)" ",1,6);
display(0,0,0,0xb2);
}
/*---------------------------------------------------------------------------
low level procedure for putting data to T6963 display controller
Arguments: inf(1byte code, 2byte code or just data)
Result: none
*/
void display(byte inf, byte dat0, byte dat1, byte cmd_dat)
{
LATA|=0x0F; //shadowPORTA;
LATB=0xFF; //shadowPORTB;
ClrCS();
switch(inf) //communication protocol for the display controller
{
case 0: //send code with automatic adress pointer
{
getDisplayStatus();
LATB=cmd_dat; //Latch Data to WR Latch
SetCD(); ClrWR(); SetWR(); //wr to lcd (PORTB)
}break;
case 1: //send single byte code plus single command
{
getDisplayStatus();
LATB=dat0;
ClrCD(); ClrWR(); SetWR();
getDisplayStatus();
SetCD();
LATB=cmd_dat;
SetCD(); ClrWR(); SetWR();
}break;
case 2: //send 2 byte data plus single command
{
getDisplayStatus();
LATB=dat0;
ClrCD(); ClrWR(); SetWR();
getDisplayStatus();
SetCD();
LATB=dat1;
ClrCD(); ClrWR(); SetWR();
getDisplayStatus();
SetCD();
LATB=cmd_dat;
SetCD(); ClrWR(); SetWR();
}break;
case 3: //send data without command
{
getDisplayStatus();
transl;
LATB=cmd_dat;
ClrCD(); ClrWR(); SetWR();
getDisplayStatus();
SetCD();
}break;
}
SetCS();
}
void cls(void){
// clear the screen
for(i=1u;i<=14u;i++)
printToScreen(0,0,(rom byte*)"| |",0,i);
}
/*---------------------------------------------------------------------------
waits for T6963 to be ready
Arguments: none
Result: none
*/
void getDisplayStatus(void)
{
byte temp=0;
LATB=0xFF;
LATA|=0x0F;
ClrCS();
TRISB=0xFF;
SetWR();
SetCD();
ClrRD();
usDelay(5);
while(temp!=0x03u)
{
temp=PORTB;
temp&=0x03u;
}
SetRD();
TRISB=0x00;
return;
}
/*---------------------------------------------------------------------------
reads a key from keypad (4x4) column is driven by 74xx138
Arguments: none
Result: 0xFF if no key is pressed, index of pressed key otherwise
see IO.h for the decoding
*/
byte waitForSingleKey(byte dataSel)
{
byte n=0x00; // KEYPAD columns (4x4) connected via 74xx138
// lines selected by PORTD D0 + D1 (1111 1100 = 0xFC)
// PORTD.7 .. PORTD.4 KEYPAD row input
byte m=15;
byte result=0x00;
byte temp;
LATD=0xFF;
do
{
LATD=0b00001100+n;
//MsDelay(10);
result=PORTD;
temp=result;
result|=0x0F;
if(result!=0xFFu)
{
do
{
temp&=0b11110011; //PORTD.2 and PORTD.3 dont care
if(keypad[1][m]==temp){
switch (dataSel){
case HEX:
return(keypad[2][m]);
break;
case ASCII:
return(keypad[0][m]);
break;
}
}
}while(m--);
}
n+=1;
}while(n<=0x04u);
// create the following bitmask:
// xxxx xx00
// xxxx xx01
// xxxx xx10
// xxxx xx11
// so each run selects a different column of the keypad with PortD.0 and PortD.1
// connected to line A and B of the 74xx138
return(0xFF);
}
word getWholeCommand(void)
{
byte keyStroke=0xFF;
byte key[2];
keyStroke=waitForSingleKey(HEX);
if(keyStroke!=0xFF)
{
command|=keyStroke;
command<<=4;
while(waitForSingleKey(HEX)!=0xFF) // SW debounce and wait until switch release
msDelay(100);
key[0]=hex2ascii(keyStroke);
key[1]='\0';
adr=key;
//adr++;
//*adr='\0';
//adr--;
printToScreen(0,adr,0,33+i,15);
keyStroke=0xFF;
i++;
}
if((i==3u)&&(keyStroke==0xFF)) //(i==3)&&(keyStroke==0xFF)
{
keyStroke=0;
msDelay(500);
printToScreen(0,0,(rom byte*)"cmd: ",29,15);
//command=0x0000;
i=0;
return command;
}
keyStroke=0xFF;
}
void txOut(byte character, rom byte *stringConst)
{
if(!character)
{
while(*stringConst!='\0')
{
TXREG=*stringConst;
stringConst++;
while(!TXSTAbits.TRMT);
}
}
else
{
TXREG=character;
while(!TXSTAbits.TRMT);
}
}
void sramWrite(word adress, byte data)
{
word adressbuffer;
TRISB=0x00;
ClrLAL();
ClrHAL();
SetAEN();
adressbuffer=adress;
LATB=adressbuffer;
SetLAL();
ClrLAL();
adressbuffer>>=8;
LATB=adressbuffer;
SetHAL();
ClrHAL();
ClrSRAMSEL();
ClrAEN();
LATB=data;
ClrWR();
SetWR();
SetSRAMSEL();
SetAEN();
}
byte sramRead(word adress)
{
word adressbuffer;
byte data;
ClrLAL();
ClrHAL();
SetAEN();
adressbuffer=adress;
LATB=adressbuffer;
SetLAL();
ClrLAL();
adressbuffer>>=8;
LATB=adressbuffer;
SetHAL();
ClrHAL();
ClrSRAMSEL();
ClrAEN();
TRISB=0xff;
ClrRD();
data=PORTB;
SetRD();
TRISB=0x00;
SetSRAMSEL();
SetAEN();
return data;
}
/*---------------------------------------------------------------------------
mirrors a byte... keep it, just in case... :-)
Arguments:input byte
Result: output byte (mirrored)
*/
/*
byte mirror(byte n )
{
n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
return n;
}
*/