Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 14:27 27 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Oneshot CFunction for MM2

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8579
Posted: 10:33am 10 Jun 2018
Copy link to clipboard 
Print this post

This function takes a trigger pulse on pin-15 (28-pin) or pin-42 (44-pin) MM2 and uses it to create an output pulse on a user-specified pin with a user-specified delay and of user-specified duration.

NB: The Cfunction can not be used at at the same time as IR input




In the example a pulse is created 10mSec after the trigger for 500uSsec

Once set up the pulse generation will operate in the background and create a pulse whenever is sees the trigger. The parameters for the generated pulse can be changed at any time by calling the function again.

The maximum pulse length is a function of the CPU speed and is defined by the equation:

maximum pulse length in seconds = (65536*8)/(CPU speed in Hz)
CPU speed must be either 40MHz or 48MHz

The demo code should be self-explanatory. C-code is also attached for interest

'
' To use the test program connect pin 4 to pin 15
'
setpin 15,CIN
pwm 1,60,0.5
pause 3000
print pin(15)
oneshot 14,10000,500
timer=0
do
loop while timer<10000
oneshot 14,5000,1500
timer=0
do
loop while timer<10000
oneshot 0,0,0
end
'
' subroutine oneshot
' outputs a positive going pulse after a pre-determined period after a trigger pulse is received
' will continute to respond to each trigger pulse received until the output is turned off.
' The trigger pulse must be applied to pin 15 of a 28-pin Micromite which must be previously set up as a counting input

'
' This function takes as parameters the following
'
' pin number of the pin to output the pulse
' pause after trigger before the pulse in microseconds (maximum is 524288/CPU speed = 13.1mSec @ 40MHz
' duration of the pulse in microseconds (maximum is 524288/CPU speed = 13.1mSec @ 40MHz
'
' Use pin number =0 to turn off the output
'
' WARNING - the program has no error checking. Pulse durations greater than specified above will be rounded down modulus 524288/CPU speed

'
CSub oneshot
00000030
'T1Int
3C039D00 8C62008C 8C450014 3C04BF80 AC850620 8C440008 8C420004 AC820000
8C63008C 8C62000C 2444FFFF 14400008 AC64000C 24030010 3C02BF88 AC431064
3C02BF88 AC401068 3C02BF80 AC400600 03E00008 00000000
'E2Int
3C029D00 8C43008C 24040001 AC64000C 8C42008C 8C430010 3C02BF80 AC430620
34038010 3C02BF80 AC430600 24020010 3C03BF88 AC621064 3C03BF88 AC621068
03E00008 00000000
'getFPC
27BDFFF8 AFBF0004 00852023 03E42021 ACC40000 8FBF0004 03E00008 27BD0008
'main
27BDFFC0 AFBF003C AFB70038 AFB60034 AFB50030 AFB4002C AFB30028 AFB20024
AFB10020 AFB0001C 00808821 00A08021 00C09021 00002021 3C059D00 24A5010C
27A60010 0411FFE6 00000000 8E530000 3C029D00 8C420000 8C520000 8E220000
8E230004 00431025 1440000E 8E140000 24030010 3C02BF88 AC431064 3C02BF88
AC401068 3C02BF80 AC400600 3C029D00 8C4300CC AC600000 8C4200AC 10000046
AC400000 24160010 3C02BF88 AC561064 3C02BF88 AC401068 3C109D00 8E020010
8E240000 24050008 0040F809 00003021 8E15008C 8E020024 8E240000 0040F809
24050007 AEA20008 8E17008C 8E020028 0040F809 8E240000 24150001 00551004
AEE20004 8E02001C 8E240000 0040F809 24050005 8FA40010 8E0200CC 3C039D00
24630058 00641821 AC430000 8FA40010 8E0200AC 3C039D00 24630000 00641821
AC430000 8E02008C AC55000C 001290C2 3C02000F 24424240 0242001A 004001F4
00009012 8E02008C 7254A002 AC540010 8E03008C 72539002 AC720014 8E02008C
8C430010 3C02BF80 AC430620 34038010 3C02BF80 AC430600 2403001C 3C02BF88
AC4310A4 24030004 3C02BF88 AC4310A8 3C02BF88 AC561034 8FBF003C 8FB70038
8FB60034 8FB50030 8FB4002C 8FB30028 8FB20024 8FB10020 8FB0001C 03E00008
27BD0040
End CSub



