This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RM48 N2HET PWM - Stop PWM after 'n' edges.

Hi,

I have a PWM output operational from N2HET1_0.

I wish to stop the PWM signal after an exact number of rising edges of the PWM output.

Can anyone please suggest how this would be possible using N2HET's.  Do I have to create my own program using HET IDE, or is this as simple as setting up a register or two on the PWM.

Thanks in advance

Stomp!

  • Stomp,

    If your PWM is slow enough, you could probably disable the PWM in an interrupt service routine after the N'th edge.

    But N2HET can do this sort of job all by itself.  You will need to write your own PWM routines but there are templates in the HET IDE for this job.

    Let's assume that your PWM is implemented with an ECMP instruction.   You can make the ECMP instruction jump to some code when the compare matches (use the 'cond_addr' field of ECMP).   This code can decrement a counter (to count your N edges) using the DJZ instruction.   Then if the counter decrements all the way to '0' you could execute a MOV64 instruction that changes the ECMP instruction to en_pin_action = OFF.

    That would be one way to attack the problem,  in this case the count and compare operations would continue uninterrupted but your pin would stop toggling.  You might also attack the problem by changing the ECMP data field with the MOV64 instruction so that it doesn't fall within the valid range of the counter.   Or you could put a BR instruction before the ECMP and use the MOV64 to change whether the BR branches around the ECMP or not.   These would be some different ways to attack the problem, and depending on what else you are trying to do in the program you might find that one works better with the rest of the program than the others.

     

  • Anthony,

    Thanks very much for your quick and highly detailed answer.  I knew HET IDE would be the way to go, was just trying to avoid the learning curve :).

    I'll do some experimenting and see what happens.

    Kind Regards

    Stomp!.

  • Hi Anthony,

    I made and simulated a simple N2HET program as follows:

    I have the following questions though.

    1.  Does each N2HET program (for each pin) get executed in parallel or is the entire 160 instructions executed sequentially?.  I mean if I have two of these programs in HET memory, are they executed in parallel or sequentially?  Does that mean one program needs to finish before the other starts?

    2. Given the single instance of A.B.T registers, would you have to implement the counters as memory decrement functions, as opposed to register based counters if you wanted many of these programs to run on many pins.

    Thanks

    Stomp!

  • Hi Stomp.

    The N2HET program executes sequentially but the pin updates are done at each loop resolution boundary so it acts like a program that executes in parallel but with a sample rate of loop resolution.

    Not to make it too complicated, but the Hi-Res feature is then an 'offset' from the loop resolution clock allowing you to get back the resolution of the hi-res clock period, as long as you are only getting/setting the offset of one edge per pin per loop.

    I'm not 100% sure what you mean by two programs.  If it fits in memory it could be one program but have different modes.  Now be careful because the HET program always starts and ends at address 0x00 so you cannot really have two different programs;  they at least need to share the instruction at address 0.  But this might be something like a conditional branch to 'subprogram 0' or 'subprogram 1'.  I'd call that one program but with two distinct modes.

    The CNT instruction writes the current count back to memory at it's immediate address, plus it writes the count to register A.   So from loop to loop you don't need to maintain the value in "A" because it will be taken from the RAM IMM address at the beginning of CNT execution.   The write back to 'A' is an optional feature - to save you from using an extra instruction just to get the count value from memory into a register. 

    The reason you need the count in a register though is to use instructions like "ECMP" against it.

    So if you have multiple counts/compares in the same program you would group them by count1 followed by related compares,  count 2 followed by it's related compares, etc.  So that if you can, you will execute all the instructions that need count1's value before executing count 2.   (Assuming you use "A" as the register for both count1 and 2..)  If you need to though, you can always execute a MOV32 to grab the count value out of it's memory location back into a register, assuming you have written over the register for some other purpose.  But that's an extra instruction and you want to minimize the # of instrucitons in a HET program because memory and cycles are limited resources.