Home  |  Contents 

Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: Oneshot CFunction for MM2 Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2338
Posted: 10 June 2018 at 8:33pm | IP Logged Quote matherp

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 on 10 June 2018 at 9:03pm



Back to Top View matherp's Profile Search for other posts by matherp
 
Zonker
Guru
Guru
Avatar

Joined: 18 August 2012
Location: United States
Online Status: Offline
Posts: 692
Posted: 10 June 2018 at 10:12pm | IP Logged Quote Zonker

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..!!
Back to Top View Zonker's Profile Search for other posts by Zonker
 
Chopperp
Senior Member
Senior Member
Avatar

Joined: 03 January 2018
Location: Australia
Online Status: Offline
Posts: 283
Posted: 10 June 2018 at 11:21pm | IP Logged Quote Chopperp

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
Back to Top View Chopperp's Profile Search for other posts by Chopperp
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2338
Posted: 11 June 2018 at 12:58am | IP Logged Quote matherp

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
Back to Top View matherp's Profile Search for other posts by matherp
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2338
Posted: 11 June 2018 at 3:27am | IP Logged Quote matherp

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
 
Back to Top View matherp's Profile Search for other posts by matherp
 
Chopperp
Senior Member
Senior Member
Avatar

Joined: 03 January 2018
Location: Australia
Online Status: Offline
Posts: 283
Posted: 11 June 2018 at 7:02am | IP Logged Quote Chopperp

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
Back to Top View Chopperp's Profile Search for other posts by Chopperp
 
Chopperp
Senior Member
Senior Member
Avatar

Joined: 03 January 2018
Location: Australia
Online Status: Offline
Posts: 283
Posted: 11 June 2018 at 10:51am | IP Logged Quote Chopperp

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 on 11 June 2018 at 10:56am


__________________
ChopperP
Back to Top View Chopperp's Profile Search for other posts by Chopperp
 
Chopperp
Senior Member
Senior Member
Avatar

Joined: 03 January 2018
Location: Australia
Online Status: Offline
Posts: 283
Posted: 12 June 2018 at 8:59pm | IP Logged Quote Chopperp

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
Back to Top View Chopperp's Profile Search for other posts by Chopperp
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2338
Posted: 12 June 2018 at 11:34pm | IP Logged Quote matherp

You probably need to call "oneshot 0,0,0" to reset it. Let me know if it works

Edited by matherp on 12 June 2018 at 11:35pm
Back to Top View matherp's Profile Search for other posts by matherp
 
Chopperp
Senior Member
Senior Member
Avatar

Joined: 03 January 2018
Location: Australia
Online Status: Offline
Posts: 283
Posted: 13 June 2018 at 12:16am | IP Logged Quote Chopperp

Will try tomrrow
Thanks

__________________
ChopperP
Back to Top View Chopperp's Profile Search for other posts by Chopperp
 
Chopperp
Senior Member
Senior Member
Avatar

Joined: 03 January 2018
Location: Australia
Online Status: Offline
Posts: 283
Posted: 13 June 2018 at 7:24am | IP Logged Quote Chopperp

Thanks matherp, that worked

__________________
ChopperP
Back to Top View Chopperp's Profile Search for other posts by Chopperp
 


If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Search the forums using Google.
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1250 seconds.
Privacy Policy     Process times : 0, 0.02, 0, 0.11