#define Version 100 //Version 1.00
#define _SUPPRESS_PLIB_WARNING // required for XC1.33 Later compiler versions will need PLIB to be installed
#include <plib.h> // the pre Harmony peripheral libraries
#include "../cfunctions.h"
//#define MX470
#define magic 0
#define t1pin 1
#define t1port 2
#define t1count 3
#define t1dwell 4
#define t1duration 5
//
void T1Int(void){
PR1=CFuncRam[t1duration];
*(volatile unsigned int *)CFuncRam[t1port]=CFuncRam[t1pin];
if(!(CFuncRam[t1count]--)){
mT1IntEnable(0);
T1CON=0;
}
}
void E2Int(void){
CFuncRam[t1count]=1; //used to hold count of cycles
PR1=CFuncRam[t1dwell];
T1CON = 0x8010; // T5 on, prescaler as input
mT1IntEnable(1);
}
__attribute__((noinline)) void getFPC(void *a, void *b, volatile unsigned int *c)
{
*c = (unsigned int) (__builtin_return_address (0) - (b -a)) ;
}

void main(long long *pin, long long *dwell, long long *duration){
volatile unsigned int libAddr ;
getFPC(NULL,&&getFPCLab,&libAddr) ; // warning can be ignored, stupid editor
getFPCLab: { }
int mydwell=*dwell, myduration=*duration, tickspersecond;
tickspersecond=CurrentCpuSpeed/8;
if(*pin==0){
mT1IntEnable(0);
T1CON=0;
CFuncInt2=NULL;
CFuncT1=NULL;
return;
}
mT1IntEnable(0);
ExtCfg(*pin,EXT_DIG_OUT,0);
CFuncRam[t1port]=(int)(volatile unsigned int *)GetPortAddr(*pin,LATINV);
CFuncRam[t1pin]=1<<GetPinBit(*pin);
PinSetBit(*pin, LATCLR);
CFuncInt2=(unsigned int)&E2Int + libAddr;
CFuncT1=(unsigned int)&T1Int + libAddr;
CFuncRam[t1count]=1; //used to hold count of cycles
CFuncRam[t1dwell] = (mydwell*(tickspersecond/1000000)); //wait time in ticks
CFuncRam[t1duration]=(myduration*(tickspersecond/1000000));
PR1=CFuncRam[t1dwell];
T1CON = 0x8010; // T5 on, prescaler as input
mT1SetIntPriority(1); // high priority
mT1ClearIntFlag(); // clear interrupt flag
}

Edited by matherp 2018-06-11
 
Zonker

Guru

Joined: 18/08/2012
Location: United States
Posts: 761
Posted: 12:12pm 10 Jun 2018
Copy link to clipboard 
Print this post

A big tip of the hat to you Matherp for investing your time to help everyone here on the forum..!! A very giving person you are fine Sir..!!
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1032
Posted: 01:21pm 10 Jun 2018
Copy link to clipboard 
Print this post

Hi matherp

I concur with Zonker.

Took me a while to work it out, but I eventually got something going. About the first thing I found out was to use integers for the delay & width variables.

I have it going in a 1 sec loop monitoring a voltage for the width & triggering at 100Hz. (Rectified mains). However, every so often, it will miss a beat as shown by the snapshot below or the width varies slightly. (Delay 1000mS & width ~ 6700mS). It would need to run in a faster loop.

Did some more testing & my trigger signal probably needs improving. I reduced the level slightly from what photo shows & it is behaving much better. More testing needed on my part.

It does appear to have an occasional hiccup when running from the test signal. My test setup is a bit rough though.

This is certainly a huge step in the right direction. Very, very much appreciated.



option autorun on
' To use the test program connect pin 4 to pin 15
'
setpin 15,CIN
setpin 7, ain

do
delay% = max(1000, min(8000, pin(7) * 3000))
oneshot 14, 1000, delay%
pause 1000
loop
end



ChopperP
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8579
Posted: 02:58pm 10 Jun 2018
Copy link to clipboard 
Print this post

Do you have delay and width backwards in your code or is this what you mean? The first parameter is the delay after the trigger and the second is the width of the pulse.

There may be some timing issues associated with changing the parameters while a pulse is running. I'll have a look at how this can be fixed
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8579
Posted: 05:27pm 10 Jun 2018
Copy link to clipboard 
Print this post

This version waits if a pulse is active until it completes before applying the new parameters. I've tested it on the scope and can't see any evidence of faulty pulses.

