pulse duration measurement with flowcode

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

alaind
Posts: 1
Joined: Tue Apr 04, 2006 1:56 pm
Contact:

pulse duration measurement with flowcode

Post by alaind »

Hello, I'm a new user of Flowcode.
I would like to measure a pulse duration arriving on a pin of a PIC (16F88 or 16F877A).
Is it possible to do this operation with flowcode ? Can you give me an exemple ?
Thank's a lot.

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

One way to do this would be to enable the timer interrupt at a given frequency (this will depend on the expected range of your pulse length). Within the TIMER0 macro, you can increment a count variable - this will keep track of the length of the pulse.

In the main body of your program, you would continually monitor the pulse input. When it changes state, the value of your count will indicate the length of the pulse. You should then reset your count variable (i.e. set it to zero).

The value of your count will be proportional to the pulse width. Calculating the exact length of the pulse may involve some complicated maths, but it is possible.

If you want a greater resolution, you can read the TMR0 register to get a low-byte value of your counter. You can read this register by using a C code icon with code similar to the following:

Code: Select all

FCV_MYVAR = tmr0;
The value of the TMR0 register will change 256 times faster than the interrupt is called. If you do use TMR0, remember to also set this to zero whenever you reset your counter variable (again, using a C icon).

I hope this info sets you off inthe right direction.

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Will the same C code work for AVR

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: pulse duration measurement with flowcode

Post by Steve »

You should be able to use a similar technique on the AVR. But the names of the registers will be different.

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

What will be the code to read timer0 for atmega32 ? I am using downscaled timer 1 and timer0 for pulse width measurement. I am also loading an attachment of Flowcode which I am developing for phase angle measurement, but it hangs during operation with Flowkit.Flowcode1.fcf_avr[/attachment]
Attachments
Flowcode1.fcf_avr
This is an flowcode file for AVR ATMEGA32 TO MEASURE PHASE ANGLE BY CONNECTING IT TO ZERO CROSSING DETECTOR
(12 KiB) Downloaded 1125 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pulse duration measurement with flowcode

Post by Benj »

Hello,

First you need to make a Flowcode variable say called timer_count.

Once you have done this you can then use a C code icon to read the value of the timer into your variable.

Code: Select all

FCV_TIMER_COUNT = TCNT0;

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Dear Sir,
I am posting a FlowCode4 AVR File for ATMEGA32. It is simulating alright but when loaded on the processor for debugging with FLOWKIT. It hangs after 4-5 steps. Mostly after Timer0 and TIMER1 INTERRUPTS are enabled. The job of this program is to sense AD6 AND THEN START TIMER0 AND TIMER1, AND THEN WHEN AD7 IS high IT SHOULD DISABLE TIMER0 AND AGAIN CHECK FOR AD6 AND WHEN AD6 IS low IT SHOULD DISABLE TIMER1. Please help me in correcting the program. Program :)
Attachments
Flowcode1.fcf_avr
(11 KiB) Downloaded 937 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pulse duration measurement with flowcode

Post by Benj »

Hello,

There is currently a problem when using the FlowKit with interrupts. We have got a work around for this which will be included in the up and coming release of Flowcode PIC. For now I would advise that you only use the FlowKit for programs that do not contain interrupts. If you need to use the Flowkit with interrupts then let me know and I will post details for the workaround.

User avatar
petesmart
Valued Contributor
Valued Contributor
Posts: 395
Joined: Thu May 06, 2010 11:42 am
Location: Sydney, Australia
Has thanked: 187 times
Been thanked: 140 times
Contact:

Re: pulse duration measurement with flowcode

Post by petesmart »

Hi Ben,

I am using a MIAC and TMR0 interrupt. Have attempted to use flowkit to debug code and could not understand why it did not work properly...

Appreciate if you could publish the workaround....

Any Idea when the next rev of Flowcode 4 will be available?

Thanks

All the best

Pete
sorry about that Chief!

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hi Benj,
Thanks for the feedback
I have to use FLOW KIT with interrupts as referred in the program so please tell me how to work around. More over I am using FLOWCODE4 for AVR. Please help me to solve this problem because to initiate any timer as counter i have to seek help of interrupt. If there is any is any other way to initialise a counter for AVR in FlowCode by C code then in that case i may not use an interrupt but rather chech the condition and intiate the timer 0 or 1 but I am not very good at C. IF YOU COULD PROVIDE THE C CODE FOR INITIALISING, STOPPING AND READING TIMER0 AND TIMER1 WHICH CAN BE USED IN C BOX IN FLOW CODE 4 for AVR then I think the solution can be found out with flowkit without interrupt.
:)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pulse duration measurement with flowcode

Post by Benj »

Hello,

