Setting interrupts at with 20M2

Pic8581

New Member
Hi

I have some trouble with interupts and ADC setup; (beginner)

Code:
fvrsetup FVR4096       ; set FVR as 4.096V

setint %10000000,%10000000
If I select an 20M2 chip, it complains that it can only do C.0. ... C.5 HW interrupts (but I want to use the internal timer).
I can't find any documentation that the internal Timer would not work with this chip.


On the other had if I change to an 20X2 device, it does not like the fvrsetup FVR4096 command.

Just odd....

Thanks for any help.
 

AllyCat

Senior Member
Hi,

AFAIK, PICaxe M2s do not support any "Timer" function. They do have a "Time" variable which is purely a software feature. The "base" PIC chips do have timer hardware, but it is not directly supported by PICaxe Basic (only by PEEK/POKESFRs).

Furtrhermore, there is a fundamental issue that (M2) interrupts cannot be activated directly by signals generated inside the chip (i.e. by reading the pin). This is because a pin must be configured as either an output or an input at any instant. If you set a pin as an interrupt input, it cannot output any signals!

But if you do create or identify a suitable "Timer" signal, then you only need to connect it externally to one of the Port C input pins.

Personally, I don't use X2s, but I do note that Manual 2 only shows 28 and 40X2 PICaxes supporting the FVRSETUP command, not the 20X2.

Cheers, Alan.
 

inglewoodpete

Senior Member
As noted in the Command reference manual, the Fixed Voltage Reference is available in "some" PICAXE chips, probably as a result of the base PIC (silicon) capability. Notably, not the 20X2.

I am a big fan of the 20X2 and have not missed the FVRSetup feature.

Both the 20M2 and 20X2 have limited "polled" input-state interrupts (Pins C.1 to C.5) - refer to the SetInt command description. The 20X2 also has two hardware, latched, interrupts on port B for capturing very short events.

Within its limitations, the PICAXE is a very capable chip. You will find that there will be little benefit in having a large number of interrupting inputs due to coding overheads.

Could you provide an overview of your project so that forum members can offer suggestions? A solution is often not universal but tailored to the capabilities of a specific chip.
 

Pic8581

New Member
Hi
Thanks for the answers.

I am just looking at the settimer command for internal timers (in manual 2, p223) and it does not mention that the 20M2 has no such thing.

I also though that a read that you always can upgrade from "M' to "X", without loosing functionality ( which seem not to be true for the ADC.


As for my "project": I have no real project, just trying some functions to get familiar with the picaxe.

I have managed so far to attach a 4x20 parallel LCD and can randomly update digits on any position that I am reading our from a pot through
the ADC (kind of a Multimeter function...). My next step was to do a measurement every so many seconds with the internal timer.

I looked at the "calibadc" function but have not figured out yet how to use it as a workaround for the FRVSETUP

Thank again!
 

Pic8581

New Member
So if the M@ does not have any timers, how would I get a repeating task to work, let's say every 1.333 seconds...?
Isn't this a basic thing for a MCU?
 

AllyCat

Senior Member
Hi,

I am just looking at the settimer command for internal timers (in manual 2, p223) and it does not mention that the 20M2 has no such thing.
Yes it does. The top three "footprints" on the left-hand side are blank. Only the "X" footprints are labelled to show that the command is relevant to them.

I also though that a read that you always can upgrade from "M' to "X", without loosing functionality ( which seem not to be true for the ADC.!
The M2s are the most recently-designed chips, so they may have some features not included with some of the earlier chips. I'm afraid you have to read the data quite carefully to see what's available for each device. But some of the features may be available in the "silicon" (hardware) if you don't mind reading the "base" PIC data sheets and using PEEK/POKE SFR commands.

I looked at the "calibadc" function but have not figured out yet how to use it as a workaround for the FRVSETUP
Basically, CALIBADC allows you to calculate the voltage on the supply rail, which is the default "reference" voltage for the ADC (etc.). Then you can calculate the ADC "step size" from the supply voltage instead of the (nominal) FVR voltage, to calculate the actual measured analogue voltage. There are several examples in the "code snippets" section of the forum.

