Pic Assembly Q

Electronics Computer Programming Q&A
Post Reply
CrazyBillyBob
Posts: 11
Joined: Tue Aug 05, 2008 5:27 pm
Location: Charlotte, NC
Contact:

Pic Assembly Q

Post by CrazyBillyBob » Tue Aug 18, 2009 11:38 pm

Ok I'm working on a project where I have a button, and I want to keep track of how many times it's pushed, even if the output is on. Because each time it's push I need to set an output high for 10sec.
I'm getting hung up on the polling V Interrupt as my first design is using simple loops to poll the switch,de-bounce it and start the 10sec delay ... the problem is that during the 10secs that the output is high, I can't poll the switch... which is bad. I'm sure it's a nooby thing as this my first real project with Pics (did the 12 lessons that comes with the pickit2 demo board I've got)

I'm using the PIC16F887 on the Pickit2 demo board.

At this point I'd really like to stick with the assembly!!

Thanks!
Bruce

psycho
Posts: 388
Joined: Thu Jan 10, 2008 8:13 pm
Location: Northwest Indiana
Contact:

Re: Pic Assembly Q

Post by psycho » Wed Aug 19, 2009 1:01 pm

Well, you could set up a timer to fire an interrupt every 1ms. I am very rusty when it comes to asm because, first, I use C almost always. Also, I use 18F series PICs almost exclusively.

This would be the basic process:

A counter would be used to count the number of ticks that have passed. A tick is 1ms.

Each time the timer interrupt fires, bump up the tick counter.

When the tick counter reaches 1000, 1 second has passed.

Since you want to keep track of multiple button presses and add 10 seconds for each press, you set up a variable so that in your main loop, you do two things with it. First, for each button press, you add 10 (seconds) to it. Also, if the variable is zero, the output is off - otherwise, the output is on.

You will use the timer to decrement the seconds counter, provided it is not zero.

Hope it helps,
Kevin

CrazyBillyBob
Posts: 11
Joined: Tue Aug 05, 2008 5:27 pm
Location: Charlotte, NC
Contact:

Re: Pic Assembly Q

Post by CrazyBillyBob » Wed Aug 19, 2009 2:16 pm

That sounds like a way better method then what I was first trying to do! I'm going to have to do some research into timers to figure out what the prescaler needs to be set at to get a 1ms tick.

This is good stuff!!
I've got a couple 18F's here too... there just not on a nice neat board. I'm not married to the 16F it's just what I had ready to test with. This is the proof of concept when all said and done I really only need 5 I/O pins to do what I want.... So if you have a "better" way on the 18F's let me know!


Thanks Again!!!

CBB

psycho
Posts: 388
Joined: Thu Jan 10, 2008 8:13 pm
Location: Northwest Indiana
Contact:

Re: Pic Assembly Q

Post by psycho » Wed Aug 19, 2009 2:51 pm

Not really "better" so to speak. The 18F's are just more powerful. I do alot with USB and Ethernet - they are my favorite things to work with. If I were you, I would use something other than asm. Don't get me wrong... Asm is a great thing to know. However, you can get faster results with C. The compiler takes care of working with multi-byte numbers (i.e. greater than 255).

And, yes... The first time you work with timers and interrupts, it can be frustrating. But, after a while, you shoukld get used to it.

Kevin

CrazyBillyBob
Posts: 11
Joined: Tue Aug 05, 2008 5:27 pm
Location: Charlotte, NC
Contact:

Re: Pic Assembly Q

Post by CrazyBillyBob » Wed Aug 19, 2009 3:57 pm

At some point in the future I'd love to work with the USB interface and a PIC I've got some Ideas that being able to easily connect to a PC for adjustments, data retrieval, and control would be great but for this project it's over kill.... Plus I've not got the skills just yet to tackle a bigger project Baby steps!! :)

But I am looking at the 18Fs for future use.


CBB

User avatar
philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

Re: Pic Assembly Q

Post by philba » Thu Aug 20, 2009 12:30 pm

The reason to use the 18F PICs is they are much easier to debug with PICKit 2 or 3 .

The timer based approach is a good one. You can even set up the switch polling to use timer interrupts as well. You might want to set your timer to interrupt at 10 mS intervals as you can then use it for both timing and debounce.

