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.

interrupts with timer2 pic 18f452

Status
Not open for further replies.

french_student

New Member
Hello,

Does anyone knows how can i programm an interrupts of 7 micro sec with timer 2 for a 18f452?

Thanks.
 
thanks but when i ask a question it means i didn't found the answer reading the datasheet.
So if you have another answer, it 'll really help me.
Thanks.
 
generating an interrupt every 7 µS is very fast you know.
Even when running the pic at maximum speed of 40Mhz one instruction will take 0.1 µS. Wich means only 70 instructions can be executed between interrupts. That's not much... There would be very little you can do with the interrupt...

Why do you need it ?
 
Thanks.

The pic is running with a 20 MHz quartz so one instruction takes 200ns.
I don't need to do many instructions during the interrupt.
i need this interrupt because i use the spi module to generate a clock signal but i need to generate it every 7 us.

Thanks.
 
At 20Mhz you have only 35 instructions left between interrupts. Since you are using SPI i assume you are communicating with another device. The information sent or received has to be processed, am i right? you want to do something with it... well, with only 35 instructions there is really about NOTHING you can do with it, context saving for the interrupt alone takes about 8 instructions leaving only 27 left!

You say you don't need many instructions between interrupt's, but in the other post i saw you're writing C. Do realize that 1 line of C can be translated to more then 30 Assembly instructions (depending on what c instruction it is), wich already is too much.

I think you're expecting too much from the little pic. 7µS is really too fast.
Why do you need it that fast anyway? what are you trying to communicate with, and what needs to happen with the data ?
 
Oh, I don't think you should write in C codes, why don't you use MPASM for more ease?

You can found in INDEX of the datasheet for Timer 2 and look the way it works.

By the way, you can download High End Range MCU Family Reference for sure.

Using interrupt with high level language is difficult, and it takes more instruction. I don't know what are the problems
 
Lacking sufficient data, your application IMHO looks like a bit weird. There may be better ways to do it. Anyways, generating interrupts at 7 usec rate using TMR2 requires the ff. steps:

1. Initialize T2CON register with the value 04h (TMR2ON, 1:1 prescale& postscale).
2. Initialize the period register PR2 with value 35 (decimal) for 20Mhz operation or 70 (decimal) for 40Mhz operation.
3. Set TMR2IE bit on the PIE1 register.
4. Optionally, set IPEN bit on the RCON register to enable high priority interrupt for Timer2. Low priority interrupts may be used for others.
5. Set TMR2IP bit on the IPR1 register to enable high priority interrupts for Timer 2. No other bits in IPR1 and IPR2 should be set or you might not handle all high priority interrupts.
6. Use the high priority interrupt vector located at address 08H as the starting location for your interupt service routine.
7. You should use the FAST register stack for context saving to reduce the length of the ISR. This is only one level deep and so if you are using more than one interrupt, IPEN should be set and other interrupts should use low priority. To use the fast register stack, use the instruction "RETFIE FAST".
8. Finally, set both GIE and PEIE bits of the INTCON register to globally enable interrupts.

N.B. You must write the ISR code in assembly language because you only have 35 instruction steps @20Mhz to do it. Using the fast register stack will save you the task of context saving in software but still, this is not much. You should use 40Mhz operation. There is no reason to use 20Mhz unless you were upgrading from the mid-level PICs (PIC16F877s) and you are working to preserve existing code and hardware.
 
Code:
i need this interrupt because i use the spi module to generate a clock signal but i need to generate it every 7 us.

If you look at the SPI block diagram of the PIC18F452 datasheet, the CLK signal is available at the RC3 pin. It can be either input or output.
 
First of all i want to thank everyone for his answers.

Writing in asm will be difficult for me : i don't have lot of time (the project must be finished in last week' beginning) and i don't remember well how we write in asm. So i'll try to make it in C.

A question for motion: where and how can i realise all the steps you gave me to use TMR2 interrupt. In fact to create my file .c i use PIC C W and i supposed that when i configured my project all the steps were realised automatically, but apparently they weren't! So must i write them in c in the main part of my program? What are the codes?

