Log in

View Full Version : Timing issue



gadelhas
- 25th November 2011, 22:12
Hi everyone;

I'm facing a problem with the code below. IT works everything fine, except the timing. It takes always the double of the time in the "pause" instructuion. For instance, if i put PAUSE 5000, it takes 10s to execute.
The micro is 12F683 and is powered by a CR2032 battery.

Can some one help me? Thanks!!!



@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _FCMEN_OFF & _IESO_OFF
DEFINE OSC 4
'76543210
OSCCON = %01100111

'===================SRF AND PINOUT CONFIGURATION - 1-In / 0-Out=================
'76543210
TRISIO = %00000001
GPIO = %00000000
IOC = %00000001
WPU = %00000001
INTCON = %00001000
CMCON0 = 7
VRCON = 0
ANSEL = %00000000
OPTION_REG.7=0
'==================================ALIAS========== ==============================
BOTAO VAR GPIO.0
VAZIO1 VAR GPIO.1
VAZIO2 VAR GPIO.2
VAZIO3 VAR GPIO.3
VAZIO4 VAR GPIO.4
LED vAR GPIO.5
'================================MAIN LOOP======================================
Main:
@ SLEEP
INTCON.0 = 0
High led
Pause 5000
low led
GOTO Main
END

gadelhas
- 25th November 2011, 22:25
HI again;

One more thing that i notice is that when i power the circuit for the first time, it always turn on the led, however, the first instruction is "sleep", but it is not executed, but only when i power the circuit, after that it works fine, like it should work!

Charles Linquis
- 26th November 2011, 05:55
You should confirm that the device is really running at 4Mhz. Perhaps it is running at 2. You often have to set up the OSCCON register. Check the datasheet.
Also, the interrupt is triggered right away beause you have not cleared the interrupt flag BEFORE enabling the interrupt (usually PIR.x, but again, check your device).

gadelhas
- 26th November 2011, 16:16
You should confirm that the device is really running at 4Mhz. Perhaps it is running at 2. You often have to set up the OSCCON register. Check the datasheet.
Also, the interrupt is triggered right away beause you have not cleared the interrupt flag BEFORE enabling the interrupt (usually PIR.x, but again, check your device).

Hi Charles;

First of all, thanks for your answer.

If you look at my code, you can see that the OSCCON is configured correctly. I allready found the time issue. The pic was actually running at 4Mhz, however the main code was executed 2 times. To resolve this problem, i change this code:



Main:
@ SLEEP
INTCON.0 = 0
High led
Pause 5000
low led
GOTO Main


With this, just changed one line;



Main:
@ SLEEP
High led
Pause 5000
low led
INTCON.0 = 0
GOTO Main


Now the main code is only executed one time and them goes to sleep.

However, on power up the led still turns on, but it shoul not, because the first instruction is sleep. I follow your advice of clearing the interrupt flag before, however the behavior is the same.
I'm still try to figure it out. If someone has any idea please let me know.

Thanks

mackrackit
- 27th November 2011, 02:00
It should not make a difference, but it might... Bits 0 - 3 in the OSCCON are status bits. Try setting OSCCON = %01100000

You have the LED pin TRISed correctly in the setup but you did not make it LOW in the setup.
TRIS sets direction, the state should also be set.

gadelhas
- 27th November 2011, 02:08
It should not make a difference, but it might... Bits 0 - 3 in the OSCCON are status bits. Try setting OSCCON = 100000

You have the LED pin TRISed correctly in the setup but you did not make it LOW in the setup.
TRIS sets direction, the state should also be set.

Already tried to put the OSCCON like you said. It is the same thing. like you said, they are status bits.

You can see on my code, after the TRIS instruction, that i put all the GPIO low, with:



'76543210
TRISIO = %00000001
GPIO = %00000000

mackrackit
- 27th November 2011, 02:21
Sorry, I missed the GPIO setting.

Acetronics2
- 27th November 2011, 09:53
Hi,

Three things ...

1) you use the IOC interrupt ... so that rings the " Mismatch condition " bell in my head ... see Sleep datasheet chapter.

2) Microchip recommends to place a NOP instruction just past the Sleep one, when using the feature this way.

3) when you push a button ... you obviously release it one day ! makes two input changes ...

Alain

bogdan
- 29th November 2011, 01:32
2) Microchip recommends to place a NOP instruction just past the Sleep one, when using the feature this way

try to add

@ sleep
@ nop


If the GIE
bit is clear (disabled), the device continues execution at
the instruction after the
SLEEP instruction. If the GIE bit
is set (enabled), the device executes the instruction
after the
SLEEP instruction, then branches to the interrupt
address (0004h). In cases where the execution of
the instruction following
SLEEP is not desirable, the
user should have a
NOP after the SLEEP instruction.

gadelhas
- 29th November 2011, 14:13
Hi All, and thanks for your answers!!!


Sorry, I missed the GPIO setting.

No problem Dave!!!



Hi,

Three things ...

1) you use the IOC interrupt ... so that rings the " Mismatch condition " bell in my head ... see Sleep datasheet chapter.

2) Microchip recommends to place a NOP instruction just past the Sleep one, when using the feature this way.

3) when you push a button ... you obviously release it one day ! makes two input changes ...

Alain

Alain; I read the datasheet but couldn't get anywhere, believe me, I tried.


try to add

@ sleep
@ nop

bogdan, tried also that, but its not the solution for my problem.

Ok, the only conclusion that i came up, is this:

If i disable the WPU on the button pin, put a pull-down resistor in this pin and connecting the VCC to the button, i don't get the issue. I only get the issue, if i take of the pull down resistor, enable the WPU and connect the GND to the button.
So the conclusion that i came up, not knowing it it is the correct one, is that, on power up the GPIO state is LOW. So because i have the WPU enable on this pin, i think a interrupt is triggered, because i have a mistmach condition on this pin. If i disable the WPU, put a pull-down resistor in this pin and connecting the VCC to the button, i don't have the mistmatch condition, so the PIC goes to sleep.

If this conclusion is not the correct one, i don't know, why the micro is not going to sleep on power up!!!