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 = 0x01; // 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;
263 for(i = 0; i < digit; i++){
264 c = (data % ref) / div + '0';
273 unsigned int subst_numeric(unsigned int start, unsigned char pos, unsigned char c)
275 unsigned int p = pos;
280 val = (start / 10) * 10 + c;
283 val = (start / 100) * 100 + start % 10 + c * 10;
286 val = (start / 1000) * 1000 + start % 100 + c * 100;
289 val = (start / 10000) * 10000 + start % 1000 + c * 1000;
292 val = start % 10000 + c * 10000;
301 unsigned int read_numeric(unsigned int initial, unsigned char digit,
302 char startx, char starty)
308 unsigned char input_flag;
315 _LOCATE(startx, starty);
316 print_numeric_nosupress(val, digit);
319 input_flag = readkey_compare();
321 } while(input_flag == 0);
325 val = subst_numeric(val, i, 0);
327 } else if((c >= charcode_1) && (c <= charcode_9)) {
328 val = subst_numeric(val, i, c - charcode_1 + 1);
329 } else if(c == charcode_f) {
332 } else if(c == charcode_d) {
335 val = (val / 10) * 10;
338 } else if(c == charcode_b) {
343 print_numeric_nosupress(val, d);
352 * Set signal tune status led assigned to RC0.
353 * You should modify if you modify circuit.
355 void setsignal_tune(unsigned char flag)
365 * Set power of lcd backlight assigned to RB0.
366 * You should modify if you modify circuit.
368 void set_backlight(unsigned char flag, unsigned int val)
379 * Read IOPORTS for KEY. You should modify if you modify circuit.
381 void readkey_io(void)
384 unsigned char portvar;
385 unsigned char latchvar;
388 if(keyin_counter > 16) keyin_counter = 0;
389 for(i = 0; i < 3; i++){
390 keyin_old[1].byte[i] = keyin_old[0].byte[i];
391 keyin_old[0].byte[i] = keyin_now.byte[i];
392 keyin_now.byte[i] = 0x00;
395 latchvar = LATA | 0x02;
398 low = (portvar & 0x3c) >>2;
399 latchvar = LATA & 0xfd;
402 latchvar = LATB | 0x02;
405 high = (portvar & 0x3c) >>2;
406 latchvar = LATB & 0xfd;
409 keyin_now.byte[0] = (low << 4) | high;
412 latchvar = LATB | 0x04;
415 low = (portvar & 0x3c) >>2;
416 latchvar = LATB & 0xfb;
419 latchvar = LATB | 0x20;
422 high = (portvar & 0x3c) >>2;
423 latchvar = LATB & 0xdf;
426 keyin_now.byte[1] = (low << 4) | high;
429 keyin_now.BIT0F = PORTBbits.RB1;
430 keyin_now.BIT1F = PORTBbits.RB2;
431 keyin_now.BIT2F = PORTBbits.RB3;
432 keyin_now.BIT3F = 0; // Reserve
435 unsigned char readkey_compare(void)
445 for(d = 0; d < 3; d++) {
447 for(b = 0; b < 8; b++){
449 if((keyin_now.byte[c] & shift) != 0) c++;
450 if((keyin_old[0].byte[c] & shift) != 0) c++;
451 if((keyin_old[1].byte[c] & shift) != 0) c++;
454 * Clear older-inputs on .
457 keyin_old[0].byte[c] &= ~shift;
458 keyin_old[1].byte[c] &= ~shift;
459 keyin_now.byte[c] &= ~shift;
461 push_keyinfifo(charcode_0);
465 push_keyinfifo(e + 1);
478 * Initialize sequence:
483 * In interrupt/unsleep hook(call per Nms):
487 * In application handler:
488 * c = pop_keyinfifo();