CrazyBillyBob
Posts: 11
Joined: Tue Aug 05, 2008 5:27 pm
Location: Charlotte, NC
Contact:

Re: Pic Assembly Q

Post by CrazyBillyBob » Thu Aug 20, 2009 4:07 pm

Speaking of the Timer interrupt does anyone have a good, documented example of one? Microchip seems to note they are used allot, and then assumes you just know how to do it.... I'm not that good. I've got the prescaler figured out, and can load and reset the timer but I stuck on the interrupt part...

psycho
Posts: 388
Joined: Thu Jan 10, 2008 8:13 pm
Location: Northwest Indiana
Contact:

Re: Pic Assembly Q

Post by psycho » Thu Aug 20, 2009 4:52 pm

What are you having a problem with, exactly? You put a jump instruction in the interrupt vector that will jump to your ISR. Then you have to write the ISR. In the ISR, you need to check to see WHAT caused the interrupt. Then, you process whatever you need to, depending on what caused it. Then, you need to clear the interrupt flag (or else the interrupt won't fire again).

I am looking at the 16F877A's datasheet. If you tell me what Timer you are using, I can tell you more.

Kevin

User avatar
philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

Re: Pic Assembly Q

Post by philba » Mon Aug 24, 2009 3:39 pm

Look at various Microchip application notes. They often have source code and many of them use timer interrupts. What you will discover is that there isn't much to it but if you've never done it before, it looks pretty intimidating.

riki
Posts: 6
Joined: Sun Dec 13, 2009 4:34 am
Contact:

Re: Pic Assembly Q

Post by riki » Sun Dec 13, 2009 4:49 am

The reason C is used to program the 18F PIC series is that the compiler generates all the code to initialise the PIC.A task which would be prohibitively time consuming if written in assembler.I still use assembler within the C program,something that some readers may have missed.

The reason I now use the PIC18F series is that they all contain UARTS,PWM etc and much greater memory space.I got a bit fed up bit banging for serial RS232 comms using the PIC16F series.

psycho
Posts: 388
Joined: Thu Jan 10, 2008 8:13 pm
Location: Northwest Indiana
Contact:

Re: Pic Assembly Q

Post by psycho » Sun Dec 13, 2009 11:00 am

The reason C is used to program the 18F PIC series is that the compiler generates all the code to initialise the PIC.A task which would be prohibitively time consuming if written in assembler.I still use assembler within the C program,something that some readers may have missed.
That is just a bonus that is hardly worth mentioning because initializing the PIC is trivial... If you look below (the features of a randomly chosen PIC18 part) you will see why C is used.
CPU
Up to 10 MIPS performance
C compiler optimized RISC architecture
8 x 8 Single Cycle Hardware Multiply
System
Internal oscillator support - 31 kHz to 8 MHz, up to 32 MHz with 4X PLL
Fail-Safe Clock Monitor – allows safe shutdown if clock fails
Watchdog Timer with separate RC oscillator
Wide operating Voltage range; 2.0V to 5.5V
nanoWatt Power Managed Modes
Run, Idle and Sleep modes
Idle mode currents down to 5.8uA typical
Sleep mode currents down to 0.1uA typical
Analog Features
12-bit ADC, 13 channels, 50K samples per second
Programmable Low Voltage Detection Module
Programmable Brown-out Reset module
Two Analog comparators with input multiplexing
Peripherals
Master Synchronous Serial Port supports SPI ™ and I2C™ Master and Slave Mode
EUSART module including LIN bus support
Four Timer Modules
Up to 2 PWM outputs
Up to 2 Capture / Compare
The part that is shown above is the 18f2423. But ALL 18 series parts have a C optimized core.
http://www.microchip.com/wwwproducts/De ... e=en026428

Kevin

riki
Posts: 6
Joined: Sun Dec 13, 2009 4:34 am
Contact:

Re: Pic Assembly Q

Post by riki » Mon Dec 14, 2009 8:58 pm

I also have microchips pdf data sheets on the PIC 18F series.
SO DON'T PREACH TO THE CONVERTED!

As to initialsing the PIC18F is 'trivial' try writting it in assembler

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests