Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
Binary file removed Canbus/.DS_Store
Binary file not shown.
Binary file removed KSlibs/.DS_Store
Binary file not shown.
39 changes: 25 additions & 14 deletions KSlibs/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
#include <wiring.h>
#define DelayMS(n) delay(n)

/*
Real important stuff:
Pins 0..2 of PORTG are Strobe, R/W, and Reg Select
Pins 0..3 of PORTA are D0..D3
*/

#define DISP_IN {PORTA |= 0x0f; DDRA &= ~0x0f;}
#define DISP_OUT {DDRA |= 0x0f; }
#define DISP_OUT {DDRA |= 0x0f;}
#define DISP_EN_HIGH {PORTG |= 0x01;}
#define DISP_EN_LOW {PORTG &= ~0x01;}
#define DISP_READ {PORTG |= 0x02;}
Expand All @@ -31,18 +36,18 @@ void Disp_Init() {
unsigned char Disp_ReadSR() {
unsigned char highnib, lownib;

DISP_IN;
PORTG &= ~7;
DISP_IR;
DISP_READ;
DISP_IN; // {PORTA |= 0x0f; DDRA &= ~0x0f;} Make PORTA input, turn on pull-up resistors
PORTG &= ~7; // Turn off RW and RS
DISP_IR; // {PORTG &= ~0x04;} Turn off RS?
DISP_READ; // {PORTG |= 0x02;} Turn on RW

DISP_EN_HIGH; // read IR en
highnib = DISP_DATA_IN;
DISP_EN_LOW;
DISP_EN_HIGH; // read IR en // {PORTG &= ~0x01;} Strobe on
highnib = DISP_DATA_IN; // (PINA & 0x0f) Read nibble
DISP_EN_LOW; // {PORTG &= ~0x01;} Strobe off

DISP_EN_HIGH;
lownib = DISP_DATA_IN;
DISP_EN_LOW;
DISP_EN_HIGH; // {PORTG &= ~0x01;} Strobe on
lownib = DISP_DATA_IN; // (PINA & 0x0f) Read nibble
DISP_EN_LOW; // {PORTG &= ~0x01;} Strobe off

return (highnib << 4) + lownib;
}
Expand Down Expand Up @@ -152,9 +157,9 @@ void Disp_Reset() {


void Disp_Wait() {
// while (Disp_ReadSR() & 0x01) ;
//delay(1);
delayMicroseconds(75);
// while (Disp_ReadSR() & 0x40);
// delay(1);
delayMicroseconds(75);
}


Expand Down Expand Up @@ -209,3 +214,9 @@ void Disp_CursOff() {
Disp_WriteIR(0x0c); // display on, cursor off
Disp_Wait();
}

void Disp_Clear() {
Disp_WriteIR(0x01); // display clear
//Disp_Wait();
DelayMS(2);
}
1 change: 1 addition & 0 deletions KSlibs/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void Disp_PutChar(char);
void Disp_PutStr(char*);
void Disp_CursOn();
void Disp_CursOff();
void Disp_Clear();



Expand Down
27 changes: 27 additions & 0 deletions KSlibs/gpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdint.h>
#include "gpio.h"

#ifndef _BV
#define _BV(bit) (1<<(bit))
#endif

// Set bit in DDR
void gpio_set_ddr(gpio_s* p){
*(p->port - 1) |= _BV(p->pin);
}
// Clear bit in DDR
void gpio_clr_ddr(gpio_s* p){
*(p->port - 1) &= 0xFF & ~_BV(p->pin);
}
// Set pin in PORT
void gpio_set_pin(gpio_s* p){
*p->port |= _BV(p->pin);
}
// Clear pin in PORT
void gpio_clr_pin(gpio_s* p){
*p->port &= 0xFF & ~_BV(p->pin);
}
// Get PIN value
unsigned char gpio_get_pin(gpio_s* p){
return *(p->port - 2);
}
21 changes: 21 additions & 0 deletions KSlibs/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef GPIO_H
#define GPIO_H
typedef struct gpio {
volatile uint8_t *port;
unsigned char pin;
} gpio_s;

#ifdef __cplusplus
extern "C" {
#endif
void gpio_set_ddr(gpio_s*); // Set bit in DDR
void gpio_clr_ddr(gpio_s*); // Clear bit in DDR
void gpio_set_pin(gpio_s*); // Set pin in PORT
void gpio_clr_pin(gpio_s*); // Clear pin in PORT
unsigned char gpio_get_pin(gpio_s*); // Get PIN value

#ifdef __cplusplus
}
#endif

#endif // GPIO_H
18 changes: 18 additions & 0 deletions KSlibs/intmath.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
unsigned u_sublim (unsigned minuend, unsigned subtrahend, unsigned limit) {
unsigned x;
if (minuend < limit) return limit; // Minuend must already be > limit
if (subtrahend > minuend) return limit; // Avoid unsigned underflow (result < 0)
x = minuend - subtrahend;
if (x > limit) return x;
else return limit;
}

unsigned u_addlim (unsigned augend, unsigned addend, unsigned limit) {
unsigned x;
if (augend > limit) return limit; // Augend must be < limit
if (addend > limit) return limit; // Addend must be < limit
x = augend + addend;
if (x < addend) return limit; // Overflow must have occurred (usually undefined behavior)
if (x < limit) return x;
else return limit;
}
17 changes: 17 additions & 0 deletions KSlibs/intmath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
intmath.h - integer math convenience functions
*/

#ifdef __cplusplus
extern "C" {
#endif

// Subtract and limit the result
unsigned u_sublim (unsigned minuend, unsigned subtrahend, unsigned limit);

// Add and limit the result
unsigned u_addlim (unsigned augend, unsigned addend, unsigned limit);

#ifdef __cplusplus
}
#endif
22 changes: 9 additions & 13 deletions KSlibs/keypad.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
#include <avr/io.h>



void Kpd_Init() {
PORTA |= 0xf0;
DDRA &= ~0xf0; // key0 - key3 are inputs with pullup
Kpd_Reset();
}



void Kpd_Reset() {
PORTA = 0xf0; // Makse sure all key pins are set for input
DDRA = 0x0f; // key0 - key3 are inputs with pullup

}



#define PBIT(n) (1 << (n))
#define NBIT(n) (0x0f ^ PBIT(n))
#define PBIT(n) (1 << (n)) // Set a single bit
#define NBIT(n) (0x0f ^ PBIT(n)) // Clear a single bit, but keep the rest set (low nibble)
#define SCAN(n) {PORTA |= NBIT(n); PORTA &= ~PBIT(n); DDRA &= ~NBIT(n); DDRA |= PBIT(n); }
#define NOSCAN { PORTA |= 0x0f; DDRA &= ~0x0f; }
#define NOSCAN { PORTA |= 0x0f; DDRA &= ~0x0f; } // Set all bits in lower nibble; clear all bit in lower nibble of DDR


int Kpd_Scan() {
unsigned char i, row;

Kpd_Reset(); // This fixes a bug where keys aren't read after the display is cleared
for (i=0; i<4; i++) {
SCAN(i);
row = ~((PINA & 0xf0) >> 4);
row = ~((PINA & 0xf0) >> 4); // Read key bits from upper nibble and shift to lower nibble, then invert
if (row & 0x01) return i << 2;
if (row & 0x02) return (i << 2) + 1;
if (row & 0x04) return (i << 2) + 2;
Expand All @@ -42,7 +39,6 @@ int Kpd_Scan() {
#undef NOSCAN



int Kpd_GetKeyAsync() {
static int keylast = -1;

Expand Down
Loading