Pic Lab, PIC18, Experiment #1.1, UART / USART (updated version)

Recently, I was constantly struggling with the fact that microchip was removing more and more support libraries, so it was not wise to rely on them in all projects. Anytime I return again to my old functions – if it used the support libs, there is a pretty good chance it is screwed.

So this code works with XC8 version 2.36 and doesn’t require the involvement of extra support lib.

Functions are already described a couple of times:

For PIC16

For PIC18

So, I am not going to stop by and describe it all again, just give the code which I tested on the PIC18F1230.

The uart file:

#include <xc.h>

void InitUart()  //Initialization of UART 
{
   //PIC18F1230
    SPEN = 1;     //Serial Port enabled
    TRISA3 = 1;
    TRISA2 = 1;
    
    /*------BRG to 8 bit async -----------*/
    SYNC = 0;
    BRG16 = 0;
    BRGH = 0;
    /*------end of BRG -------------------*/

    /*-------BAUD rate coefficients-------
    BRG 0 0 0
    4M
    2.4K - 25
    9.6K - 6
    19.2 - 2
    57.6 - 0

    8M
    2.4K - 51
    9.6K - 12
    
    10M
    2.4K - 64
    9.6K - 15
    19.2K - 7
    --------------------------------------*/

    SPBRG = 12; //9615 baudrate with 8MHz
    


    TX9 = 0; //no need to use 9th bit
    TXEN = 1;

    //Reciever part
    RCIE = 1; //RX interrupts enable
    RX9 = 0;
    CREN = 1;

}//InitUart

char BusyUSART(void)
{
  if(!TXSTAbits.TRMT)  // Is the transmit shift register empty
    return 1;          // No, return FALSE
  return 0;            // Return TRUE
}


void writeByteUart(char tx_data)  //Writing of a single byte
{
  TXREG = tx_data;      // Write the data byte to the USART
}//writeByteUart

void writeDataUart( char *data)
{
  do
  {  // Transmit a byte
    while(BusyUSART());
    writeByteUart(*data);
  } while( *data++ );
}

void NumToUart(unsigned int Num)   //Number to uart procedure, max 5 digits
{

  unsigned int bignum = 10000;
  unsigned char numtemp = 5;

  if (!Num)
  {
      writeByteUart('0');         //If num is equal to 0, no need to do anything
      while(BusyUSART());         //wait till the buffer will be free
  }
  else 
  {
	  while(numtemp>0)             //determine how many digits in the number
	  {
	    if (Num/bignum)
	        break;
	    numtemp--;
	    bignum = bignum / 10;  
	  }  
	
	
	
	  for (unsigned char i = numtemp; i>0; i--)
	    {
	      writeByteUart( (Num - (Num/(bignum*10))*bignum*10 )/bignum + '0');         //pushing digits one by one from left to right
	      while(BusyUSART());                                                       //wait till buffer will be free
	      bignum = bignum/10;
	    }
   } 
}


char ReadUart()
{
    char data;   // Holds received data
    data = RCREG;                      // Read data

  return (data);                     // Return the received data

}

Then a simple main file to test the stuff:

void main(void) {
//INTOSC TO 8MHz
 IRCF2 = 1;
 IRCF1 = 1;
 IRCF0 = 1;
 SCS1 = 1;
//INTOSC setup  

InitUart(); //Initialization of USART

writeDataUart((char *) "diymicro.org\r\n");
writeDataUart((char *) "Testing one byte tx, wait for it it is - ");
writeByteUart('X');
writeDataUart((char *) "\r\nNow the number 25660 is about to roll - ");
NumToUart(25660);

//global interrupts, priority is disabled
IPEN = 0;
GIE = 1;
PEIE = 1;



while(1)
{
    if (rx_data_recieved)  //some data recieved
    {
        writeByteUart(rx_data_recieved);
        while(BusyUSART());

        rx_data_recieved = 0;
    }    
    
    
}//while(1)

}//main

void __interrupt(high_priority) HighISR(void)
{

    if (RCIF)
    {
       rx_data_recieved = ReadUart();
    }
   

}//high priority interrupts

Still haven’t find the perfect solution for a terminal under Linux, somehow minicom stopped working properly and putty is not that convenient, so far using cutecom to check UART:

result of the work in cutecom

All worked fine, with no issues. The source code is available here.

1 thought on “Pic Lab, PIC18, Experiment #1.1, UART / USART (updated version)

  1. Pingback: LED lamp repair/mod | diymicro.org

Leave a Reply

Your email address will not be published.