Skip to main content

PIC18f Serial Communication Tutorial

PIC18f4580 Serial Communication Tutorial

Basics of Serial Communication:

When a microprocessor connects with the outside world, it offers the data in byte-sized chunks. In particular cases, such as printers, the information is simply taken from the 8-bit data bus and offered to the 8-bit data bus of the printer. This can work only if the cable is not too long, because long cables reduce and even distort signals.

Moreover, an 8-bit data path is expensive. Keeping this in mind, serial communication is used for transmitting data between two systems situated at distances of hundreds of feet to millions of miles apart.

serial versus parallel data transfers.

One will simply analyze major distinction between serial and parallel communication topologies.

Parallel communication within which a lot of connections and so on associative hardware as a result of in parallel communication technique  it transmits every bit at he same time. In contrast, Serial communication  requires less hardware and connections, simply one can transmit data on a single line using serial transmission.

 To grasp operation of  serial communication you can take shift register as an example, In fact actual hardware in most machine to for serial communication is parallel-in-serial-out shift register.

 Now it makes sense that on the receiving end an invert operation is required which is a serial-in-parallel-out shift register to receive serial stream of data and for further byte packetizing.

 In daily life ,land line or telephone is very good example of serial communication for long distance, Our voice signal which is sinusoidal signal will be converted in digital signals 0s and 1s and then send to modulator to transmit , on the other hand, to receive the signal first we demodulate it using demodulator and then convert it to digital to analog which is our voice.

 Modulation is only required when the distance is long between sender and receiver ,One more communication which we observe daily is keyboard interaction with the mother board, in this case distance is short and we do not need modulation and expensive modulators.

 In this post our emphasis will be on short distance communication which does not require any modulation technique. 

Asynchronous and Synchronous are two ways which generally processors used to establish communication between two ends.

 In case of  synchronous communication, the information transferred between two ends 

·         in sequence

·         bit after bit

·         with fixed baud rate

·         and the clock frequency is transmitted along with the bits

That means both ends are synchronized by an identical clock frequency which is transmitted along with the information. Generally, data encoded with the frequency and transmitted on a single line while some times separate connection used for frequency. 

In Asynchronous communication  the information is sent with a specific pattern which consist START and END data bits. Timing is not important, transfer can initiate at any time, it is up to receiver to recognize start and stop bit. 

In general, Asynchronous communication packet is detect by 
  • “Start” bit marks the beginning of a new frame.
  • “Stop” bit marks the end of the frame.

Why we need Protocol in communication?
In digital communication all the data in the form 0s and ones, there is a requirement to define some protocol that defines how the data is packed with defined number of characters and which bits needs to be detect as a start and end of frame.

Start and stop bits
In asynchronous communication frame is developed in such a way that every character is placed between start and stop bit .

The start bit is usually one bit however the stop bit are often one or 2 bits. The start
bit is usually  ZERO  and the stop bit(s) is 1 (high). as an example, check up on Figure in which the ASCII character "a" is framed between the start bit and a single stop bit. Notice that the Least significant bit  is sent out first.
serial transmission of 'a' using UART

Why we need UART/USART in built Module:

It is attainable to write down computer code to use either of above mentioned strategies, however the programs may be tedious and long. For this reason, special IC chips manufactured by many manufacturers for serial data communications. 

These chips known as UART (universal asynchronous receiver-transmitter) and USART (universal synchronous-asynchronous
receiver-transmitter). The PIC 18 chip has a built-in USART module, which is discussed
in detail during this post.

Data transfer rate
The rate of data transfer in electronic communication is declared in bps (bits
per second). Another wide used nomenclature for bps is baud rate. However, the
baud and bps rates do not seem to be essentially equal. This is often as a  result baud rate is the
modem nomenclature  and is defined as the number of signal changes per second. 

In modems, typically one single change of signal transfers many bits of data. As far as the conductor wire is concerned, the baud rate and bps are the same, and for this reason during this post we tend to use the terms bps and baud interchangeably.

Now, latest machines supports higher data rates as compared to early IBM machines that could provide data rate of 100 to 9600 bps. 

RS232 standards

In this POST we tend to refer RS232, RS232 is that the most generally used serial I/O interfacing standard. This standard is employed in personal computers and plenty of forms of  instruments used for test and verification. Because the standard was set long before the appearance of the TTL logic family, however, its input and output voltage levels do not seem to be  TTL compatible.

In RS232, a 1 is represented by -3 to -25 V, whereas  0 bit is +3 to +25 V,
making -3 to +3 undefined. Hence, to establish communication using RS232 to a microcontroller system we need voltage converters such as MAX232 to transform the TTL logic levels to the RS232 voltage level, and vice versa. MAX232 IC chips are generally known as line drivers. 


MAX232 circuit digram

From the above discussion, we know that microcontroller use TTL level UART(5V for logic 1 and 0V for logic 0)while the PC serial port  uses RS-232.since both standards uses identical software protocol and able to communicate via UART.

However, because of the difference in voltage level and polarity, we will need a level shifter to interface TTL level UART with RS-232

