A little while back I made a passing remark about writing a programming example for Microchip PIC.
Edited by wakibaki - 12/13/12 at 7:05am
A little while back I made a passing remark about writing a programming example for Microchip PIC.
I for one will be following along with great interest mate. I'll not be able to contribute to the topic with knowledgable questions, but I'll wade into the water when I can. I for one hugely appreciate your time and effort to do this Fred.
Really appreciate this as well. Being a computer science major, I have a bit of programming experience, but only rudimentary knowledge of assembly and low level programming. I really wish I had taken more electrical engineering classes for stuff like this.I have 0 knowledge on pic programming. I'll definitely be keeping up with this thread as well :)
Hi, Mike.
Nice to hear from you.
One of the hardest things when talking 'into the blind' like this, is trying to remember the things that I found puzzling and needed to know when I was first learning the subject and making sure that I don't skip over necessary details because I've become so familiar with them.
So just give me a poke if you think that I'm running on a bit too quickly, or, on the other hand, if I start to labour the point a bit and get boring, tell me to get a move on...
Fred
Hi, gambit300, nice to make your acquaintance.
What's in a PIC?
Definitely very interested :-)
haven't spoken hex in years
Thanks for taking this on Fred...we are all ears (or eyes as the case may be...) 
This looks very cool...
It's hard to be a computer user in this day and age, particularly one who is as technologically aware as somebody who builds their own audio gear is likely to be, and not be aware that binary arithmetic is out there.
There are 10 types of people in the world: those who understand binary, and those who don't.
Good treatment of 'base' number systems Fred! As a software developer, I can't think of anything critical to add as you move into to PIC programming.
Here's the circuit for the meter:-
...you shouldn't have any difficulty making out the detail at this scale. This is jumping ahead a bit, but I'll review a lot of the design decisions later. In the meantime you have the circuit to refer to and think about.
The circuit is simple.
Only the vaguest nod is given to signal conditioning in the form of C2, which will smooth the input a bit, but this thread is not about signal conditioning. Most any relatively steady DC voltage will readily be read by the PIC. The input DC resistance of the circuit is 10k.
How is the circuit intended to function?
The program will use the PIC's internal ADC to read the voltage on pin 17. This will produce a 10-bit (0-1023) number. The program will cause the lower 3 (decimal) digits of this number (0-999) to be shown on the display.
Referring to the schematic, J1 is the PIC_ISP_HDR, a 6-pin PCB header (only 5 are used, but Microchip evidently intend to use the sixth at some point in the future). This is the In System Programming interface, it enables us to program the chip without removing it from the PCB. The significant connections are GND, VDD, VPP, ICSPDAT and ICSPCLK.
If you look at the symbol for the 16F690, you will see that the pins are multi-purpose, and have titles like RA0/C1IN+/ICSPDAT/ULPWU. These pins can take on different functions, depending either on the program, which can configure them, or the actions of the hardware programmer (PicKit).
The PicKit supplies a limited amount of power to the circuit via the VDD and GND pins. This is sufficient to permit operation of simple circuits such as this one, and will light up the display without any other power supply connected.
When the PicKit wants to program the chip, it raises VPP to a voltage not encountered in normal usage. This causes pins 18 and 19 to shift into their ICSPCLK and ICSPDAT modes. The programmer sets ICSPDAT either high or low, (5V or 0V), and toggles ICSPCLK. When the ICSPCLK pin toggles, the value in the ICSPDAT pin is transferred into the first bit programming location. The programmer sets the ICSPDAT pin to the next value to be stored and toggles the clock. This process is repeated until the entire program has been shifted into the PIC. The exact details of this process are of little concern to us at the moment, suffice it to say that the whole process takes a minute or two at the most.
J3 is the sense input. The voltage to be displayed is connected to this input. It is important to observe correct polarity when connecting to this input, the circuit as designed has no capacity to correct for an inverted connection in the way that a digital multimeter can. The voltage is applied to a preset pot. which allows us to calibrate the system so that an appropriate number is displayed. The wiper of the pot is connected to pin 17 (RA2/AN2/T0CKI/INT/C1OUT). The important designator in this case is AN2, this indicates that this pin can act as an analog input to the built-in analog to digital converter (ADC). Any one of 12 pins can be connected to the internal ADC, I simply chose the lowest numbered that was not being used for another purpose. We could actually use pin 19, AN0, with some precautions, because the programmer will release this pin for other functions once the chip is programmed, but I thought it simpler for the purposes of the tutorial not to deal with the issues involved.
Each 7-segment display comprises 8 LEDs, 7 bars and the decimal point. These are connected via 470R resistors to the pins for RC0 thru RC7. RC means port (or register) C, which is byte-wide. Not all the ports on the 16F690 are a full byte. The resistors simply provide a little current limiting, the circuit will probably function without them. The 16F690 outputs are designed to provide (or accept, input, sink) sufficient current to operate an LED. Not all PICs have such current capacity, you must read the datasheet to find such details. The bars of the display are named (by convention) A, B, C, D, E, F, G starting at the topmost bar, A, and working round clockwise to F, with the remaining horizontal bar being G. The decimal point is called DP. Again you can identify which pin corresponds to which bar (A, B, C) by reading the datasheet for the display in question.
7-segment displays come in 2 types, common anode and common cathode. This indicates whether the common connection goes to the positive rail or the negative rail respectively. In the case of multiplexed displays such as these (3 digits in a package) the common terminal is called D1, D2 or D3, for each of the digits, left to right.
D1, D2 and D3 are connected to the positive supply via the 3 transistors, Q1, 2 and 3 respectively. This is because, although a PIC pin can source sufficient current for one display segment, up to 8 segments of any digit may be turned on simultaneously and the transistor is necessary to pass this much current without a problem. Each of the transistors can be turned on by driving RB4, RB5 or RB6 LOW.
So in order to turn on segment A of digit 3, pin 16 (RC0) and pin 11 (RB6) must both be driven LOW. The digits and segments are said to be ACTIVE LOW. This is a consequence of using a common-anode-type display. The display could be organised with the digits and segments ACTIVE HIGH if a common cathode display and NPN transistors were used, the circuit simply reflects what inventory I had in stock. The display is blanked (dark) when RB4,5 & 6 are HIGH OR when RC0-7 are HIGH.
w
I just finished my digital design course last quarter. I should be able to follow everything in this thread (hopefully). 
As someone who's messed with AVR chips and Arduino boards before, and who's learning semiconductor theory but never had a "real" bottom up course on chips and logic, I'm really enjoying this. Definitely going to keep following!
Nick
What does a microcontroller or microprocessor do?