So if the M@ does not have any timers, how would I get a repeating task to work, let's say every 1.333 seconds...?
Isn't this a basic thing for a MCU?
For a simple program, you might just put a PAUSE 1330 inside a loop, together with the task instruction(s). For more complex programs you have the choice of the "time" variable, 20 ms pulses from a servo pin, almost any frequency from a PWM pin, interrupt inputs, SLEEP and NAP commands, or multitasking, etc..

Because PICaxe Basic is High Level language, precise timings are not generally defined, but, for more critical timing situations, there are some threads which give an indication of individual instruction execution times.

Cheers, Alan.
 

Goeytex

Senior Member
So if the M@ does not have any timers, how would I get a repeating task to work, let's say every 1.333 seconds...?
Isn't this a basic thing for a MCU?

The Picaxe 20M2 is a PIC16F1829 with Picaxe Basic Firmware. The PIC16F1829 has 5 timers, however the firmware does not include any high level commands that allow using these timers. You can however configure these timers using the pokesfr command. The timers(s) can be started and stopped by setting/clearing a bit in respective TXCON register and you can read the timer value by peeking the TMRx register.

Timers 0 and 1 natively support interrupt on overflow and timers 2/4/6 natively support "interrupt on match" to the PRx register. However Picaxe Basic does not allow reading writing to the INTCON register (0x0B) with POKESFR to enable hardware interrupts. Attempting to access this register will cause a reset! You should be able to poke the PIEx register to enable the respective TMRxIE bit and then poll the respective flag bit to determine if a timer has overflowed or there is a timer match. None of this is trivial, but it is doable if you have the time and patience.

So the "easiest" way to perform a repetitive task is to use pauses while incrementing a variable within a loop. If you are up to the task you could use pokesfr to configure/use one of the hardware timers, but beware of conflicts with commands that use these timers.

Another option is to use an external timer (555?) or an 08M2 to trigger an external interrupt on the 20M2 with a short pulse every 1.333 seconds.
 
Last edited:

inglewoodpete

Senior Member
The M2 series have a simple timer that ticks away once enabled. The value can be read using your code. There are a few stumbling blocks that need to be avoided when relying on the timer. Note that the value may 'tick' at 1 second, 2 seconds or 0.5 seconds, depending on the chip master clock speed that you configure (Command=SetFreq).

Commands: EnableTime, DisableTime ' Read here
Variable: Time 'Can be set, reset, read or compared (I think it's 16-bits)​

The X2 series have a more comprehensive timer configuration. It can be set up to tick at almost any speed and you can cause interrupts on timer ticks. This is one of the reasons I prefer the X2 series.

Commands: SetTimer, SetIntFlags ' Start here
Variables: TOFlag, Timer
 

Goeytex

Senior Member
So if the M@ does not have any timers, how would I get a repeating task to work, let's say every 1.333 seconds...?
Isn't this a basic thing for a MCU?
Here is one way. Should be accurate to within +- 1ms. Pulsin is only used to wait for a pulse before incrementing the ms counter, so the value of B27 is irrelevant. Change the "interval" constant to 1333 for an interval time of 1.333 seconds

Code:
#picaxe 20M2
#no_Data

Setfreq m32


'************************************************
'*  Connect PinB.1 to PinC.1 Via a 1K resistor                     *
'************************************************

Symbol Interval = 20       '// Change to 1333 for 1.333 sec	
Symbol ms_Timer = W1
  
pwmout pwmdiv64, B.1, 124, 25       '// 1000 Hz 5% duty  

 MAIN_LOOP:
 Do  
     Pulsin c.1, 1, B27      '// B27 is "Don't Care"
     Inc ms_Timer            '// Increment timer by 1 ms
     if ms_Timer >= Interval then gosub Task1
 Loop
 
 
 Task1:
     Sertxd ("Performed Task",cr,lf) 
     '// Perform the task here
      ms_timer = 0      '// Clear the timer or optionally preload with a value that represents the task time.
 
 Return     '// Back to the Main Loop  
 
 END
 
Top