Code for analyzing an arbitrary waveform

John West

Senior Member
I need coding help with the analysis of an arbitrary waveform. As usual for me, it was not difficult to prep the waveform in hardware so it is presented in a clean ttl level (0 to 5 V) format to be analyzed by a 28X2 PICAXE running at 64 MHz. However, after that things fall apart.

I'm attempting to verify that the incoming waveform looks like a short pulse burst at approximately 150 kHz that is followed by a dead time of approximately 10 seconds, then repeats. In order to verify this I'd like to be able to read the frequency and duration of the pulse burst and the duration of the logic low dead time to within about 5% and display that info on a serial LCD.

I have a 16 MHz xtal that I will carefully trim so I have an accurate 64 MHz clock to do the counting and timing with.

While I'm a pretty good hardware tech, my coding skills are very weak so far, and this difficult piece of code is clearly beyond me at this point. I've been reading up on interrupts and setting counters, as I expect that's what will need to be done in order to pull out the necessary info to send to the display.

If one of the forum's coding experts could walk me through the process of how to do this analysis (assuming a 64MHz PICAXE is even fast enough to do the job) it would be much appreciated, as I'd like to be able to do more such analysis on my own in the future.

EDIT: PICAXE choice updated from 20X2 to 28X2 per comment #2 below.
 
Last edited:

jtcurneal

Senior Member
If you want to use an external xtal, You will have to go to at least a Picaxe 28X2
( the 20X2 does not have provision for an external xtal )

Joel
 

John West

Senior Member
Ah, I misread the info on that. I read this bit from manual 2 and called it good:

"Note that the X2parts [sic] have an internal x4 PLL inside the chip. This multiplies the
external resonator speed by 4."

I didn't look for the resonator pins on the 20X2 pinout drawing. A mistake. Thanks.

I can use any PICAXE I want, so I'll switch to the 28X2. Not an auspicious start, overlooking something so basic. I figured my problems would be entirely on the code front. It's good to have others looking over my shoulder on this project. This forum is an invaluable resource, especially when I'm confronted with problems outside my area of competence.
 
Last edited:

Goeytex

Senior Member
Hi John

150 Khz means a time period of 6.66us. At 50% duty a single pulse will be approx 3.33 us. Pulsin MAY be able to read the first pulse, but not the second one since it will take the Picaxe approx 60us to recover and get to the next Pulsin.

As a test I would suggest you send the 150khz signal to the Picaxe and then try to read it with 4 consecutive pulsin reads directly to the scratchpad. It will skip about 10 pulses between reads due to overhead. The value you get should be microseconds / .625. So something around 5 or 6 . Then we can go from there
 

John West

Senior Member
I was already figuring that it would be trickier than using the PULSEIN command could handle. I suspected it would require starting a counter at the first rising edge (logic 1,) counting the total number of transitions during the burst, then noting when the rising edges cease repeating (logic 0) for a longer period, stopping the running counter and reading that counter number, then subtracting an amount equal to however long it took to figure out the pulse burst had ceased.

At that point in my thinking I decided I had opened a can of worms and needed some expert help, and posted my code problem here.

BTW, the individual pulses of the pulse burst will toggle at about a 50% duty cycle, so that will help a little with counting considerations. The pulse length of each rise and fall doesn't matter, just that the overall burst is about the right number of pulses and is about the right frequency.

The burst will likely be about a dozen pulses long about every 10 seconds. I don't have the circuit here to check that aspect as yet, and it may vary a few cycles from burst to burst, which is why I need to count them. It's a timing analysis of a multiple feedback loop analog output signal, so there will be some variations in pulse duration and numbers. Thus the need for the timing and counting analysis in order to ensure the circuit is working as expected.

I wanted to see if PICAXE code could be written to do the job before I built up the circuit I wish to test, because it's a fairly complex bit of hardware and I want to be sure the PICAXE at least appears to be up to the job before I start to build.
 
Last edited:

Goeytex

Senior Member
I wrote this little snippet to test. It works as expected. I am sending a 150Khz 50% duty signal to Leg 9 (c.0)

Code:
#picaxe 20x2
#no_table


