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.

Help me write a PIC code in C for serial data transmission

Status
Not open for further replies.

miryazdan

Newbie level 3
Newbie level 3
Joined
Mar 9, 2007
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,310
hello

plz help me in writing the PIC code in C for transmittimg serial data......
 

Re: PIC serial data

You ca Unse printf in ccs compiler. for examples look for this ebook: programming in c pic microcontroller by nigel gadner
 
Re: PIC serial data

If you are using CCS:

Code:
/////////////////////////////////////////////////////////////////////////
////                          EX_ENCRY.C                             ////
////                                                                 ////
////  This program shows how to implement two serial ports to        ////
////  transfer data between the ports and to encrypt/decrypt         ////
////  the data on one side for a secure communications link.         ////
////                                                                 ////
////  +------+    +-----+             +-----+    +------+            ////
////  |  PC  |    | PIC |             | PIC |    |  PC  |            ////
////  |  A   |====|  A  |=============|  B  |====|  B   |            ////
////  +------+    +-----+    secure   +-----+    +------+            ////
////                          wire                                   ////
////                                                                 ////
////  Configure the CCS prototype card as described below.           ////
////                                                                 ////
////  This example will work with the AFL. The                       ////
////  following conditional compilation lines are used to include a  ////
////  valid device for each compiler.  Change the device, clock and  ////
////  RS232 pins for your hardware if needed.                        ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services         ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other  ////
//// licensed users of the CCS C compiler.  No other use,            ////
//// reproduction or distribution is permitted without written       ////
//// permission.  Derivative programs created using this software    ////
//// in object code form are not restricted in any way.              ////
/////////////////////////////////////////////////////////////////////////


#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif

#define BUFFER_SIZE 32

///////////////////////////////////////////////////////////////// PORT 1
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

byte buffer1[BUFFER_SIZE];
byte next_in1 = 0;
byte next_out1 = 0;

#int_rda
void serial_isr1() {
   int t;

   buffer1[next_in1]=getc();
   t=next_in1;
   next_in1=(next_in1+1) % BUFFER_SIZE;
   if(next_in1==next_out1)
     next_in1=t;           // Buffer full !!
}

#define bkbhit1 (next_in1!=next_out1)

byte bgetc1() {
   byte c;

   while(!bkbhit1) ;
   c=buffer1[next_out1];
   next_out1=(next_out1+1) % BUFFER_SIZE;
   return(c);
}

void putc1(char c) {
   putc(c);
}


///////////////////////////////////////////////////////////////// PORT 2
#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0)

byte buffer2[BUFFER_SIZE];
byte next_in2 = 0;
byte next_out2 = 0;

#int_ext
void serial_isr2() {
   int t;

   buffer2[next_in2]=getc();
   t=next_in2;
   next_in2=(next_in2+1) % BUFFER_SIZE;
   if(next_in2==next_out2)
     next_in2=t;           // Buffer full !!
}

#define bkbhit2 (next_in2!=next_out2)

byte bgetc2() {
   byte c;

   while(!bkbhit2) ;
   c=buffer2[next_out2];
   next_out2=(next_out2+1) % BUFFER_SIZE;
   return(c);
}

void putc2(char c) {
   putc(c);
}

///////////////////////////////////////////////////////////////////////////////////////

void main() {
   char c;

   enable_interrupts(global);
   enable_interrupts(int_rda);
   enable_interrupts(int_ext);

   printf(putc2,"\r\n\Running...\r\n");


   do {
      if(bkbhit1) {
		   c = bgetc1();
			putc2(c);
		}
      if(bkbhit2) {
		   c = bgetc2();
			putc1(c);
		}
   } while (TRUE);
}

This is an example from CCS. Check EX_ENCRY.C in "Example" folder.

Regards,

Ric
 

Re: PIC serial data

But how can I use both serial at a time,Means ,if I am using printf command where should the data will go? I mean to which Serial?
 

Re: PIC serial data

So where you want to transmit the data serially?
 

Re: PIC serial data

I am using an X bee module which is having a serial communication and the same controller is connected to the PC through Serial port so that I can log the data from X bee module to PC and vice versa?
 

Re: PIC serial data

you want to send data to xbee and PC both.I dont think so that same serial port of controller is use for both operations.you have to use either pc or xbee.
if you have one xbee connected with pc and microcontroller than where the 2nd xbee is? where are you sending data through xbee?

find the attached file are you looking for this scenario?
 

Attachments

  • Interface_ckt.bmp
    24.6 KB · Views: 148

Re: PIC serial data

Sorry , I will make it clear. My project is a Home Automation/Security System. My sensors are interfaced with a micro controller and connected to Xbee module so that if any of the sensors is active the module will send the data to the main module via xbee. The main module is connected to a Server system which logs these reports. So I want two serial connections one is for Xbee module and the for PC
 

Re: PIC serial data

No problem, you just need two UARTs.
You have not told us which PIC you use. Some have two USARTS in silicon, some have one. The types with a single USART can still be used, you can either connect an external UART IC and interface to it or you may be able to use a normal digital IO pin and implement a software UART. The software method is sometimes called "bit banged serial".

Brian.
 

Re: PIC serial data

i think best is to use software uart.
 

Re: PIC serial data

I am Using PIC 16F877A which is having only one UART. So I am planning to use software UART.There arises my first question. I am using CCS compiler so we can use # use RS 232 to specify any other pins as TX and RX then how will command Printf works? to Hardware UART or Software UART?

---------- Post added at 05:19 ---------- Previous post was at 05:18 ----------

Thanks for quick responses in this form
 
Re: PIC serial data

@ ratheeshbr
I have never used software uart but i read the manual for printing after your post and found that first you initalize that
Code:
#use rs232(baud=9600, xmit = PIN_C6 , rcv = PIN_C7 , stream = CH1) 
#use rs232(baud=9600, xmit = your assign pin , rcv = your assign pin , stream = CH2)

then when you are printing use fprintf instead of printf
fprintf (stream, cstring, values...)

in the stream here you would define CH1 or CH2 and then cstring and values

Hope this helps you,Let me know if it works it will also an exprience for me

Best Regards.
 

Re: PIC serial data

Thanks for the reply. I think I got the correct solution for my problem. I will reply after testing this. I am waiting for the hardware now.
Thanks
 

Re: PIC serial data

What type of hardware?
 

Re: PIC serial data

I mean Xbee module.
Will reply about the program soon.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top