Sign In

Troubly To Receive Serial Data With The PIC18F2420

Manpreet Singh manpreet1335@yahoo.com

Hardware: PIC18F2420 Running At 8MHz Software: "c" Language Using XC8 Compiler

Looking at the PIC18F2420 I managed to send data from the pic to the PC but cannot do the opposite! I downloaded a library which was meant to RECEIVE data as well as sending it but still I can only send data and not receive any. Can anyone help?

Images

Program

/******************************************************************************

 Serial communication library for PIC16F series MCUs.

 Compiler: Microchip XC8 v1.12 (http://www.microchip.com/xc)

 MCU: PIC16F877A
 Frequency: 20MHz

                                     NOTICE

NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A
WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART
OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR
HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN
COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.


WRITTEN BY:
AVINASH GUPTA
me@avinashgupta.com

*******************************************************************************/

#ifndef USART_PIC16_H
#define	USART_PIC16_H

#include <stdint.h>
#include <xc.h>

#ifdef	__cplusplus
extern "C" {
#endif

//Constants
#define RECEIVE_BUFF_SIZE 64

//Varriables
volatile char URBuff[RECEIVE_BUFF_SIZE];	//USART Receive Buffer
volatile int8_t UQFront;
volatile int8_t UQEnd;
void USARTInit(uint16_t baud_rate);
void USARTWriteChar(char ch);
void USARTWriteString(const char *str);
void USARTWriteLine(const char *str);
void USARTWriteInt(int16_t val, int8_t field_length);
void USARTHandleRxInt();
char USARTReadData();
uint8_t USARTDataAvailable();
void USARTGotoNewLine();
void USARTReadBuffer(char *buff,uint16_t len);
void USARTFlushBuffer();

#ifdef	__cplusplus
}
#endif





/******************************************************************************

 Serial communication library for PIC16F series MCUs.

 Compiler: Microchip XC8 v1.12 (http://www.microchip.com/xc)

 Version: 1.0 (21 July 2013)

 MCU: PIC16F877A
 Frequency: 20MHz

                                     NOTICE

NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A
WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART
OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR
HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN
COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.


WRITTEN BY:
AVINASH GUPTA
me@avinashgupta.com

*******************************************************************************/


void USARTInit(uint16_t baud_rate)
{
    //Setup queue
    UQFront=UQEnd=-1;
        
    //SPBRG
    switch(baud_rate)
    {
     case 9600:
        SPBRG=25;
        break;
     case 19200:
        SPBRG=64;
        break;
     case 28800:
        SPBRG=42;
        break;
     case 33600:
        SPBRG=36;
        break;
    }
    //TXSTA
    TXSTAbits.TX9=0;  //8 bit transmission
    TXSTAbits.TXEN=1; //Transmit enable
    TXSTAbits.SYNC=0; //Async mode
    TXSTAbits.BRGH=1; //High speed baud rate

    //RCSTA
    RCSTAbits.SPEN=1;   //Serial port enabled
    RCSTAbits.RX9=0;    //8 bit mode
    RCSTAbits.CREN=1;   //Enable receive
    RCSTAbits.ADDEN=0;  //Disable address detection

    //Receive interrupt
    RCIE=1;
    PEIE=1;

    ei();
}

void ISR(void)
{
    if (RCIE && RCIF) {
        USARTHandleRxInt();
        return;
    }
}


void USARTWriteChar(char ch)
{
  while(!PIR1bits.TXIF);

  TXREG=ch;
}

void USARTWriteString(const char *str)
{
  while(*str!='')
  {
      USARTWriteChar(*str);
      str++;
  }
}

void USARTWriteLine(const char *str)
{
    USARTWriteChar('r');//CR
    USARTWriteChar('n');//LF

    USARTWriteString(str);
}

void USARTHandleRxInt()
{
  if(PORTBbits.RB1==1)
    PORTBbits.RB1=0;
  else
    PORTBbits.RB1=1;
  
    //Read the data
    char data=RCREG;

    //Now add it to q
    if(((UQEnd==RECEIVE_BUFF_SIZE-1) && UQFront==0) || ((UQEnd+1)==UQFront))
    {
        //Q Full
	UQFront++;
	if(UQFront==RECEIVE_BUFF_SIZE) UQFront=0;
    }

    if(UQEnd==RECEIVE_BUFF_SIZE-1)
        UQEnd=0;
    else
	UQEnd++;

    URBuff[UQEnd]=data;

    if(UQFront==-1) UQFront=0;
    
}

