PIC10F222 checking TMR0 - Page 1
Forum section: electronics / general

author: shakalnokturn
author_rank: Super Contributor
timestamp: March 07, 2020, 11:51:10 am
content: This is a newbie question as the last and only previous time I did any ASM was on a PIC16F84 many years ago.Although I know it's a very limited μC, I have settled on the PIC10F222 because of SOT23-6 footprint and I don't need more for the task.I want to do it in assembler because it seems the most adequate for such a small device.I'd like to use TMR0 with highest prescaler clock division and I'm looking for the most efficient way to test when time's-up.Starting timer with a value ≠0 then BTFSS the STATUS, Z flag is one solution, right?What would the best solution be to update the Z flag before testing it: MOVF STATUS, f or MOVF STATUS, w ?Will either do? Is the first likely to stop TMR0 counting? In the second case will the STATUS register be updated or should I BTFSS the W register instead?Thanks

author: StillTrying
author_rank: Super Contributor
timestamp: March 07, 2020, 01:34:52 pm
content: "it's a very limited μC,"Certainly is!ww1.microchip.com/downloads/en/DeviceDoc/40001270F.pdfIthinkthis is all you could do waiting for TMR0 = 0LOOPMOVF TMR0,WBTFSS STATUS,ZGOTO LOOPTMR0 was = 0

author: ggchab
author_rank: Frequent Contributor
timestamp: March 07, 2020, 05:22:54 pm
content: Code:LOOPMOVF TMR0,WBTFSS STATUS,ZGOTO LOOPThere is no guaranty you will ever see TMR0=0 as TMR0 is always incrementing. Let's say it reaches 0 during BTFSS, then it will no longer be 0 when MOVF executes !! Should be ok if prescalar is at least 1:4. Then, you may also use MOVF TMR0,f to not "sacrifice" the content of WREG.With less rudimentary PIC, a flag is automatically set when TMR0 reaches 0

author: cv007
author_rank: Frequent Contributor
timestamp: March 07, 2020, 06:04:20 pm
content: >I want to do it in assembler because it seems the most adequate for such a small device.Nothing wrong with using C, even XC8 will do a decent job of it.This is code for a little remote control I made with a 322-https://github.com/cv007/2button_remote/blob/master/main.cI'm not sure I like all the defines/macros I used, but that is just the way it is in C. The remote has been working for a few years on the same cr2032, so I guess its safe to say its ok. Although it would take a little bit to figure out the code again, I would have a much easier time than trying to figure out asm code after even a short time away from it.You may also want to consider why you are using a 322. I really had no requirement in the above project, and used a 322 just because it was small and was all that I needed. Its still probably not a great idea as there was really no reason not to go for a larger device like a 14 pin 16f. Even though it is easy to switch from one pic to another in the newer devices, and if the 322 has a limitation you run into (flash size, pins) its easy to switch to something larger. But why not just spend the extra 20 cents right away and oversize a little to leave room. Extra pins are always nice where you can leave the icsp pins alone. Extra flash and ram are nice, and harmless if unused. Unless you are going to buy large quantities (you probably are not), forget about the nickles and dimes and go a little larger than you think you need. Nothing wrong with using a 322 though, as you have a minimal datasheet that can be a little less overwhelming.edit-I misread 322 for 222, now I think you should at least go for a 322.Here is 222 in C-Code:#include <xc.h>void main(void) {TRISGPIO = 0b1110; //GP0 outputOPTION = 0b11011111; //timer0 fosc/4, T0CS=0for(;;){while( TMR0 );TMR0 = 256-128; //fosc4/128GP0 = !GP0;}}9: #include <xc.h>10: void main(void) {11: TRISGPIO = 0b1110;01F3 0C0E MOVLW 0xE01F4 0006 TRIS GPIO12: OPTION = 0b11011111; //timer0 fosc/4, T0CS=001F5 0CDF MOVLW 0xDF01F6 0002 OPTION13: for(;;){14: while( TMR0 );01F7 0201 MOVF TMR0, W01F8 0743 BTFSS STATUS, 0x201F9 0BF7 GOTO 0x1F715: TMR0 = 256-128; //fosc4/12801FA 0C80 MOVLW 0x8001FB 0021 MOVWF TMR016: GP0 = !GP0;01FC 0C01 MOVLW 0x101FD 01A6 XORWF GPIO, F17: }01FE 0BF7 GOTO 0x1F718: }Why not let the compiler worry about little details like status flags, how to test/compare values, etc., or at least let it teach you how to do asm (which at that point you will conclude the compiler is better at it).

