Dear Steve
I have a question about XGATE application.
I want to use XGATE to create a standard square wave output from a GPIO.
interrupt void SoftwareTrigger0_handler(void)
{
long i;
PTP_PTP3 ^= 0x01;
i = 0x5AC;
while (i --);
asm SIF;
}
I did get a square wave from the PTP3 pin, but it's not a exactly standard square wave. what's the root cause? Does the instruction time of XGATE be changed ?
Thanks in advance! Srimt
XGATE can execute up to two instructions per bus cycle. It will do 2 instructions only if it is executing from RAM, doesn't perform R/W accesses and if CPU doesn't access RAM in the current bus cycle. Executing from flash XGATE can do up to 1 instruction per bus cycle in case CPU doesn't access flash. Accessing registers space also adds some nuisances who wins and who has to wait a bit. Sometimes CPU12X slows down the XGATE, also there are cases when XGATE can slow down the CPU12X...
Code like yours should make some jitter on output pin, it's expected.
Thanks for your answer. But I need to implement a 8KHz standard square wave, and the timer just can implement 1KHz, Do you have any good suggestions for me ?
Thanks again!
PWM module, ECT module, TIM module all can be used to produce suqare wave with frequencies up to busclock /2. Of course there are some restrictions.
For example using PWM module with least possible PWM prescaler, default left aligned mode, first you divide busclock / target frequency. Say busclock is 40MHz, target - 8kHz. 40M / 8k = 5000. 5000 is >=256, so you should concatenate two 8bit PWM channels into one 16bit PWM. Period - 5000, duty cycle 5000/2=2500.
Thanks, I have got a 8KHz Standard spuare wave over ECT and XGATE.
Busclock = 32MHz
Buts tick time = 31.25ns
ECT_MCCNT = 2000;
while(ECT_MCCNT >= 5);
asm SIF;
PTP_PTP3 ^= 1;
Now my system works well. Thanks all good guys here.
You code doesn't look right.
1. MCCNT is not writeable in normal modes, ECT_MCCNT = 2000 won't work without BDM pod connected.
2. MCCNT is upcounting. If MCCNT would accept MCCNT = 2000, condition ECT_MCCNT >=5 would be true about 65536-2000-5 timer ticks, almost 2ms @32MHz. You code works because in special mode (when BDM pod is attached and you aren't using hotplug debugging), writing anything to MCCNT resets MCCNT.
This should work in all modes (not tested):
signed int targettcnt;
for(;{
targettcnt = TCNT + 2000;
while( (signed int)(TCNT - targettcnt) < 0) ) {}
PTP ^= (1<<3);
}
But jitter still will be high compared to automatic pin action on timer output compare, or compared to PWM. Here's the code (not tested) for 8kHz square wave on PP3/PWM3
PWMPER23=4000; // 32M / 8k
PWMDTY23=2000;
PWMCTL |= PWMCTL_CON23_MASK; // concatenate PWM 2 and 3
PWME |= (1<<3);
1. I initialized the MCCNT value 2000 one time.
2. I set MCEN bit which means modulus Down_Counter enable
3. I set FLMC bit which means Force Load Register into Modulus Counter Count Register
So after counter down count to 0, system will auto reload 2000 to the counter.
I have tested my code, it works well. BTY, my cpu is MC9S12XEQ512.
And PWM sources have been used by other programs.