how I turn on and off led with TPM module  using TPMMOD

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how I turn on and off led with TPM module  using TPMMOD

Jump to solution
3,213 Views
yecosuma
Contributor III

how I turn on and off led with TPM module  using TPMMOD thanks for your answer

Labels (1)
0 Kudos
Reply
1 Solution
1,317 Views
yecosuma
Contributor III

hello, thanks for your answers, realy thaks already the circuit is working, I learned very things

a freerunning mode always TMOD=0; and others thinks of settings and more; thanks thanks thanks. bicmag

I will follow learn;

other question;  what do  i have that make  2 cop  once within the delay loop and other in  the main loop?.

another  I want use a pull up in PTC OF THIS MCU  MC9S08LC60  BUT i obtain this message

error at location 1838  attemp to write  to read-only location

the port register PTCPE IS READ AND WRITE I DONT HAVE BECAUSE HAPPEN THIS  THANKS NEWLY FOR YOU ANSWER

 

View solution in original post

0 Kudos
Reply
8 Replies
1,317 Views
bigmac
Specialist III

Hello,

 

There seems to be some confusion.  The TPMMOD register determines the modulo count for the TPM module - the count value at which the TPM counter overflows, and restarts again from a zero value.

 

If your LED application is simply to provide a visual indicator at a relatively slow flash rate, i.e. for a period much greater than the TPM overflow period, use of TPM output compare mode is inappropriate, and variation of the TPMMOD value will mostly be unnecessary.

 

A method of LED timing control is to set a short TPM overflow period (using a low prescale value), and then choose the LED on and off periods so that they can be represented by an integral number of overflow periods.  A RAM based counter register is used to keep track of the number of overflows.  During each overflow interrupt, the counter is decremented, and when it reaches zero, the LED is turned on or turned off via a GP output pin, and the counter is set for the next timed period.

 

Regards,

Mac

.

0 Kudos
Reply
1,317 Views
yecosuma
Contributor III

I am to use xtal of 16Mhz for mcu and TMOD CPWMS=1; THIS  IS CORRECT?

 how I turn on and off led with TPM module using TPMMOD to one second 

i dont undertand because it not work

 

 

void tiempo(unsigned char  contador)
  {  
    while (contador >0)
    {
       while(TPM1SC_TOF<1);     
        TPM1SC_TOF=0;
        contador--;
    }
 
  }
void main(void) {

     
      SOPT1_BKGDPE=1 ;
      PTCDD = 0B11111111;
      PTCD  =   0B00000001;
       TPM1SC=0B00111111;
       TPM1MOD=3125
  

  for(;:smileywink:{
       PTCD^=0X01;
       tiempo(10);         
 
  } /* loop forever */
  /* please make sure that you never leave main */
}

0 Kudos
Reply
1,317 Views
bigmac
Specialist III

Hello,

 

Firstly, you do not show the setup code to make use of the crystal as an external reference.  You will also need to set the ICG module for FBE mode (you cannot use FEE mode at 16MHz).  On this basis, the use of the fixed frequency clock is not applicable.  Using the bus clock as a clock source will give a frequency of 8MHz or lower, depending on lthe RDF setting within the ICGC2 register.

 

I notice that you are persisting to alter TPM1MOD to a very low value, yet you have a prescale setting (within TPM1SC) of 128.  This does not make any sense - reduce the prescale setting to 1, and use a freerunning TPM1 counter (TPM1MOD = 0), or use a much higher TPM1MOD setting, if not freerunning mode.

 

Using a prescale of 1 with a bus frequency of 8MHz, the TPM overflow period would be 8.192 milliseconds in freerunning mode.  This could be easily used to generate a one second delay, requiring a count value of 122.

 

You have some additional issues with the TPM1SC setting.  CPWMS bit should remain at zero since you are not using centre aligned PWM mode for the module.  You have also selected the external TPM clock source, which is not correct.  Select the bus clock source.

 

Within the code for the delay loop, you should also consider periodically reseting the COP timer, to prevent timeout.

 

Regards,

Mac

 

0 Kudos
Reply
1,317 Views
yecosuma
Contributor III

thanks  for your answer bigmac are very useful for  my; i am learn, realy thanks

i have a question, how do I get 8.192 milliseconds? how do I calculate  these value? what a count value of 122?

thnaks for your answer

0 Kudos
Reply
1,317 Views
bigmac
Specialist III

Hello,

 

When the TPM module operates in freerunning mode, the overflow count will be 65536 (2^16).  For a prescale value of 1, this will represent the number of bus clock cycles.  Assuming 8MHz bus clock, the overflow period will be 65536 / 8 = 8192 microseconds.  A quantity of 122 overflow periods gives a total period very close to one second.  The timing will have an uncertainty of one overflow period, since the timed period may start anywhere within the overflow cycle.  This will give an average period of 121.5 overflows.

 

Regards,

Mac

 

0 Kudos
Reply
1,317 Views
yecosuma
Contributor III

hello thanks for your answers already I learned more with you help.

now  I belive this module this working but not correctly, I believe I am failing in something because the led flashing until that the value count is 1......28  when is 29........122 not working i dont understand what happend i send code for check you which is the error please; is probable I am setting bad the registers or something missing

0 Kudos
Reply
1,317 Views
bigmac
Specialist III

Hello,

 

As I said in my earler post, you should reset the COP timer, both within the delay loop and the main loop.  This is otherwise known as "feeding the watchdog".  Most likely is that you currently have COP timeout, causing reset of the MCU, prior to the completion of the delay.

 

Regards,

Mac

 

0 Kudos
Reply
1,318 Views
yecosuma
Contributor III

hello, thanks for your answers, realy thaks already the circuit is working, I learned very things

a freerunning mode always TMOD=0; and others thinks of settings and more; thanks thanks thanks. bicmag

I will follow learn;

other question;  what do  i have that make  2 cop  once within the delay loop and other in  the main loop?.

another  I want use a pull up in PTC OF THIS MCU  MC9S08LC60  BUT i obtain this message

error at location 1838  attemp to write  to read-only location

the port register PTCPE IS READ AND WRITE I DONT HAVE BECAUSE HAPPEN THIS  THANKS NEWLY FOR YOU ANSWER

 

0 Kudos
Reply