Now a days this can be easily done with the commonly available IC such MAX232 from Maxim.

The following Diagram shows how MAX232 is connected to UART of  PIC18f4580 and with PC serial port.

MAX232 interfacing with PIC microcontroller

Normally, PC has a male connector and the device connected to serial port has a female DB9 connector.



PINOUT of DB9 ,normally used for serail pot (RS232).


miKroC Library for UART:
The UART hardware module is available with a number of PIC compliant MCUs. The mikroC PRO for PIC UART Library provides comfortable work with the Asynchronous (full duplex) mode.

You can easily communicate with other devices via RS-232 protocol (for example with PC, see the figure at the end of the topic – RS-232 HW connection). You need a PIC MCU with hardware integrated UART, for example 16F887. Then, simply use the functions listed below.

  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 1 to 2.
  • Switching between the UART modules in the UART library is done by the UART_Set_Active function (UART modules have to be previously initialized).  
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

Library Routines

  • UARTx_Init
  • UARTx_Data_Ready
  • UARTx_Tx_Idle
  • UARTx_Read
  • UARTx_Read_Text
  • UARTx_Write
  • UARTx_Write_Text
  • UART_Set_Active
The example demonstrates a simple data exchange via UART. When PIC MCU receives data, it immediately sends it back. If PIC is connected to the PC (see the Proteus Simulation below), you can test the example using virtual terminal tool available in Proteus.






miKroC Code for serial Communication using PIC184580

char uart_rd;

void main() {
  ADCON1 = 0xff;
  CMCON = 0x7;

  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

  UART1_Write_Text("Welcome to microcontroller Blog\r");        // "/r" is used for new line
    Delay_ms(1000);
  UART1_Write_Text("\rPress any key to test recieving\r ");
       Delay_ms(1000);
  UART1_Write_Text("\rMCU will Re-Transmit it\r ");
  

  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     // read the received data,
       UART1_Write_Text("\ryou pressed: ");
      UART1_Write(uart_rd);       // and send data via UART
    }
  }
}




 CSC Code for serial Communication using PIC184580

char ch;

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
printf("Welcome to microcontroller Blog");
printf("\r");
delay_ms (1000);
printf("Press any key to test recieving");
delay_ms (1000);
printf("MCU will Re-Transmit it");


while (1){
printf("\r");
ch=getc();
printf("\r");
printf("You Pressed: %c ",ch);



}

}

         Friends for further learning about PIC18 checkout 

Comments

Popular posts from this blog

How to use SPI Debugger

  SPI Debugger Hi Friends, in this post, we will discuss how to use SPI Debugger available in proteus for serial peripheral interface (SPI) which is a built-in feature of PIC microcontroller to communicate and data exchange between PIC and other devices. SPI Debugger Introduction to SPI Guys, SPI can be considered as a programmable shift  register, The SPI module is a synchronous serial I/O port that shifts a serial bit stream of variable length and data rate between the PIC and other peripheral devices. Here “synchronous” means that the data transmission is synchronized to a clock signal. To avoid further delay, let's see some important connections and different configurations to effectively use SPI Debugger. How to use SPI Debugger in MASTER & SLAVE configuration Select the virtual instrument and from the instrument, list selects "SPI Debugger". SPI Debugger Insert two SPI Debugger on the working area, select any one of them,

PIC18f4580 Timer0 calculation using miKroC , CSS and proteus

PIC18f4580 TIMER PROGRAMMING The PIC18f4580 has four timers. they're named as Timers zero, one, two, and three. they will be used either as timers to come up with a time delay or as counters to count events happening outside the microcontroller. First, we see however Timers zero is employed to come up with time delays. Every timer wants a clock pulse to tick. The clock supply will be internal or external. If we have a tendency to use the inner clock supply, then 1/ fourth of the frequency of the crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) is fed into the timer. Therefore, it's used for time delay generation and for that reason is termed a timer. By selecting the external clock choice, we have a tendency to feed pulses through one among the PIC18's pins: this is often known as a counter. GIF  taken from  https://exploreembedded.com Basic registers of the timer Majority of t timers in 18F are 16 bits wide. Because the PIC 18 has an 8- bit ar

Frequency Counter using Interrupts PIC18f4580 Project

Frequency Counter  using PIC18f458 0 Proje ct This POST describes the construction of small frequency counter with a cheap PIC18f4580 microcontroller with 16 x 2 LCD.   Prerequisites: PIC18F4580 TIMER Programming. PIC18F4580 COUNTER Programming. PIC18F4580 Interfacing with 16x2 LCD. PIC18F4580 Interrupts Programming  ( we will cover in this POST) Concept: Frequency  is the number of occurrences of a repeating event per unit time. in our case we will measure a number of clocks generated by clock source per unit time. In this project, LCD is used to display the frequency and PIC timer 1 to measure the input signal and Timer0 to generate an indication that one second has gone. System software utilizes Timer-1 in the 16-bit counter mode to count the input signal and overflows of the counter are added to provide the aggregate count in multiples of 65536. Totaling the existing value of the counter at the conclusion provides the full count.