Embedded Computing with PIC16F877(A) -Assembly Language Approach
A complete guided project book for PIC students, 2006. p. 475
by Charles Kim, Ph.D.
 
 
 
What hardware components you must need [Read Carefully]:
Before your first coding practice, and before your long steps to the PIC programming, you have to check if you have everything you need. You don't want to stumble at the start line and you don't want to see your PIC engine fizzle out. Basically, you need four things : (a) a PIC board - commercially available one or your own breadboard, (b) PIC burner - which downloads your code (or your bootloader code), (c) Bootloader code which you download to your PIC chip using the burner so that your application code can be downloaded without using the burner anymore, and lastly (d) a Windows program to actually download your code. Of course, you have to have PIC assembler, and you can get it free from Microchip web site. The exact name of the assembler (which comes with editor, assembler, and simulator, among other amenities) is MPLAB.
Check my PIC Start-up page for the detailed explanation for (1)what minimum hardware you have to have for your PIC project, (2) how to select a proper 16F877 or 16F877A chip for the project, (3) where you get a bootloader, (4) where you can find a PIC burner to burn your PIC16F877 or 16F877A bootloader, (5) where you can download the Windows-based PIC downloader program (which downloads your own hex code to the bootloaded PIC chip), and (6)other important stuffs you have to check before ignite your PIC engine - a must read for all beginners. Chapter 4 of the book describes step by step for this coding environment with sample codes.

About the Author: Dr. Charles Kim is a professor in the Department of Electrical and Computer Engineering 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 expert systems.

Table of Contents (*note: each link below contains just first two pages of the chapter.)
 
0. Chapter 0: Book Cover, Table of Contents, and Preface
 
1. Chapter 1: Introduction Very skimpy introduction of 16F877. I thought a lengthy one a waste of time or a little insulting to readers who I think are already exposed to the microcontroller. But everybody writes introduction, so I am kind of caved in by the peer pressure, and have my own introduction here. You can skip it without any loss.
 
2. Chapter 2: PIC16F877 Microcontroller - Overview This chapter is to shake loose your muscle as a relief pitcher does to record another save. This time it's one for PIC16F877. You learn about the architecture, pin and package, and block diagram that you can forget after the shake. Many things are done in hardware level, then why we programmers bother to meddle with the good hardware designers. However, the structure of program memory (RAM) and where special registers are already occupying in four different banks are important. Also important is where the empty RAM spaces ("file register") are located for our use. They are scattered in 4 different banks. Then we discuss how input and output ports are controlled to be used as input or output, with example codes. Very simple LED turn on and off code is presented to apply the I/O configuration. That's all, folks!
 
3. Chapter 3: Instructions Sets details PIC16F877 instruction sets. Bit- and byte-oriented instructions are explained with detailed examples. One example seems more effective than 1 page description, in my humble opinion. Instruction cycles and their period are briefed. Then, I added several tricks of using the instructions. The reason why we need tricks is that, first of all, PIC is centered around "peripheral", which means PIC is very good in dealing with inputs and outputs. But we are trained to calculate something using the instruction (PIC) which does not have much calculation instructions. we want to declare a variable as we do in C, like "long x", but how do we do that in PIC? How do I check if a certain bit in a byte is set or cleared? How can I check if an input from a motion detection sensor goes to zero or stays in high? Do we have DO loops or FOR loops or WHILE loops in PIC? How can I change the blinking frequency of an LED without using whatever is built inside PIC. I mean, using just simple instructions. These are the tricks. Every bit genuine, though.
 
