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.

[PIC] Pic to pic UART Communication Multiple Datas

Status
Not open for further replies.

Emin As

Newbie level 4
Joined
Dec 18, 2014
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
47
Hi i am newbie on programming and communications. I have two pics , one measure mutiple voltages and temprature. I want to send these voltage and temprature datas to second pic and displaying through lcd. Problem is how can i send multiple variables and how can receiver pic understand which data is voltage data which data is temprature data? I send one data and it works on proteus. But for multiple datas i don't know the codes that i should use.Could you give some code examples for this. I am using CCS C and pic16f877.

Thanks.
 

Have you decided the communication protocol between two pics which you are using? If not decide that first it can be anything and should be easy for communication. Name somthing like Master (The one which controls LCD and receives the data from PIC) and slave (The one which receives values of voltages and temperature)

Then about the various datas: That has to be decided by the code which you are writing, It can be something like send all datas together simultaneously by using any separators between the datas. After the reception need to separate it and display it using LCD.


Or else send one by one acknowledge it by master then after the acknowledgement send the another data. Display one by one immediately after the reception.
 
Hi,

we don´t know how you do this now..
Is it a single byte variable or multiplebyte variable, ist it binary encoded or ASCII or anything else.

***
all other than a single byte needs some protocol.
This can be additional sent characters or a timing.

***
Lets say A, B, C are individual bytes of one or more variables.

Sending A, A, A, A... is no problem

But for sending A, B, C you need some frame sync:
like: wait 100ms, then send A, B, C immediately after each other. (the "wait" acts as frame sync.)
or: send [FS], A, B, C, [FS], A, B, C,... here [FS] is one or more known byte(s) for the receiver to show that a new set of data is being sent.
best is, if the values for [FS] is not valid for the data A, B, C (like ASCII, where valid values are considered to be 0...127, so any value of 128...255 can be used as [FS].

Klaus
 
Hi Amin,

when you wants to communicate between two PIC ,you have to chose firstly which kind of Communication you need to send & receive that .So Firstly there are SPI,I2C,and UART communication available in PIC controller.So You can select .
if you select SPI based communication than its software based system which used master -slave communication.

This SPI is 4 pin communication ,-clock,Slave selection,data send & data received.

Interfacing of PIC to PIC by using this SPI pin.you can directly select your slave device to send/receive a data.

This Communication is based on software based .and clock based too.this data is controlled by master device.
 

Hi thanks for answers i think i understand the way to do this. I decided to use uart and i researched protocols before so i know the differences . my problem is on the coding side . I dont have experience . Which functions should i use for adding frame ? i can't find example for like this situations. Actualy i need example code for fully understand. Thanks for the answers . I am still searching example.
 

Hi,

my problem is on the coding side.
we still don´t know what data you want to transmit. So it is hard to help.

You said you currently transfer data...
If you can transfer one (variable) data, ...where is the problem to additionally transfer another (fixed) data as framesync.

Klaus
 

Data is float type variable that i get from adc channel. I send one variable in while loop and receiver pic get one variable so there is no problem . Here the example of what i did.
master pic
Code:
int x = 0;

void main()
{
while(1)
{
putc(x);
x++;
if(x=20) x=0;
delay_ms(1000);
}
}

slave, receiver pic

Code:
int x ;
void main()
{
lcd_init();

while(1)
{
x = getc();
printf(lcd_putc,"\f x = %d",a);
delay_ms(100);
;
}
}

it's like nothing i said i am newbie on programming,

thanks
 

Hi,

Please give the all your informations at once.

Data is float type variable
You want to transmit a single float variable.

How often?
What baud rate?

I gave you two methods. Is the timed method ok?
Do you need to transmit the float as 4 bytes binary, or is it ok to transmit as ASCII.

Klaus
 

Yes i am trying to send multiple float variable. For now i try to send just one float here is my code.
Code:
#include <16f877.h>
#include <stdlib.h>
#use delay(clock=4000000)
#use fast_io(b)
#define use_portb_lcd TRUE
#include <lcd.c>
#use rs232 (baud=2400, xmit=pin_c6, rcv=pin_c7, parity=N, stop=1)

char array [8];
float32 V1;
int32 x ;
#int_rda
void serial_interrupt()
   {
   disable_interrupts(int_rda);
   gets(array);
   x=atoi32(array);
   V1=(float)x/100;
   }
void main()
{
lcd_init();
enable_interrupts(GLOBAL);

   while(1)
   {
  
   printf(lcd_putc,"\f V1=%f" V1);
   delay_ms(1000);
   printf(lcd_putc,"\f V1=?");
   delay_ms(1000);
   enable_interrupts(int_rda);
   }
}

transmitter
Code:
#include <16f877.h>
#include <stdlib.h>
#use delay(clock=4000000)

#use rs232 (baud=2400, xmit=pin_C6 , rcv=pin_C7, parity=N, stop=1)
int32 y;
char dizi[8];
float V1=3.58;

void main()
{

while(1)
{
y=V1*100;
itoa(y,10,dizi);
puts(dizi);
}
}

The problem is lcd first shows V1=00.00 then V=? and stays at V=? . I try parts separately.There is no problem with char to int and calculations. I think Problem is on the interrupt . I used gets() function in while loop it takes value one time i saw V1=3.58 once then V1=00.00 and stays there again.

I use CCS C Compiler, i try other baud rates 9600 4800 1200 600 nothings change.

Thanks for answers.
 

Beyond identifying multiple data items in a serial stream, you have obviously problems with basic serial send and receive operations.

To start with a simple thing, your latest code example overruns the receiver by continuously sending data. There's no time for decoding and displaying the data which can't be performed simultaneously by your kind of programming. On the other hand, the receiver doesn't wait for a new data telegram, so it will never synchronize again to the received data after getting the first data by chance.

Although receiving while displaying data would be possible, it's much simpler for the time being to leave sufficient pauses between data packets, e.g. by a ms_delay() call in the receiver.

You are now sending <CR><LF> terminated data values, it would be simple to send a line of multiple data values, separated by a comma or space.

On the receiver side, you'll scan the received data for the separator character to isolate individual data items.

Personally I would fastly get rid of puts() and gets(), using printf() on the sender side and decode the received character the stream in your own code, with or without using interrupts.

Your receiver code doesn't use UARTs interrupts in a reasonable way, it could better poll the UART in a loop inside main().

A reasonable way would be to setup an interrupt driven receive FIFO which gives you the chance to receive new data while displaying the last one. CCS is shipped with UART FIFO example code. But for a first try, you can do without any UART interrupts.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top