Setting Interrupt to 512Hz

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
User avatar
Mantas
Posts: 221
Joined: Tue May 15, 2012 10:32 pm
Location: Klaipeda, Lithuania - North sea, UK
Has thanked: 57 times
Been thanked: 27 times
Contact:

Setting Interrupt to 512Hz

Post by Mantas »

Hello everyone,

Can anyone help me with setting interrupt service routine to run at 512.0Hz? This is for ARM chip on EB031 board. I was trying the settings inside flowcode, but it is either above or bellow the required frequency. I think that it needs a bit of C code to do this, am I right? I need this frequency to sample some sensors for data on I2C.

Best regards,
Mantas
Science is my true religion.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Enamul »

Hi Mantas,
I don't have ARM flowcode that's why make it in FCv5.4 for PIC. You can modify that for ARM. What you need to do you can select any of the timer interrupt like TMR0, then you have to go for pre-scaler which gives interrupt frequency less than your required frequency. Now you just need to tune to the right frequency you are looking for just by adjusting the initial TMR0 value..I am posting here the example code in which you will get 512Hz for clock 19.66MHz.

Clock=19660800Hz, T = 1/19660800, 4*T = 4/19660800, TMR0 normally overflows at 256 (if 8-bit). Using 1:64 pre-scalar the calculation will be 64*4*T*256 = 0.003333333
So the interrupt frequency will be 1/0.00333333 = 300Hz (if you select 19.66MHz as clock and 1:64 pre-scalar you will see 300Hz as interrupt frequency).

But you want to make that 512 Hz. How?
Clock =19660800 Hz, T = 1/19660800, 4*T = 4/19660800, 1:64 pre-scalar 64*4*T = 64*4/19660800
You need to get the value of TMR0 in which it overflows at freq 512. so
512=1/(64*4*T*x) where x is the increment value to cause TMR0 overflow.
x=150 so TMR0 should be initialized with 256-150 = 106 or 0x6A
Attachments
TMR0_512.fcf
(12 KiB) Downloaded 330 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Mantas
Posts: 221
Joined: Tue May 15, 2012 10:32 pm
Location: Klaipeda, Lithuania - North sea, UK
Has thanked: 57 times
Been thanked: 27 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Mantas »

Thanks Enamul,

Those interrupts always make my head hurt :D I was thinking about the same, how to make it overflow at different count. I will check your code, but I think I will have to recalculate the value as the tmr0 for arm is running at 47923200Hz. Can anyone from MM can confirm that calling tmr0 in C block on arm is the same? Because I know that C for arm is a lot different than for PIC.

Best regards,
Mantas
Science is my true religion.

User avatar
Mantas
Posts: 221
Joined: Tue May 15, 2012 10:32 pm
Location: Klaipeda, Lithuania - North sea, UK
Has thanked: 57 times
Been thanked: 27 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Mantas »

One small question, why 4*T? does this have to do something with clock edges? :?
Science is my true religion.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Enamul »

For pic one instruction executes every 4 clock cycles.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Mantas
Posts: 221
Joined: Tue May 15, 2012 10:32 pm
Location: Klaipeda, Lithuania - North sea, UK
Has thanked: 57 times
Been thanked: 27 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Mantas »

What about ARM? any clue where I can find this info?

Best regards,
Mantas
Science is my true religion.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Enamul »

It seems for ARM it requires 3 clock cycles for execution of each instruction..
In Cortex M3, most of the instruction(for example AND instruction) executed by alu unit require 3-clock cycle.2-cycle for fetch and decode. In 3rd posedge clock alu execution unit will sample the data coming from decoder and calculate the result which it has to update in destination register in the same cycle because its 3-clock cycle operation.
http://forums.arm.com/index.php?/topic/ ... lculation/
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Mantas
Posts: 221
Joined: Tue May 15, 2012 10:32 pm
Location: Klaipeda, Lithuania - North sea, UK
Has thanked: 57 times
Been thanked: 27 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Mantas »

Thank You Enamul,

You are as always very helpful! :)

So basically the formula I derived from your post is:

Code: Select all

tmr0=(2^bit_count)-(system_clock/(prescalar*req_freq*clock_exe_cycle))
It would be nice if flowcode would let to choose the required frequency and do this calc automatically, without too much of a head scratching...

Best regards,
Mantas
Science is my true religion.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Enamul »

Thanks for the head into the expression. I will use your expression later on whenever I need to calculate. I would be nice to get this option in Flowcode.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Mantas
Posts: 221
Joined: Tue May 15, 2012 10:32 pm
Location: Klaipeda, Lithuania - North sea, UK
Has thanked: 57 times
Been thanked: 27 times
Contact:

Re: Setting Interrupt to 512Hz

Post by Mantas »

Good morning,

I can confirm that ARM7TDMI executes instruction in 3 cycles. More info can be found here: http://infocenter.arm.com/help/index.js ... CJEHC.html

Best regards,
Mantas
Science is my true religion.

Post Reply