PIC18f4580 Serial Communication Tutorial
Basics of Serial Communication:
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
- “Start” bit marks the beginning of a new frame.
- “Stop” bit marks the end of the frame.
serial transmission of 'a' using UART |
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.
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 |
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.
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
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
Post a Comment