I have Problem with the TIM

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

I have Problem with the TIM

Jump to solution
1,441 Views
marvicdigital
Contributor II

Hello. Excusme my  bad english, I from Colombia.

I work with HC08JL8, and now I using the 2 TIM, TIM1 and TIM2; my idea is generate frecuency the 4HZ  to 100 HZ  with TIM1, and TIM2 for Pulse width positive, the 50uS to 2000uS; but no working, I need you help, or one idea but answer my problem. Attached part of the code in C.

 

 

void  TIM1_Setup(void){
TIM1_TSC=0x36;
TIM1_TMOD=1000; // Or Frecuency
TIM1_TSC=0x46;
}

void  TIM2_Setup(void){
TIM2_TSC=0x36;
TIM2_TMOD=100; // Or Pulse Width

}


interrupt 6 void TIM1_isr(void){
PTD5 = 1;  // On PTD5
T2SC=0x46;
          T2SC_TOF=0;
          T1SC_TOF=0;}

interrupt 9 void TIM2_isr(void){
PTD5 = 0;  // off PTD5
TIM2_TSC=0x36; /

T2SC_TOF=0;
}

void Main (void){

EnableInterrupts;
TIM1_Setup();
TIM2_Setup();
....
....

// more code

......
......
}

 Thanks for your help.

Ohh, I forgot to mentionthe Crystal is 20MHZ

 

Bye.

 

 

 

Message Edited by marvicdigital on 2009-11-07 03:50 AM
Labels (1)
0 Kudos
1 Solution
415 Views
marvicdigital
Contributor II

Ok, I found the solution with the  processor expert, I made the modifications and now it works to me well. Thanks to all by the aid.

 

 

void TM0_Cal (void){                              T1SC=0x30;          T1MOD = Vreal_Frecuencia;           (void)T1SC;          T1SC=0x46;                                      }void TM1_Cal (void){                    T2SC=0x30;          T2MOD = Vreal_AnchoPulso;                (void)T2SC;          T2SC=0x36;                    }interrupt 6 void INT_TIM1Ovrisr(void){                    T2SC=0x30;          (void)T2SC;          T2SC=0x46;                     PTD_PTD5 = 1;          T1SC=0x30;          (void) T1SC;          T1SC=0x46;                             }interrupt 9 void INT_TIM2Ovrisr(void){                              PTD_PTD5 = 0;          T2SC = 0x30;          (void) T2SC;          T2SC=0x36;                    }

 

 

Bye.

 

 

View solution in original post

0 Kudos
5 Replies
415 Views
bigmac
Specialist III

Hello, and welcome to the forum.

 

Are you using Code Warrior? I notice that some of the register names are inconsistent with the CW header file for the HC908JL8 device. I wonder if this might be your problem? Are you getting compiler or linker errors?

 

However, an alternative method to do what you require would be the use of PWM mode, applied to a single TIM channel (say TIM1 channel 1)? Here, the pulse repetition period would be determined by the T1MOD setting, and the pulse width by the T1CH1 register value.

 

For PWM mode, the overflow interrupt is not required, and the channel interrupt need be enabled only at the time you require to alter the pulse width setting (to prevent timing discontinuity the T1CH1 register would be updated within the ISR). PWM mode should produce a better timing accuracy, as well as using fewer MCU resources.

 

// TIM1 Ch1 setup for unbuffered PWM:

T1MOD = 1000;  // Repetition period

T1CH1 = 100;   // Pulse width

T1C1SC = 0x1A; // Clear O/P on compare

T1SC = 0x06;   // Start timer, prescale 64

 

EnableInterrupts;

 

Regards,

Mac

Message Edited by bigmac on 2009-11-07 04:43 PM
0 Kudos
415 Views
marvicdigital
Contributor II

Thanks bigmac. last night I wrote of memory and the real name of the registries did not remember, excusme,and no get linker errors in the CW,  thi is the real name registries:

 

T2SC
T2MOD

T1SC
T1SC_TOF

 

 

if use the PWM is possible to obtain frequencies of 5 Hz up to 100 Hz and width of pulse from 20uS to 2000uS?

 

Thanks for your help.

 

Bye.

 

 

 

0 Kudos
415 Views
bigmac
Specialist III

Hello, 


marvicdigital wrote:

if use the PWM is possible to obtain frequencies of 5 Hz up to 100 Hz and width of pulse from 20uS to 2000uS?

 

With a 5 MHz bus frequency, the formula for calculating the T1MOD value, to correspond with a pulse repetition frequency F hertz, would be:

 

MOD = 5000000/P/F - 1

where P is the prescale value.

 

For a minimum frequency of 5 Hz, this suggests that a prescale setting of 16 would be optimum, giving a maximum value for T1MOD of 62499, and a pulse width increment of 3.2 us. T1CH1 = 6 would give a pulse width of 19.2 us, the nearest value to 20 us achievable.

 

If you require exactly 20 us pulse width, perhaps use a bus frequency of 4 MHz or 8 MHz, rather than 5 MHz.

 

Regards,

Mac

0 Kudos
415 Views
marvicdigital
Contributor II

bigmac, thanks for  heplme and thanks to explain the PWM unit I understood to me well I will use and it in next projects.

 

Regards.

 

Vic.

0 Kudos
416 Views
marvicdigital
Contributor II

Ok, I found the solution with the  processor expert, I made the modifications and now it works to me well. Thanks to all by the aid.

 

 

void TM0_Cal (void){                              T1SC=0x30;          T1MOD = Vreal_Frecuencia;           (void)T1SC;          T1SC=0x46;                                      }void TM1_Cal (void){                    T2SC=0x30;          T2MOD = Vreal_AnchoPulso;                (void)T2SC;          T2SC=0x36;                    }interrupt 6 void INT_TIM1Ovrisr(void){                    T2SC=0x30;          (void)T2SC;          T2SC=0x46;                     PTD_PTD5 = 1;          T1SC=0x30;          (void) T1SC;          T1SC=0x46;                             }interrupt 9 void INT_TIM2Ovrisr(void){                              PTD_PTD5 = 0;          T2SC = 0x30;          (void) T2SC;          T2SC=0x36;                    }

 

 

Bye.

 

 

0 Kudos