To motion again: i do use the RC3 pin to generate the clock signal. In fact, from what i understood reading the datasheet concerning the spi module, the clock signal is generated when we write something in the buffer SSPBUF. That's what i'm doing and apparently it works, i do have the clock signal with the frequency i programmed. The issue is the time between two successive clock signal (7 us is what i need).

Apparently you all lack explanations of what i'm doing, so i try to be more explicit.
So, to be simple, for my project i need to make an acquisition and conversion of different types of signals (sinus, square, "bits signal" (i don't know the english word),...) with a sufficient sampling frequency in order to have sufficient samples to rebuild the analog signal later. So i use an ADC with good features, the AD7827, which needs a serial communication with a microcontroler. This one is the 18F452. To understand my issue it would be good to look at the datasheet of the AD7827 and the chronogrammes, but i can't post them here! Anyway the fact is that i need to generate a clock signal and send it to the ADC so that this one serialize the data and send them to me (to the 18F452). To have a stable and fast sampling frequency i thought i 'd better use an interrupt. Maybe i'm wrong!

I hope you understand what i say. It's quite hard for me to explain a concrete technical issue in english. I'm french and i try to do my best!

Really really thanks to everyone.
I really want the project to work well!
 
french_student said:
Writing in asm will be difficult for me : i don't have lot of time (the project must be finished in last week' beginning) and i don't remember well how we write in asm. So i'll try to make it in C.

As already mentioned (in this thread, and the other one) it looks very doubtful you can do what you want in 7uS, even writing in assembler - in C you don't have a hope!. Don't forget, it's only the time critical section you would need to write in assembler, the rest of the program could be in C - so at the very most, you only have less than 35 assembler commands to write.

You also mentioned you were only using interrupts because you thought it would be faster - it won't be - you're wasting a considerable number of your 35 commands saving and restoring registers, and even jumping to and returning from the interrupt routine.

So, don't use interrupts, and write it in assembler - not C - you could initially write it in C, them manually optimise the assembler code the compiler produces. Even at that, you may well be struggling to manage it in 7uS, including storing the data.

Is there a specific reason you need to take a reading every 7uS?, it seems a very strange timing to choose - also how many readings do you need?.
 
Writing in asm will be difficult for me : i don't have lot of time (the project must be finished in last week' beginning) and i don't remember well how we write in asm. So i'll try to make it in C.

You should be writing your code in C if you are comfortable with that. However, as I said, the interrupt service routine should be written in assembly to allow you to to control exactly the number of program instructions it will use up. I am not familiar with PIC C because I have been writing code in assembly for PICs but I would think PICC should allow you to mix a small amount of assembly code with C code. The ISR written in assembly could be as short as the ff.

Code:
         ORG 8
ISR:
        BSF     SCLK
        BRA    $+2
        BCF     SCLK
        RETFIE FAST

Only 4 instruction steps! It wasn't painful as you thought it would, was it?

A question for motion: where and how can i realise all the steps you gave me to use TMR2 interrupt. In fact to create my file .c i use PIC C W and i supposed that when i configured my project all the steps were realised automatically, but apparently they weren't! So must i write them in c in the main part of my program? What are the codes?
I already gave the code for the ISR above. I think you should be able to initialize the registers using C.

To motion again: i do use the RC3 pin to generate the clock signal. In fact, from what i understood reading the datasheet concerning the spi module, the clock signal is generated when we write something in the buffer SSPBUF. That's what i'm doing and apparently it works, i do have the clock signal with the frequency i programmed. The issue is the time between two successive clock signal (7 us is what i need).

With a 20Mhz crystal you can't because of the odd timing. However, if operate at 40Mhz you can. If you replace the crystal with a 10Mhz one and select "HS oscillator with PLL enabled/Clock frequency = (4 x FOSC)" in the config register1, you should get a 40Mhz clock. With 40Mhz, you can initialize the period register PR2 with the value 35. This should give you 7us SCLK clock period on SCLK as per datasheet.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top