Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C code of bitstream input in PIC

Status
Not open for further replies.

ceibawx

Junior Member level 2
Joined
Oct 13, 2008
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
chongqing,China
Activity points
1,606
c code for bitstream

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);
}
}
}
 

code input pic24

In C, the index of array usually starts from zero.
Please, firstly correct all these bugs:

for (i=0; i<20;i++)
 

bitstream data type in c

Thanks a lot.
I have modified it.Can you see this for me?

I have seen output. but it is not right.
Thanks.


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

#define GP0 PIN_C0 // CLOCK 64KHZ
#define GP1 PIN_C1 //DATA 64KBAUD
#define GP2 PIN_C2 //DATA OUTPUT
#define GP3 PIN_C6 //UART OUTPUT

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

void main()
{
int eeg[80],b[];
int i,j;

while(TRUE)
{
for (i=0;i<80;i++)
{
while (input(GP0)==0); //{}
eeg=input(GP1);
// output_bit(GP2,eeg);
while (input(GP0)==1); // {}
}
for (i=0;i<10;i++)
{
for (j=0;j<8;j++)
{
b=0;
b+=eeg[(8*i)+j]*pow(2,j);

}
putc(b);
delay_us(8);
}

}

}
 

shift_left function

int eeg[80],b[]; <--- You didn't allocate memory for b[]

According to you code, it should be array with at least 10 elements:

Code:
  int eeg[80],  b[10];
[/b]
 

encefalograma 16f877

I can understand some.
Yesterday a labmate debugged it, finally he got the result.
He used a int b. and it does work.
the error is position of "b=0". it should be outside j loop, and inside i loop.
And it does work.
But now the question is that output sometime is 01010101, sometimes it is 10101010. I think I need find the error to modify it.


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

#define GP0 PIN_C0 // CLOCK 64KHZ
#define GP1 PIN_C1 //DATA 64KBAUD
#define GP2 PIN_C2 //DATA OUTPUT
#define GP3 PIN_C6 //UART OUTPUT

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

void main()
{
int eeg[80],b;
int i,j,k;

while(TRUE)
{
for (i=0;i<80;i++)
{
eeg=0;
while (input(GP0)==0); //{}
//eeg=input(GP1);

if(input(GP1))
eeg=1;
//output_bit(GP2,eeg);
while (input(GP0)==1); // {}
}


//output_bit(GP2,1); // first time it is needed. then output exists, we can delete it.
//output_bit(GP2,0); // first time it is needed. then output exists, we can delete it.

/* for(i=0;i<80;i++) //80 loop costs 888us.
{ //20 loop costs 222s.
// output_bit(GP2,eeg);
putc(eeg);
delay_us(100);
}
*/
//output_bit(GP2,1); // first time it is needed. then output exists, we can delete it.
//output_bit(GP2,0); // first time it is needed. then output exists, we can delete it.

for (k=0;k<10;k++)
{
b=0;
for (j=0;j<8;j++)
{
b+=eeg[(8*k)+j]*pow(2,j);
//b+=eeg[(8*k)+j];

}
putc(b);
output_toggle(PIN_C4);
delay_us(100);
output_toggle(PIN_C4);
}

}

}







B_A_D said:
int eeg[80],b[]; <--- You didn't allocate memory for b[]

According to you code, it should be array with at least 10 elements:

Code:
  int eeg[80],  b[10];
[/b]
 

shift_left pic16f8

Look, int CCS int-type by defaut is same as int8.
Int this loop

Code:
b=0; 
for (j=0;j<8;j++) 
{ 
  b+=eeg[(8*k)+j]*pow(2,j); 
}

variable b can be overflowed, because sum of this expression
Code:
  b+=eeg[(8*k)+j]*pow(2,j);
can be greater than 256. So , you need to define b as int16.
-------------------------------------------
The better way to recieve 80-bits data stream is following
Code:
int8 in_bits[10]; //only 10 bytes in memory, one bit per bit

//recieve 80 bits stream
for (i=0;i<80;i++) 
{ 
while (input(GP0)==0); //{} 
shift_left(in_bits, 10,  input(GP1)); //see help for shift_left function
while (input(GP0)==1); // {} 
} 

//then send it
for (i=0;i<10;i++) 
{ 
   putc(in_bits[i]); 
   output_toggle(PIN_C4); 
   delay_us(100); 
   output_toggle(PIN_C4); 
}
 

pic shift_left

I will tell you result when I go to lab.
Where are you? In Europe? I guess. And I am in US,east area.
Thanks for your care.





B_A_D said:
Look, int CCS int-type by defaut is same as int8.
Int this loop

Code:
b=0; 
for (j=0;j<8;j++) 
{ 
  b+=eeg[(8*k)+j]*pow(2,j); 
}

variable b can be overflowed, because sum of this expression
Code:
  b+=eeg[(8*k)+j]*pow(2,j);
can be greater than 256. So , you need to define b as int16.
-------------------------------------------
The better way to recieve 80-bits data stream is following
Code:
int8 in_bits[10]; //only 10 bytes in memory, one bit per bit

//recieve 80 bits stream
for (i=0;i<80;i++) 
{ 
while (input(GP0)==0); //{} 
shift_left(in_bits, 10,  input(GP1)); //see help for shift_left function
while (input(GP0)==1); // {} 
} 

//then send it
for (i=0;i<10;i++) 
{ 
   putc(in_bits[i]); 
   output_toggle(PIN_C4); 
   delay_us(100); 
   output_toggle(PIN_C4); 
}
 

c code for reading input on a pic

:D
I got it.
You are excellent. Why you know so much?
You are pretty familiar with PIC.
I bow to you.
I really appreciate your teaching.

Added after 21 minutes:

Another question.
In project 256kbps input bitstream is needed. However Now input speed is 120kbps.

How can I do it ?
*change baud rate setting for asynchronous in PIC?
Now default baud as 312.5k is used. Shall we change it into high speed. set BRGH=1, then baud rate is 1250k under 20MHz clock.
*Change PIC? Now it is pic16f877. Now I used its default baud,it should be 312.5k under 20MHz clock.
*Use assembly language ?
*Other ways?

Which is helpful?


Thanks.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top