Using Arduino as UART?

I am pretty new to all this stuff and don’t really get it, there is an SPI library, and this device I have called a uLog(http://www.sparkfun.com/products/9228) with an ATTiny24 has “Access to ATtiny24 SPI pins”( the pins on the breakoutboard are labeled MOSI, MISO, SCL, RST, 3.3V and GND). It says it can be reprogrammed, but these pins are also for connecting to UART. My question is since there is a way to use the Arduino as an ISP, can you also use it as a UART? I wrote a sketch that is able to recieve the first three chars that the uLog is supposed to print when connected to UART, but cannot get it to accept my input.

void setup() {
  pinMode(13, OUTPUT);
  pinMode(7, INPUT);
  pinMode(8, OUTPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  
  int data[100];
  
  Serial.begin(57600);
  Serial.println("Connected...Hello");
  
  digitalWrite(8, LOW);
  digitalWrite(13, LOW);
  
  while(!digitalRead(7));
  Serial.println("Starting...");

  digitalWrite(8, HIGH);
  digitalWrite(13, HIGH);

  while(!digitalRead(10));
  delayMicroseconds(5);
  int ch1 = readCh();
  int ch2 = readCh();
  int ch3 = readCh();
  Serial.println(ch1);
  Serial.println(ch2);
  Serial.println(char(ch3));
  
  //Button attached to pin 7
  while(!digitalRead(7));
 
  
  writeCh('r');
  for(int i = 0; i <100; i++) {
    data[i] = readCh();
  }
  for(int i = 0; i <100; i++) {
    Serial.println(data[i], BIN);
  }
  Serial.println();
  Serial.println("Done");

}

int readCh() {
  while(digitalRead(10))
    if(digitalRead(7)) break;
  delayMicroseconds(34);
  int ch = 0b00000000;
  int i = 0;
  for(i = 0; i<8;i++) {
    bitWrite(ch, i, digitalRead(10));
    delayMicroseconds(20);
  }
  return ch;
}

void writeCh(char ch) {
  Serial.print("Writing ch: ");
  Serial.print(ch);
  Serial.print(", ");
  Serial.println(ch, BIN);

  digitalWrite(8, LOW);
  delayMicroseconds(21);
  
  for (int k = 0; k < 8; k++)//8 data bits
  {
    if (bitRead(ch,k)) digitalWrite(8, HIGH);
    else digitalWrite(8, LOW);
    delayMicroseconds(21);
  }
  digitalWrite(8, HIGH);
  delayMicroseconds(21);
}

void loop() {
}

here is the relevant code on the ATTiny24:

//From main(void)
	//check for UART connection
	PORTA &= 0xDF;//RX pin low to discharge a floating pin
	PORTA |= 0x40;//TX pin hi to start communication
	DDRA |= 0x20;
	DDRA &= 0xDF;
	
	delay_us(5);
	
	a = PINA;
	a &= 0x20;
	
	//If there's a high-ish voltage on the MISO line,
	//we're likely connected to a serial link
	if (a & 0x20) menu();


void menu(void)
{

	char x, z;
	short y;
	
	while(1)
	{
		putchr(10);
		putchr(13);
		putchr('?');
		
		x = getc();
		putchr(x);
		
		if (x == 'e') erase();
		
		else if (x == 'r')
		{
			for (y = 1; y < 4096; y++) 
			{
				x = read_flash(y);
				
				if (x == 1) break;
			
			}
			
			putchr(10);
			putchr(13);
			
		}
		
	}
	
}

char getc(void)
{
	char k, l = 0, m = 0;
	
	k = 0x20;
	while(k & 0x20)//0010 0000 
	{
		k = PINA;
		k &= 0x20;
	}

	delay_us(36);
	
	for (k = 0; k < 8; k++)
	{
		l >>= 1;
		
		m = PINA;
		if (m & 0x20) l |= 0b10000000;
		else l &= 0b01111111;

		delay_us(24);
	}
	
	return l;
}




void putchr(char x)
{
	char k;
	
	PORTA &= 0b10111111;
	delay_us(24);//bit width at 38400
	
	for (k = 0; k < 8; k++)//8 data bits
	{
		if (x & 0b00000001) PORTA |= 0b01000000;
		else PORTA &= 0b10111111;
		x >>= 1;

		delay_us(24);
	}
	
	PORTA |= 0b01000000;//set it high again

	delay_us(24);
	
}

When I run it is spits out
10
13(sometimes like 168 or something)
?

But then when i try to put in a char to read the data I get only like 3 values and then 255 for the rest. I have had this logger on without being connected and it samples at 50hz so there should be a lot more values before they are all 255. Is there a way to do this with SPI.transfer instead of trying to write my own timing stuff?

...I wrote a sketch...

Which you ran on what kind of board?

Arduino Uno R2

Arduino IDE version? 1.0? 0023?

Baud rate mismatches can give wild and crazy data on the recieve end.

I have Arduino 1.0

Is it the same problem that you need to use 0022 or something like when using ArduinoISP?

Of course mismatched baud rates would cause problems, but I set my receiving delays based on the delays in the transmitting code. I tried adjusting the times to make up delays in other functions I call, but still to no avail.

I suggest using SoftwareSerial. It's a library that comes with Arduino 1.0.

int readCh() {

while(digitalRead(10))
   if(digitalRead(7)) break;
 delayMicroseconds(34);
 int ch = 0b00000000;
 int i = 0;
 for(i = 0; i<8;i++) {
   bitWrite(ch, i, digitalRead(10));
   delayMicroseconds(20);
 }
 return ch;
}

Well, I’m confused. What is all that doing exactly? And what have you got connected to what?

I wrote a sketch that is able to recieve the first three chars that the uLog is supposed to print when connected to UART, but cannot get it to accept my input.

What input? I am having trouble visualising what is happening here.

Right, so I have pin 13 going to a transistor that keeps the ulog from getting power till 13 goes high.

Pin 7 is connected to a button that I am using to help make sure timing isn't an issue

pin 8 goes to a resistor voltage divider since output is 5v and I need 3.3v It is then connected to the MISO on the uLog

Pin 10 is connected to MOSI of the uLog

Pin 9 is connected to SCL(I think it is the SCK but I haven't even used it really...)

So 10 is usually high until sending data, so while reading a char firstly I wait until it is pulled low to signal data is coming, while waiting if the button connected to pin 7 is pushed we'll just continue(for debugging purposes in case it got stuck)

Then the code on the chip says it waits and then I initiate a char, and set each bit based on what 10 is doing... The delays are based off of the code from the chip

From the code it also says the first three chars it will print are:

putchr(10);
putchr(13);
putchr('?');

I recieve these upon pushing the button attached to 7, so I am recieving correctly

OK, so why aren't you using the SPI library?

Might you provide an example of how that should look? How do I receive with transfer? When I use
SPI.transfer(0x00); I just get a zero…I can’t get it to work at all.

SPI.transfer(0x00); I just get a zero...I can't get it to work at all.

0x00 is zero, SPI.transfer sends a byte not a formatted string.

Zula:
Might you provide an example of how that should look? How do I receive with transfer?

byte a = SPI.transfer (b);

That sends b and receives a.

Right, but what should b...be? Does that matter? I know that 0x00 is 0, In the SPI examples I have looked at it sends a 0 to get data...

void setup() {
Serial.begin(9600);

// start the SPI library:
SPI.begin();
Serial.print(SPI.transfer(0x00));

}

That should print the first number sent by the chip no?

Zula:
I am pretty new to all this stuff and don't really get it, there is an SPI library, and this device I have called a uLog(uLog - The Lil'est Logger - WIG-09228 - SparkFun Electronics) with an ATTiny24 has "Access to ATtiny24 SPI pins"( the pins on the breakoutboard are labeled MOSI, MISO, SCL, RST, 3.3V and GND). It says it can be reprogrammed, but these pins are also for connecting to UART.

... Is there a way to do this with SPI.transfer instead of trying to write my own timing stuff?

I've re-read your first post. This device doesn't seem to use SPI, so I'm not quite sure what you are asking here.

Hmm...well, Overall I am asking if there is a way I can communicate with this device using my Arduino Uno R2, I have IDE 1.0 and 0022. The website(www.sparkfun.com/products/9228) says "Access to ATtiny24 SPI pins" So I thought maybe it was using the SPI interface, as I said I don't really know about these acronyms and such. I included what looked like the relevant parts of the code(from the website) that did the communication, and that's why I first just tried to match my timing with that in their program but that received the data 85% of the time and didn't successfully send any. I am thinking now though that since the first time I juts hooked the rx pin on the uLog(3.3v) directly to the digital pin(5v) the first time I probably messed up that pin and need a new one and get an ftdi(I think) this time.

The website (datasheet) says “Access to ATtiny24 SPI pins”

I presume they say that so you could reprogram the Attiny if you wanted to.

I gather you connect up something to it at 38400 baud and if you type “r” it will send the data it has collected. You could connect its serial pin to the D2 pin of your Arduino and make a simple “read and re-send program”.

Something like this:

#include <NewSoftSerial.h>

NewSoftSerial uLog (2, 3);  // Rx, Tx

void setup ()
{
  Serial.begin (38400);
  uLog.begin (34800);
  uLog.print ('r');  // tell it to read
}

void loop ()
  {
   // copy from logger to monitor
   if (uLog.available ())
     Serial.write (uLog.read ());
  }

Sorry to interrupt here. I check the device schematic. This device use 3.3 V. I saw a regulator ? , maybe input +5 V in but the output of the data pins ( RX, TX , ect ) are output & input for a 3.3 V signal. So to use them Directely to / from a 5 V signal is ...bad idea in my opinion. It need a proper level translator for the input / output of these signals. When you got the proper signal translation, than you can access this device properly. I don't what the code is to access it...yet.

Just my observation.

Zula, the SPI pins on the Tiny are hardware supported to work with Serial Bus Types (SPI, I2C) and you can put the Tiny and the Duino on the SPI bus with other SPI bus devices... that's what -Bus- is about. You can hook lots of things up on a bus. But the pins don't -have- to be used for that. They can be used for other I/O like simple RS-232 that uses TX (transmit), RX (receive), and ground or even 2-wire (there's libraries) or other method. You got options but with so few pins as a Tiny has got you don't have many options at the same time.