Dicas de 12F675.pdf

22
 Ian Burn January 31, 2005 V5.0 1 PICadilly 12F675 “A Dilly of a Wee PIC” by Ian Burn Contents: 1. Introduction Page 1  2. What is the 12F675 ? Page 1  3. Front-end setup Page 2  4. Blink! Page 3 - 4  5. Making Music Page 5 - 6  6. In Circuit Serial Programming (ICSP) Page 7- 9  7. Analog to Digital Converters (ADCs) Page 10-14  8. Sleeping Page 15  9. Interrupt on Change Page 16-20 10. Lost Calibration Code Page 21 11. Feedback and Acknowledgements Page 22 1. Introduction I have been using the PIC12F675 for the past three years. During that time I have made many mistakes, sometimes more than once. These notes were prepared to assis t me in future applications using this small but mighty microcontroller. Hopefully you will also f ind them useful. Be sure to check the PIC12F629/12F675 datasheet at Microchip for detailed information. (See: www.microchip.com) All of my source code is written for Micro Engineering Labs’ PIC Basic Pro (PBP) Compiler and the chip is programmed using MELab’s EPIC Plus Programmer. 2. What is the 12F675? It in a very versatile 8 pin microcontroller of the Microchip PIC. Some of the features include:  1024 x 14 bit words of programmable memory  64 x 8 bytes of data memory  128 x 8 bytes of EEPROM data memory  A built-in RC oscillator operating at 4 MHz (no resonator or crystal required)  5 ports that can be either inputs or outputs  1 port that can be an input or the MCLR (master clear pin)  4 ports can be used as Analog to Digital Converters (ADCs)  ADC reference voltage can be set or the supply voltage used  Operates from 2.0 to 5.5 VDC with a low current - very low SLEEP current  Each port can sink/source 25 mA up to a total of 125 mA  Individual programmable weak pull-up resistors for each port  Price - about $2 each The only serious limitations are the number of ports and amount of memory.

Transcript of Dicas de 12F675.pdf

  • Ian Burn January 31, 2005 V5.01PICadilly 12F675A Dilly of a Wee PICby Ian BurnContents: 1. Introduction Page 1 2. What is the 12F675 ? Page 1 3. Front-end setup Page 2 4. Blink! Page 3 - 4 5. Making Music Page 5 - 6 6. In Circuit Serial Programming (ICSP) Page 7- 9 7. Analog to Digital Converters (ADCs) Page 10-14 8. Sleeping Page 15 9. Interrupt on Change Page 16-2010. Lost Calibration Code Page 2111. Feedback and Acknowledgements Page 221. IntroductionI have been using the PIC12F675 for the past three years. During that time I have made manymistakes, sometimes more than once. These notes were prepared to assist me in future applicationsusing this small but mighty microcontroller. Hopefully you will also find them useful.Be sure to check the PIC12F629/12F675 datasheet at Microchip for detailed information.(See: www.microchip.com)All of my source code is written for Micro Engineering Labs PIC Basic Pro (PBP) Compiler and thechip is programmed using MELabs EPIC Plus Programmer.2. What is the 12F675?It in a very versatile 8 pin microcontroller of the Microchip PIC. Some of the features include: 1024 x 14 bit words of programmable memory 64 x 8 bytes of data memory 128 x 8 bytes of EEPROM data memory A built-in RC oscillator operating at 4 MHz (no resonator or crystal required) 5 ports that can be either inputs or outputs 1 port that can be an input or the MCLR (master clear pin) 4 ports can be used as Analog to Digital Converters (ADCs) ADC reference voltage can be set or the supply voltage used Operates from 2.0 to 5.5 VDC with a low current - very low SLEEP current Each port can sink/source 25 mA up to a total of 125 mA Individual programmable weak pull-up resistors for each port Price - about $2 eachThe only serious limitations are the number of ports and amount of memory.

  • Ian Bur 12F675 Pin-outVdd = +2.0 to 5.5 VDCVss = power groundGP0 to GP5 are the six GPIO ports with all but GPIO.3 (pin 4) being either inputs or outputs. GPIO.3 can be either an input port or MCLR port.3. Front-End SetupAs with most PICs, a number of initial setting must be placed at the beginning of the program.With some PICs, such as the 16F84, one can write a simple program to blink a LED by using all thedefault settings and no front-end statements are required. Not so with the 12F675.The following lines of code are used in many of my 12F675 projects and are described as follows:ansel=0 Sets the ADC inputs as digital when not using the ADCs.cmcon=7 Turns the comparator OFF when using ports digitallyThese two statements are critical to digital operation such as the flashing LEDs. Next one would normally configure the GPIO ports as either inputs or outputs. GPIO.3 (pin 4) is theMCRL pin or can be used as an input BUT not as an output. If used as an input, MCLR must beturned OFF in the configuration line below. If it is turned ON, it must have an external pull-up resistorof 4.7k to 10k. You would also likely want a reset button connecting the MCLR pin to ground (Vss)via a 100 ohm resistor.TRISIO =%000000 is the binary equivalent of TRISIO=0 which sets all ports as outputsThe Compiler seems to sense that GPIO.3 must be an input and adjusts for this incorrect code.If all the ports except GPIO.3 are going to be outputs, a preferred code would be:TRISIO=%001000 which is the binary equivalent of TRISIO=8 If all ports except GPIO.0 are inputs, for example, you would use eithern January 31, 2005 V5.02TRISIO=%111110 or TRISIO=62

  • Ian Burn Setting the 12F675 configuration codes (sometimes called fuses) can be done at the programmer or,for most settings, by way of an @DEVICE statement in the code. For example:@ Device PIC12F675,WDT_ON,PWRT_ON,PROTECT_OFF,MCLR_ON,BOD_OFF This defines the PIC as a 12F675 The watchdog timer (WDT) is turned ON (usually only OFF if using the @SLEEP command asthe WDT keeps waking up the PIC) Power-on reset (PWRT) is turned ON Code protect (PROTECT) is turned OFF MCLR is turned on so an external pull-up resistor is needed Brown out detect (BOD) is turned OFF (this reduces sleep current)Other front-end statements will be introduced as required.4. Blink!The standard test for successfully programming any PIC is to blink a LED.This circuit uses three AA batteriesfor a 4.5 volt supply. A 0.1 mfd capacitor is connected between pins 1 and 8 as close as possible to the pins. DONT omit this capacitor!The MCLR pin is pulled high via the10k resistor and can be pulled low toreset the PIC with the button and the 100 ohm resistor. Microchip recommend the 100 ohm resistor on the MCLR pin to Vss.The LED is connected between GPIO.0and Vss via a 1 k resistor resulting inabout 3 mA of LED current. the maximum allowable current is 25 mA.The LED could be reversed andconnected to the Vdd (+4.5V) in whichcase the LED would come on when GPIO.0 went LOW. BAT1 4V5C10.1+ January 31, 2005 V5.03 Blinking LED with RESETGP1/AN1 6GP0/AN0 7Vss/Gnd 8Vdd/+1 GP52 GP4/AN33 GP2/AN2 5GP3/MCLR4 U1PIC12F675 D2LEDR2 1kR110k SW1 R3 100

  • Ian Burn 4The PIC BasicPro code to blink the LED when using the MCLR RESET is as follows: Blinking a LED with a PIC 12F675 January 25, 2005 The LED will blink 10 times then turn off until the PIC is resetFront-end setup@ Device PIC12F675,WDT_ON,PWRT_ON,PROTECT_OFF,MCLR_ON,BOD_OFFthis statement is explained under item 3. aboveANSEL=0 Sets the ADC inputs as digital when not using the ADCs.CMCON=7 Turns the comparator OFF when using ports digitallyTRISIO=8 All ports are outputs except MCLR - if a port is not used, set it as an outputDefine VariablesLED VAR GPIO.0 sets the GPIO.0 port as LEDx VAR BYTE x is a counter and will not have a value larger than 255Set initial valuesLED=0 ensures that LED is off at the beginn ngProgramFOR x = 1 to 10 Program will repeat statements betwLED = 1 Turns on LED. Could also use HIGPAUSE 500 Pause 500 millisecondsLED = 0 Turns off LEDPAUSE 500 Pause 500 millisecondsNEXT x if x is not = 10, returns to FOR x = 1END ends the program. The reset button The same result would occur if the MCLR was turnedGPIO.3 then becomes an input port.I connect the GPIO.3 port to ground or to Vdd if I amnoise and prevents a deep SLEEP if using this commThe configuration line would become: @ Device PIC12F675,WDT_ON,PWRT_ON,PROTECT_OFF,The LED will only blink the 10 times and cannot be reset except by removing the power.Once the power is removed, it may be necessaryto discharge C1 to turn off the PIC. To ensurea reset. Blinking LED without RESET i January 31, 2005 V5.0een here and next x 10 timesH LED but uses a bit more memory to 10 statementwill start it at the beginning. OFF and R1, R3 and SW1 were omitted. not using it as in input, otherwise it will pick upand.MCLR_OFF,BOD_OFF GP1/AN1 6 GP0/AN0 7 Vss/Gnd 8 Vdd/+1 GP52 GP4/AN33 GP2/AN2 5 GP3/MCLR4 U2PIC12F675BAT2 4V5 C10.1 D1LEDR2 1k+

  • Ian Burn January 31, 2005 V5.055. Making MusicI have had a lot of fun making music with PICs. This is a case where the 12F675 is a bit limitingbecause of its memory size but one or two tunes are possible.One application was to blink the nose of a reindeer that played the tune, Rudolph the Red NosedReindeer. Others have included musical clocks and door chimes.Although the PIC cannot get the exact audio frequency for musical notes, it can come quite close.The key command used is:SOUND pin, [tone #, tone duration, tone #, tone duration, ..]pin is the port used to put out the sound [I usually define the port such as: MPIN VAR GPIO.1]tone # is a value between 0 and 127 with 0 being silence (127 is about 10,000 Hz)tone duration is the length of the tone in 12 millisecond incrementsThe following tables can be used to equate tone # values to approximate musical notes:Tone # Note Tone # Note 1 Ds2 88 B36 E2 90 C4 Middle C13 F2 92 Cs420 Fs2 94 D426 G2 96 Ds431 Gs2 98 E437 A2 99 F442 As2 101 Fs447 B2 102 G451 C3 104 Gs456 Cs3 105 A460 D3 107 As464 Ds3 108 B467 E3 109 C571 F3 110 Cs574 Fs3 111 D577 G3 112 Ds580 Gs3 113 E583 A3 114 F5 Fs5 missing85 As3 115 G5For example: E2 is the musical note E in the second octave.Ds3 is the musical note D sharp in the third octave.To convert this information into a musical tune takes a bit of patience.

  • Ian Burn I did find one problem when using the 12F675 for music.When using the 12F675s internal oscillator (may not be significant) and operating at lower voltages (3to 4.5) distortion of the higher notes can occur if the coupling capacitor is 10 mfd. This may also be afunction of the speaker used. By dropping thecap to 3.3 mfd the problem was eliminated and it worked down to 3V.In the Sound Example circuit the 12F675 is normally in SLEEP mode and draws less than 1 microamp. SW1 operates the RESET on the MCLR pin and wakes up the PIC, starting the music. The @SLEEP command is used in thisprogram and is explained later on inSection 8This short tune is Shave and a Haircut.I have programmed a number of full length songs but it does a fair bit of time. A Sound Example'A Sound Example - January 27, 2005The tune, Shave and a Haircut is played and then the 12F675 goes to sleep toconserve the batteriessleep current is less than I microampMCLR reset is used to wake up the 12F675Front-end Seup@ Device PIC12F675,WDT_OFF,PWRT_OFF,PROTECT_OFF,MCLR_ON,BOD_OFF sets configurations ADCON0=0 'no ADC usedANSEL=0 'no ADC input - IMPORTANT if using ports in digital modeCMCON=7 ' turns off comparator function - IMPORTANT if using ports in digital modeTRISIO= 8 Sets GPIO.3 as input, all others as outputs. Equivalent to TRISIO =%001000 Define variablesMPIN VAR GPIO.2 'music pin set as GPIO.2y VAR BYTE adjustable time delay between notesProgramSTART:y=4 set delay between notes = 4 x 12 mSec = 28 mSecSound MPIN, [109,40,0,y,102,20,0,y,102,20,0,y,105,40,0,y,102,20,0,y,0,40,108,40,0,y,109,40,0,y] music@ SLEEP assembler sleep forever command Pause 100 give PIC time to wakeupGOTO STARTEnd171 words GP1/AN1 6GP0/AN0 7Vss/Gnd 8Vdd/+1 GP52 GP4/AN33 GP2/AN2 5GP3/MCLR4 U1PIC12F675 SPEAKER8 OhmsR110k C13.3 mfd3 AA BATTERIES4V5SW1 C20.1R2 100 January 31, 2005 V5.06

  • Ian6. In Circuit Serial Programming (ICSP)I have experimented with this method of programming and have found some things that work andsome that dont. The following is based on experience with the PIC12F675 and MeLabs EPIC PlusProgrammer although I expect most points will be directly transferable to other PICs and otherprogrammers.Advantages of ICSPThe advantage of ICSP in circuit development is that you do not have to continually transfer the PICto the programmer every time you make a software change.What is ICSP?ICSP, as I have been using it, is the normal high voltage programming usually done with the PICmounted in the programmer only in this case it is left in the circuit with the other components attached.Five lines run from the programmer to the PIC and associated circuit. These are:+5VDC (Vdd)Ground (Vss)Data lineClock lineVpp/MCLR programming lineIde cept the 5lin ines to beeaDeTh OCK , DATA andM voltages. Similarlythe HINT: If memory space becomes critical, you can save a few words by usingLED=0 and LED=1 each use 1 word of memoryinstead of:Low LED and High LED each use 3 words of memoryHINT: Constantly moving a PIC between a test circuit breadboard and the chipprogrammer can lead to pin damage. One way to minimize this damage is tomount the PIC in a machine pin socket and move the complete unit back andforth. I know, you do that already!!sign requirementse Microchip data sheet indicates that the external circuitry connected to the CLCLR ports must be done via a resistor so as not to load down the programming capacitance on these ports must be limited for the same reason.ally one would have a jack mounted on the circuit board or breadboard that would aces via a plug and cable connected to the programmer. This allows the programming lsily removed while the program is running. Burn January 31, 2005 V5.07

  • Ian BI have found that R1 and R2 can be as low as 470 ohms thus allowing the driving of LEDs from the CLOCK and DATA ports.If a PIC is used that is set up for an internal MCLR resistor (MCLR OFF), R3 can be omitted unless it is used as an input port.Programmer current limitationsNormally the supply voltage to the breadboard or end product circuit board is removed during the ICSP process. Since the +5VDC from the programmer is feeding the PIC and since this lead on Basic ICSP Connectionsthe PIC is connected to other components,the programmer will have to be able to supply enough current to operate the overall circuit. There areway around this if it is a problem.TestI use Diodeven Vdd (+5V)Vss (gnd)ClockDataMCLR/VppVss (gnd)ClockDataVppVdd (+5V)Connectionsfrom Programmer PICR1 R2 R310k VddTo normal circuitTo normal circuits urn January 31, 2005 V5.08ing ICSP - Things that work and things that dont.d the internal RC oscillator and the internal and turned the MCLR OFF.es D1 and D2 allowed me to have both the programmer and the circuit activated at the same time though I was using 4.5 volts to operate the circuit and 5 volts from the programmer.Testing ICSP with the 12F675Testing ISCP with the 12F675GP1/AN1 6GP0/AN0 7Vss/Gnd 8Vdd/+1 GP52 GP4/AN33 GP2/AN2 5GP3/MCLR4 U1PIC12F675 SPEAKER8 OhmsD1 LED R2 470C1 3.3 mfdC20.1D2DIODE D3DIODE D4LEDD5LEDR1470 R4470 D6 LED R3 470Vcc Data ClockVPPVdd PROGRAMMER+4.5 VDCGND 2k

  • Ian Burn January 31, 2005 V5.09I was able to blink LEDs D1, D4 and D5 and have a tone at the speaker. I was even able to leave theprogrammer connected and send in new programming data to the 12F675 with the circuit under power.But as soon as LED D6 was added I ran into trouble if the programmer was connected and I tried torun the circuit. Within the EPIC Programmer there is an effective resistance of 2K between theCLOCK and DATA lines such that the outputs from the PIC at these two ports were effectivelyconnected together causing the LEDs to interfere with each other. This problem is obviouslyeliminated by disconnecting the programmer lines when the circuit is operating. I also could NOT get the PIC to sleep properly with the programmer connected.I next tried to use the external MCLR resistor (MCLR=ON) and found that I could not run the circuitwith the programmer Vpp/MCLR line left connected. The best practice is to disconnect the programmer once the programming has been completed.Again, a convenient plug/jack would be helpful.

  • Ian Burn 107. Analog to Digital Converters (ADCs)The 12F675 has four ADC inputs, AN0 - AN3 (check the pin numbers on page 2. )The output from the ADC converter will be a number between 0 and 1023 representing the lower andupper voltage limits.The input voltage must not exceed the Vdd-Vss range. The upper voltage limit can be set at Vdd or toa Vref+ voltage less than Vdd. In the example below, the Upper limit of the input is set at Vdd and would give a value of 1023. IfVref+ was used and was set at 1.5 VDC, then 1.5 V would equal a value of 1023 and 0 V = a zeroADC value. When Vref+ is used, it is applied to pin6 (see pin out on Page 3)All ADC inputs must use a common upper limit ofeither Vdd or Vref+. They cannot be set individuallyon the 12F675.The 12F675 does not offer a Vref- option.To use one or more of the four ADC inputs, a number of parameters must be set.Since it is a 10 bit ADC use:DEFINE ADC_BITS 10Use the internal clock and set:DEFINE ADC_CLOCK3 ADC Input at AN3, GPIO.4Sample timing is set by: (Input range Vss to Vdd)DEFINE ADC_SAMPLEUS 50For detailed explanations of these statements,please refer to the Microchip PIC12F629/675Datasheet. Assuming that the Comparator function is not being used, it must be turned off:CMCON=7 It is next necessary to set the ADCON0, ANSEL and TRISIO GP1/AN1 6GP0/AN0 7Vss/Gnd 8Vdd/+1 GP52 GP4/AN33 GP2/AN2 5GP3/MCLR4 U1PIC12F675BAT14V5 C10.1R110k Vss/Gnd 8Vdd/+1 U1BAT1 4V5 C10.1R110kVref+ January 31, 2005 V5.0ADC Input at AN3, GPIO.4 (Input range Vss to Vref+) registers GP1/AN1 6GP0/AN0 7GP52 GP4/AN33 GP2/AN2 5GP3/MCLR4 PIC12F675 Vref+

  • Ian B From Microchips PIC12F629/675 Datasheet - Page 41ADCON selects the output format, the reference voltage and which ADC inputs you wish to use.In the example that follows we want the following:Right justified output therefore BIT 7 = 1Reference voltage at Vdd therefore BIT 6 = 0Bit 5 and 4 are not used and set at 0One ADC input, GPIO.4 is used (AN3) thereforeBIT 3 = 1BIT 2 = 1The ADC input will start right away and always be on thereforeBIT 1 = 1BIT 0 = 1This results in an ADCON0=10001111 for the 8 bits but this must be expressed as a decimal numberwhich is 143, thereforeADCON0=143urn January 31, 2005 V5.011

  • Ian B From Microchips PIC12F629/675 Datasheet - Page 42The ANSEL register is used to set the timing and to set the ADC pin.In the example that follows, timing was set at Fosc/2 (see Microchip Datasheet for details) therefore:BIT 7 = 0 UnimplementedBIT 6 = 0BIT 5 = 0BIT 4 = 0We are selecting GPIO.4 or AN3 for the ADC input line, therefore:BIT 3 = 1BIT 2 = 0BIT 1 = 0BIT 0 = 0ANSEL = 00001000 ANSEL=8urn January 31, 2005 V5.012

  • Ian BurTRISIO Setting the input/output pinsIn our example it is necessary to set all pins as outputs except for GPIO.4 and the MCLR or GPIO.3.GPIO.4 will be the ADC, input pin TRISIO=%011000 or TRISIO=24The previous ADC circuits are fine except it would be useful to have some form of output.The following example will use a variable voltage input into AN3 (GPIO.4) with the range beingbetween ground and the supply voltage, Vss and Vdd. This will provide ADC values from 0 to 1023.The outputs will be a LED that turns on if ADC is above 500 and sound ranging from note values of1 to 120, corresponding to ADC values of 0 to 1023.The MCLR will be turned OFF, making GPIO.3 an input. To prevent noise entering this un-used port,it will be connected to Vss. 3 AA BATTERIES4V5C2n January 31, 2005 V5.013 ADC Example with Audio Frequency Output Proportional to Input VoltageGP1/AN1 6GP0/AN0 7Vss/Gnd 8Vdd/+1 GP52 GP4/AN33 GP2/AN2 5GP3/MCLR4 U1PIC12F675 SPEAKER8 OhmsD1LED R2470C13.3 mfd0.1R110k

  • Ian Burn January 31, 2005 V5.014'ADC Example #1'A variable voltage is applied to AN3, GPIO.4 The range will be Vss to Vdd'The LED will come on if the ADC value equals or exceeds 500'Sound output, a 120 mSec beep, will have a frequency proportional to the input voltage.'A #120 tone is used at the top end as the higher audio frequencies sometimes distort'depending upon the speaker. A 0.001 mfd cap across the speaker may allow use of up to #127'Front-end setup@ Device PIC12F675,WDT_ON,PWRT_ON,PROTECT_OFF,MCLR_OFF,BOD_OFF 'sets configurationADCON0=143 'set AN3 ON ie GPIO.4ANSEL=8 'sets Fosc/2 and GPIO.4 [AN3] as ADC inputCMCON=7 'turns off comparator functionTRISIO=%011000 'sets GPIO.4 as in input and all others as outputs could also use TRISIO=24DEFINE OSCCAL_1K 1 ' Set OSCCAL for 1K device - calibrates the internal oscillator - not really needed hereDEFINE ADC_BITS 10 'ADC settingsDEFINE ADC_CLOCK 3 'ADC settingsDEFINE ADC_SAMPLEUS 50 'ADC settingsDefine variablesRDG VAR WORD 'ADC reading from AN3MPIN VAR GPIO.2 'Music pin output to speakerLED VAR GPIO.0 'LED outputTONE VAR WORD 'tone value for the sound command'Initial settingsLED=0'ProgramSTART:LED=0 reset LED to off at the beginning of each program cycleADCIN 3, RDG 'get voltage reading from AN3 and place ADC value in 'RDG'Pause 100 allow time for ADC calculationIF RDG =>500 Then LED=1 'turn on LED if ADC value is equal to or larger than 500TONE = 1+(rdg/10)*119/102 'converts ADC value to a 'TONE' value of 1 to 120'necessary to divide by 10 to avoid 65535 word limit'ADC of 0 = tone of 1, ADC of 1023 = tone of 120Sound MPIN,[TONE,20] 'provides a tone proportional to input voltage. Duration =20x12=120 mSecGoTo STARTEnd'277 wordsHINT: ADC circuits frequently have a voltage divider across the battery, constantlydrawing current. Depending upon how often ADC readings are needed, you canconserve battery life by supplying the ADC reference voltage from an unused port.Make the port high just prior to taking an ADC reading then return it to low whenfinished. You will still be using the Vdd as reference, not the Vref+ option.For example a thermistor used in a divider to monitor a deepfreeze temperature mayrequire a reading every 10 minutes - no need to be drawing current all the time.

  • Ian8. SleepingMany of my 12F675 circuits operate on 3 AAA or AA batteries. If the circuits operate continuouslythere is often an opportunity to have the PIC SLEEP during a major part of its operating cycle.The typical sleep current for the 12F675 is less than 1 microamp. I am not sure how much less as mymeTh@@A SL65lootheM ters dont go low enough, but the current will be close to the shelf life of the battery!e two SLEEP commands that I use are:SLEEP and SLEEP period Burn January 31, 2005 V5.015SLEEP is an assembler sleep instruction which puts the PIC to sleep until it is awakened by: the watchdog timer because you forgot to turn it off!! an interrupt (eg interrupt on change) a reset of the MCLR if it is used Pause 100 after the @SLEEP command allows time for the PIC to wakeupEEP period is a PBP Compiler statement where period is the number of seconds between 1 and535 - a range of from 1 second to 18.2 hours. Longer periods have to be accomplished by usingps. (eg for x= 1 to 10; SLEEP 64800; next x .= 18 hours x 10 =180 hours). This command uses watchdog timer which must be turned ON, unlike the @SLEEP command.any things can influence the sleep current, including: Brown out detection - turn off to minimize sleep current ie BOD_OFF in the configuration Set all unused ports as outputs. If GPIO.3 is not used as MCLR, connect to either Vss or Vdd Tie the unused ports to Vdd using the internal weak pull-up (see page 19) If using the POT command, set the POT port low before going to sleep, bring high onawakening Turn off the ADC reference voltage by using the statement : VRCON=0 turn off watchdog timer is using @SLEEP ie WDT_OFF in configuration input data must not be changing.HINT: Continually changing data entering an INPUT port will prevent the 12F765from reaching minimum SLEEP current. Depending upon the input circuitry, you mightconvert the port in question to an OUTPUT during the SLEEP part of the program. Besure to change it back to an INPUT on awakening. eg TRISIO.4=0..then..TRISIO.4=1

  • Ian9. Interrupt-on-ChangeA useful way to wake up a sleeping PIC is by using an interrupt-on-change.For example if a battery powered 12F675 is meant to start some sequence of outputs after receiving anincoming pulse, it is likely a good idea for it to be in SLEEP mode until the pulse arrives.The Interrupt-on-Change (IOC) can be set for either a positive or negative going pulse and can beallocated to any GPIO pins by setting the IOCB Register . The GPIE variable must be set in theINCON Register and the falling edge option is set in the OPTION Register. Heres how to do it.First lets assume we have a project wherea switch closure, SW1, causes a LED, D1,to flash 10 times. A very simple objective.GPIO.3 and 4 will be set as inputs andGPIO.3 is not used and will be connected to dd.R2ThbeThwh FUs GP1/AN1 6GP0/AN0 7Vss/Gnd 8Vdd/+1 GP52 GP4/AN33 GP2/AN2 5GP3/MCLR4 U1 BAT1 4V5C1 0.1 R11k+R210kV Burn January 31, 2005 V5.016 will hold GPIO.4 high until SW1 closes.e interrupt on change at GPIO.4 will negative going.e next step is to tell the IOCB Register Interrupt on Changeich ports are using the IOC function.rom Microchips PIC12F629/675 Datasheet - Page 20ing the information above, we will set IOCB= 00010000 or IOCB = 16PIC12F675 D1SW1

  • INext we have to set the Interrupt Control Register, INTCON Question - will SLEEP 65535 wake up with interrupt on change? From Microchips PIC12F629/675 Datasheet - Page 13We arent dealing with a lot of the information here. We only need to enable the GPIE variable. Bit 7 = 0 Must be 0 in this applicationBit 6, 5 and 4 = 0 Not used in this caseBit 3 = 1 Must Enable GPIEBit 2 and 1 = 0 Not used in this caseBit 0 = 0 Initially set at 0 and again just before @SLEEPT erefore INTCON = 00001000 or INTCON = 8 or you could use INTCON.3=1han Burn January 31, 2005 V5.017

  • IOne other key item is to turn off the GPIF, Bit 0, of INTCON just prior to the @SLEEP command.This can be done by using INTCON.0=0'Example of Interrupt on Change January 26, 2005'It will then blink a LED times and go back to sleep From Microchips PIC12F629/675 Datasheet - Page 12Two items of interest are in this register - Bit 6 and Bit 7 both of which need to set at 0by using: OPTIONS_REG.6=0 This set interrupt on the falling edge of the pulse. OPTIONS_REG.7=0 This enable the weak pull up resistors needed to stabilize the unused portsHowever, all the bits could be set at 0 without a problem so we could also use:OPTION_REG=0an Burn January 31, 2005 V5.018

  • Ian urn January 31, 2005 V5.0 From Microchips PIC12F629/675 Datasheet - Page 12GPIO ports 1, 2 and 5 are not used and can be tied to Vdd using the weak pull-up resistorstherefore WPU=%00100110 or WPU = 38After all this nonsense, here is the program!'Example of Interrupt on Change January 26, 2005'A change from Vdd to Vss on GPIO.4 will cause the 12F675 to wake up'It will then blink a LED times and go back to sleep'Front end setup@ Device PIC12F675,WDT_OFF,PWRT_ON,PROTECT_OFF,MCLR_OFF,BOD_OFF 'sets configurationTRISIO=24 'sets GPIO.3 and GPIO.4 as inputs and all others as outputs (or %00011000)CMCON=7 'turns off comparator functionADCON0=0 'no ADC - doesn't seem necessary but may be a good ideaANSEL=0 'no ADC input - IMPORTANT if using ports in digital modeVRCON=0 'turns off voltage reference to minimize sleep currentINTCON=8 Enables port change interrupt ie GPIEIOCB=16 'sets GPIO.4 for Interrupt on changeOPTION_REG.6=0 'INT on falling edge OPTION_REG.7=0 '00000000 WPU enabled,WPU=38 '00100110 = 38 set unused ports 1,2 and 5 tied to weak pull-up resistors'Define Variableswakeup VAR GPIO.4 'input, interrupt on change.LED VAR GPIO.0 'LED output x VAR BYTE'Initial settingsLED=0 B 19

  • Ian Burn January 31, 2005 V5.020'ProgramSTART:INTCON.0=0Pause 100@ SleepPause 100For x= 1 TO 10LED=1Pause 300LED=0Pause 300Next xGoTo startEnd'90 WordsHINT: When building a circuit with a sleep component, removing the power from thecircuit doesnt necessarily turn off the PIC. The charged 0.1 mfd bypass capacitor cankeep a sleeping PIC operating for awhile after the power is removed. To be sure it hasreset, short out Vdd to Vss once the power is removed.The other option is to use the MCLR with a reset button.HINT: Using the internal weak pull-up resistors can eliminate external pull-up resistoron switched inputs.

  • Ian Burn 2110. Lost Calibration codeIn the last flash memory slot of a new 12F675 there is a calibration code that is placed during chipmanufacture. The code has a hex value of 34xx where the xx part varies from chip to chip. This codeis retrieved using the DEFINE OSCCAL_1K 1 statement. The last two hex digits, xx, of the codeare then placed in the OSCCAL register and the chips internal oscillator is adjusted to operate at 4 MHz.If serial data, for example, is not being sent by the 12F675 then an exact 4 MHz may not be necessaryand the DEFINE statement can be omitted. It is possible to erase the calibration code, in which case the DEFINE OSCCAL_1K 1 commandfinds nothing to forward to the OSCCAL register and the chip will not operate.If you do erase the calibration code, you have three options: Use the 12F675 in applications where timing isnt critical and omDEFINE OSCCAL_1K 1 statement. IF you know the last two hex digits of the calibration code you into the OSCCAL register using: OSCCAL=$xxFor example if the code is 34a8, use: OSCCAL=$a8DO NOT use the DEFINE OSCCAL_1K 1 statement as theand then transfer to the OSCCAL register. If you DO NOT know the calibration code (as in my case, with tuse OSSCAL $80 as a starting point. (do not use DEFINE OSCConnect a scope or frequency counter to Pin 3 (GPIO.4/OSC2/CWhen programming the chip, set the Oscillator Configuration toThis must be done within the programmer software not the @DEYou will get a square wave at 1/4 of the oscillators frequency ieHere are my results: OSSCAL= $80 Freq=966 kHz OSSCAL= $90 Freq=1001 kHz OSSCAL= $85 Freq=977 kHz OSSCAL= $89 Freq=991 kHz OSSCAL= $8c Freq=998 kHz OSSCAL= $8d Freq=1000 kHzI have now labeled this PIC as an "8d" and will use OSSCAL= $8d when I need accurate timing!There is obviously some merit in recording the calibration numbchip! January 31, 2005 V5.0it the can insert them directly re is no calibration code to readwo 12F675's!!!)CAL_1K 1 )LKOUT). "INTRC Clockout".VICE codes. 1 MHz on Pin 3INTRC Signaler on the back of each 12F675

  • Ian Burn January 31, 2005 V5.02211. Feedback and AcknowledgementsIf you have found this useful and/or have any suggestions, please let me know:Ian BurnEdmonton, Alberta, [email protected] thanks to Melinda Burn and Angus Cameron in assisting with PDF conversions and editing suggestions.

    1. Introduction2. What is the 12F675?12F675 Pin-out

    3. Front-End Setup4. Blink!5. Making Music6. In Circuit Serial ProgrammingAdvantages of ICSPWhat is ICSP?Design requirementsProgrammer current limitationsTesting ICSP - Things that work and things that dont.

    7. Analog to Digital Converters8. Sleeping9. Interrupt-on-Change10. Lost Calibration code11. Feedback and Acknowledgements