Hello,
Currently I use S12G128 MCU 48pins and using TC0 in timer interrupt to set the period of pulse. In the same time I also use PTT0 as GPIO pin for PUSH Button Input because I will use the other pins for another purpose . But The PTT0 GPIO pins looks got affected by the TC0, how to avoid PTT0 get affected by TC0?
My code is as below:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define PERIOD 750
#define UP_1 PTT_PTT0 //button up1
#define DOWN_1 PTT_PTT1 //button down1
long time_count;
//==============================================================================
//SetPEEmodeBUSCLK
//==============================================================================
void SetPEEmodeBUSCLK(byte _synr, byte _refdv, byte _postdiv)
{
CPMUSYNR = _synr;
CPMUREFDIV = _refdv;
CPMUPOSTDIV = _postdiv;
CPMUOSC_OSCE = 1; //enable external oscillator OSCE
while(!CPMUFLG_UPOSC)
{// you can check for timeot here with error message report
};
while(!CPMUFLG_LOCK)
{// you can check for timeot here with error message report
};
//--- select clocks --------------------
CPMUCLKS = 0B10000011; // bus=fPLL/2; COP is clocked from OSCCLK
if(CPMUCLKS != 0B10000011) // After writing CPMUCLKS register, it is strongly recommended to read
{ // back CPMUCLKS register to make sure that write of PLLSEL,
asm nop; // RTIOSCSEL, COPOSCSEL0 and COPOSCSEL1 was successful.
}
//--------------------------------------
}
//==============================================================================
//TC0_ISR
//==============================================================================
#pragma CODE_SEG NON_BANKED
interrupt 8 void TC0_ISR(void)
{
PTP_PTP4 = ~PTP_PTP4; //toggle PTP4
PORTD_PD0 = ~PORTD_PD0; //toggle PTD0
time_count++;
TC0 = TC0 + PERIOD;
TFLG1=0x01;
}
#pragma CODE_SEG DEFAULT
//==============================================================================
//Timer_Init
//==============================================================================
void Timer_Init(void)
{
TIOS = 0x01; //channel 0 as output compare
TIE = 0x01; //interrupt enable
TSCR2 = 0x03; //prescaler = 8
TC0 = PERIOD;
TSCR1 = 0xA0; //timer enabled
}
void main(void) {
/* put your own code here */
ECLKCTL_NECLK = 1; // enable ECLK output (bus clock is visible on pin PB0 for TWR-S12G128)
SetPEEmodeBUSCLK(0x02, 0x80, 0x00); //24Mhz BUSCLK from 8 MHZ oscclk, PEE mode
DDRD = 0x0F; //LEDs output
DDRP = 0x0F; //LEDs output
DDRT=0x00; Port T as Input
PTP_PTP1=0;
PTP_PTP2=0;
Timer_Init();
EnableInterrupts;
for(;;)
{
_FEED_COP();
if (UP_1==0)
{ PTP_PTP1=0;}
else { PTP_PTP1=1;}
if (DOWN_1==0)
{ PTP_PTP2=0;}
else { PTP_PTP2=1;}
}
}
Thanks & Regards,
Lukman
Hi,
Have your tried reading PTIT (Port T Input Register) instead of PTT (Port T Data Register)?
Regards,
Daniel
Hello Daniel,
Thanks for your response, for this case, I already solve the problem with disconnect the OCPD channel 0.
I put OCPD_OCPD0=1; on my Timer_Init.
Thanks & appreciate your response.
Best Regards,
Lukman