# Apply PWM signal to PIN of a Microcontroller

I am using the MC9S08DZ60. I have been give a circuit diagram according to which i must program the micro-controller. I am using Code warrior with Processor Expert which allows me to initialize the required registers, i don't have to write the code for the same.

Requirement from the code: Detect a PWM pulse on pin 0 of register D. In response, switch on LED and apply PWM signal on pins 3,4,5,6 of Register A. According to the datasheet, i have equated the value of the Channel value registers to the expected pin on the microcontroller. How to apply PWM signal at the required PIN ?I have made an attempt for the same in the following code.

Please refer to the given code:

unsigned int result;
unsigned int pwmSignal()
{
If(PTDD_PTDD0) // to check for the incoming PWM signal.
{
PTDD_PTDD2 = 1;  //light up LED.
do // check Channel for 300ms.
{
//my understanding is to latch the values of the channel value registers to the
//expected pin.
result = TPM2C0VH;
result <<=8;
result = result + TPM2C0VL;
result = PTAD_PTAD4;
result = PTAD_PTAD5;
} while (TPM2C0_CH0F == 0);
}
else PTDD_PTDD2 = 0;
}


The code compiles absolutely fine. but i dont see any change. At this point i wanna tell, this is my first attempt at doing this.

Also i have one more question, how do i use, for example PIN 2 of Register D, for two different operations in a single main.c ?? Please let me know if i can provide anymore data about all of the above. Thanks in advance!

• Might not be the cause of the problem, but my first impression (without being familiar with your MCU) is that you may want to look into using a pin change interrupt or timer input capture function to read the incoming pulse. Also, you appear to be missing a closing bracket at the end of your code. – Joe Baker Mar 10 '13 at 21:24
• Actually, the missing bracket is supposed to go between while and else. – stanri Mar 11 '13 at 9:36
• What Joe said - if you need to detect a pin change, use an IRQ pin. – John U Mar 11 '13 at 9:45
• @JoeBaker: thnaks mate. i will read about IRQ(this was my second question). but i am still stuck with the first problem, How to apply PWM signal to a specific PIN? – sheetansh Mar 11 '13 at 9:54

## 2 Answers

There is a missing bracket between the while and else in your code, and an extra one at the end.

Here, like this:

unsigned int result;
unsigned int pwmSignal()
{
If(PTDD_PTDD0) // to check for the incoming PWM signal.
{
PTDD_PTDD2 = 1;  //light up LED.
do // check Channel for 300ms.
{
//my understanding is to latch the values of the channel value registers to the
//expected pin.
result = TPM2C0VH;
result <<=8;
result = result + TPM2C0VL;
result = PTAD_PTAD4;
result = PTAD_PTAD5;
} while (TPM2C0_CH0F == 0)

} // here, this one is missing.
else PTDD_PTDD2 = 0;
}
// }
// ^ This one you added at the end isn't necessary.


It helps a lot if your code is indented correctly. I'm very surprised your compiler didn't complain about this.

• Thanks stacey! I had lost the bracket when i was editing the question and code for to upload here. Its my lack of diligence. None the less i have made the change. thank you for your input! – sheetansh Mar 11 '13 at 9:48

Apprently, according to the Datasheet, PWM can only be configured to the specific pins of the MCU ie Edge-aligned PWM. And at the same time, only some specific pins(according to the datasheet) can only be configured to detect PWM ie Input Capture.

As this question doesnt hold anymore interest in the forum i will consider this as the answer.