top of page

Pic Serial Communication Assembly Code: A Comprehensive Guide to PIC USART Module and Interrupts

  • tergpapacirgabook
  • Aug 20, 2023
  • 6 min read


SidebarI/O Port usage summaryPORT register: TRISIO - The port directionregister.You can reset bits (low) to define port pins asoutputs.You can set bits (high) to define port pins asinputs.PORT register : GPIO -The input/output register.You write to this register to set outputpins high or low.You read from this register to read thecurrent value input pins.All of these actions were used in theprevious two tutorial pages and the all work using bit values:TRISIO = 0x01; sets bit 0 of TRISIO all the rest zero.TRISIO = 0x04; sets bit 2 of TRISIO all the rest zero.GPIO = 0x01; sets bit 0 of GPIO all the rest zero.GPIO = 0x04; sets bit 2 of GPIO all the rest zero.Note: In otherPIC devices ports are labeled alphabetically and they do not use the GPIO nameinstead they use the text 'PORT'. So the first port is labeled PORTA the nextPORTB etc. Corresponding port direction registers are TRISA and TRISB. Youcan use them in exactly the same way as GPIO andTRISIO.PIC Serial Port : Software TXpartThe code for theTX part of the USART is simplified in that it generates 10 bits of serial datawith no parity bit.The basic PIC Serial port configuration for the TX software is:if(typeof ez_ad_units!='undefined')ez_ad_units.push([[580,400],'best_microcontroller_projects_com-medrectangle-3','ezslot_2',107,'0','0']);__ez_fad_position('div-gpt-ad-best_microcontroller_projects_com-medrectangle-3-0');Bits per second (BAUD)2400Number of bits8ParityNoneStop bits1Flow controlNoneNote: To review how the RS232 pic serial portworks click here.Since each bit takes 1/2400 seconds the total time to transmit a digit is4.16ms.Note: The bit timing won't be exact as thePIC chip runs at 4MHz but it will be good enough for bench top use.CircuitThe circuit uses the standard MAX232 level translator chip but you could usean SP202ECP which has identical pin out (and is cheaper!) and lets you use100nF capacitors instead of electrolytic ones. You can also use USB to RS232 or USB to SerialDigital converters and with these you don't even need the MAX232 levelconverter everything is done at 5V (or 3V3)..Connect the ground and transmit output to a serial port connector as shown andto a serial cable from it to the PC and it's ready to go.For setting up Tera Term click here.SolderlessbreadboardAdd the MAX232 Chip and capacitors.Learn about the tool used for creatingthis diagram.Circuit diagramAgain the picserial port circuit is easier to see on a schematic.


12F675 pinoutsOther views:SoftwareSource code files :To get the filesoftware project files and c source code click here.PIC Serial Port OperationThe code flashes the LED 3 times then repeats a message to the serialport.The following code is part of the pic serial port transmitter://////////////////////////////////////////////////////////////////////void_Soft_USART_Write(unsigned short chr) {unsigned short mask=1,i;unsigned int txdata; txdata = chr ProgramMemory.Scroll to bottom and check the CAL value at the last location.Now go back to the project Options and turn off "Program calibrationmemory" which will now be protected (until you lose it again!).Re Check it has workedYou can check it worked by re-building the original hex file (so themanually inserted CAL value is lost). Re-program this new hex into the PIC.Then upload the hex data. View memory as above and scroll to bottom - the newCAL value will be there.OSCCAL settingsThis register changes the oscillation frequency when you write to itMaximumfrequency0xFFMiddlefrequency0x80Minimumfrequency0x00Only the upper six bits are used as the calibration bits so values must bedifferent by 4 to change to the next OSCCAL setting e.g. values 32,33,34 and 35would give the same frequency setting.Microchip calibrates the 12F675 microcontroller and places a calibration valueat address 0x3FF. This is a RETLW instruction that returns a value when CALLedthis is the value to use in the OSCCAL register.ImportantYou have to write and use a routine to set the register OSCAL with thestored value : It is not done automatically for you!Note: You have to use the followingassembler code:The code the data sheet suggests you use to set the calibration value into theOSCCAL register is :bsf STATUS, RP0; Bank 1call 0x3ff; Get the cal valuemovwf OSCCAL; Calibratebcf STATUS, RP0; Bank 0All this does iscall the location that contains the RETLW value which then returns thecalibration value. This assembler code is packaged intothe get_set_osc_cal() routine and does the OSCCAL setting for you.




