Hi all, I'm doing a project where I'm reading a serial stream that's proprietary.
The data format is [16 bit header] [4 bits of data] [word0] [word2] ... [word12]
where each word is [start bit] [8 data bits] [2 stop bits]
First question:
I can't directly use the standard serial reader since the data doesn't really fit into a neat 8 bit format, or even consistent stop/start bits. One option is to read in the stream as if it were just 8 bit characters and then parse my data out of it in chunks, but not sure if it's worth the effort. Also, it's a non-standard data rate.. somewhere around 100baud. Will the hardware and/or serial port work reliably with a non-standard baud rate?
Second question:
I don't know the exact baud rate. My initial effort was to oversample the signal and just count the shortest time between a RE and FE. This MOSTLY worked and I was able to pull some data out, but there were random bits thrown in here and there. This means either a) the arduino wasn't nuts on with its 1kHz timing or b) the signal isn't exactly 100 baud. I attached the sketch at the bottom, but I basically just used timer1 to setup my 1kHz signal. Just for future reference, what's the accuracy of reading like this, roughly? Can I safely assume that it's actually reading at 1kHz for purposes like this?
Third question:
I just found the pulseIn() function... This would probably be a much better way to measure the signal for its baud rate, yes? Seems like there would be less phase problems.
If I end up doing my own "serial" reading, does this algorithm make sense?
- I know each packet starts with 16 bits of "1"'s. I would look for this and then the first falling edge (it's the same place every packet) and set the timer to take the first sample after 1/2 the frequency (and at the actual frequency for the rest of the packet)
It seems like if I didn't have something like this, the signal might drift over time..
here's the code i used for the 1kHz sampling.
The post kinda got kinda long, thanks for reading guys, much appreciated! =)
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
ByteBuffer buffer;
byte sensorValue = 0; // value read from the pot
void setup() {
//Setup our buffer
buffer.init(256);
// TIMER SETUP- the timer interrupt allows preceise timed measurements of the reed switch
//for mor info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
cli();//stop interrupts
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0;
// set timer count for 1khz increments
OCR1A = 1999;// = (16*10^6) / (1000*8) - 1
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
// initialize serial communications at 9600 bps:
Serial.begin(115200);
}
ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
// 8 butes is good enough for us
sensorValue = analogRead(analogInPin) >> 2; //get val of A0
buffer.put(sensorValue);
}
void loop() {
//Our buffer should be getting filled in the background
while (buffer.getSize() > 0)
Serial.write(buffer.get());
}
