I have designed a custom board using the Atmega328 from Sparkfun. It "talks to other hardware using a latched parallel port or register. All seems well except for bit0 and bit1 appear stuck. I realize they are RX and TX on an Arduino, but I know they can be used as digital I/O also.
Some googling and forum searching suggested the command UCSR0B = 0; Is this the proper command to allow use of pin 0 and 1 on a 328.
It doesn't appear to work.
/*
Name: inttest.ino
Created: 8/17/2016 9:20:54 AM
Author: 121776
*/
// Choose a valid PinChangeInterrupt pin of your Arduino board
//this works on the arduino board - not on the pcb - int to fast
// TEST WITH ADDED LATCH 74LS74 - WORKED 8/12
// added alt serial to debug read reg 8/15 tx now on pin 9
/* this routine is meant to verify recent addition of u18 latch. This latch catches a writeto Base + 0 or base +2
and is reset by a read of the same port.
/*this worked on 8/22 with the exception of a stuck bit
TX is on pin 15 for this code.
*/
#include <AltSoftSerial.h>
#include <SPI.h>
#include <AH_MCP320x.h>
#include "PinChangeInterrupt.h"
// AltSoftSerial always uses these pins:
// Arduino Uno tx 9 rx 8
// #define PC3 A3
#define busWrtADC A5
#define busWrtDAC A4
#define MISO 12 //pin 18
#define MOSI 11 // pin 17
# define SCK 13 //pin 19
# define dac_sel 8 // pin 14
// # define adc0_select 9 // pin 15
# define adc1_select 10 // pin 16
//# define led 13 //Uno not std board
//# define pc0 A0 //REG SELECT PINS
//# define pc1 A1
//# define pc2 A2
//# define LATCH A3 //REG strobe
const byte DAC_REF_INT = 8; //dac definitions
const byte DAC_UPDATE = 3;
const byte FILLER = 0;
const byte DACLO = 0; //PC0, PC1, PC2 read
const byte DACHI = 1; //PC0, PC1, PC2 read
const byte Enable_INT_REF = 1; // for enable
const byte ADCMD = 2;
const byte DESEL = B0111;
const byte SEL_EN = B0000;
const byte OBF = 1; // output buffer full status
const byte adcmdreg = 0; //PC0, PC1, PC2 read
const byte adchireg = 1; //PC0, PC1, PC2 write
const byte adcloreg = 0; //PC0, PC1, PC2 write
const byte adc_status_reg = 5; //PC0, PC1, PC2 write
const byte REG_SELECT = 3;
//SoftwareSerial mySerial = SoftwareSerial(rx, tx);
volatile char adc_cmd_reg_data = 0; //used in int
volatile int dac_low_reg_data = 0;
volatile int dac_hi_reg_data = 0;
volatile byte adc_status = 0;
volatile byte dac_status = 0;
int previous = 0;
AH_MCP320x ADC_SPI(10);
AltSoftSerial altSerial;
/*************************************** prototypes *****************************************************/
byte micro_rd_data_reg(byte reg);
void micro_wrt_data_reg(byte reg, byte wrt_value);
/*************************************** setup *****************************************************/
void setup() {
UCSR0B = 0; //allow use of pins 0, 1
altSerial.begin(9600);
pinMode(busWrtADC,INPUT);
pinMode(busWrtDAC,INPUT);
/*in,in,out.out,out,out select no ports U6 (7 n/c
enable hardware selection U6
power up set to enable select addr 7 - 0-6 high */
DDRC = B001111;
PORTC = DESEL;
DDRD = B00000000;
// Attach the new PinChangeInterrupt and enable event function below
attachPCINT(digitalPinToPCINT(busWrtADC), adc, RISING);
attachPCINT(digitalPinToPCINT(busWrtDAC), dac, RISING);
altSerial.println("Analog board online - inttest.ino 8/18");
}
void loop() {
if (previous != dac_low_reg_data){
altSerial.println(dac_low_reg_data,HEX);
previous = dac_low_reg_data;
}
/*
altSerial.print("dac ");
altSerial.print(dac_status);
altSerial.print(" data ");
altSerial.print(dac_low_reg_data);
altSerial.println("");
*/
}
int micro_rd_data_reg(int reg) //reads a byte from register specified
{
char rd_val = 0;
DDRD = B00000000; //set port D 1 to 8 as input
PORTC = DDRC | reg | SEL_EN ; //SELECT PORT
rd_val = PIND; // input data
PORTC = DDRC | DESEL; //de SELECT PORT
return rd_val;
}
void micro_wrt_data_reg(int reg,int value) //write a byte to register specified
{
byte wrt_val;
DDRD = B11111111; //set port D 1 to 8 as output
PORTC = DDRC | reg | SEL_EN; //SELECT PORT
PORTD = wrt_val;
PORTC = DDRC | DESEL;
}
//*********************** interrupt routine*****************************************
void adc(void) { //PIN 26
DDRD = B00000000; //set port D 1 to 8 as input
PORTC = DDRC | ADCMD ;
adc_cmd_reg_data = PIND; // input data
PORTC = DDRC | DESEL;
++adc_status;
}
void dac(void) //PIN 27
{
/* this routine is entered when a bus read to Base +0 occurs
it updates - global var dac_hi_reg_data and dac_low_reg_data*/
DDRD = B00000000; //set port D 1 to 8 as input
//PORTC = DACHI;;
//dac_hi_reg_data = PIND; // input data
PORTC = DACLO; // select port
delayMicroseconds(1000);
dac_low_reg_data = PIND; // input data
delayMicroseconds(1000);
PORTC = DESEL; //deselct port
++dac_status; // debug
}