<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>8-bit MicrocontrollersのトピックRe: Help needed. To generate PWM on a freescale microcontroller</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156691#M9049</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello, and welcome to the forum.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have successfully used the assembly equivalent of the following code to generate antiphase PWM signals to directly&amp;nbsp;drive a piezo buzzer device.&amp;nbsp; The code was tested on a '9S08SE8, but should also work for the device you are using.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// TPM1 MODULE:// Generates 4 kHz PWM push-pull output from Ch0 and Ch1 for piezo.// Overflow period: 250 us#ifdef  LOW_TRIM#define TPM1MODval  999     //(1) (250*4.00 - 1)  4.00 MHz bus#else#define TPM1MODval  1249    //(2) (250*5.00 - 1)  5.00 MHz bus#endif#define TPM1SCval   0x08 /* 0b00001000  TPM1SC register                              |||||+++- PS     Prescaler divisor 1                              |||++---- CLKS   Bus rate clock                              ||+------ CPWMS  Edge aligned PWM mode                              |+------- TOIE   No overflow interrupt                              +-------- TOF    Read only flagNote: Clearing TPM1SC register will disable TPM1 module*/#define TPM1C0SCval 0x28 /* 0b00101000  TPM1C0SC register                              ||||++--- ELS0    High true output pulses                              ||++----- MS0     PWM mode (edge aligned)                              |+------- CH0IE   No channel interrupt                              +-------- CH0F    Read only flag*/#define TPM1C1SCval 0x2C /* 0b00101100  TPM1C1SC register                              ||||++--- ELS1    Low true output pulses                              ||++----- MS1     PWM mode (edge aligned)                              |+------- CH1IE   No channel interrupt                              +-------- CH1F    Read only flagNote: Clearing TPM1CnSC register will disable PWM output*//***************************************************************/// TPM1 module initialisationvoid TPM1_init( void){   TPM1MOD = TPM1MODval;   TPM1SC  = 0;             // Initially disable module}/***************************************************************/// TURN AUDIBLE BEEP ON/OFF#define DUTY_50   (TPM1MODval+1)/2 // PWM duty cycle 50%void BUZZ_ON( void){   TPM1SC   = TPM1SCval;    // Enable TPM1 module   TPM1C0SC = TPM1C0SCval;  // Enable TPM1 output Ch0   TPM1C1SC = TPM1C1SCval;  // Enable TPM1 output Ch1   TPM1C0V  = DUTY_50;      // PWM duty cycle 50%   TPM1C1V  = DUTY_50;      // PWM duty cycle 50%}void BUZZ_OFF( void){   TPM1C0SC = 0;   TPM1C1SC = 0;}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:00:43 GMT</pubDate>
    <dc:creator>bigmac</dc:creator>
    <dc:date>2020-10-29T09:00:43Z</dc:date>
    <item>
      <title>Help needed. To generate PWM on a freescale microcontroller</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156690#M9048</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all&lt;BR /&gt;&lt;BR /&gt;Am using Freescale Micro controller &lt;STRONG&gt;MC9S08SH8&lt;/STRONG&gt;. Iam trying to create a PWM using this controller. Though I have carefully set all the registers related to PWM, Am still unable to generate PWM on this.&lt;BR /&gt;&lt;BR /&gt;The main task was to create a edge aligned PWM on all channels of each PWM.&lt;BR /&gt;also am using internal oscillator it has.&lt;BR /&gt;&lt;BR /&gt;Can any one give me some example C Codes for the same or try correcting my code.&lt;BR /&gt;&lt;BR /&gt;Tools am using: Codewarrior suite with P&amp;amp;E Programmer&lt;BR /&gt;Controller: MC9S08SH8 20pin&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Any kind of help is greatly appreciated&lt;BR /&gt;&lt;BR /&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My Code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#include &amp;lt;hidef.h&amp;gt; /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void main(void) {&amp;nbsp; EnableInterrupts; /* enable interrupts */&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //TIMER1 CH0 n CH1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1SC=0b01001000;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1C0SC=0b00101000;&amp;nbsp; //Edge aligned pwm with high true pulses&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1C1SC=0b00101000;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1MOD=400;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1C0V=400;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1C1V=400;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //TIMER2 CH0 n CH1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2SC=0b01001000;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C1SC=0b00100100; //Edge aligned pwm with low true pulses&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC=0b00100100;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2MOD=400;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C1V=400;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0V=400;while(1){TPM1C0V=300;TPM1C1V=300;TPM2C1V=300;TPM2C0V=300;}&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:00:41 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156690#M9048</guid>
      <dc:creator>Ram_newbee</dc:creator>
      <dc:date>2020-10-29T09:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed. To generate PWM on a freescale microcontroller</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156691#M9049</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello, and welcome to the forum.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have successfully used the assembly equivalent of the following code to generate antiphase PWM signals to directly&amp;nbsp;drive a piezo buzzer device.&amp;nbsp; The code was tested on a '9S08SE8, but should also work for the device you are using.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// TPM1 MODULE:// Generates 4 kHz PWM push-pull output from Ch0 and Ch1 for piezo.// Overflow period: 250 us#ifdef  LOW_TRIM#define TPM1MODval  999     //(1) (250*4.00 - 1)  4.00 MHz bus#else#define TPM1MODval  1249    //(2) (250*5.00 - 1)  5.00 MHz bus#endif#define TPM1SCval   0x08 /* 0b00001000  TPM1SC register                              |||||+++- PS     Prescaler divisor 1                              |||++---- CLKS   Bus rate clock                              ||+------ CPWMS  Edge aligned PWM mode                              |+------- TOIE   No overflow interrupt                              +-------- TOF    Read only flagNote: Clearing TPM1SC register will disable TPM1 module*/#define TPM1C0SCval 0x28 /* 0b00101000  TPM1C0SC register                              ||||++--- ELS0    High true output pulses                              ||++----- MS0     PWM mode (edge aligned)                              |+------- CH0IE   No channel interrupt                              +-------- CH0F    Read only flag*/#define TPM1C1SCval 0x2C /* 0b00101100  TPM1C1SC register                              ||||++--- ELS1    Low true output pulses                              ||++----- MS1     PWM mode (edge aligned)                              |+------- CH1IE   No channel interrupt                              +-------- CH1F    Read only flagNote: Clearing TPM1CnSC register will disable PWM output*//***************************************************************/// TPM1 module initialisationvoid TPM1_init( void){   TPM1MOD = TPM1MODval;   TPM1SC  = 0;             // Initially disable module}/***************************************************************/// TURN AUDIBLE BEEP ON/OFF#define DUTY_50   (TPM1MODval+1)/2 // PWM duty cycle 50%void BUZZ_ON( void){   TPM1SC   = TPM1SCval;    // Enable TPM1 module   TPM1C0SC = TPM1C0SCval;  // Enable TPM1 output Ch0   TPM1C1SC = TPM1C1SCval;  // Enable TPM1 output Ch1   TPM1C0V  = DUTY_50;      // PWM duty cycle 50%   TPM1C1V  = DUTY_50;      // PWM duty cycle 50%}void BUZZ_OFF( void){   TPM1C0SC = 0;   TPM1C1SC = 0;}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:00:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156691#M9049</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2020-10-29T09:00:43Z</dc:date>
    </item>
    <item>
      <title>Thank You</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156692#M9050</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank You bigmac, I ll try to work it out and will update you accordingly&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Oct 2010 16:25:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156692#M9050</guid>
      <dc:creator>Ram_newbee</dc:creator>
      <dc:date>2010-10-21T16:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed. To generate PWM on a freescale microcontroller</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156693#M9051</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I discovered that the code I previously posted was from an early version of the firmware.&amp;nbsp; The later version is different, presumably because of problems experienced&amp;nbsp;during debugging.&amp;nbsp; Only the BUZZ_ON() function is affected, and the modified code follows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;void BUZZ_ON( void){   TPM1SC   = 0;            // Disable TPM1 module   TPM1C0V  = DUTY_50;      // PWM duty cycle 50%   TPM1C1V  = DUTY_50;      // PWM duty cycle 50%   TPM1SC   = TPM1SCval;    // Enable TPM1 module   TPM1C0SC = TPM1C0SCval;  // Enable TPM1 output Ch0   TPM1C1SC = TPM1C1SCval;  // Enable TPM1 output Ch1}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I now recall that the channel register settings did not update unless the TPM module was first disabled.&amp;nbsp; I apologise for any confusion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:00:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156693#M9051</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2020-10-29T09:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed. To generate PWM on a freescale microcontroller</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156694#M9052</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey thanks again for the update. I have changed the code to suit my prupose and since I dont need to use it for buzzer, I didn't encounter any problem. I have a few basic doubts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What does this do?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#ifdef&amp;nbsp; LOW_TRIM&lt;BR /&gt;#define TPM1MODval&amp;nbsp; 999&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //(1) (250*4.00 - 1)&amp;nbsp; 4.00 MHz bus&lt;BR /&gt;#else&lt;BR /&gt;#define TPM1MODval&amp;nbsp; 1249&amp;nbsp;&amp;nbsp;&amp;nbsp; //(2) (250*5.00 - 1)&amp;nbsp; 5.00 MHz bus&lt;BR /&gt;#endif&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And also How can I generate PWMs with various frequencies. I mean what register should I vary if I have to change the frequency of my PWM.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Oct 2010 13:39:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156694#M9052</guid>
      <dc:creator>Ram_newbee</dc:creator>
      <dc:date>2010-10-22T13:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed. To generate PWM on a freescale microcontroller</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156695#M9053</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The PWM period is determined by the modulo setting, within the TPMxMOD register.&amp;nbsp; Assuming the TPM module clock is sourced from the bus, and usually with a prescaler division by 1 for PWM, the number of bus cycles per PWM period will be one more than the modulo value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The purpose of the preprocessor conditional #defines is to allow for PWM period calibration, in this case for a 250 microseconds period, for two different trim settings associated with the internal reference of the&amp;nbsp;ICS module (using FEI mode).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Oct 2010 19:50:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Help-needed-To-generate-PWM-on-a-freescale-microcontroller/m-p/156695#M9053</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2010-10-22T19:50:40Z</dc:date>
    </item>
  </channel>
</rss>

