Ok so I have a screenkey, found the sparkfun code, modified it to run on the arduino. Heres the thing, I might of modified to make it work doesnt mean I understand a single part of it.
Im fumbling through here which Im ok with, I can figure most things out given enough time - but... not being able to read this AVR code doesnt help at all.
All I want to know is - where the hell is the part that talks about the button and its pin assignment? The original code say PB7 which according to the ardiuno/pin assignment is a crystal. Still dont get where that part is.
If someone could point out the bit of code relating to that pin assignment then I could figure out how it uses the PB1/dig pin9 and PB0/dig pin8. Or if there is a dummies dummy guide to AVR atleast Id take that
#include <avr/io.h>
#include <avr/interrupt.h>
#include "start.h"
#include "stop.h"
volatile uint8_t phase;
volatile uint16_t bits;
volatile uint8_t cnt;
volatile uint16_t fcnt=0;
// This file is designed to be used on an atmega 640, running @ 16mhz,
// but should be portable to other processors
// Pin assignments are:
// PB7 - Button input [button drives pin low] (somewhere?)
// PB1 - Data (digital pin 9)
// PB0 - Clock (digital pin 8)
ISR(TIMER1_OVF_vect){
fcnt++;
TCNT1 = 65536-80;
phase = ~phase;
PORTB = (PORTB & 0XFE) | (phase?1:0 );
if (phase)
{
if (!cnt)
{
PORTB |= 0x2;
return;
}
PORTB = (PORTB & 0xFD) | ((bits & 0x1) ? 2:0);
cnt --;
bits = bits >> 1;
}
}
/* Prepares a word to be sent to the lcd */
/* Parity is 1 for odd parity, 0 for even */
void screenkey_write(uint8_t data, uint8_t parity)
{
/* Block while theres still a word being sent */
while (cnt);
/* Calculate parity */
uint8_t pb = data ^ (data >> 1) ^ (data >> 2) ^ (data >> 3) ^
(data >> 4) ^ (data >> 5) ^ (data >> 6) ^ (data >> 7)
^ parity;
/* Setup the bits to send */
bits = 0x0C00 | (data << 1) | ((pb & 0x1) << 9);
/* queue it up */
cnt = 12;
}
/* Start / Stop characters */
void screenkey_start()
{
screenkey_write(0x00, 0);
}
void screenkey_stop()
{
screenkey_write(0xAA, 0);
}
// Write a 1 byte register
void screenkey_reg_1(uint8_t reg, uint8_t val)
{
screenkey_start();
screenkey_write(reg,1);
screenkey_write(val,1);
screenkey_stop();
}
// Write a 2 byte register
void screenkey_reg_2(uint8_t reg, uint8_t val1, uint8_t val2)
{
screenkey_start();
screenkey_write(reg,1);
screenkey_write(val1,1);
screenkey_write(val2,1);
screenkey_stop();
}
// Write a 108 byte image to the screen
// Data is a pointer to 108 bytes of image data to display
void screenkey_write_img(uint8_t * data)
{
uint8_t i;
screenkey_start();
screenkey_write(0x80,1);
for (i=0; i<108; i++)
screenkey_write(data[i], 1);
screenkey_stop();
}
// Bright and dark constants for each color
#define BR_GREEN 0x44
#define BR_RED 0x22
#define BR_BLUE 0x11
#define DK_GREEN 0x04
#define DK_RED 0x02
#define DX_BLUE 0x01
// Call with constants above, use | to make composite colors
// aka Blueish purple: BR_BLUE | DK_RED
void screenkey_set_color(uint8_t color)
{
screenkey_reg_1(0xED, color);
}
// Application specific
void show_start()
{
screenkey_set_color(BR_GREEN);
screenkey_write_img(start_img);
}
void show_stop()
{
screenkey_set_color(BR_RED);
screenkey_write_img(stop_img);
}
void setup()
{
enum {
stopped,
running,
valve,
x
} state;
uint8_t btn_db;
uint8_t br_dim;
cnt = 0;
DDRB = 0x3;
PORTB = 0x83;
TIMSK1=0x01;
TCCR1A = 0x00;
TCCR1B = 0x01;
TCCR1C = 0x0;
sei();
// Setup the control registers
screenkey_reg_1(0xEE, 0x00);
screenkey_reg_2(0xEF, 0x07, 0x00);
state = stopped;
show_start();
while (1)
{
/* Button Debounce */
uint8_t btn = PINB & 0x80;
if (!btn)
{
if (btn_db < 0x4)
btn_db ++;
if (btn_db == 0x4)
{
btn_db = 0xFF;
/* Button State */
if (state == running)
{
state = stopped;
show_start();
}
else if (state == stopped)
{
fcnt = 0;
state = running;
show_stop();
}
}
}
else
btn_db = 0;
/* Blink animation for stop display */
if (state==running)
{
if (fcnt & 0x8000)
if (!br_dim)
{
screenkey_set_color(DK_RED);
br_dim = 1;
}
if (! (fcnt & 0x8000))
if (br_dim)
{
screenkey_set_color(BR_RED);
br_dim = 0;
}
}
}
}
void loop()
{
}
Also yes, I feel very dumb for not being able to figure this out myself yet.