help me about USART with PIC16F or PIC12F

Status
Not open for further replies.

Sancho1977

New Member
Hi all, I am biginner about the USART.
I have a problem with the complier,XC8. Though I tried to include the header file called as usart.h at several times, I could not complete it.
Someone , help me about teaching me a code as macro.
I need also void(unsigned char?) putsUSART( char * buffer, char length 20){}

I could get a code on web about void getsUSART like this.

void getsUSART(char *buffer, unsigned char len)
{
char i; // Length counter
unsigned char data;
for(i=0;i<len;i++) // Only retrieve len characters
{
while(RCIF==0);// Wait for data to be received
data = RCREG; // Get a character from the USART
// and save in the string
*buffer = data;
buffer++; // Increment the string pointer
}
}

P.S. As I do not have a Debugger for this program, I do not want to use a wrong code for it.
↓ could the following code be used for my requirement?

void putsUSART_( unsigned char *buffer)
{
while( *buffer>0) {
TXREG=*buffer++;
}
}


Thanks in advance
 
Last edited:
I show you the program below among 1822 transmitter and 1822 reciever.
But it did not move at all as I thought .
C:
/*
PIC12f1822 for transmitter
*/
#include <xc.h>
#define _XTAL_FREQ  8000000  // for delay
 
__CONFIG(CLKOUTEN_OFF & FOSC_INTOSC & FCMEN_OFF & IESO_OFF & BOREN_ON &
  PWRTE_ON & WDTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF) ;
__CONFIG(PLLEN_OFF & STVREN_ON & WRT_OFF  & LVP_OFF);
 
unsigned char *RCV_Buff ;  // buffer
unsigned int Flag;
 
void getsUSART(char *buffer, unsigned char len)
{
  char i;  // Length counter
  unsigned char data;
  for(i=0;i<len;i++)  // Only retrieve len characters
  {
  while(RCIF==0);// Wait for data to be received
  data = RCREG;  // Get a character from the USART
                                // and save in the string
  *buffer = data;
  buffer++;  // Increment the string pointer
  }
}
 
void putsUSART( unsigned char *buffer2)
{
  while( *buffer2) {
__delay_ms(50);
while(!TXIF);
TXREG=*buffer2++;
  }
}
 
int main(void)
{
  OSCCON = 0b01110010 ;  //8MHz
  ANSELA = 0b00000000 ;  //all digital
  TRISA  = 0b00101000 ;  // RA5= input
  PORTA  = 0b00000000 ;  // all clear
  OPTION_REG= 0b10000000; //no use weak-pull up
 
  // inicialize USART
  RXDTSEL = 1 ;  // RA5= RX
  TXCKSEL = 1 ;  // RA4 =TX
  TXSTA  = 0b00100100 ;  // Asynchrous, 8bit,SYNC=0,BRGH=1
  RCSTA  = 0b10010000 ;  // SPEN=1, CREN=1
  SPBRG  = 51 ;  // baud-rate 9600for 8MHz
 
unsigned char *message = "INDICATE";
  __delay_ms(2000);
  putsUSART(message);
  TXIF=0;
  while(1) {}
}
C:
/*
PIC12f1822 for reciever
*/
#include <xc.h>
#define _XTAL_FREQ  8000000  // for delay
 
__CONFIG(CLKOUTEN_OFF & FOSC_INTOSC & FCMEN_OFF & IESO_OFF & BOREN_ON &
  PWRTE_ON & WDTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF) ;
__CONFIG(PLLEN_OFF & STVREN_ON & WRT_OFF  & LVP_OFF);
 
unsigned char *RCV_Buff ;  // buffer
unsigned int Flag ;
 
void getsUSART(char *buffer, unsigned char len)
{
  char i;  // Length counter
  unsigned char data;
  for(i=0;i<len;i++)  // Only retrieve len characters
  {
  while(RCIF==0);// Wait for data to be received
  data = RCREG;  // Get a character from the USART
                                // and save in the string
  *buffer = data;
  buffer++;  // Increment the string pointer
  }
}
 
void putsUSART( unsigned char *buffer2)
{
  while( *buffer2) {
__delay_ms(50);
while(!TXIF);
TXREG=*buffer2++;
  }
}
 
void interrupt InterReceiver( void ) // for reciving the buffer
{
 
  if (RCIF == 1) {  // usart?
  getsUSART(RCV_Buff,8) ;  // set
 
  Flag = 1 ;
  RCIF = 0 ;  //reset
  if(RCV_Buff =="INDICATE"){
 
LATA2=1;   //Output as Debugger
__delay_ms(2000);
LATA2=0;  //Output as Debugger
 
   }
  }
 
   RCIF = 0 ;
}
 
int main(void)
{
  OSCCON = 0b01110010 ;  //8MHz
  ANSELA = 0b00000000 ;  //all digital
  TRISA  = 0b00101000 ;  // RA5= input
  PORTA  = 0b00000000 ;  // all clear
  OPTION_REG= 0b10000000; //no use weak-pull up
 
  // inicialize USART
  RXDTSEL = 1 ;  // RA5= RX
  TXCKSEL = 1 ;  // RA4 =TX
  TXSTA  = 0b00100100 ;  // Asynchrous, 8bit,SYNC=0,BRGH=1
  RCSTA  = 0b10010000 ;  // SPEN=1, CREN=1
  SPBRG  = 51 ;  // baud-rate 9600for 8MHz
  RCIF = 0 ;
  RCIE = 1 ;
  PEIE = 1 ;
  GIE  = 1 ;
  Flag = 0 ;
  while(1) {}
}
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…