main:
setfreq m64

Pause 8000  '500 ms

ptr = 0                 
pulsin c.1,1,@ptrinc    'SP location 0  read off time   
pulsin c.1,1,@ptrinc    'SP location 1
pulsin c.1,1,@ptrinc    'SP location 2 
pulsin c.1,1,@ptrinc	'SP location 3 

pulsin c.1,0,@ptrinc    'SP location 0  read off time  
pulsin c.1,0,@ptrinc    'SP location 1
pulsin c.1,0,@ptrinc    'SP location 2 
pulsin c.1,0,@ptrinc	'SP location 3 

pause 100
  
setfreq m32  
             
for ptr = 0 to 3          
sertxd ("On Time = ",#@ptr," ",CR,LF)     
next

sertxd (CR,LF)

for ptr = 4 to 7          
sertxd ("Off Time = ",#@ptr," ",CR,LF)     
next

sertxd (cr,lf)
goto main
 

John West

Senior Member
Thanks very much for the code, Goeytex. I see it's written for the 20X2. It appears I'll need to be using the 28X2 in my tester, but I'll set up a 20X2 for now and run the code with varying inputs (and study it) to see how it performs with an approximate 64 MHz clock until I can pick up a 28X2.
 

Goeytex

Senior Member
John,

I just setup 2 Picaxes where one sends a burst of 150Khz pulses and the other counts them. The counting Picaxe consistently reports a count of 125 pulses + or - 1 1 pulse which is about right for the calculated time duration time. Seems doable with some creative code. Counting Picaxe running at 64mhz ( 20X2)

Having an arterial graft in my leg tomorrow so I will be offline for about 4 days while in hospital. Hope you get this worked out.
 

John West

Senior Member
I'm curious what code you are running to generate the pulse bursts. Is it just a nested loop? It would be handy for me to do the same here as I have another 20X2 handy and don't yet have the circuit I'm going to be testing. I have an arbitrary waveform generator on my bench, but using a PICAXE for the task sounds more educational (and fun.)

I hope everything goes well with your surgery. I've spent far too much of my life in and out of hospitals. I hope to have something up and running here by the time you get back home. Thanks again for the code help, (and the hardware test.) It's nice to know there are such good software (and hardware) folks willing to help out when it's needed.
 
Last edited:

Goeytex

Senior Member
This generates the burst.

Code:
#Picaxe 18m2
setfreq m32

pwmout B.3, 52, 0

Main:
pwmduty b.3, 107   '150khz @ 50%   
pwmduty b.3, 0      ' turn off immediately based upon Picaxe overhead minimum time between commands
pause 10000
goto main ' repeat
 

John West

Senior Member
A PWM burst and a delay. Duh. I keep forgetting about that PWM command for short pulse outputs. I have yet to need it in a program so I tend to forget that Rev Ed has created such useful little commands. I'm still stuck in old school BASIC's where one must generate most output the hard way. I think this project will finally drag me into the modern PICAXE BASIC world. It's about time.
 

vttom

Senior Member
If it were me, I'd probably do something like:

Code:
setfreq m64
enable interrupts on input pin
seconds_since_last_burst=0
do
  pause 16000                              ' a 1-second pause
  inc seconds_since_last_burst
loop

interrupt:
  ' Count pulses during 1ms window
  count inpin, 16, pulse_count
  input_satisfies_requirements=0
  if pulse_count > 145 and pulse_count < 155 then
    if seconds_since_last_burst > 8 and seconds_since_last_burst < 12 then
      input_satisfies_requirements=1
    endif
  endif
  seconds_since_last_burst=0
  re-enable interrupt
  return
 

John West

Senior Member
Thanks, vttom. If it were you, you wouldn't need to swallow your pride and ask the forum to help you with the code. :)

Unfortunately I'm me, and not very literate in PICAXE BASIC as yet. So I'll look carefully through the code suggestions until I understand them thoroughly, then I'll cut and paste, modify and embellish and implement whatever chunks will work best in my application. And hopefully I'll have learned a bit and won't have to ask a similar question in future. Rev. Ed. is well named.
 
Last edited:
Top