Hi Radek,
I have one more question to the timer. Recently I try two channels TC0 & TC1 with defined period. The problem is when I combine with Hardware interrupt, The MCU become unstable, some time hangup. But when I run separately the Hardware Interrupt & Timer working fine, anything I miss?
*Note: on *.prm file I already include the Vector Address for Hardware Interrupt:
VECTOR ADDRESS 0xFFCE my_ISR
the code is as below:
//==============================================================================
//TC01_ISR
//==============================================================================
#pragma CODE_SEG NON_BANKED
interrupt 8 void TC01_ISR(void)
{
if(TFLG1_C0F)
{
TFLG1_C0F = TFLG1_C0F_MASK; //clear the flag
PORTD_PD0 = ~PORTD_PD0; //toggle PORT D1
PTP_PTP3 = ~PTP_PTP3; //toggle PTP4
time_count[0]++;
TC0 = TC0 + PERIOD;
}
if(TFLG1_C1F)
{
TFLG1_C1F = TFLG1_C1F_MASK; //clear the flag
PORTD_PD1 = ~PORTD_PD1; //toggle PORT D1
PTP_PTP4 = ~PTP_PTP4; //toggle PTP4
time_count[1]++;
TC1 = TC1 +(2*PERIOD);
}
}
#pragma CODE_SEG DEFAULT
//==============================================================================
//Timer_Init
//==============================================================================
void Timer_Init(void)
{
TIOS = 0x03; //channel 0 & 1 as output compare
TIE = 0x03; //interrupt enable channel 0 &1
TSCR2 = 0x03; //prescaler = 16
TC0 = PERIOD;
TC1 = (2*PERIOD);
TSCR1 = 0xA0; //timer enabled
OCPD = 0x03; //disconnect GPIO pin T0 & T1 from TC0 & TC1//
}
//==============================================================================
//Hardware interrupt for ripple_count
//==============================================================================
void Init_Hardware_interrupt_4(void)
{
PPSJ = 0x1E; //set pull-up PPSJ0..4 (pull-up/falling edge=0, pull-down/rising edge=1)
PERJ = 0x1E; //enable pull-up on PERJ0..4
PIEJ = 0x1E; //interrupt enable/disable setting on PERJ0..4
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void my_ISR(void)
{
if (PIFJ_PIFJ4==1)
{
if(runtime_direction[0]==1)
{
if(count[0]<2280)count[0]++;
}
else
{
if(count[0]>0) count[0]--;
}
PIFJ= 0x10;
}
if(PIFJ_PIFJ1==1)
{
if(runtime_direction[1]==1)
{
if(count[1]<2280) count[1]++;
}
else
{
if(count[1]>0) count[1]--;
}
PIFJ= 0x02;
}
if(PIFJ_PIFJ2==1)
{
if(runtime_direction[2]==1)
{
if(count[2]<2280) count[2]++;
}
else
{
if(count[2]>0) count[2]--;
}
PIFJ= 0x04;
}
if(PIFJ_PIFJ3==1)
{
if(runtime_direction[3]==1)
{
if(count[3]<2280) count[3]++;
}
else
{
if(count[3]>0) count[3]--;
}
PIFJ= 0x08;
}
}
#pragma CODE_SEG DEFAULT
void main(void) {
ECLKCTL_NECLK = 0;
SetPEEmodeBUSCLK(0x02, 0x80, 0x00);
DDRD = 0x03; //LEDs output
DDRP = 0xF8; //LEDs output
Timer_Init();
Init_Hardware_interrupt_4();
EnableInterrupts;
for(;;) {
_FEED_COP();
}
}
I appreciate your help.
Best Regards,
Lukman