using standard Arduino fcns for bit banging

This commit is contained in:
morgan 2016-07-09 20:29:01 -07:00
parent 705884497a
commit 6a889580f2
9 changed files with 40 additions and 89 deletions

View File

@ -122,10 +122,12 @@ HamShield::HamShield() {
devAddr = A1; // devAddr is the chip select pin used by the HamShield devAddr = A1; // devAddr is the chip select pin used by the HamShield
sHamShield = this; sHamShield = this;
pinMode(A1, OUTPUT); pinMode(devAddr, OUTPUT);
digitalWrite(A1, HIGH); digitalWrite(devAddr, HIGH);
pinMode(A4, OUTPUT); pinMode(CLK, OUTPUT);
pinMode(A5, OUTPUT); digitalWrite(CLK, HIGH);
pinMode(DAT, OUTPUT);
digitalWrite(DAT, HIGH);
} }
/** Specific address constructor. /** Specific address constructor.
@ -137,10 +139,12 @@ HamShield::HamShield() {
HamShield::HamShield(uint8_t cs_pin) { HamShield::HamShield(uint8_t cs_pin) {
devAddr = cs_pin; devAddr = cs_pin;
pinMode(A1, OUTPUT); pinMode(devAddr, OUTPUT);
digitalWrite(A1, HIGH); digitalWrite(devAddr, HIGH);
pinMode(A4, OUTPUT); pinMode(CLK, OUTPUT);
pinMode(A5, OUTPUT); digitalWrite(CLK, HIGH);
pinMode(DAT, OUTPUT);
digitalWrite(DAT, HIGH);
} }
/** Power on and prepare for general usage. /** Power on and prepare for general usage.

View File

@ -9,9 +9,9 @@
#define _HAMSHIELD_H_ #define _HAMSHIELD_H_
#include "HamShield_comms.h" #include "HamShield_comms.h"
#include "SimpleFIFO.h" //#include "SimpleFIFO.h"
#include "AFSK.h" //#include "AFSK.h"
#include "DDS.h" //#include "DDS.h"
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
// HamShield constants // HamShield constants
@ -491,15 +491,17 @@ class HamShield {
void toneWait(uint16_t freq, long timer); void toneWait(uint16_t freq, long timer);
void toneWaitU(uint16_t freq, long timer); void toneWaitU(uint16_t freq, long timer);
bool parityCalc(int code); bool parityCalc(int code);
// void AFSKOut(char buffer[80]);
//TODO: split AFSK out so it can be left out
// AFSK routines // AFSK routines
bool AFSKStart(); //bool AFSKStart();
bool AFSKEnabled() { return afsk.enabled(); } //bool AFSKEnabled() { return afsk.enabled(); }
bool AFSKStop(); //bool AFSKStop();
bool AFSKOut(const char *); //bool AFSKOut(const char *);
class AFSK afsk; //class AFSK afsk;
private: private:
uint8_t devAddr; uint8_t devAddr;

View File

@ -33,54 +33,27 @@ int8_t HSreadWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data)
uint16_t temp_dat; uint16_t temp_dat;
// bitbang for great justice! // bitbang for great justice!
*data = 0; *data = 0;
cli(); pinMode(DAT, OUTPUT);
DDRC |= ((1<<5) | (1<<4)); // set direction to output
sei();
regAddr = regAddr | (1 << 7); regAddr = regAddr | (1 << 7);
//cli();
digitalWrite(devAddr, 0); //PORTC &= ~(1<<1); //devAddr used as chip select digitalWrite(devAddr, 0); //PORTC &= ~(1<<1); //devAddr used as chip select
//sei();
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
temp = ((regAddr & (0x80 >> i)) != 0); temp = ((regAddr & (0x80 >> i)) != 0);
cli(); digitalWrite(CLK, 0); //PORTC &= ~(1<<5); //
PORTC &= ~(1<<5); //digitalWrite(CLK, 0); digitalWrite(DAT, temp);
sei(); digitalWrite(CLK, 1); //PORTC |= (1<<5); //
//digitalWrite(DAT, regAddr & (0x80 >> i));
temp = (PORTC & ~(1<<4)) + (temp << 4);
cli();
PORTC = temp;
sei();
delayMicroseconds(9);
cli();
PORTC |= (1<<5); //digitalWrite(CLK, 1);
sei();
delayMicroseconds(9);
} }
// change direction of DAT // change direction of DAT
cli(); pinMode(DAT, INPUT); // DDRC &= ~(1<<4); //
DDRC &= ~(1<<4); //pinMode(DAT, INPUT);
sei();
for (int i = 15; i >= 0; i--) { for (int i = 15; i >= 0; i--) {
cli(); digitalWrite(CLK, 0); //PORTC &= ~(1<<5); //
PORTC &= ~(1<<5); //digitalWrite(CLK, 0); digitalWrite(CLK, 1); //PORTC |= (1<<5); //
sei(); temp_dat = digitalRead(DAT); //((PINC & (1<<4)) != 0);
delayMicroseconds(9);
cli();
PORTC |= (1<<5); //digitalWrite(CLK, 1);
sei();
cli();
temp_dat = ((PINC & (1<<4)) != 0);
sei();
temp_dat = temp_dat << i; temp_dat = temp_dat << i;
*data |= temp_dat; // digitalRead(DAT); *data |= temp_dat;
delayMicroseconds(9);
} }
digitalWrite(devAddr, 1); //PORTC |= (1<<1);// CS digitalWrite(devAddr, 1); //PORTC |= (1<<1);// CS
cli();
DDRC &= ~((1<<5) | (1<<4)); // set direction all input (for ADC)
sei();
return 1; return 1;
} }
@ -118,52 +91,24 @@ bool HSwriteWord(uint8_t devAddr, uint8_t regAddr, uint16_t data)
//digitalWrite(13, HIGH); //digitalWrite(13, HIGH);
// bitbang for great justice! // bitbang for great justice!
cli(); pinMode(DAT, OUTPUT);
DDRC |= ((1<<5) | (1<<4)); // set direction all output
//PORTC |= (1<<5) & (1<<4);
sei();
regAddr = regAddr & ~(1 << 7); regAddr = regAddr & ~(1 << 7);
//cli();
digitalWrite(devAddr, 0); // PORTC &= ~(1<<1); //CS digitalWrite(devAddr, 0); // PORTC &= ~(1<<1); //CS
//sei();
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
temp_reg = ((regAddr & (0x80 >> i)) != 0); temp_reg = ((regAddr & (0x80 >> i)) != 0);
cli(); digitalWrite(CLK, 0); //PORTC &= ~(1<<5); //
PORTC &= ~(1<<5); //digitalWrite(CLK, 0); digitalWrite(DAT, regAddr & (0x80 >> i));
sei(); digitalWrite(CLK, 1); // PORTC |= (1<<5); //
//digitalWrite(DAT, regAddr & (0x80 >> i));
temp_reg = (PORTC & ~(1<<4)) + (temp_reg << 4);
cli();
PORTC = temp_reg;
sei();
delayMicroseconds(8);
cli();
PORTC |= (1<<5); //digitalWrite(CLK, 1);
sei();
delayMicroseconds(10);
} }
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
temp_dat = ((data & (0x8000 >> i)) != 0); temp_dat = ((data & (0x8000 >> i)) != 0);
cli(); digitalWrite(CLK, 0); //PORTC &= ~(1<<5); //
PORTC &= ~(1<<5); //digitalWrite(CLK, 0); digitalWrite(DAT, temp_dat);
sei(); digitalWrite(CLK, 1); // PORTC |= (1<<5); //
//digitalWrite(DAT, data & (0x80000 >> i));
temp_reg = (PORTC & ~(1<<4)) + (temp_dat << 4);
cli();
PORTC = temp_reg;
sei();
delayMicroseconds(7);
cli();
PORTC |= (1<<5); //digitalWrite(CLK, 1);
sei();
delayMicroseconds(10);
} }
digitalWrite(devAddr, 1); //PORTC |= (1<<1); //CS digitalWrite(devAddr, 1); //PORTC |= (1<<1); //CS
cli();
DDRC &= ~((1<<5) | (1<<4)); // set direction to input for ADC
sei();
return true; return true;
} }