Electronic – Apply PWM signal to PIN of a Microcontroller

cfreescalemicrocontrollerpwm

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!

Best Answer

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.

Related Topic