SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By ceibawx
#58036
I need all of your help to write a code to make PIC communicate with bitstream data.
bitstream data at 256kbaud is input in PIC, then it is stored in PIC memory, finally the data is transmit in another port of pic. And among them, a 512khz clock is used to record the bitstream.
But now the code doesn't work. So can you help me to find the error?
Any information is appreciated.


-----------------
#include <16F877.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, stream=TXBEE)


void main()
{
int eeg[20];
int i;



while(TRUE)
{
for (i=1;i<21;i++)
{
while (input(PIN_C0)==0) {}
eeg=input(PIN_C1);
while (input(PIN_C0)==1) {}


}

for (i=1;i<21;i++)
{
output_bit(PIN_C2,eeg);
}
}
}
By jasonharper
#58047
You're running off the end of your array: int eeg[20] has valid indexes from 0 to 19, not 1 to 20 as you're using.

256 kbps, with apparently a 20 MHz clock, means that you only have 19 instruction cycles to process each bit - it's entirely possible that your compiler is producing code that's not fast enough. You'd need to examine the compiler output, or run the code in a simulator, to find out just how quickly that for loop can run. It may help to unroll the loop:

while (input(PIN_C0)==0) {}
eeg[0]=input(PIN_C1);
while (input(PIN_C0)==1) {}
while (input(PIN_C0)==0) {}
eeg[1]=input(PIN_C1);
while (input(PIN_C0)==1) {}
... and so on, for 18 more repetitions.