4. Chapter 4: Coding Environment has some old and new pieces of information on the Microchip's coding environment, MPLAB. I liked the MPLAB v.5.2 very much and I loved the simulation. But V5.2 has problem with WindowsXP, so I start to teach V 6.40. This chapter still has several pages of V5.2 and I think it does not hurt anybody. I put the instruction of using the coding environment MPLAB in detail. I know you surely will agree to it. Then, I explained how you download the hex code generated from MPLAB into your PIC chip. For those who decided to build their own PIC board in a breadboard, I detailed the minimum hardware for that. Use of MAX232 for RS-232 protocol and level conversion between the PIC chip and the serial communication port of your PC are detailed. I introduced a commercially available PIC board with either DB-9 serial communication port or USB port. I notice that some laptop comes without the once standard DB-9 port. Instead it comes with USB port. I added a few suggestions and recommendations when your port does not match with your computer's port. Of course, you have to pay some dollars to match the mismatch. I introduced PIC burner (a cheap but reliable one, which I currently use) and bootloader details. Also, I put all my PIC experiences at a section, Troubleshooting of your PIC board. It's worth reading. Then, I suggested a wire-wrapping method of circuit installation instead of usual soldering. Invest some money for your health. I hate to inhale the soldering smokes. Then, as if a bonus or gift, I happened to add a last section for a Piezoelectric buzzer example code. You'd like this complete code to test your PIC board, after testing with LED. Let your mom hear the buzzing and forgive your 3-year tuition money. She'd love to send you money for the last year education. [Note: If you start to use banksel to position to a proper bank for file register access, instead of the field manual way of p0 and p1 clearing or setting, be sure to remember that banksel does not like a company at its left: there must not be a label in the same line with banksel. Push up the label one line above.]
 
5. Chapter 5: Serial Communication If you are here, you covered the basics of PIC programming. Not very good programmer, though. To become a good one, one has to know how to use PIC modules built inside the microcontroller. You can say a module a nice coffee machine. You select a coffee type, strong or bland, sugar or no-sugar for your taste by pressing a few buttons in a sequence. Then, slide your cup and you have your coffee. PIC has numerous built-in modules for us programmers' easy use of the microcontroller. The first module we deal with is a module for serial communication, USART. To use a module, we have to know how to command the module to work for our purpose. But even before that, to know about the module's function is a must. Therefore, before the USART and its use for communication between PIC board and your PC, I give you a tour of serial communication first, at the first section. The famous serial communication protocol is RS-232 and one popular chip for level conversion between RS-232 and logic level is MAX232 chip. Then, I take you to how to use a Windows serial communication program, Hyperterminal, and set up for connecting your PC to PIC board. Then, the USART module. Its structure, internal registers we have to control, including the communication speed in bps (bit/sec), etc. Then, an example program follows to echo on your computer screen of the key-in you typed using your keyboard of PC. Sounds trivial? But it involves a lot more than just a trivial single key-in. The key-in read from keyboard travels first to your computer's serial port, then through the communication cable between your PC and PIC board, reaches at the MAX 232 chip, where the level is converted to logic level, and keep moving to the serial communication pins of PIC chip. The USART module converts the serially transmitted bits into a single byte. This process is called a "read" in the PIC programming. You "read" but there is nothing in the screen. Since you do not write. The "write" a byte is exactly the reverse order to echo what you typed. Anyway, the first example code will give your the full understanding of the subject. Serial communication between PIC and PC can be possible without using the USART module. Using the instructions and generating logic pulses the same as the USART module does. This is handy when you need more than one serial communication from PIC, like one to PC the other to another PIC board. I detailed this software approach of serial communication with full code. Sound knowledge in serial communication is a key to the many application examples discussed in the chapters to follow. So invest your time here.
6. Chapter 6: LCD Displaying and IR Remote Control Applications This chapter is long. It's because I have to explain a lot of stuffs. LCD is very popular and a convenient tool to display, replacing 7 segment LED modules. There are two types of LCD: LCD and serial LCD. LCD is just the standard LCD module you can buy at $20 a piece. A serial LCD is just the standard LCD module with something else attached at the back plane of the module and you pay $20 more for that something. By the way, you can take an LCD as a module which you can command to work for you. But you have to know how to bark your command, in bytes or, mostly, bits. There are two ways to connect the regular LCD to PIC. 8 wires between two and only 4 wires between two. We call them, "8 bit Interface" and "4 bit Interface", respectively. The serial LCD module is connected serially, that means via only 2 wires between PC and the serial LCD. How to command these monsters to work for you is the main subject of the chapter. Connection diagrams and coding examples are all over the chapter. Then, I decided to add Infra Red (IR) remote controller decoding example. Your TV/VCR remote controller sends code of signal, as you do in channel flipping, then an IR receiver hidden behind clouded plastic cover receives the signal and decode ("breaking the code") and then flip the channel. How to receive the code from your remote controller and decode the message and display the message in your PC screen is the subject of the lengthy description of the subject. I start from different protocols of sending the code from your TV remote: Sony and Sharp as two famous ones. Then, step by step procedure for receiving, decoding, and displaying follows. Hardware connection diagrams and full code are listed at the end of the chapter. Enjoy it.
7. Chapter 7: Motor Control and Robot Project The subject sounds very heavy but controlling motors, whether DC, Stepper, or whatever, is rather light. Here I focus on motor control method of different type of motors. Then, I introduce motor control chips, which you can easily obtain (buy or get free as samples), and how to use the motor control chips to control the motors. So main point is to tell you how to send command to the chips, as we send command to IR receiver or a small guy, LED. That's why motor control is such a small matter, according to me. One may thing stepper motor is in the different league. Well, there are stepper control chips, whether your stepper motor is unipolar or bipolar, which send a command for you to step around for a flamingo or samba. Several chips are introduced along with connection diagrams and example codes. So robot making does not seem too difficult after working on this chapter. At least, you can make a thing which can move. With IR on top, you can even remote control your thing. Great, isn't it?
8. Chapter 8: A/D Conversion and Data Acquisition Another built-in module we discuss is an A/D conversion module. A/D means Analog-to-Digital, and which further means than the value we feel in the world must be changed to a digital number for PIC. Temperature or voltage can be measured by PIC. I labored to give you a good introduction of using the A/D module. Then, the first example, to practice, is voltage reading from a variable resistor powered by DC 5Volt source. So the max voltage you get is 5Volt and the min, 0 Volt. Then, we display the result in PC screen. This involves a lot of coding actually. The example code will ease your job to understand the whole process. To further practice of A/D module, I introduce a distance finder example. Using an IR ranger, which generates voltage according to the distance, we apply the same A/D approach to display the distance of an object from the IR ranger. Connection of the IR ranger (Sharp GP2D12) actual coding are presented. Also presented are some other parts and elements you can apply A/D to, and one of them is a current sensor. Well, you can use this module in many different applications. I bet.
 