The current workaround involves a couple of steps.

1) Go into your Flowcode vx/FCD folder and open the internals.c or internals.h if no internals.c exists.

Find the following functions in the code.

Code: Select all

void FC_MARKER(signed int block_id)
void ICD_PUSH(char macro_id)
void ICD_POP()
At the top of the function add these lines.

Code: Select all

		if(ICD_Ignore == 0)
		{
and at the bottom add a closing brace }

Here is the converted ICD_POP function. Repeat this for all 3 functions.

Code: Select all

	void ICD_POP()
	{
		if (ICD_Ignore == 0)
		{
			//note: there is no specific stack underflow test here (there will be at the PC end though)
			icd_stack_ptr--;
		}
	}
In the internals file find this line in the code.

Code: Select all

#ifdef USE_FLOWCODE_ICD
Directly underneath this line add the following code.

Code: Select all

extern char ICD_Ignore;
Once you have done this you need to close and save the internals file. I recommend you make a backup of the file first so you can revert if you need to.

Next open the FCD file for your device. At the bottom of the file are the interrupts.

For every interrupt you need to enable in your program to run with the Flowkit you need to do the following.

Find the handler code section of the interrupt.

Eg.

Code: Select all

HandlerCode="#ifndef MX_INTHANDLER_intcon_TMR0IF\n#define MX_INTHANDLER_intcon_TMR0IF\nif (ts_bit(intcon, TMR0IF) && ts_bit(intcon, TMR0IE))\n{\n\tFCM_%n();\n\tcr_bit(intcon, TMR0IF);\n}\n#else\n#warning "This interrupt has previously been enabled, so the macro <%n> may never get called."\n#endif\n"
And change to add the code to switch the control variable on and off: ICD_Ignore = 0;\n and ICD_Ignore=1;\n.

Eg.

Code: Select all

HandlerCode="#ifndef MX_INTHANDLER_intcon_TMR0IF\n#define MX_INTHANDLER_intcon_TMR0IF\nif (ts_bit(intcon, TMR0IF) && ts_bit(intcon, TMR0IE))\n{\n\tICD_Ignore = 1;\n\tFCM_%n();\n\tcr_bit(intcon, TMR0IF);\n\tICD_Ignore = 0;\n}\n#else\n#warning "This interrupt has previously been enabled, so the macro <%n> may never get called."\n#endif\n"
Save and close the FCD, You should make a backup of this file too in case you need to go back.

Finally in your Flowcode program click on View -> Project options -> Enable Supplementary code -> Edit Supplementary code.

In the defines code section add the following line of code.

char ICD_Ignore = 0;

The code should now compile and work with the Flowkit without crashing or causing any errors.

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
Thanks for the reply, I did as suggested but in the present case I am not able to compile a simple program of pulse duration measurement with Timer0 alone. The pragram and error listing is here by attached. Please try to help me out so that I could use timer0 and timer1 in AVR ATMEGA32 ALONG WITH Flowkit else let me know the code for initialising, reading and stopping TIMER0 AND TIMER1 in AVR C WITHOUT INTERRUPT SO THAT I COULD USE THE C BLOCK FOR THIS JOB AND USE FLOWKIT TO TEST THE PROGRAM.
with regards
pravat
Attachments
PulseMeasure.msg.txt
The error listing file
(6.15 KiB) Downloaded 879 times
PulseMeasure.fcf_avr
The FlowCode file on which the error listing file is generated
(7.5 KiB) Downloaded 891 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pulse duration measurement with flowcode

Post by Benj »

Hello Pravat,

Sorry I left out one step in the process.

In the internals file find this line in the code.

Code: Select all

#ifdef USE_FLOWCODE_ICD
Directly underneath this line add the following code.

Code: Select all

extern char ICD_Ignore;

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
thanks.I have corrected internal.c as suggested but for the same prgram i am still unable to compile to chip due to compilation error. Please note that earlier internals.h was also modified for handling floating point as suggested in the forum and all the correction i have made is in internals.c. The error file is enclosed for your refrence and flow code is the same as attached in the last message.
with regards
pravat :?
Attachments
PulseMeasure.msg.txt
error listing during compilation
(6.03 KiB) Downloaded 737 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pulse duration measurement with flowcode

Post by Benj »

Hello,

