Can't write to PWM Registers (MC9S08QG8)

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Can't write to PWM Registers (MC9S08QG8)

2,911件の閲覧回数
Ryanst
Contributor I
Hi eveyone.

This problem is pretty simple to explain. When I write to either PWM register on the MC9S08QG8 nothing happens the first several times I try. For example:

while (counter less than 100){
TPMC0V++;
counter++;
}

At the end of 100 loops TPMC0V would be about 10 and my counter would be 100. I can watch this behaviour when I step through the code and it's severely affecting my robot's control system. Any ideas?

THANKS!
ラベル(1)
0 件の賞賛
返信
6 返答(返信)

1,279件の閲覧回数
peg
Senior Contributor IV
Hi Ryanst,
 
The following quote from the manual may explain your problem:
 

Because the HCS08 is a family of 8-bit MCUs, the settings in the timer channel registers are buffered to

ensure coherent 16-bit updates and to avoid unexpected PWM pulse widths. Writes to either register,

TPMCnVH or TPMCnVL, write to buffer registers. In edge-PWM mode, values are transferred to the

corresponding timer channel registers only after both 8-bit bytes of a 16-bit register have been written and

the value in the TPMCNTH:TPMCNTL counter is 0x0000. (The new duty cycle does not take effect until

the next full period.)

especially the last sentence "AND the value in the counter is 0"

 

0 件の賞賛
返信

1,279件の閲覧回数
Ryanst
Contributor I
So I should be looking at the counter variable and waiting for it to be zero before I continue my loop then? Right now I'm losing lots of information by writing and having nothing happen.

I'm actually wondering about Codewarrior in general. It seems to blantantly ignore lines of code for no apparent reason. For example:

i = i+1000;

gives me i. I'm not sure why... but i is an integer and equal to zero to start with. Feels like similar behaviour to the PWM registers.
0 件の賞賛
返信

1,279件の閲覧回数
peg
Senior Contributor IV
Hi,
 
Personally, I would wait for the register you just wrote to (through the buffer) was equal to the last value written. This should make it more bullet-proof.
I don't use CodeWarrior so can't really comment on the second part of your question.
 
0 件の賞賛
返信

1,279件の閲覧回数
bigmac
Specialist III
Hello,
 
There would seem little point in attempting to increment the PWM value before the previous value has taken effect.  If you are trying to achieve a ramp output, you would need to pace the PWM increment with the PWM period, i.e. the timer overflow rate.  The most efficient way of doing this is to update the next PWM value within the timer overflow ISR (but this would not actually take effect until the next overflow occurred).
 
I would suggest not directly incrementing the timer register, but writing the incremented value to the register.
 
e.g. within the timer overflow ISR -
 
if (counter < 100) {
  counter++;
  TPMC0V = counter;
}
 
Regards,
Mac
 
0 件の賞賛
返信

1,279件の閲覧回数
peg
Senior Contributor IV
Hi Mac,
 
If you check the piece I quoted from the manual you will see that the updating is indeed "paced" with the PWM.
 
0 件の賞賛
返信

1,279件の閲覧回数
bigmac
Specialist III
Hello Peg,
 
I realise that the PWM output will not be updated until timer overflow.  However, it is less clear to me, should there be more than one write to the register before the output is updated, whether the first word value is latched, or whether the last word value written prior to the overflow will prevail.  The "pacing" referred to the update of the register, rather than the update of the output.
 
Regards.
Mac
 
0 件の賞賛
返信