author: KL27x
author_rank: Super Contributor
timestamp: March 07, 2020, 07:53:09 pm
content: Z check is one way. A carry check is a little more versatile. This is the same thing, but it also triggers on any overflow, even if it's checking too late.this could contain errors. There's a near 50% chance the logic is dyslexic. I never bother trying to remember. But you get the idea.movf TMR0,wsublw user.constantbtfss STATUS,C ;if set, user.constant >= tmr0;if clear, user.constant < tmr0goto overflow.occurredstill.waiting[exit/return]overflow.occurredbsf user.tmr0.IF ;and/or w/e else you want to happen here, unconditionally.;clear tmr0 and/or turn off timer0 and/or your enable bit, or whatnot you have to do[exit/return]

author: shakalnokturn
author_rank: Super Contributor
timestamp: March 08, 2020, 12:13:12 am
content: Thanks for your replies, so if I've read evryone's answers right...EitherMOVF TMR0, ForMOVF TMR0, Wshould update the Z flag, just the first avoids avoids losing whatever may be in W at the time, part of the question was knowing if the MOVF TMR0, F only reads the register in 4 clock cycles or reads it and writes it back leaving a very small chance of the TMR0 zero transition being overwritten by that instruction execution.For low prescaler ratios there is a chance of missing the zero transition because of the asynchronous nature of the test but I should be fine considering I'm going for the highest division ratio.I understand checking the C (carry) flag leaves a lot more chances ofcatchingnot missing the wanted TMR0 event as long as the full 256 counts aren't needed.I realise this is a very odd hardware choice out of all those around, not having an interrupt or even dedicated flag for TMR0 defies the purpose of having a timer.The main reason I'm sticking with this one and assembler is the small package, simple hardware to practice on, having limited resources can push you to optimise the code, I have a little experience with assembler whereas I have none at all with C. (And just can't get used to the syntax.)

author: Mazo
author_rank: Regular Contributor
timestamp: March 08, 2020, 12:36:03 am
content: I have done the same task on a PIC10F202(same single timer no interrupt or flag for overflow).I programmed it using C and to get reliable frequency(aka don't miss any overflows) I had to give it a "breathing space" of around ~10counts.The only other thing the uC was doing was toggling a GPIO pin at a predetermined,hardcoded value(PWM you guessed it).I think the prescaler was set to 1:1,but it's just a vague memory,so don't take it for granted.

author: NorthGuy
author_rank: Super Contributor
timestamp: March 08, 2020, 03:31:57 pm
content: Quote from: shakalnokturn on March 08, 2020, 12:13:12 am322 is the same package and better in every way. I'm not sure, 222 might be couple cents cheaper, but may be not.

author: Lunasix
author_rank: Regular Contributor
timestamp: March 08, 2020, 03:43:31 pm
content: Sometime I use PIC12 in SO8 package for small applications. I also used PIC10 in SOT23 but I prefer PIC12, as debug is not possible with PIC10 without additionnal hardware. I first used ASM but it takes more time to write as with C. Moreover when debug is not possible. Then, I rewrote some programs (and processor was almost full) with XC8 compiler in order to compare and find the winner. Without optimization, my asm was a little better. But with optimization, the compiler win. So, now, I always use C...

author: StillTrying
author_rank: Super Contributor
timestamp: March 11, 2020, 10:12:44 am
content: Quote from: shakalnokturn on March 08, 2020, 12:13:12 amIfMOVF TMR0, Fdoes write to TMR0 there could be other effects such as missing counts and the prescaler being constantly reset, I don't know whether it is or not!"When assigned to the Timer0 module, all instructions writing to the TMR0 register (e.g., CLRF 1,MOVWF 1, BSF 1,x, etc.) will clear the prescaler"Page 27

author: TomS_
author_rank: Frequent Contributor
timestamp: March 15, 2020, 01:04:55 am
content: Quote from: shakalnokturn on March 07, 2020, 11:51:10 amIf you have the ability or can influence a change to the 10F32x then you can have interrupts for the timers and a few other sources, which should make things so much better.Price is essentially no different to the 222, and Microchip even seems to be hinting at considering the 32x over all of the other 10F models on their website.