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.

Timerinterupt with 16F628A and Oshonsoft

Status
Not open for further replies.

revave

Member
I need some help to create an interupt for every second.
Therfore i want to use an internal timer (TMR0 or TMR1).
The hardware consists: 16F628A microcontroller, external crystal 8MHz.
Used software: Oshonsoft PIC Simulator

Ho do I program the internal timer so that every second an interupt appears?

And how to calculate presets if anaother crystal wil be used?
 
hi,
Are you using Basic or Assembler in Oshonsoft.??
 
Hi,

I am using the Basic-compiler..

Hi,
With a 8Mhz crystal , the internal clock period will be 0.5uSec

Using Timer 1 [16bit], set the prescaler to 8, this will give a clock period of 4uSec.

If you load Timer1 counter with 0000, you will get 65536 counts before it 'rolls' over and generates an interrupt, this will be a total time of 0.262144 Secs.

So make the total time equal to 0.25Secs, you need to load Timer1 counter 65536 - 62500 =3036 decimal or 0x0BDC.

So at the 0.25S interrupt inc or dec a counter, so that at every 4th Interrupt it will be 1 Second interval.

In the initialising section of your program you must enable the Tmr1 and Global interrupts.

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
 

Attachments

  • OneSecIntr1.bas
    606 bytes · Views: 400
Hello Eric,

Thank you very much for your fast reply!

1: I have one question about the calclulation. How is the clock period calculated to 0,5uS with a 8 Mhz crystal?

2: Where do you set the prescaler to 8 in the program?

Below your code with some comment with my questions. I cannot find this instructions in the manual and I'd like to now the meaning of he instructions.

Best regards,
Reijnko Vast


Define CONF_WORD = 0x3f31 'What's the meaning of this instruction and why this value?
AllDigital

Dim intrcnt As Byte

trisb = 0x00
'adcon1 = 0x84 'can be used for using an anolog input??

T1CON = 0x30 '??
T1CON.T1OSCEN = 1 '??
T1CON.TMR1CS = 0 '??
T1CON.TMR1ON = 1 '??
TMR1H = 0x0b 'high byte of countvalue
TMR1L = 0xdc 'low byte of countvalue

INTCON.GIE = 1 '??
INTCON.PEIE = 1 '??

PIE1.TMR1IE = 1 'enable timer 1??
PIR1.TMR1IF = 0 'set countervalue to 0 ??

Enable

loop:
Goto loop

End

On Interrupt 'every 0,25 second
Save System '??

PIR1.TMR1IF = 0 'set countervalue to 0 ??

intrcnt = intrcnt + 1

If intrcnt = 4 Then '= 1 second
intrcnt = 0

Break ''' TEST ONLY,, your 1 second code here
Endif

TMR1H = 0x0b 'load new values to counter
TMR1L = 0xdc 'load new values to counter


Resume
 
Hello Eric,

Thank you very much for your fast reply!

1: I have one question about the calclulation. How is the clock period calculated to 0,5uS with a 8 Mhz crystal?
The PIC divides the frequency of the external crystal by 4, so the 8MHz becomes 2MHZ which has a period of 500nS [ 0.5uS]

2: Where do you set the prescaler to 8 in the program?,,,, see below

Below your code with some comment with my questions. I cannot find this instructions in the manual and I'd like to now the meaning of he instructions.

Best regards,
Reijnko Vast


Define CONF_WORD = 0x3f31 'What's the meaning of this instruction and why this value?
Look at the CONFIGURATION REGISTER in the 16F628A datasheet

AllDigital
This is an Oshonsoft command which sets ADCON so that the PORTA is digital

NOTE: the 628A will initialise to using the Comparators for PORTA, so AllDigital will over-ride

Dim intrcnt As Byte

trisb = 0x00
'adcon1 = 0x84 'can be used for using an analog input??

T1CON = 0x30 '?? sets T1CKPS1 and 0, which sets the pre-scale to 8
T1CON.T1OSCEN = 1 '??
T1CON.TMR1CS = 0 '??
T1CON.TMR1ON = 1 '??
TMR1H = 0x0b 'high byte of countvalue
TMR1L = 0xdc 'low byte of countvalue

INTCON.GIE = 1 '??
INTCON.PEIE = 1 '??

PIE1.TMR1IE = 1 'enable timer 1??
PIR1.TMR1IF = 0 'set countervalue to 0 ??

hi,
The PIC divides the frequency of the external crystal by 4, so the 8MHz becomes 2MHZ which has a period of 500nS [ 0.5uS].

The answers to the other questions you ask are all in the datasheet.
 
Hello,

Thank you.
I've looked to the datasheet and its all clear now.
Thank you very much for your very fast help!!

Reijnko
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top