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.

Timing interrupts

Status
Not open for further replies.

camerart

Well-Known Member
Hi,

My limitations are only knowing one type of programming language 'picbasic' and I used Oshonsoft to compile them.

I can only do simple programs, and manage to make interesting little projects.

At the moment I am trying to program a 12F683 chip for a timing project. I'm used to looping 'waitms 100' and counting seconds, but there are timing errors, that need 'adjusting' to make it work.

Could anyone send me examples of counting programs showing timer interrupts please?

Cheers, Camerart.
 
Thanks JJW,

I've copied it for my reference, and can almost read it, but I use a higher level language, this seems to be after the language I use, 'PIC basic' has been compiled. It looks as though I might be able to back convert it? It does show me the architecture as well.

As you can see, I'm still on the baby table:), and too old to get off it.

Cheers, Camerart.
 
This is Oshon basic... Eric posted it
Code:
Define CONF_WORD = 0x3f31

AllDigital

Dim intrcnt As Byte

TRISB = 0x00
'adcon1 = 0x84

T1CON = 0x30
T1CON.T1OSCEN = 1
T1CON.TMR1CS = 0
T1CON.TMR1ON = 1
TMR1H = 0x0b
TMR1L = 0xdc

INTCON.GIE = 1
INTCON.PEIE = 1

PIE1.TMR1IE = 1
PIR1.TMR1IF = 0

Enable
loop:
   Goto loop
End                                              

