4 * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
10 * This library / program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; see the file COPYING. If not, write to the
17 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 * As a special exception, if you link this(includeed from sdcc) library
21 * with other files, some of which are compiled with SDCC,
22 * to produce an executable, this library does not by itself cause
23 * the resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why
25 * the executable file might be covered by the GNU General Public License.
31 keyin_defs keyin_old[2];
42 for(i = 0; i < 3; i++) {
43 keyin_old[0].byte[i] = 0x00;
44 keyin_old[1].byte[i] = 0x00;
45 keyin_now.byte[i] = 0x00;
47 for(i = 0; i < 16; i++) keyin_fifo[i] = 0x00;
53 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
55 * For 28Pin PIC(18F2xK22), I2C lcd using.
58 void keyin_ioinit(void)
60 /* Initialize IOPORTS*/
78 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20)
80 * For 28Pin PIC(18F2xK20), I2C lcd using.
83 void keyin_ioinit(void)
85 /* Initialize IOPORTS*/
88 ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
102 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20)
104 * For 40Pin PIC(18F4xK20), paralell or I2C lcd using.
106 void keyin_ioinit(void)
108 /* Initialize IOPORTS*/
111 ANSEL = 0x00; // Use RA0 AS ADC, Another is not used.
121 TRISC = TRIS_C_VAL_O;
124 * You can use PORTD,RE0-RE2 extention, when using I2C lcd.
134 void keyin_ioinit(void)
136 /* Initialize IOPORTS*/
139 // ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
149 TRISC = TRIS_C_VAL_O;
153 * Push to keyin fifo; not used atomic-writing.
155 void push_keyinfifo(char b) __critical
158 if((keyin_nowp > 15) || (keyin_nowp < 0))keyin_nowp = 0;
159 keyin_fifo[keyin_nowp] = b;
161 if(keyin_counter > 16) keyin_counter = 16;
165 * Pop from keyin fifo; not used atomic-reading.
167 char pop_keyinfifo(void) __critical
170 if(keyin_counter <= 0) {
172 return charcode_null ;
174 if(keyin_readp > 15) keyin_readp = 15;
175 c = keyin_fifo[keyin_readp];
177 if(keyin_readp > 15) keyin_readp = 0;
179 if(keyin_counter < 0)keyin_counter = 0;
183 void print_numeric(int i, unsigned char supressf)
185 if((i == 0) && (supressf != 0)){
193 unsigned char supress = 0;
194 if(supressf == 0) supress = 1;
203 _PUTCHAR((l & 0x0f)+ '0');
205 } else if(supress != 0) {
211 _PUTCHAR((l & 0x0f)+ '0');
213 _PUTCHAR((l & 0x0f)+ '0');
220 _PUTCHAR((l & 0x0f)+ '0');
222 _PUTCHAR((l & 0x0f)+ '0');
229 _PUTCHAR((l & 0x0f)+ '0');
231 _PUTCHAR((l & 0x0f)+ '0');
235 _PUTCHAR((i & 0x0f) + '0');
241 void printstr(char *s)
245 if(s == NULL) return;
247 if(s[p] == '\0') break;
253 void print_numeric_nosupress(unsigned int data, unsigned char digit)
260 if(digit == 0) return;
261 if(digit >= 5) digit = 5;
295 unsigned int subst_numeric(unsigned int start, unsigned char pos, unsigned char c)
297 unsigned int p = pos;
302 val = (start / 10) * 10 + c;
305 val = (start / 100) * 100 + start % 10 + c * 10;
308 val = (start / 1000) * 1000 + start % 100 + c * 100;
311 val = (start / 10000) * 10000 + start % 1000 + c * 1000;
314 val = start % 10000 + c * 10000;
323 unsigned int read_numeric(unsigned int initial, unsigned char digit,
324 char startx, char starty)
330 unsigned char input_flag;
337 _LOCATE(startx, starty);
338 print_numeric_nosupress(val, digit);
341 input_flag = readkey_compare();
343 } while(input_flag == 0);
347 val = subst_numeric(val, i, 0);
349 } else if((c >= charcode_1) && (c <= charcode_9)) {
350 val = subst_numeric(val, i, c - charcode_1 + 1);
351 } else if(c == charcode_f) {
354 } else if(c == charcode_d) {
357 val = (val / 10) * 10;
360 } else if(c == charcode_b) {
365 print_numeric_nosupress(val, d);
374 * Set signal tune status led assigned to RC0.
375 * You should modify if you modify circuit.
377 void setsignal_tune(unsigned char flag)
387 * Set power of lcd backlight assigned to RB0.
388 * You should modify if you modify circuit.
390 void set_backlight(unsigned char flag, unsigned int val)
401 * Read IOPORTS for KEY. You should modify if you modify circuit.
403 void readkey_io(void)
406 unsigned char portvar;
407 unsigned char latchvar;
410 if(keyin_counter > 16) keyin_counter = 0;
411 for(i = 0; i < 3; i++){
412 keyin_old[1].byte[i] = keyin_old[0].byte[i];
413 keyin_old[0].byte[i] = keyin_now.byte[i];
414 keyin_now.byte[i] = 0x00;
417 latchvar = LATA | 0x02;
420 low = (portvar & 0x3c) >>2;
421 latchvar = LATA & 0xfd;
424 latchvar = LATB | 0x02;
427 high = (portvar & 0x3c) >>2;
428 latchvar = LATB & 0xfd;
431 keyin_now.byte[0] = (low << 4) | high;
434 latchvar = LATB | 0x04;
437 low = (portvar & 0x3c) >>2;
438 latchvar = LATB & 0xfb;
441 latchvar = LATB | 0x20;
444 high = (portvar & 0x3c) >>2;
445 latchvar = LATB & 0xdf;
448 keyin_now.byte[1] = (low << 4) | high;
451 keyin_now.BIT0F = PORTBbits.RB1;
452 keyin_now.BIT1F = PORTBbits.RB2;
453 keyin_now.BIT2F = PORTBbits.RB3;
454 keyin_now.BIT3F = 0; // Reserve
457 unsigned char readkey_compare(void)
467 for(d = 0; d < 3; d++) {
469 for(b = 0; b < 8; b++){
471 if((keyin_now.byte[c] & shift) != 0) c++;
472 if((keyin_old[0].byte[c] & shift) != 0) c++;
473 if((keyin_old[1].byte[c] & shift) != 0) c++;
476 * Clear older-inputs on .
479 keyin_old[0].byte[c] &= ~shift;
480 keyin_old[1].byte[c] &= ~shift;
481 keyin_now.byte[c] &= ~shift;
483 push_keyinfifo(charcode_0);
487 push_keyinfifo(e + 1);
500 * Initialize sequence:
505 * In interrupt/unsleep hook(call per Nms):
509 * In application handler:
510 * c = pop_keyinfifo();