Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Baseline PIC TMR0 Question

Status
Not open for further replies.

throbscottle

Well-Known Member
I've managed to get press or double-press detection of a switch using TMR0. What happens is, the timer is tested periodically during the main loop. When a switch is pressed once, it sets a countdown, which is changed every time the timer overflows a set value. If the switch is pressed again within that time, it counts as a double press. If the counter reaches zero, it counts as another single press.

This is where it gets interesting. When I look at tutorials such as Gooligum, the timer is always tested for a specific value using XOR. This can't work in my case, because the amount of instruction cycles in-between tests can be different depending on which switch was pressed, so the timer can be at various values when it hits the overflow. I've used subtraction instead - if the timer gets over 200 the counter increments.

So the solution works, but the timer can't be accurate. What's a better way?

Code:
; test and read inputs up to here
; tactile press timer
    movlw 0xC8 ; test the timer for double press events
    subwf TMR0,W ; carry will be clear if under 200
    btfss STATUS,C ; carry will be set if it's over 200
    goto CLR_C ; next stage
    movf tact_counter,f ; counter is set when single press occurs
    btfss STATUS,Z ; don't decrement the counter past zero
    decfsz tact_counter,f ; double press timer
    goto CLR_C
    bcf pre_fl ; flag that is set when single press occurs
CLR_C
; start reading tactile data
; loop back
 
I vaguely remember someone wrote a pic programmer using an Arduino. It was probably LVP only but it wouldn't be hard to add the high voltage bit.

Mike.

That's essentially how all the MicroChip programmers work - they have a processor on board (usually either PIC or AVR), and that receives the data via USB, and does the actual programming of the target device.

Basically all programmers do is 'wiggle' lines up and down :D and it's easier to do that using a PIC/AVR than using a modern PC - old PC's were fine, you had hardware access, but since Win3.1 that all changed.

The MPLAB Express boards are quite fun - the programmer is built-in, and use a much higher powered AVR processor to program a fairly low end PIC processor :D

If you wanted to make an Ardunio PIC programmer, you could always use a separate 13V (or whatever) supply to give HV programming, or do like the PICKit programmers do, and use the processor to generate a high voltage supply using PWM, in which case you could make it variable voltage, like the PK's.
 
It's to save/restore values for convenient use as presets. When you press enter on the keypad, the function which does the maths and writes the tuning word to the DDS also makes a copy of the BCD value - originally to provide a 2 stage escape function (eg, in case you pressed it by mistake). However it's also proved very useful in this case.
So, single click, write some saved value to BCD.
Double click, restore the value from copy back to BCD, and save that value instead.
No debouncing required, the PT6315 VFD driver reads switches and provides a variable with the appropriate information.
 
Just tried it with the single-click timer added in. Apart from needing a bit of tweaking, working very nicely!

So this my first PIC program (or any significant program), and is an AD9850 controller, using Bob Weaver's BCD to FTW maths (no intermediate conversion to binary) to work out the FTW, with a 9 digit VFD (because it was the cheapest) driven by a PT6315, which also provides 4 LEDs and more tactile switch inputs than I can shake a stick at.

The spare 9th digit of the display is used to show "messages" indicating what function has been selected. Display is formatted with commas and dp's in the right places, choice of 3 formats. VFD has a cursor so I've used that to select individual digits to change, as an option. Default is whole value entry.

Supports "tuning" via a rotary encoder, sweep mode in 128 or 256 bit steps, sweep can be set as centre frequency+range, or as high and low limits.

3 hard-coded presets, just 1k, 100k and 1M, nothing special, and 3 user settable presets which you guys have been very kindly helping me with :)

So finally, everything works, except one thing - getting the fnorking DDS to accept a setting it's supposed to default to at switch on (400Hz, in honour of its use long ago by the BBC to accompany the white spot when there's no more telly, because it's so annoying apparently). Don't know what I'm doing wrong...

It's taken me a very long time (10 years maybe) because I've only been able to work on it in little bits at a time and a few times had to re-learn what everything I wrote, does, due to the long gap. I'll probably re-visit some parts of it since I've learnt a lot since I started - a few improvements to make no doubt.

Just need to perform some hackery on the AD9850 module because the filter it has is /terrible/!
 
So finally, everything works, except one thing - getting the fnorking DDS to accept a setting it's supposed to default to at switch on (400Hz, in honour of its use long ago by the BBC to accompany the white spot when there's no more telly, because it's so annoying apparently). Don't know what I'm doing wrong...

Set and save the defaults in data EEPROM, and simply read from that as the program starts.
 
There's no problem setting a default value - it's getting it write to the DDS that's the problem. Uses the same routine as if a physical button had been pressed. Seems to be missing out the call.
Think I might have a stray page setting bit.
 
Hi throbscottle,

I have attached some code for a 12F683 (one step up from baseline) that I wrote several years ago. I am sure it would be different today, but rather that just post snippets, I thought you might want to see how I enter a starting frequency.
 

Attachments

  • AD9850 DDS Signal Generator 12F683 v.3.1.asm
    18.4 KB · Views: 240
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top