9. Chapter 9: Timer Modules and Digital Clock Applications Early we made out time delay using just using regular instructions. Here we study some modules for time delay and their application in digital clock. Only Timer0 module is heavily discussed. Its use and command and control. LED blinking with Timer0 is the first example for a practice. Then comes many different versions of digital clock. Times are displayed on the computer screen. Then, we connect 4 buttons and LCD to the PIC for a serious digital clock. In the last Digital Clock project, with 3 buttons for Hour, Minutes, and Second setting and another button for "setting" itself, I introduced and applied PIC's external interruption pin (RB0/INT). Therefore, if you're interested in learning 'interrupt', this section of the chapter is the one you're looking for. As usual, connection diagrams and full code are scattered around the chapter. At the last section, I briefly introduced the use of Timer 1 and its application in color sensing application. It's very exciting practice. I got the color sensing chip free as sample and I think you'd do that too.
10. Chapter 10: Synchronous Serial Communication and Keyboard Connections Synchronous communication is run by a universal clock. And keyboard is connected and run with synchronous communication. I first discuss about the IBM AT or PS/2 type keyboard protocol. Then, with pain, I detailed about display of key code sequence. This is essential to understand how to connect a keyboard to PIC board. Keyboard reading involves more than just reading a button or IR coding. You heard about "making code" and "braking code"? Display of keys as the first practice follows. This practice is the most important step. Read every line of my explanation and take pain to read every single line of the code. Then we move to the next section, Display of Key in LCD. This time, no computer is involved. Just your PIC board, a keyboard, and your LCD. You build a stand alone system of keying and displaying, which you rush to a hospital room so that your friend who was just rolled back after a several stitches in his mouth can type you what happened. To make this stand-alone system, you may have to burn some oil first. It takes time to fully understand the process. Easier way, though, is just copy the code and download to your PIC. Impressive demonstration comes so easy, which I do not endorse. take pain and get the gain, folks. And find much higher, nobler application for this exciting subject.
 
11. Chapter 11: Voice Synthesizer Project This chapter extends the previous chapter into voice generation from typed key. The main chip I use is DoubleTalk RC8650 Voice Synthesizer. I explain how to use the chip and provide coding example for RC8650. This time, the stand-alone system we built in Chapter 10 has an addition: voice. So the new system consists of PIC board, keyboard, LCD, RC8650, and a speaker. Now you can help hearing impaired people, too. And much more application in mind rushing through, I guess. Coding and diagrams and explanation of each segment of the code, as usual.
12.. Chapter 12: Internal EEPROM Access Inside PIC, there is a small size EEPROM(Electrically Erasable Programmable Red Only Memory). It stores only 256 bytes. This one can be overwritten, indefinitely, and keeps the data even after you turn off your power to PIC and finally get some much needed sleep when a gallon of coffee or tea cannot keep your eyes open. How we hate microcontroller programming, especially under the looming deadline. How to read from and write to the EEPROM is all about the chapter. As practice, I use telephone number storage and retrieval. If you are here, I know you are serious in PIC programming. And the success of your team is on your shoulder. So go out and get some stretch and come back to your desk. Good luck.
13. Chapter 13: CCP Module and PWM Pulse width modulation (PWM) is to generate a pulse of two different parameters: period and duty rate. Period of a pulse is, period of a pulse, which composed of high duration and low duration. The high duration in a pulse period is called duty rate. In other words, you can make a pulse of 1 ms period. Out of 1ms, if you want to have 0.5ms high followed by 0.5 ms low, then your duty rate is 50%. Duty rate of 60% in 0.1 ms period will have 0.06 ms high duration followed by 0.04 ms low duration. PWM module of PIC allows you to generate a pulse with two parameters. Your job is to provide the two parameters in the terms the PIC and the PWM module understand. So the chapter talks just about that with sample codes. Whatever the application may be, motor control, voltage control, etc, you just provide the output pulse to the guy. By the way there are two PWM pins to be used. That means you can control two things at the same time.
 
14. Chapter 14: SSP Module and I2C Bus for External EEPROM (24LC16B -16K) Access I2C (Inter-IC) bus is very popular, particularly in home electronics products, TV, VCR, etc. The reason is that connection between processors needs only 2 or 2 lines, unlike 8 or more lines in the traditional internal bus configuration. I first discuss about the I2C bust and its protocol, and I guarantee that you hate this part. Complex and complicated. But I tried to cut and untangle this mess. Then, we apply the I2C bus to a serial EEPROM which connected to PIC. This serial EEPROM should not be confused with the small size internal EEPROM. The serial EEPROM we attach to PIC is 24LC16B which is 16K bit sized. If you have a lot of data to store and play around, this project is worthy of try. But mind you that it's complicated. But just follow my instruction and coding example.
 
15. Chapter 15: Armatron Control Armatron is an old technology, and chances are you have not heard about it. A few decades ago, this one came out with robotic arms on wheel. This chapter explains how to control the Armatron with your PIC alone. Simple relay control command the Armatron to work: forward and reverse move, turn, pick up, etc. An IR receiver and a TV remote allow you to remotely command this antique. A student of mine cruised through this project with no sweat. You can do, too.
 
16. Chapter 16: Digital Control using PC with IR This project is interesting. We still use the subject of IR, but here IR is not just for reception, but transmission too. In other words, there are decoding and encoding of IR command involved using MC145026 (Encoder) and MC145027 (Decoder). Put simply, you can make your own remote controller. This project combines the IR reception and IR transmission via your computer for any simple control application. Another thing introduced here is a Windows based visual basic code for Windows interface for the command of IR. Hardware description, details of complete code, and Visual basic code for Windows programming for serial Communication with 16F877.
 
How to get this Book in electronic format [475 pages,6.5 MB in pdf] ?  
Donation of $20 will get you the electronic book and the entire codes in the book.
Why am I asking for donation of $20?
It is practical. Running this website for free resources and materials for my students (and you) needs about $250 annually. That's my humble target. Any dollar above that is, to me, a Jackpot. So if you happen to have some extra money in your Paypal account, donate $20, so that this free material reaches to every PIC student and does so continually. Thanks for your donation. All $20 donors will receive the book in pdf format and a zipped file of all the pic source codes of the book in their emails. Thanks. *** Please clearly indicate your email address ***
 
Official PayPal Seal
 
Click "Add to Cart" then "View Cart" []
$20.00 Donation
 


   *NOTE:  Some slides of the above chapters may be found in the links:  ucF08, ucF09, and EMBedded

 

MWFTR.COM