On Interrupt
   Save System

   PIR1.TMR1IF = 0
   intrcnt = intrcnt + 1

   If intrcnt = 4 Then
       intrcnt = 0
       Break  '''  TEST ONLY,,  your 1 second code here
   Endif

   TMR1H = 0x0b
   TMR1L = 0xdc

Resume
 
Thanks Ian,

This is brilliant for me. for the chip I'm using 12F683 I only had to change 'TRISB = 0x00' to TRISIO = 0x00. for it to simulate and use.

I have now spotted the download.

And thanks to Eric if he's following.

Cheers, Camerart.
 
Hi,

Perhaps I spoke to soon? Although the program will simulate, I hadn't noticed that it wasn't getting to the 'INTERRUPT', only going round the loop.

Here is a program that does go to 'INTERRUPT', and I think it uses TMR0 where the Eric Gibbs program uses TMR1. Is there any reason to use one or the other. This is above my skill level, and all I can do is copy and paste with a bit of data sheet checking, hoping to add the correct words in.

Would someone please run this program, to see if I can use 't' (or anything) in the 'INTERRUPT' routing to keep count. I'm using 12F683, and I will need to modify the program for 16F648A and 16F819 PICs.

Camerart.
Code:
OSCCON = %01100000  'Internal 4MHz osc
ADCON0 = 0  'A/D off
CMCON0 = 7  'Comparators off
ANSEL = 0  'Set all digital
WPU = 0  'Internal pull-ups = off
'TRISIO = %00000100
TRISIO.0 = 0  'PIN 7 as RLED
TRISIO.1 = 0  'PIN 6 as WLED
TRISIO.2 = 0  'PIN 5 as GLED
TRISIO.3 = 1  'PIN 5 as input (IN ONLY)
TRISIO.4 = 0  'PIN 4 as LED
TRISIO.5 = 0

Symbol rled = GP0
Symbol wled = GP1
Symbol gled = GP2
Symbol bled = GP4
Symbol inbutton = GP3
GPIO = %00000000  'All outputs = 0 on boot


Dim t As Word
Dim s As Byte
Dim m As Byte
Dim h As Byte

t = 0
s = 0
m = 0
h = 0

'trisb = 0xff  CHECK>>>>>>>>>>>
TRISIO = 0
OPTION_REG.T0CS = 0  'OPTION_REG = %10000000  'Pull-ups = off, GPIO.2 = I/O, prescaler to Timer1
INTCON.T0IE = 1
Enable
loop:
If t >= 3965 Then  '3965 for 1 second
   s = s + 1
   t = 0
   Toggle rled
Endif
If s >= 60 Then
   m = m + 1
   s = 0
   Toggle wled
Endif
If m >= 60 Then
   h = h + 1
   Toggle gled
Endif

If inbutton = 1 Then
   bled = 1
   Else
     bled = 0
Endif

Goto loop
End  

On Interrupt
Save System
t = t + 1
INTCON.T0IF = 0
Resume
 
Last edited by a moderator:
I have just been thinking!!!! Are you simulating this in Oshonsoft!!!
1 second will take a very long time... it takes about 2 minutes to simulate 31mS..

For the purpose of simulation calculate it for real time.... t=256 ( every interrupt ) the trouble is the code won't be executing fast enough for you to press buttons and such...
 
I have just been thinking!!!! Are you simulating this in Oshonsoft!!!
1 second will take a very long time... it takes about 2 minutes to simulate 31mS..

For the purpose of simulation calculate it for real time.... t=256 ( every interrupt ) the trouble is the code won't be executing fast enough for you to press buttons and such...

Yes, in Oshonsoft.

I can run is fast to see what happens, and 't' doesn't add in 'WATCH VARIABLES'

Did you run the program in post #6? This uses TMR0, and also works for me. If you would now run the Eric Gibbs program after changing 'TRISB = 0x00' to TRISIO = 0x00.' for 12F683, which uses TMR1, please. this doesn't work for me.

Camerart.
 
Yep!! I changed it to the pic12f683... Changed the config to 0x33C4.... Changed TRISB to TRISIO..

I waited for 10 minutes, but the timer was taking forever.... I changed the TMR1H reg to 0xFE so it spills quicker and it fired the interrupt...
 
Yep!! I changed it to the pic12f683... Changed the config to 0x33C4.... Changed TRISB to TRISIO..

I waited for 10 minutes, but the timer was taking forever.... I changed the TMR1H reg to 0xFE so it spills quicker and it fired the interrupt...

I couldn't get it to work. Which TMR1H did you change, the one at the top or the one in the interrupt loop?
Did the 't' count up in 'WATCH VARIABLES'?

Camerart.
 
I might be getting mixed up, I'm exceeding my mental limit!!

Could you try adding t = t +1 where it says break, inside Eric's interrupt loop, please. Then see if the variable 't' increments.

Camerart.
 
Code works for me..

I posted the exact code here.. (I have changed the timings as 1 second id simulation is an age)

Code:
Define CONF_WORD = 0x33c4

AllDigital

Dim intrcnt As Byte
Dim t As Byte
TRISIO = 0x00
'adcon1 = 0x84

T1CON = 0x30
T1CON.T1OSCEN = 1
T1CON.TMR1CS = 0
T1CON.TMR1ON = 1
TMR1H = 0xfe  ' originally 0x0B
TMR1L = 0xdc

INTCON.GIE = 1
INTCON.PEIE = 1

PIE1.TMR1IE = 1
PIR1.TMR1IF = 0

Enable
loop:
Goto loop
End   

On Interrupt
Save System

PIR1.TMR1IF = 0
intrcnt = intrcnt + 1

If intrcnt = 4 Then
     intrcnt = 0
     t = t + 1
     Break  '''  TEST ONLY,,  your 1 second code here
Endif

   TMR1H = 0xfe
TMR1L = 0xdc

Resume
 
Hi Ian,

It works in mine now, not sure why, but I keep learning stuff that might be helping.

The simulator kept stopping after 't'. I've just found out what BREAK means.

Also just found out why you changed TMR1H to only one bit left.

Thanks for your help. Keep watching, because now I need to add a little bit more to make it into a working program, that I can store in my 'MODULE' folder.

Cheers Camerart.
 
Last edited:
It appeared to be working,i,e, using the timing tracking option, it showed it looping through 'LOOP' and 'GOTO LOOP' as they were both flashing. I watched the TMR1 H and L registers fill up and it tripped into 'INTERRUPT' then looped in there incrementing 't' ok.

As soon as I started padding it out to be a useful program, by putting a LED turn on after 't' was 'something' in the LOOP, and did a simulation. It looped the 'loop' for a while, then once in the 'interrupt' routine, got stuck in there, adding 't's but not going to 'LOOP'.

Any ideas why?

Camerart.
 
Now you are toying on the ridiculus.... You are now setting your timer SOOOO fast the main loop cannot execute..

You will have to set the timer to aleast 30 or 40 to get any processing done in the main loop.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top