Please can you attach your Flowcode file, your output C code file and your internals file and I will see if I can find out what the problem is. The error related to the unknown variable has gone now so at least this has been fixed.

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
Thankyou very much for helping me out. I am attaching all the required files as suggested. I will also upload ATMEGA32.FCD and internals.h in the next message.
with regards
pravat :?
Attachments
internals.c
This is internals.c file
(27.09 KiB) Downloaded 713 times
PulseMeasure.fcf_avr
This is the flowcode file
(7.5 KiB) Downloaded 786 times
PulseMeasure.c
this is the output c file
(14.61 KiB) Downloaded 768 times

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj
in continuation to my earlier reply i am also attaching ATMEGA32.FCD and internals.h for your convineance.
with regards
pravat :?
Attachments
internals.h
internals.h for c/c++
(2.16 KiB) Downloaded 721 times
ATMEGA32.fcd
ATMEGA32.FCD MODIFIED FILE
(7.1 KiB) Downloaded 710 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pulse duration measurement with flowcode

Post by Benj »

Hello Pravat,

The problem was an extra } in the internals file.

Here is a copy that should allow your program to compile correctly.
Attachments
internals.c
(27.02 KiB) Downloaded 748 times

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
Thanks a lot. The program is compiling and responding however timer requires some fine tuning which i am looking into.
with regards
pravat :D

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
The program enclosed is the ultimate outcome of all efforts however I am not getting the stable LCD DISPLAY i.e. though the input square wave frequency,phase and amplitude are fixed but i am not getting a stable lcd read out, it keeps on changing. What can be the reason. Please help me in the modification of this flowcode or tell me the reason as to why the lcd display is not giving an stable value. The Flow code is here by attached.
with regards
pravat :?
Attachments
PulseMeasure new.fcf_avr
phase measurement program through flow code
(10 KiB) Downloaded 805 times

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
finally the pw was measured but timer0 could noy be used more than once so i decided to go for ordinary macro based counters for my project but unfortunately it is again giving the same problem rather it seems tha flowcode and flowkit are not suitable for timer and counter operation. The simulation and compilation is alright but when it comes to real debugging the debugger hangs. In the present program i am using macro to count as timero could not be used twice.There is no timer or interrupt used in this case. Please look into the program and tell why this program hangs when debugged?
with regards
Pravat :cry: :shock:
Attachments
TEST1.fcf_avr
this file uses macros as counters but hangs during debugging form flowkit.
(11.5 KiB) Downloaded 767 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pulse duration measurement with flowcode

Post by Benj »

Hello Pravat,

Please can you send me your C code file too and I will see if I can spot what is going wrong.

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
I am sorry to say that a simple file without macro also hangs. Iam also sending the C file as desired. I an using the original internalc.c and ATMEGA32.FCD and not the modified one which i used with timer0. The bigger trouble is that AVRC USED BY FLOW CODE IS NOT COMPATIBLE TO WINAVRC so a c file developed or genrated by flowcode can not be used with avr studio.so the user has no choice but to use flowkit and it hangs in debugging simple programs which are not using timer or interrupts then do not know what will happen when interrupts are initiated.
with regards
pravat :shock:
Attachments
TEST1.c
this is the c file
(18.39 KiB) Downloaded 744 times
TEST1.fcf_avr
this is simple file which is not even using macros
(10.5 KiB) Downloaded 819 times

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: pulse duration measurement with flowcode

Post by echase »

Similar issue but for PICs.

In the help file for interrupts there is a link to some Custom PIC interrupts. The last one may do what I want which is to time an event 0.1-10 secs long using TMR1 to count up the number of internal clock cycles. Can you confirm:-

1) Will this time quite long events like this?

2) Do I need to add any C code or Flowcode symbol to set the T1G/RA4 pin as the digital input that triggers the interrupt?

3) I am not clear what the Handler code does. When is this executed? Is it the code that executes when the interrupt happens/ends?

4) If the event becomes even longer the counter will overflow and the interrupt flag TMR1IF will be set. I presume the code in the help file will not do anything with this flag. Somehow I need to capture it and count the number of times it overflows.

5) Does the interrupt trigger upon say the falling edge of the pulse and then stop on the rising one, at which point the counter contents is passed to the variable? Does the counter then clear as I want it to continuously measure every new pulse of this pulse steam?

6) T1CON is set to 79 which on a 16F690 uses the LP oscillator (with external crystal needed?) and an Internal clock (FOSC/4). I am confused at this point as it appears to be trying to use both internal and external clocks to increment the count. I was planning to use the internal 8MHz global clock with a slower clock for TMR1. Para 6.6 of the 1F690 datasheet says I can I have a 32kHz crystal for the TMR2 and the 8MHz internal clock.

pravat
Flowcode V4 User
Posts: 19
Joined: Thu Aug 05, 2010 4:47 am
Contact:

Re: pulse duration measurement with flowcode

Post by pravat »

Hello Benj,
I sent the file and its c code. please try to rectify this problem. also please tell me as to where can i find the avr-gcc-4.3.3 user mannual for download so that i could look into the problem myself.
with regards
pravat

Post Reply