UART function between PIC16F628A & XbeePro..

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

UART function between PIC16F628A & XbeePro..

Postby Myline » Tue Mar 13, 2012 11:39 am

I using C compiler tx,pin 8 and Rx,pin 7,
My target is enable UART between pic 16F628A and XbeePro
Rx,Tx.
Ur guys got any idea how to write C compiler
for UART between XbeePro,below is the sample function
of UART,but I don't how to modify into C compiler language.
Thank in advance...



void xbee_init(void)
{
uart_str("+++");//send command to enter XBee Pro command mode
delay_ms(200);// waiting for finish sending and XBee respond

uart_str("atmy1111");//send command to setting Source address
uart_send(0xD);// 0XD is hexa code of "Enter"key
delay_ms(200);

uart_str("atwr");// send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);

uart_str("atdl2222");// send command to setting Destination address
uart_send(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);

uart_str("atwr");//send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);

uart_str("atcn");// send command for Exit AT Command Mode
uart_send(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);

}
Myline
Newbie
 
Posts: 9
Joined: Sun Dec 18, 2011 10:49 am

Re: UART function between PIC16F628A & XbeePro..

Postby robosang » Wed Mar 14, 2012 8:59 am

Ei....... why got this code but no UART initialize code? Well, you need to study the datasheet a bit. Even if we point out the steps we are referring to datasheet :mrgreen:
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: UART function between PIC16F628A & XbeePro..

Postby Myline » Fri Mar 16, 2012 12:30 am

robosang WROTE:Ei....... why got this code but no UART initialize code? Well, you need to study the datasheet a bit. Even if we point out the steps we are referring to datasheet :mrgreen:



Hmm....is that refer to ....
UNIVERSAL SYNCHRONOUS/
ASYNCHRONOUS RECEIVER/
TRANSMITTER (USART)
MODULE
...
my application is half duplex when the infrared sensor give the digital
signal then transmitted from PIC to XBee,
btw what is UART initialize code for PIC16F628A...the sample din show anything?
I'm new than newbie.... :cry:
Myline
Newbie
 
Posts: 9
Joined: Sun Dec 18, 2011 10:49 am

Re: UART function between PIC16F628A & XbeePro..

Postby ABSF » Fri Mar 16, 2012 6:26 am

you can refer to the uart.c for 16f628 here

CODE: SELECT_ALL_CODE
//====================================================================================
//   Author            : Cytron Technologies      
//   Project            : UART of SK18B
//   Description         : Sample Soure Code for UART Using PIC16F628
//====================================================================================


//====================================================================================
#include<pic.h>


//configuration
//====================================================================================
__CONFIG (0x3F22);


// define IO port
//====================================================================================
#define      SW      RB0
#define      led1   RB7
#define      led2   RB6


// FUNCTION PTOTOTYPE
//====================================================================================
unsigned char uart_rec(void);         //receive uart value
void uart_send(unsigned char data);
void uart_str(const char *s);
void delay(unsigned long data);         //delay function, the delay time



// Main Program
//====================================================================================

void main ()
{
   unsigned char a,b;
   //set I/O input output
   TRISA = 0b00000000;
   TRISB = 0b00000011;

   //Initial Condition
   led1 = 0;
   led2 = 0;

   //Configure UART
   SPBRG=129;         //set baud rate as 9600 baud
   BRGH=1;            //baud rate high speed option
   TXEN=1;            //enable transmission
   TX9 =0;            //8-bit transmission
   RX9 =0;            //8-bit reception   
   CREN=1;            //enable reception
   SPEN=1;            //enable serial port

   
   if(SW==0)
   {
      uart_str("============== SK18B UART Test Program ==============");
      uart_send(10);
      uart_send(13);
      uart_send(10);
      uart_send(13);

      uart_str(" - type Ok to switch ON led");
      uart_send(10);
      uart_send(13);
   
      uart_str(" - type No to switch OFF led ");
      uart_send(10);
      uart_send(13);
      uart_send(10);
      uart_send(13);
      
      while(1)
      {
         a = uart_rec();
         
         if(a == 'O');
         {
            a = uart_rec();
            if (a == 'k')
            {
               led1=1;
               led2=1;
            }
         }
         uart_send(32);
         b = uart_rec();
         if(b == 'N');
         {
            b = uart_rec();
            if (b == 'o')
            {
               led1=0;
               led2=0;
            }
         }
         uart_send(32);
      }      

   }

}            



// Subroutine UART
//================================================================================

unsigned char uart_rec(void)         //receive uart value
{
   unsigned char rec_data;
   while(RCIF==0);                  //wait for data
   rec_data = RCREG;            
   return rec_data;               //return the data received
}

void uart_send(unsigned char data)
{   
   while(TXIF==0);            //only send the new data after
   TXREG=data;               //the previous data finish sent
}


void uart_str(const char *s)
{
   while(*s)uart_send(*s++);
}

void delay(unsigned long data)         //delay function, the delay time
{                              //depend on the given value
   for( ;data>0;data--);
}   


You can find uart code for other PIC from the links below but I think they are almost the same.

http://www.cytron.com.my/viewProduct.php?pid=GhQNCQIsHhoANSEDExQ7LKi!!!!!k1TZta36EYts93!!!!!Za0w=&store=
http://www.cytron.com.my/viewProduct.php?pcode=SK40C&name=Enhanced%2040%20pins%20PIC%20Start-Up%20Kit

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests