« S vs CThe Matrix. »

Expanding the PIC

10/08/08 | by anthony [mail] | Categories: Informative

Most of the code on the side barely scratches the surface of the PIC microcontrollers capabilities. There are tons of functions and features in an average PIC that makes they incredibly powerful and useful devices

Even though the PIC is a 2$ microcontroller at its maximum clock speed it is capable of doing 1 million instructions per second. An average program of sophisticated design only is about 100 or so lines in assembly will be executed 10000 times in a second. This is something you must consider when writing a program.

A good example of this is when asking for user input, you'd want to add a delay routine to account for the physical bouncing of the switch as a switch isn't a perfect on/off device a switches contacts will bounce several times before actually settling to a specific state.

Applications using timers want to have a routine that handles overflows so you have data integrity.

With a microcontroller speed and efficiency is a double edged sword, you want as much speed as possible and you want as much speed as possible both however lead to less data resolution. Slower clunkier routines tend to be more reliable and accurate (not always true) As a programmer you want to be on a middle ground between having reliable data and a quick update time


Interrupts

Interrupts are very crucial parts of taking advantage of a PIC microcontroller. An interrupt in a nutshell is a special case where the PIC will stop what its doing and go to the interrupt routine, after its done with that it goes back to where it came from and waits for the next one to trigger.

This makes software incredibly efficient as you don't have to keep checking the state of something hoping that you happen to catch it while its changing, instead you just enable it as an interrupt and have it execute code relative to its function.

There are several standard interrupts available on the PIC MCU as well as several special to each device.

All interrupt related bits are held in the INTCON register which is accessible through all banks

(For PIC16F690)

Bit 7 is the global interrupt enable bit, this is the bit that controls weather interrupts are enabled are not, you set this high when you want to use interrupts.

Bit 6 enables or disables the extra peripheral interrupts, this doesn't need to be enabled unless using the extra interrupts

Bit 5 enables the Timer 0 module overflow interrupt. Most all PICs have atleast one timer module, Timer 0 is an 8 bit module thus it overflows at a max value of 255. When it does this interupt triggers

Bit 4 is the RA2 interrupt pin, this triggers when RA2 is set high (as in a button press)

Bit 3 is the RA/RB change interrupt bit, this is for setting up the Interrupt on Change feature of PORTA and PORTB, after this is set you need to go to the register IOCA and IOCB for PORTA and PORTB to choose what pins trigger the interrupt

Bit 2 is the flag for the Timer 0 overflow interrupt, this flag is high when the interrupt occurs.

Bit 1 is the RA2 interrupt flag, this is set when the RA2 interrupt occurs

Bit 0 is the RA/B change interrupt flag, set when the RA/B change interrupt occurs.

Where does an interrupt go when it is triggered?

The org function tells the compiler where to place the code in memory, 0x00 is the beginning of the programming registers and 0x04 is where interrupts are stored.

An example:

org 0x00
goto PORTS
org 0x04
"Place interrupt here"
retfie
PORTS:
do work
MAIN:
do work

If you have your main function set up to loop, the program will go to the main function and loop until an interrupt is triggered. To exit an interrupt we use the retfie function which means return from interrupt

Things to consider:

-When you are working in an interrupt you WANT to save the w reg that was in the main loop before the interrupt was triggered. You do this by loading w into a temporary register you declared earlier.
- Save the STATUS register, the interrupt will change STATUS thus it is best to save it to a temporary value. However when restoring status you want to swap nibbles twice using swapf as to not set any flags by mistake.
- TURN OFF GLOBAL INTERRUPTS, you don't want another interrupt triggering so its best to turn off the global interrupt while in the actual interrupt until your done with the routine
- Clear all interrupt flags, clear the flags and if your using more than one type of interrupt use the flags to see which interrupt occurred *btfss/c*
- Restore w

Interrupts can be extremely powerful tools when used correctly. They are essential if you ever want to seriously start making effective and efficient programs. More to come on how to get the most out of your PIC coming up in the next few days so stay tuned!

Permalink

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Array

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Array
May 2012
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Ads by Google

This blog is dedicated to working with digital circuitry and the use of microcontrollers, small compact computers on a chip. I will be encompassing many techniques to develop projects, tools to use to write and assemble code and i will be sharing any projects i am currently working on. User feedback is a must! I do not know it all, hell im not even that experienced, but without a general place to get all the info needed i find it very hard to get into the world of microcontrollers without pursing a CE degree. So come one come all and enter the world of mystery and creativity!

Search

XML Feeds

powered by b2evolution