char USARTReadData()
{
    char data;

    //Check if q is empty
    if(UQFront==-1)
	return 0;

    data=URBuff[UQFront];

    if(UQFront==UQEnd)
    {
        //If single data is left
	//So empty q
	UQFront=UQEnd=-1;
    }
    else
    {
	UQFront++;

	if(UQFront==RECEIVE_BUFF_SIZE)
            UQFront=0;
    }

    return data;
}

uint8_t USARTDataAvailable()
{
    if(UQFront==-1) return 0;
    if(UQFront<UQEnd)
	return(UQEnd-UQFront+1);
    else if(UQFront>UQEnd)
	return (RECEIVE_BUFF_SIZE-UQFront+UQEnd+1);
    else
	return 1;
}

void USARTWriteInt(int16_t val, int8_t field_length)
{
    char str[5]={0,0,0,0,0};
    int8_t i=4,j=0;

    //Handle negative integers
    if(val<0)
    {
        USARTWriteChar('-');   //Write Negative sign
        val=val*-1;     //convert to positive
    }
    else
    {
        USARTWriteChar(' ');
    }

    if(val==0 && field_length<1)
    {
        USARTWriteChar('0');
        return;
    }
    while(val)
    {
        str[i]=val%10;
        val=val/10;
        i--;
    }

    if(field_length==-1)
        while(str[j]==0) j++;
    else
        j=5-field_length;


    for(i=j;i<5;i++)
    {
        USARTWriteChar('0'+str[i]);
    }
}

void USARTGotoNewLine()
{
    USARTWriteChar('r');//CR
    USARTWriteChar('n');//LF
}

void USARTReadBuffer(char *buff,uint16_t len)
{
	uint16_t i;
	for(i=0;i<len;i++)
	{
		buff[i]=USARTReadData();
	}
}
void USARTFlushBuffer()
{
	while(USARTDataAvailable()>0)
	{
		USARTReadData();
	}
}




#endif	/* USART_PIC16_H */


#include <xc.h>

#include "usart_pic16.h"

// Configuration Byte



#pragma config OSC = HS  	//HS oscillator
#pragma config PWRT = ON, BOREN = OFF  //power-up timer on, brown-out detect off
#pragma config WDT = OFF	//watchdog timer off
#pragma config LVP = OFF  	//low voltage programming off
#pragma config PBADEN = OFF  	//Port B all digital i/o
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    

void main()
{
  //Initialize USART with baud rate 9600
  USARTInit(9600);
   TRISA = 0b00000000; //All bits output (none used in this program)
    TRISB = 0b00000000; //All bits output (none used in this program)
    TRISC = 0b1000000; //All bits output 

  //Write intro text
  USARTWriteLine("***********************************************");
  USARTWriteLine("USART Test");
  USARTWriteLine("----------");
  USARTWriteLine("Type a character on keyboard");
  USARTWriteLine("it will reach the PIC MCU via the serial line");
  USARTWriteLine("PIC MCU will return the same character but ");
  USARTWriteLine("enclosed in a <>");
  USARTWriteLine("--");
  USARTWriteLine("For example if you type 'a' you will see <a>");
  USARTWriteLine("appear on serial terminal.");
  USARTWriteLine(" ");
  USARTWriteLine("This checks both way serial transfers.");
  USARTWriteLine("");
  USARTWriteLine("Copyright (C) 2008-2013");
  USARTWriteLine("www.eXtremeElectronics.co.in");
  USARTWriteLine("***********************************************");
  USARTGotoNewLine();
  USARTGotoNewLine();
              PORTCbits.RC5=1;
              PORTCbits.RC6=1;
  while(1)
  {
      //Get the amount of data waiting in USART queue
      uint8_t n= USARTDataAvailable();

      //If we have some data
      if(n!=0)
      {
          //Read it
          char data=USARTReadData();

          //And send back
          USARTWriteChar('<');
          USARTWriteChar(data);
          USARTWriteChar('>');
          
         
              PORTCbits.RC5=0;
              PORTCbits.RC6=0;
      
       
      }

  }
}




Replies

R Avinash
2019-02-10 12:04:38
What is the output of the above program?

Post a reply to Manpreet Singh

Think you can help Manpreet Singh ? Then post your thoughts that might help Manpreet Singh. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.