Pic Serial Communication Assembly Code !!TOP!!




It only becomes possible to design and program a PIC microcontroller after understanding its architecture. The architecture entails I/O ports, CPU, A/D converter, interrupts, oscillator, counters/timers, memory organization CPP module, and serial communication.


About the Author: Dr. Charles Kim is a professor in the Department of Electrical Engineering and Computer Science at Howard University in Washington, DC, USA. He has taught for years both for undergraduate and graduate students at many universities including Texas A&M University, University of Suwon, and Howard University. Dr. Kim wrote this book originally for his Microcomputer course and Embedded Computing course, and currently this book is used as the textbook of Embedded Computing, which is basically project-based robot building course. It was also adopted for the Microcomputer course for assembly language programming. His approach in the book is simple: code in assembly language, which is free, instead of using C which costs you with cross compiler. Dr. Kim is active in embedded computing, smart sensor network, and AI and machine reasoning systems. Contact:ckimson@gmail.com for any question.


USART is the most commonly used serial I/O module. It is also known as Serial Communications Interface (SCI). USART can be easily configured as a full-duplex asynchronous communication system that can communicate with peripheral devices, such as personal computers and CRT terminals, or it can be configured as a half-duplex synchronous communication system that can communicate with peripheral devices, such as serial EEPROMs, A/D or D/A integrated circuits, etc. USART can be configured in the following modes.


It is the popular embedded serial communication that is widely supported by many chip manufacture. It is considered as one of the fastest serial data transfer interfaces for the embedded system. Most of the time, it is used to communicate data to and from the micro-microcontroller.


SPI is implemented in the PIC microcontrollers by a hardware module called the Synchronous Serial Port or the Master Synchronous Serial Port. This module is built into many PIC microcontrollers. This module also supports II2C communication that is a two-wire communication. It supports SPI serial communication between two or more devices at a high speed and is reasonably easy to implement.


Unlike I2C, we do not use slave address in serial peripheral interface communication. But we use a chip select pin to select a slave. In the case of multiple slaves, we need a chip select wire for every slave device.


UART is the abbreviation for Universal Asynchronous Receiver/Transmitter and is a commonly used hardware module for serial communication based on communication protocols like RS 232. This chapter discusses how such a device is appropriately interfaced with a PIC microcontroller. The detailed technical process is given below.


UART is a commonly used hardware module for serial communication based on communication protocols like RS 232. Serial communication can be of a synchronous or asynchronous type. A common clock will guide both the receiver and the transmitter in synchronous communication which is absent in asynchronous communication. UART is the most common method of asynchronous serial communication between two devices. UART communication is carried out using two lines Tx (transmission line) and Rx (reception line). It is possible to transmit and receive data simultaneously (full duplex) using UART interface. PIC microcontrollers possess (most of them) an inbuilt USART hardware which can be configured in three different modes.


RS-232 is a serial communication protocol for the transmission of data between two devices. The protocol defines a logic 0 with voltage in the range of +3V to +15V and a logic 1 with voltage in between -3V to -15V. This voltage level is incompatible with a CMOS or TTL logic family device since the voltage levels are 0V for a logic 0 and 5V for logic 1. This mismatch in the voltage levels demands the requirement of a voltage level shifter to convert RS-232 logic levels into TTL logic levels and vice versa. Most commonly used voltage level shifter IC for this purpose is MAX232.


Baud rate is the measure of the speed of data transfer in serial communication in units of symbols per second. It may also be considered as the number of bits per second under some circumstances. Baud rate can be configured for different modes of UART using different formulae.


Several free software are available for displaying and debugging the serial communication. RealTerm is one such software which is widely used. Baud rate, port in which DB9 connected need to be set in the RealTerm settings. This software can be used for visualisation of the serially transmitted data. A RealTerm window is shown below.


Compile is more of a building and assembly process. This is only stated so that users do not make the wrong expectations of native machine code when making feature requests and/or Bug Reports. Nextion remains closed source.


Arduino boards have built in support for serial communication on pins 0 and 1, but what if you need more serial ports? The SoftwareSerial Library has been developed to allow serial communication to take place on the other digital pins of your boards, using software to replicate the functionality of the hardwired RX and TX lines. This can be extremely helpful when the need arises to communicate with two serial enabled devices, or to talk with just one device while leaving the main serial port open for debugging purpose. 2ff7e9595c


 
 
 

Recent Posts

See All

Comments


© 2023 by James Consulting. Proudly created with Wix.com

bottom of page