attempt to reduce space usage

- remove unecessary code
- ifdef "extra" code
- pass data through globals instead of functions accessing static data
- functions non-reentrant, locate tmps in xdata
This commit is contained in:
Jeff Wang 2019-03-31 19:29:37 -04:00
parent ecb0492534
commit 43edb291cf
8 changed files with 132 additions and 99 deletions

View File

@ -1,7 +1,9 @@
SDCC ?= sdcc SDCC ?= sdcc
STCCODESIZE ?= 13000 STCCODESIZE ?= 13312
SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 0 --stack-auto SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 256 --stack-auto --idata-loc 0x80
#SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 256 --stack-auto --model-large
FLASHFILE ?= main.hex FLASHFILE ?= main.hex
LARGE_LDFLAGS += -L/usr/share/sdcc/lib/large/
SRC = src/lcd.c src/key.c src/utils.c src/decn/decn.c src/calc.c SRC = src/lcd.c src/key.c src/utils.c src/decn/decn.c src/calc.c
@ -15,6 +17,7 @@ build/%.rel: src/%.c src/%.h
main: $(OBJ) main: $(OBJ)
$(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(CFLAGS) $^ $(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(CFLAGS) $^
# $(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(CFLAGS) $(LARGE_LDFLAGS) $^
@ tail -n 5 build/main.mem | head -n 2 @ tail -n 5 build/main.mem | head -n 2
@ tail -n 1 build/main.mem @ tail -n 1 build/main.mem
cp build/$@.ihx $@.hex cp build/$@.ihx $@.hex

View File

@ -7,13 +7,16 @@
#include "../utils.h" #include "../utils.h"
#define EXTRA_CHECKS
//#define DEBUG //#define DEBUG
//#define DEBUG_COMPARE_MAGN //#define DEBUG_COMPARE_MAGN
//#define DEBUG_ADD //#define DEBUG_ADD
//#define DEBUG_MULT //#define DEBUG_MULT
//#define DEBUG_MULT_ALL //even more verbose
//#define DEBUG_DIV //#define DEBUG_DIV
#ifndef DESKTOP #ifndef DESKTOP
//#undef EXTRA_CHECKS
#undef DEBUG #undef DEBUG
#undef DEBUG_COMPARE_MAGN #undef DEBUG_COMPARE_MAGN
#undef DEBUG_ADD #undef DEBUG_ADD
@ -34,9 +37,9 @@
#include "decn.h" #include "decn.h"
#ifdef DESKTOP #ifdef DESKTOP
uint8_t num_digits_display = DEC80_NUM_LSU*2; static const uint8_t num_digits_display = DEC80_NUM_LSU*2;
#else #else
uint8_t num_digits_display = 16; static const uint8_t num_digits_display = 16;
#endif #endif
@ -168,7 +171,9 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
if (!SEEN_POINT(curr_sign)){ if (!SEEN_POINT(curr_sign)){
//begin tracking number of digits to right of decimal point //begin tracking number of digits to right of decimal point
curr_sign |= 1; //seen point curr_sign |= 1; //seen point
} else { }
#ifdef EXTRA_CHECKS
else {
//multiple '.'s in string //multiple '.'s in string
#ifdef DEBUG #ifdef DEBUG
printf(" ERROR: multiple '.'s in string\n"); printf(" ERROR: multiple '.'s in string\n");
@ -176,6 +181,7 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
set_dec80_NaN(dest); set_dec80_NaN(dest);
return; return;
} }
#endif
} else if (signif_str[i] >= '1' && signif_str[i] <= '9'){ } else if (signif_str[i] >= '1' && signif_str[i] <= '9'){
if (nibble_i < DEC80_NUM_LSU*2){ if (nibble_i < DEC80_NUM_LSU*2){
if (nibble_i & 1) { //odd if (nibble_i & 1) { //odd
@ -245,6 +251,7 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
assert(DEC80_NUM_LSU*2 > num_lr_points); assert(DEC80_NUM_LSU*2 > num_lr_points);
new_exponent = exponent + (num_lr_points - 1); //1 digit left of implicit point new_exponent = exponent + (num_lr_points - 1); //1 digit left of implicit point
//check for overflow //check for overflow
#ifdef EXTRA_CHECKS
if (new_exponent < exponent || exponent > DEC80_MAX_EXP){ if (new_exponent < exponent || exponent > DEC80_MAX_EXP){
#ifdef DEBUG #ifdef DEBUG
printf(" overflow (new_exp, exp)=(%d,%d)\n", printf(" overflow (new_exp, exp)=(%d,%d)\n",
@ -253,12 +260,14 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
set_dec80_NaN(dest); set_dec80_NaN(dest);
return; return;
} }
#endif
} else if (num_lr_points < 0) { //right count exists } else if (num_lr_points < 0) { //right count exists
// (-num_past_point represents #0s right of decimal) // (-num_past_point represents #0s right of decimal)
// (this ends up being a subtraction) // (this ends up being a subtraction)
new_exponent = exponent + num_lr_points; new_exponent = exponent + num_lr_points;
new_exponent -= 1; //decimal point after 1st non-zero number new_exponent -= 1; //decimal point after 1st non-zero number
//check for underflow //check for underflow
#ifdef EXTRA_CHECKS
if (new_exponent > exponent || exponent < DEC80_MIN_EXP){ if (new_exponent > exponent || exponent < DEC80_MIN_EXP){
#ifdef DEBUG #ifdef DEBUG
printf(" underflow (new_exp, exp)=(%d,%d)\n", printf(" underflow (new_exp, exp)=(%d,%d)\n",
@ -267,6 +276,7 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
set_dec80_NaN(dest); set_dec80_NaN(dest);
return; return;
} }
#endif
} else { } else {
//no change //no change
new_exponent = exponent; new_exponent = exponent;
@ -319,6 +329,7 @@ static uint8_t decn_is_zero(const dec80* x){
return 1; return 1;
} }
#ifdef EXTRA_CHECKS
void set_dec80_NaN(dec80* dest){ void set_dec80_NaN(dec80* dest){
uint8_t i; uint8_t i;
@ -329,6 +340,7 @@ void set_dec80_NaN(dec80* dest){
dest->lsu[i] = 0; dest->lsu[i] = 0;
} }
} }
#endif
void negate_decn(dec80* x){ void negate_decn(dec80* x){
static const int16_t xor_val = -(0x7fff) - 1; static const int16_t xor_val = -(0x7fff) - 1;
@ -339,7 +351,7 @@ int8_t compare_magn(const dec80* a, const dec80* b){ //a<b: -1, a==b: 0, a>b: 1
uint8_t a_i, b_i; uint8_t a_i, b_i;
int16_t a_exp=0, b_exp=0; int16_t a_exp=0, b_exp=0;
int8_t a_signif_b = 0; //a<b: -1, a==b: 0, a>b: 1 int8_t a_signif_b = 0; //a<b: -1, a==b: 0, a>b: 1
dec80 a_tmp, b_tmp; static __xdata dec80 a_tmp, b_tmp;
//copy //copy
copy_decn(&a_tmp, a); copy_decn(&a_tmp, a);
copy_decn(&b_tmp, b); copy_decn(&b_tmp, b);
@ -448,7 +460,7 @@ static void _incr_exp(dec80* acc, int16_t exponent){
static void sub_mag(dec80* acc, const dec80* x){ static void sub_mag(dec80* acc, const dec80* x){
int8_t i; int8_t i;
uint8_t carry = 0; uint8_t carry = 0;
dec80 tmp; static __xdata dec80 tmp;
copy_decn(&tmp, x); copy_decn(&tmp, x);
//normalize //normalize
remove_leading_zeros(acc); remove_leading_zeros(acc);
@ -478,7 +490,7 @@ static void sub_mag(dec80* acc, const dec80* x){
} }
void add_decn(dec80* acc, const dec80* x){ void add_decn(dec80* acc, const dec80* x){
dec80 tmp; static __xdata dec80 tmp;
int8_t rel; int8_t rel;
uint8_t carry = 0; uint8_t carry = 0;
int8_t i; int8_t i;
@ -599,8 +611,8 @@ void add_decn(dec80* acc, const dec80* x){
} }
void mult_decn(dec80* acc, const dec80* x){ void mult_decn(dec80* acc, const dec80* x){
dec80 tmp; //copy of x static __xdata dec80 tmp; //copy of x
dec80 acc_tmp; //holds sum static __xdata dec80 acc_tmp; //holds sum
int8_t i, j; int8_t i, j;
uint8_t carry = 0; uint8_t carry = 0;
uint8_t is_neg; uint8_t is_neg;
@ -631,7 +643,7 @@ void mult_decn(dec80* acc, const dec80* x){
carry = digit100 / 100; carry = digit100 / 100;
assert(carry < 100); assert(carry < 100);
} }
#ifdef DEBUG_MULT #ifdef DEBUG_MULT_ALL
printf("\n%d:", i); printf("\n%d:", i);
printf("\n acc:"); printf("\n acc:");
for (j = 0; j < DEC80_NUM_LSU; j++){ for (j = 0; j < DEC80_NUM_LSU; j++){
@ -644,6 +656,8 @@ void mult_decn(dec80* acc, const dec80* x){
else else
printf(" "); printf(" ");
} }
#endif
#ifdef DEBUG_MULT
printf("\n acc_tmp:"); printf("\n acc_tmp:");
for (j = 0; j < DEC80_NUM_LSU; j++){ for (j = 0; j < DEC80_NUM_LSU; j++){
printf(" %3d", acc_tmp.lsu[j]); printf(" %3d", acc_tmp.lsu[j]);
@ -681,14 +695,16 @@ void mult_decn(dec80* acc, const dec80* x){
} }
void div_decn(dec80* acc, const dec80* x){ void div_decn(dec80* acc, const dec80* x){
dec80 tmp; //copy of x, holds current 1/x estimate static __xdata dec80 tmp; //copy of x, holds current 1/x estimate
dec80 acc_copy; //holds copy of original acc static __xdata dec80 acc_copy; //holds copy of original acc
uint8_t i; uint8_t i;
//check divide by zero //check divide by zero
#ifdef EXTRA_CHECKS
if (decn_is_zero(x)){ if (decn_is_zero(x)){
set_dec80_NaN(acc); set_dec80_NaN(acc);
return; return;
} }
#endif
//store copy of acc for final multiply by 1/x //store copy of acc for final multiply by 1/x
copy_decn(&acc_copy, acc); copy_decn(&acc_copy, acc);
//get initial estimate for 1/x, by negating exponent, and setting signif. to 1 //get initial estimate for 1/x, by negating exponent, and setting signif. to 1
@ -702,24 +718,24 @@ void div_decn(dec80* acc, const dec80* x){
for (i = 0; i < 20; i++){ //just fix number of iterations for now for (i = 0; i < 20; i++){ //just fix number of iterations for now
#ifdef DEBUG_DIV #ifdef DEBUG_DIV
extern char Buf[80]; extern char Buf[80];
dec80_to_str(buf, &tmp); dec80_to_str(Buf, &tmp);
printf("%2d: %s\n", i, buf); printf("%2d: %s\n", i, Buf);
#endif #endif
mult_decn(acc, x); mult_decn(acc, x);
#ifdef DEBUG_DIV #ifdef DEBUG_DIV
dec80_to_str(buf, acc); dec80_to_str(Buf, acc);
printf(" %20s: %s\n", "recip*x", buf); printf(" %20s: %s\n", "recip*x", Buf);
#endif #endif
negate_decn(acc); negate_decn(acc);
add_decn(acc, &DECN_1); add_decn(acc, &DECN_1);
#ifdef DEBUG_DIV #ifdef DEBUG_DIV
dec80_to_str(buf, acc); dec80_to_str(Buf, acc);
printf(" %20s: %s\n", "(1-recip*x)", buf); printf(" %20s: %s\n", "(1-recip*x)", Buf);
#endif #endif
mult_decn(acc, &tmp); mult_decn(acc, &tmp);
#ifdef DEBUG_DIV #ifdef DEBUG_DIV
dec80_to_str(buf, acc); dec80_to_str(Buf, acc);
printf(" %20s: %s\n", "recip * (1-recip*x)", buf); printf(" %20s: %s\n", "recip * (1-recip*x)", Buf);
#endif #endif
add_decn(acc, &tmp); add_decn(acc, &tmp);
//new_est(acc) = recip + (1 - recip*x)*recip, where tmp is current recip estimate //new_est(acc) = recip + (1 - recip*x)*recip, where tmp is current recip estimate
@ -737,7 +753,7 @@ void dec80_to_str(char* buf, const dec80* x){
int16_t exponent = 0; int16_t exponent = 0;
uint8_t trailing_zeros = 0; uint8_t trailing_zeros = 0;
uint8_t use_sci = 0; uint8_t use_sci = 0;
dec80 tmp; static __xdata dec80 tmp;
//copy and normalize //copy and normalize
copy_decn(&tmp, x); copy_decn(&tmp, x);

View File

@ -7,16 +7,16 @@
#include <stdint.h> #include <stdint.h>
#include "utils.h" #include "utils.h"
#include "key.h"
#ifndef DESKTOP #ifndef DESKTOP
#include "stc15.h" #include "stc15.h"
#endif #endif
#define TOTAL_ROWS 5
#define M_COLS 4 #define M_COLS 4
static uint8_t keys[TOTAL_ROWS]; //only bottom nibbles get set uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set
static uint32_t new_keys_pressed; //bottom 20 bits get set static uint32_t new_keys_pressed; //bottom 20 bits get set
static uint8_t last_state[TOTAL_ROWS]; //all used, 2 bits per key static uint8_t last_state[TOTAL_ROWS]; //all used, 2 bits per key
static int8_t last_count[TOTAL_ROWS][M_COLS]; //all used static int8_t last_count[TOTAL_ROWS][M_COLS]; //all used
@ -81,7 +81,7 @@ static void raw_scan(void){
uint8_t i, j; uint8_t i, j;
//scan top row //scan top row
keys[0] = 0; //initially Keys[0] = 0; //initially
//temporarily set P3[1:0] push pull output M[1:0]=b01 //temporarily set P3[1:0] push pull output M[1:0]=b01
P3M0 |= 0x3; P3M0 |= 0x3;
P3 &= ~(0x3); P3 &= ~(0x3);
@ -89,19 +89,19 @@ static void raw_scan(void){
//back to quasi-bidirectional w/ pullup M[1:0]=b00 //back to quasi-bidirectional w/ pullup M[1:0]=b00
P3M0 &= ~(0x3); P3M0 &= ~(0x3);
//read inputs //read inputs
keys[0] += (P3_0 ? 0 : (1<<0)); //on Keys[0] += (P3_0 ? 0 : (1<<0)); //on
keys[0] += (P3_1 ? 0 : (1<<1)); Keys[0] += (P3_1 ? 0 : (1<<1));
keys[0] += (P5_4 ? 0 : (1<<2)); Keys[0] += (P5_4 ? 0 : (1<<2));
keys[0] += (P5_5 ? 0 : (1<<3)); Keys[0] += (P5_5 ? 0 : (1<<3));
//scan matrix //scan matrix
for (i = 0; i < M_ROWS; i++){ for (i = 0; i < M_ROWS; i++){
keys[i+1] = 0; //initially Keys[i+1] = 0; //initially
for (j = 0; j < M_COLS; j++){ for (j = 0; j < M_COLS; j++){
uint8_t read; uint8_t read;
P1 = (~(1 << (j+4))); //pulldown column (all others pull up) P1 = (~(1 << (j+4))); //pulldown column (all others pull up)
read = (P1 & (1<<i)); //read button read = (P1 & (1<<i)); //read button
read = (read == 0 ? 1 : 0); //pressed if grounded by column driver read = (read == 0 ? 1 : 0); //pressed if grounded by column driver
keys[i+1] += (read << j); Keys[i+1] += (read << j);
P1 = 0xff; //reset all as pullups P1 = 0xff; //reset all as pullups
} }
} }
@ -123,7 +123,7 @@ static void debounce(void){
new_count = curr_count; new_count = curr_count;
new_state = curr_state; new_state = curr_state;
//update count //update count
if (keys[i] & (1 << j)){ if (Keys[i] & (1 << j)){
if (curr_count < COUNT_LIM_HIGH){ if (curr_count < COUNT_LIM_HIGH){
new_count = curr_count + 1; new_count = curr_count + 1;
} }
@ -198,9 +198,11 @@ void KeyScan(void){
debounce(); debounce();
} }
#ifdef DEBUG_KEYS
const uint8_t* DebugGetKeys(void){ const uint8_t* DebugGetKeys(void){
return keys; return keys;
} }
#endif
const uint32_t GetNewKeys(void){ const uint32_t GetNewKeys(void){
return new_keys_pressed; return new_keys_pressed;

View File

@ -7,10 +7,20 @@
#ifndef SRC_KEY_H_ #ifndef SRC_KEY_H_
#define SRC_KEY_H_ #define SRC_KEY_H_
//#define DEBUG_KEYS
void KeyInit(void); void KeyInit(void);
void KeyScan(void); void KeyScan(void);
const uint8_t* DebugGetKeys(void);
const uint32_t GetNewKeys(void); const uint32_t GetNewKeys(void);
#ifdef DEBUG_KEYS
const uint8_t* DebugGetKeys(void);
#endif
//definition included for determining if multiple keys are pressed
//prefer using GetNewKeys();
#define TOTAL_ROWS 5
extern uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set
#endif /* SRC_KEY_H_ */ #endif /* SRC_KEY_H_ */

View File

@ -29,9 +29,6 @@
static int row, col; static int row, col;
static int OpenFlag = 0;
static int ErrorTimeout = 0;
static int Error = 0;
#define CLEAR_BIT(port, bit) (port &= ~(_BV(bit))) #define CLEAR_BIT(port, bit) (port &= ~(_BV(bit)))
#define CLEAR_BITS(port, bits) (port &= ~(bits)) #define CLEAR_BITS(port, bits) (port &= ~(bits))
@ -112,7 +109,6 @@ static char readBusy() {
return busy; return busy;
} }
//sets ErrorTimeout flag if busy signal not cleared in time
static void wait_busy() { static void wait_busy() {
unsigned int i; unsigned int i;
for (i = 0; i < 100; i++){ for (i = 0; i < 100; i++){
@ -122,7 +118,6 @@ static void wait_busy() {
_delay_ms(1); _delay_ms(1);
} }
ErrorTimeout = 1;
return; return;
} }
@ -148,49 +143,39 @@ static void LCD_OutChar(unsigned char c) {
} }
void LCD_Open(void) { void LCD_Open(void) {
static int open_error = 0; //set ports to push-pull output M[1:0] = b01
if (!OpenFlag) { //P2 entire port
//set ports to push-pull output M[1:0] = b01 P2M1 = 0;
//P2 entire port P2M0 = 0xff;
P2M1 = 0; //P3 pins 7:4
P2M0 = 0xff; P3M1 &= ~(0xf0);
//P3 pins 7:4 P3M0 |= (0xf0);
P3M1 &= ~(0xf0);
P3M0 |= (0xf0);
_delay_ms(30); // to allow LCD powerup _delay_ms(30); // to allow LCD powerup
outCsrBlindNibble(0x03); // (DL=1 8-bit mode) outCsrBlindNibble(0x03); // (DL=1 8-bit mode)
_delay_ms(5); // blind cycle 5ms wait _delay_ms(5); // blind cycle 5ms wait
outCsrBlindNibble(0x03); // (DL=1 8-bit mode) outCsrBlindNibble(0x03); // (DL=1 8-bit mode)
_delay_us(100); // blind cycle 100us wait _delay_us(100); // blind cycle 100us wait
outCsrBlindNibble(0x03); // (DL=1 8-bit mode) outCsrBlindNibble(0x03); // (DL=1 8-bit mode)
_delay_us(100); // blind cycle 100us wait (not called for, but do it anyway) _delay_us(100); // blind cycle 100us wait (not called for, but do it anyway)
outCsrBlindNibble(0x02); // DL=1 switch to 4 bit mode (only high 4 bits are sent) outCsrBlindNibble(0x02); // DL=1 switch to 4 bit mode (only high 4 bits are sent)
_delay_us(100); // blind cycle 100 us wait _delay_us(100); // blind cycle 100 us wait
//set increment, no shift //set increment, no shift
outCsrBlind(0x4 + LCDINC + LCDNOSHIFT); outCsrBlind(0x4 + LCDINC + LCDNOSHIFT);
#ifdef DEBUG #ifdef DEBUG
//set display on, cursor on, blink on: //set display on, cursor on, blink on:
outCsrBlind(0x0c + LCDCURSOR + LCDBLINK); outCsrBlind(0x0c + LCDCURSOR + LCDBLINK);
#else #else
//set display on, cursor and blink off: //set display on, cursor and blink off:
outCsrBlind(0x0c + LCDNOCURSOR + LCDNOBLINK); outCsrBlind(0x0c + LCDNOCURSOR + LCDNOBLINK);
#endif #endif
//set display shift on and to the right //set display shift on and to the right
outCsrBlind(0x10 + LCDNOSCROLL + LCDRIGHT); outCsrBlind(0x10 + LCDNOSCROLL + LCDRIGHT);
//set 4-bit mode, 2 line, 5x7 display: //set 4-bit mode, 2 line, 5x7 display:
outCsrBlind(0x20 + LCD2LINE + LCD7DOT); outCsrBlind(0x20 + LCD2LINE + LCD7DOT);
//clear display //clear display
LCD_Clear(); // LCD_Clear();
_delay_ms(50); // _delay_ms(50);
if (!readBusy()) {
OpenFlag = 1;
} else {
open_error = 1;
}
} else { //display already open
open_error = 1;
}
} }
//row and columns indexed from 0 //row and columns indexed from 0
@ -199,8 +184,6 @@ void LCD_GoTo(unsigned int row_to, unsigned int col_to) {
outCsr(0x80 + 0x40 * row_to + col_to); //set ddram address to position outCsr(0x80 + 0x40 * row_to + col_to); //set ddram address to position
row = row_to; row = row_to;
col = col_to; col = col_to;
} else {
Error = 1;
} }
} }
@ -267,19 +250,8 @@ void LCD_OutNibble(uint8_t x){
} }
void LCD_Clear() { void LCD_Clear() {
if (OpenFlag) { outCsr(0x01);
outCsr(0x01);
} else {
outCsrBlind(0x01);
}
row = 0; row = 0;
col = 0; col = 0;
} }
unsigned char LCD_Timeout_Error(void) {
if (ErrorTimeout != 0) {
return 1;
}
return 0;
}

View File

@ -25,14 +25,17 @@ static const char KEY_MAP[20] = {
uint32_t NewKeyBuf[4]; uint32_t NewKeyBuf[4];
volatile uint8_t new_key_write_i; volatile uint8_t new_key_write_i;
volatile uint8_t new_key_read_i; volatile uint8_t new_key_read_i;
volatile uint8_t new_key_empty; volatile uint8_t NewKeyEmpty;
#define INCR_NEW_KEY_I(i) i = (i + 1) & 3 #define INCR_NEW_KEY_I(i) i = (i + 1) & 3
volatile uint8_t SecCount; volatile uint8_t SecCount;
void timer0_isr() __interrupt 1 __using 1 void timer0_isr() __interrupt 1 __using 1
//#define TRACK_TIME
{ {
#ifdef TRACK_TIME
static uint8_t count = 0; static uint8_t count = 0;
static uint8_t min_count = 0, hour_count = 0; static uint8_t min_count = 0, hour_count = 0;
#endif
uint32_t new_keys; uint32_t new_keys;
@ -40,16 +43,17 @@ void timer0_isr() __interrupt 1 __using 1
KeyScan(); KeyScan();
new_keys = GetNewKeys(); new_keys = GetNewKeys();
if (new_keys != 0){ if (new_keys != 0){
if (!new_key_empty && (new_key_write_i == new_key_read_i)){ if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
//do not overwrite keymap currently being processed //do not overwrite keymap currently being processed
INCR_NEW_KEY_I(new_key_write_i); INCR_NEW_KEY_I(new_key_write_i);
} }
NewKeyBuf[new_key_write_i] = new_keys; NewKeyBuf[new_key_write_i] = new_keys;
INCR_NEW_KEY_I(new_key_write_i); INCR_NEW_KEY_I(new_key_write_i);
new_key_empty = 0; NewKeyEmpty = 0;
} }
//track time //track time
#ifdef TRACK_TIME
count++; count++;
if (count == 200){ if (count == 200){
count = 0; count = 0;
@ -63,6 +67,7 @@ void timer0_isr() __interrupt 1 __using 1
} }
} }
} }
#endif
} }
@ -104,8 +109,15 @@ int main()
uint8_t entering_exp = ENTERING_DONE; uint8_t entering_exp = ENTERING_DONE;
uint8_t no_lift = 0; uint8_t no_lift = 0;
uint8_t exp_i = 0; uint8_t exp_i = 0;
#ifdef DEBUG_KEYS
uint8_t j = 0;
const uint8_t* keys; const uint8_t* keys;
uint8_t key_i; uint8_t key_i;
#endif
#ifdef DEBUG_UPTIME
uint32_t i;
#endif
Timer0Init(); // display refresh & switch read Timer0Init(); // display refresh & switch read
LCD_Open(); LCD_Open();
KeyInit(); KeyInit();
@ -116,12 +128,17 @@ int main()
P3M1 &= ~(0x4); P3M1 &= ~(0x4);
P3M0 |= (0x4); P3M0 |= (0x4);
#ifdef DEBUG_UPTIME
i = 0; i = 0;
j = 0; #endif
// LOOP // LOOP
while (1) while (1)
{ {
LCD_GoTo(0,0); LCD_GoTo(0,0);
#ifdef DEBUG_UPTIME
u32str(i++, Buf, 10);
LCD_OutString(Buf, MAX_CHARS_PER_LINE);
#else
//display y register on first line //display y register on first line
if (entering_exp == ENTERING_DONE){ if (entering_exp == ENTERING_DONE){
dec80_to_str(Buf, get_y()); dec80_to_str(Buf, get_y());
@ -130,6 +147,9 @@ int main()
dec80_to_str(Buf, get_x()); dec80_to_str(Buf, get_x());
} }
LCD_OutString(Buf, MAX_CHARS_PER_LINE); LCD_OutString(Buf, MAX_CHARS_PER_LINE);
#endif //DEBUG_UPTIME
#ifdef DEBUG_KEYS
//keyboard debug //keyboard debug
keys = DebugGetKeys(); keys = DebugGetKeys();
for (key_i = 0; key_i < 5; key_i++){ for (key_i = 0; key_i < 5; key_i++){
@ -150,14 +170,16 @@ int main()
} else { } else {
LCD_OutString(u32str(SecCount, Buf, 10)); LCD_OutString(u32str(SecCount, Buf, 10));
} }
#endif //DEBUG_KEYS
///new keys
if (!new_key_empty){ ///get new keys
if (!NewKeyEmpty){
uint8_t i_key; uint8_t i_key;
uint32_t new_keys = NewKeyBuf[new_key_read_i]; uint32_t new_keys = NewKeyBuf[new_key_read_i];
INCR_NEW_KEY_I(new_key_read_i); INCR_NEW_KEY_I(new_key_read_i);
if (new_key_read_i == new_key_write_i){ if (new_key_read_i == new_key_write_i){
new_key_empty = 1; NewKeyEmpty = 1;
} }
#ifdef DEBUG_KEYS #ifdef DEBUG_KEYS

View File

@ -5,11 +5,12 @@
void _delay_ms(uint8_t ms){ void _delay_ms(uint8_t ms){
//TODO: //TODO:
} }
#ifdef ACCURATE_DELAY_US
void _delay_us(uint8_t us) void _delay_us(uint8_t us)
{ {
//TODO: //TODO:
} }
#else #endif
void _delay_ms(uint8_t ms) void _delay_ms(uint8_t ms)
{ {
// delay function, tuned for 11.583 MHz clock // delay function, tuned for 11.583 MHz clock
@ -28,6 +29,7 @@ void _delay_ms(uint8_t ms)
__endasm; __endasm;
} }
#ifdef ACCURATE_DELAY_US
void _delay_us(uint8_t us) void _delay_us(uint8_t us)
{ {
// delay function, tuned for 11.583 MHz clock // delay function, tuned for 11.583 MHz clock

View File

@ -11,7 +11,13 @@
void _delay_ms(uint8_t ms); void _delay_ms(uint8_t ms);
#define ACCURATE_DELAY_US
#ifdef ACCURATE_DELAY_US
void _delay_us(uint8_t us); void _delay_us(uint8_t us);
#else
#define _delay_us(x) _delay_ms(1)
#endif
char* u32str(uint32_t x, char* buf, uint8_t base); char* u32str(uint32_t x, char* buf, uint8_t base);