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.

12f675 Timer Simulation

Status
Not open for further replies.

cybersky

Member
Hi
I have the following code

Code:
'pic12f675 - 1sec timer interupt
TMR0 = 6
INTCON.T0IF = 0
INTCON.T0IE = 0  'enable Timer0 interrupts
INTCON.GIE = True  'enable all un-masked interrupts
OPTION_REG.T0CS = False  'set Timer0 clock source to internal instruction cycle clock
AllDigital
Config GPIO.0 = Output
Config GPIO.1 = Input
Config GPIO.2 = Input
Config GPIO.3 = Input
Config GPIO.4 = Output
Config GPIO.5 = Output

Dim msec As Word  'this willhold the milli seconds
INTCON.T0IE = 1  'enable Timer0 interrupts

main:
If GPIO.3 = 1 Then
   INTCON.T0IE = 0  'enable Timer0 interrupts
     GPIO.4 = 1
   WaitMs 100
INTCON.T0IE = 1  'enable Timer0 interrupts
Endif
If GPIO.2 = 1 Then
   INTCON.T0IE = 0  'enable Timer0 interrupts
     GPIO.4 = 0
   WaitMs 100
INTCON.T0IE = 1  'enable Timer0 interrupts
Endif
Goto main
End  
On Interrupt  'interrupt routine
Save System
msec = msec + 1
If msec = 1000 Then
   msec = 0
   If GPIO.5 = 1 Then
     GPIO.5 = 0
   Else
     GPIO.5 = 1
   Endif
Endif
TMR0 = 6  '131
INTCON.T0IF = 0
Resume

When I press the button connected to gpio.3 the red led on gpio.4 lights up but goes out when I let go of the switch.
The green led connected to gpio.5 stops flashing (like it should because I have a T0IE =0). In simulation the red led remains on. When I am not pressing any buttons the green led flashes once per second.
Any idea why the led does not stay on like I want it to in the code? Am I setting up the interupt correctly. I have chopped and changed my code a few times and I get the flashing led like in the interupt section so maybe the timer setup is not completely crap?
 
How are the buttons connected?
If GPIO.2 is high when button at GPIO.3 is released, the led at GPIO.4 goes off.
The comments on Timer0 interrupt enable are confusing: INTCON.T0IE=0 means disable Timer0 interrupts
 
Hi

GPIO.2 AND GPIO.3 are both connect to ground with pull-down resistor (10k).
Sorry for the confusing comment. The reason for disabling the timer after the button press was detected is because in the
simulation if i pressed the button the code would jump to the "on interupt" section and never come back up to the main loop.
So the code is fine it is just the comment that has you confused?
 
Hi
I am not that versed in pic shorthand. what are you saying? can you provide a short piece of code?
Thanks for the reply.

a quick google -
ANSEL = 0; // Configure AN pins as digital
CMCON = 7; // Turn off the comparators

So how is ANSEL = 0 different to ALL DIGITAL
and I am setting all GPIOs to either input or output. What comparators?
 
Last edited:
I think you have your timing out a little.....

From your text, you are looking for a 1 second timer!!!! As it stands you have a 0.38mS timer..

For 1mS you need 1000 clock ticks... You already pre load your timer to 250, but you have the timer pre-scaler assigned to the watchdog so you are getting no pre-scale... On the simulator the interrupt fires every 250uS..

I want to know why you remarked..

TMR0 = 6 ' 131

If you assigned 6 to the timer it will count from 6 ~ 255 or 249 ticks..
 
Hi Ian
I changed the value after i user TMR0 =131 and the led was flashing very quickly. when I changed it to 6 the flashing seemed slower, more like 1sec.

I have been reading so many things that I had probably confused myself.
I asked for help on this forum for something similar but it was with a 16f874.
This is with 12f675 and I know there are differences with how you reference some registers in oshonsoft?

So is it because the timing is so "fast" that the gpio.4 resets itself when i release the switch?
 
You have a 100mS delay that is too fast... My button presses are at least 250mS.. If your finger is on the button for a quarter of a second the button will be read up to three times...

The crystal in the pic12f675 is 4mhz... that gives a tick cycle of 1uS.... if you set the TMR0 to 6 you have 249 counts.... with the prescaler set to 1:128 you will have 31872 tick cycles before the interrupt.. Therefore the interrupt should fire every 32mS..... If your "msec" gets to 1000... it will flash every 31 seconds....

Are you using an external crystal???
 
Hi
No I am not using an external crystal. You mentioned that I have the prescaler assigned to the watchdog. So is the chip resetting everytime and that is causing the GPIO.4 to switch off when I release the button? Could you perhaps just give me the line or 3 I need to fix in my code please? I have read the datasheet and that makes me want to add in many more lines to set registers but alot of the examples only have toie and toif and the gie.
What this little chip will do for me is check the value of a variable and then check if that value is still true when the counter hits 20. if yes then set gpio.5 = 1.
As far as I can tell the chip is counting the time (a little fast as you pointed out) and it accepts input from the switches. just for some reason the gpio.4 resets when i release the button.
 
Check the config settings... Make sure the watchdog is off.

Set the "OPTION_REG" manually

OPTION_REG = 0x27 ' binary 00100111.


Now!!!! A little bug may be stabbing you in the back... Have you used pickit2 to reprogram the bandgap values and the oscillator setting????

Every time you erase the chip they are erased as well... The chips reactions are odd when they are missing...
The chip is always erased when reprogrammed via pickit2..
 
If the controller is not resetting itself, I can't see any other explanation than that the GPIO.2 is reading high, when it should read low. Check that the pull-down resistor is connected.
Try the program without the second if-block: IF GPIO.2=1 .... ( comment it out )
Then the led at GPIO.4 should be on forever.
 
Hi
I am not using pickit2, I am using winpic800. But I always remember to 1st read the oscal value then i re-enter it manually after I load the hex file. So hopefully that is not an issue. but I bought a new chip and i will be even more meticulous.

I will remove the second if statement and check.
So can we agree on the following:
1) set the option reg and the TMR0 value - This should possibly fix the "error" in the code which is causing some eratic timer condition?
2) even though i have a 10k resistor soldered to gnd the gpio.2 is somehow floating high or could this be a condition of the chip resetting and hence we get a high on gpio.2,
so therefore removing that piece of code should prevent the led from going off?

It seemed very straight forward when I started with this project...

Thanks for the help so far.
 
Hi Ian and jjw (in no particular order)
when I add OPTION_REG=0x27 gpio.1 and 2 shows a high on the simulator. Is this right?
I also added the ANSEL and CMCON but that does not seem to change anything?
 
cybersky said:
I also added the ANSEL and CMCON but that does not seem to change anything?
As you said earlier... ALLDIGITAL does this...

cybersky said:
when I add OPTION_REG=0x27 gpio.1 and 2 shows a high on the simulator. Is this right?
Option reg has nothing to do with output pins...
 
Hi
I will post the new code later, but I used all your suggestions and also went nuts with the datasheet and "set" all the relevant bits on the intcon and option_reg registers.
and made sure I was assigning the prescaler to the tmr0.
All seems well. Maybe we can post it as a code snippet or something.

Thanks for the help
 
Status
Not open for further replies.

Latest threads

Back
Top