Using Timer 1 Gate hardware to latch a pulse

BillBWann

Member
I recently wanted to connect an 08M2 to the 1 second PPS output from an EN-406 GPS module. The high pulse is only 1uSec long so I thought I’d use the Timer1 Gate logic to latch the pulse as suggested by AllyCat in #10 of this thread.

However, it only seems to latch every second pulse (ie a 2 second interval in my case) and I wonder why? I initially assumed that it was something to do with the flip/flop controlled by T1GTM but that should be bypassed with Alan’s suggested value for T1GCON of $18. I tried various other values for T1GCON just to be sure but I couldn’t ever get it to latch successive pulses.

I hadn’t noticed this problem when I first tried out Alan’s suggestion some time ago but then I’d simply generated the pulse by touching a wire from pin C.4 to 5V to generate the pulse while the 08M2 was sleeping and it seemed to work perfectly. I suspect now that that may have been due to contact bounce generating more than 1 pulse.

I haven’t tried it with any other 08M2 but I have no reason to believe that this 08M2 is damaged in some way.

Has anyone else seen this problem?
 

AllyCat

Senior Member
Hi Bill,

That's interesting; there do seem to be a few "bugs" even in the base PIC silicon, so perhaps you have found (or confirmed) one of them !

I do vaguely remember that sometimes the timer gate results didn't seem to be quite "as (I) expected". I assumed that my interpretation or setting / reading of the register flags wasn't quite correct. There were quite a lot of modes to test. ;)

In my "final" program code, I used the "toggle" mode and polled for a change in the latch status flag. That worked correctly and I felt was probably at least as efficient, because it didn't require any additional writes to the SFR.

Cheers, Alan.
 

hippy

Technical Support
Staff member
Would it not just be easier to use the port's Interrupt-On-Change flag ...

Code:
#Picaxe 08M2

Symbol IOCAP = $F1 ; $391
Symbol IOCAN = $F2 ; $392
Symbol IOCAF = $F3 ; $393

;               543210
PokeSfr IOCAP, %010000
PokeSfr IOCAN, %000000
PokeSfr IOCAF, %000000
Code:
PeekSfr IOCAF, b0
If bit4 = 1 Then
  PokeSfr IOCAF, 0
  SerTxd( "Pin C.4 Triggered", CR, LF )
End If
 

BillBWann

Member
Would it not just be easier to use the port's Interrupt-On-Change flag ...
It certainly seems to be work well - and very flexible too in that it can be used on any input presumably - and having it edge triggered means that you don't need to check for the end of the pulse if you're counting them.

Thanks Hippy. There seems to be no end to the possibilities from these chips.

Are there any restrictions to using this?
 

hippy

Technical Support
Staff member
Are there any restrictions to using this?
That perhaps depends on how you mean; it's a rather wide-sweeping and open question.

Not all PICAXE have Interrupt-On-Change capability on all pins and the bits in SFR will not always correlate one-to-one with particular PICAXE port bits. It may not work on X2 chips which support HINTx pins or may only work when HINTSETUP is not used.
 

BillBWann

Member
... it's a rather wide-sweeping and open question.
Yes, I suppose it was Hippy. It just seemed to be such a useful bit of information that I was surprised that I hadn’t heard it talked about before – or even included in a Picaxe command. So I thought there must have been a downside to using it.

That’s a good point about the port pin mapping. It would be great if someone who knows much more about this than I do, could show us all how to do that for all the M2’s. I assume that no mapping is required for the 08M2.
 

AllyCat

Senior Member
Hi,

I assume that no mapping is required for the 08M2.
Almost. The port on the base PIC chip is actually called Port A so it's the PORTA SFR addresses that you should use. But the pin numbers are the same as the Port B/C (either can be used) on the 08M2. As an aside, I often name the pins B.0, B.1, B.2, C.3 and C.4 because they are then exactly the same as an 14M2.

In fact the "top" 8 pins (i.e. 4 on each side of the package, at the "notch" end) are completely equivalent (at the SFR level) on the 08, 14 and 20 pin M2s. So for things like the Weak Pullup Resistors it is often more convenient (and flexible) to use the WPUA SFR rather than the PULLUP command.

But the SFR ports get "messy" with the 14 and 20 pin chips, because PICaxe remaps them. The 14M2 "base" PIC uses port names A, B and C, with different numbering to the PICaxe, and the 20M2 pin names are different yet again (and to the 14M2). But all the SFR port names.numbers are equivalent (if they exist) between all three chips. The 18M2 of course has a totally different pinout and I believe the Port.pin numbers do (almost) correspond with the SFR port names (PICaxe Port C = SFR Port A).

Cheers, Alan.
 

BillBWann

Member
I'm resurecting an old thread because I now want to set an Interrupt-On-Change flag for an 18M2. My understanding from reading the datasheets is vague at best and I seem to be falling at the first hurdle.

The datasheet says that the INTCON register can be accessed from any bank and its address is 00Bh on bank 0.

Now this address needs to be converted for the pokesfr command and I don't really understand what the manual says for that either.
Code:
M2 parts
As location can only take the value 0-255 the value for M2 locations taken from
the Microchip datasheet are created as follows:
Bit 7-5 Memory Bank $00-$07
Bit4-0 Addresses $0C to $1F on this bank
($00-$0B are invalid and cause instant reset)
e.g. BAUDCON, address 01Fh on bank 3, becomes %011 11111
In #3 of this thread Hippy has the following line of code:-
Code:
Symbol IOCAF = $F3 ; $393
That address happens to be in bank 7 so it seems that Hippy has used that info to form the 3 most significant bits but he hasn't included that information in his comment so I wondered if it could have been gleaned from just the $393 that he did include? Also to get the last 5 least significant bits, he seems to have simply masked those bits from $393.

If I do that for the 18M2, I'd convert 00Bh in bank 0 to $0B which the extract from the manual above says will cause an instant reset.

Does this mean that I can't use the Interrupt on Change flags for the 18M2?

If I can use them and someone has figured it all out, I'd love to hear the details including what pins of the 18M2 I can use it on.
 

BillBWann

Member
Actually, I just realized that I was looking at the wrong register and I probably should be using $396 of bank 7 which will convert OK.

However, if someone has worked through this and can save me wading through this datasheet, it would be great to hear what they found.
 

AllyCat

Senior Member
Hi Bill,

I've not used those flags on the 18M2, but it looks as if they are (only) on PortB, so you need to set SFR $394 (PICaxe's $F4) and/or $395 ($F5) and then READSFR $F6 , flags. Not as complicated as the 20M2 where the PICaxe's ports B and C are mapped onto different pins of the base PIC's Ports A, B and C.

Cheers, Alan.
 

BillBWann

Member
Thanks Alan for your very prompt reply. It’s amazing how obvious it all became after you’ve gave me the answer. It all seemed so difficult for a time there.

Yes, that all worked out exactly as I hoped.

Thanks again.
 

hippy

Technical Support
Staff member
Code:
             .-----.---------.---------.
IOCAF $393 : | 1 1 | 1 0 0 1 | 0 0 1 1 |
             `-----^---------^---------'
               |  |  |     |   | | | |
             .-------------------------.
             | 1 1   1     1 | 0 0 1 1 | -> $F3
             `---------------^---------'
 
Top