00000030
'T1Int
3C039D00 8C62008C 8C450014 3C04BF80 AC850620 8C440008 8C420004 AC820000
8C63008C 8C62000C 2444FFFF 14400008 AC64000C 24030010 3C02BF88 AC431064
3C02BF88 AC401068 3C02BF80 AC400600 03E00008 00000000
'E2Int
3C029D00 8C43008C 24040001 AC64000C 8C42008C 8C430010 3C02BF80 AC430620
34038010 3C02BF80 AC430600 24020010 3C03BF88 AC621064 3C03BF88 AC621068
03E00008 00000000
'getFPC
27BDFFF8 AFBF0004 00852023 03E42021 ACC40000 8FBF0004 03E00008 27BD0008
'main
27BDFFC8 AFBF0034 AFB50030 AFB4002C AFB30028 AFB20024 AFB10020 AFB0001C
00808021 00A08821 00C09021 00002021 3C059D00 24A50104 27A60010 0411FFE8
00000000 8E530000 3C029D00 8C420000 8C520000 8E020000 8E030004 00431025
1440000E 8E340000 24030010 3C02BF88 AC431064 3C02BF88 AC401068 3C02BF80
AC400600 3C029D00 8C4300CC AC600000 8C4200AC 1000004B AC400000 3C029D00
8C4200CC 8C420000 1440002A 3C03BF80 24030010 3C02BF88 AC431064 3C02BF88
AC401068 3C119D00 8E220010 8E040000 24050008 0040F809 00003021 8E35008C
8E220024 8E040000 0040F809 24050007 AEA20008 8E35008C 8E220028 0040F809
8E040000 24030001 00431004 AEA20004 8E22001C 8E040000 0040F809 24050005
8FA40010 8E2200CC 3C039D00 24630058 00641821 AC430000 8FA40010 8E2200AC
3C039D00 24630000 00641821 10000004 AC430000 8C620600 1440FFFE 00000000
3C029D00 8C43008C 24040001 AC64000C 001290C2 3C03000F 24634240 0243001A
006001F4 00009012 8C43008C 7254A002 AC740010 8C42008C 72539002 AC520014
2403001C 3C02BF88 AC4310A4 24030004 3C02BF88 AC4310A8 24030010 3C02BF88
AC431034 8FBF0034 8FB50030 8FB4002C 8FB30028 8FB20024 8FB10020 8FB0001C
03E00008 27BD0038
End CSub

 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1032
Posted: 09:02pm 10 Jun 2018
Copy link to clipboard 
Print this post

Hi matherp,

Yes, Delay should have been labeled Width. Still experimenting on how to best use it.

Will test the new version bit later on today & report back.

Thankyou very much..
ChopperP
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1032
Posted: 12:51am 11 Jun 2018
Copy link to clipboard 
Print this post

Fantastic!!!! Working perfectly as designed.

As you mentioned, there is no error correction. I found a too wide a pulse gave an inverted output. Limiting the width solved that problem.

I also experimented with some gating pulses. The idea worked on the breadboard. Yet to be tested in real life though which may take a week or two. QLD Courts reckons I need to do Jury Duty. I'd better go.

EDIT OOPS Just realised that my gated pulses aren't correct. Should be after the pulse end. Back to the drawing board.

option autorun on
pwm 1, 10000, 50 'HF trigger pulses for the SCR, gated by PIN 14
setpin 15, CIN 'Trigger input
setpin 7, ain 'Sample volts in. 0-3V3
setpin 23, inth, trigger 'PIN 23 connected to PIN 15

do
width% = max(500, min(8750, pin(7) * 3000)) 'width limited from ~ 0.5mS to 8.75mS
pause 8 'simulating doing other stuff
loop

sub trigger
oneshot 14, 1000, width%
end sub




Trigger + Normal O/P


Trigger + Gated O/P



Edited by Chopperp 2018-06-12
ChopperP
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1032
Posted: 10:59am 12 Jun 2018
Copy link to clipboard 
Print this post

Hi matherp

It appears that if I stop the program below & then restart it, Oneshot does not restart.

OK if the MM is reset. Test signal on PIN 16 stops & starts OK.

option autorun on
pwm 1, 10000, 50 'HF trigger pulses for the SCR, gated by PIN 14
setpin 15, CIN 'Trigger input
setpin 7, ain 'Sample volts in. 0-3V3
width% = 8750 'initial Start
oneshot 14, 900, width%
pause 1000
setpin 23, inth, trigger 'PIN 23 connected to PIN 15
setpin 16, dout 'test point
do
width% = max(100, min(8750, pin(7) * 3000)) 'width limited from ~ 0.5mS to 8.5mS
pause 8 'simulating doing other stuff
loop

sub trigger
oneshot 14, 900, width%
pin(16) = not pin(16) 'test point
end sub

ChopperP
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8579
Posted: 01:34pm 12 Jun 2018
Copy link to clipboard 
Print this post

You probably need to call "oneshot 0,0,0" to reset it. Let me know if it worksEdited by matherp 2018-06-13
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1032
Posted: 02:16pm 12 Jun 2018
Copy link to clipboard 
Print this post

Will try tomrrow
Thanks
ChopperP
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1032
Posted: 09:24pm 12 Jun 2018
Copy link to clipboard 
Print this post

Thanks matherp, that worked
ChopperP
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024