//You should add to your code the following headers, declration of varaibles and codes.
#include "system.h"
#include "timer.h"
s32 sTimer;
s32 sPrescaler;
s32 sDivider;
s32 sEnDz;
s32 sDzlen;
s32 sAutoreload;
s32 sEnInverter;
s32 sEnInt;
s32 sEnDMA;
s32 sTCNTB;
s32 sTCMPB;
float fTimerclk;
float fTimerfreq;
u32 uTemp0;
u32 uTemp1;
u32 uTemp2;
u32 uTemp3;
s32 sContinue;
u32 uOpClk;
uOpClk = g_PCLK;
while(1)
{
//--------------------select timer -------------------------
// printf("Select Timer [0~4] :");
// sTimer = GetIntNum();
sTimer = 0;
if ( sTimer ==-1)
{
printf("\n");
break;
}
GPIO_SetFunctionEach(eGPIO_F,eGPIO_13,2); //PWM Ext
GPIO_SetFunctionEach(eGPIO_F,eGPIO_14,2); //PWM Tout0
GPIO_SetFunctionEach(eGPIO_F,eGPIO_15,2); //PWM Tout1
printf("\n");
//--------------------------------------------------------
//--------------------select prescaler-----------------------
// printf("\nSelect prescaler value [1~256] : ");
// sPrescaler = GetIntNum();
sPrescaler = 256;
if(sPrescaler == -1)
{
sPrescaler = 1;
}
else
{
}
//--------------------------------------------------------
//--------------------select dividerMUX---------------------
// printf("\nSelect dividerMUX value \n ");
// printf("0. 1/1 1. 1/2 2. 1/4 3. 1/8 4. 1/16 5. externalTCLK1\n");
// printf("[0~5] :");
// sDivider = GetIntNum();
sDivider = 3;
if(sDivider== -1)
{
sDivider= 1;
}
else if (sDivider == 5 )
{
printf("\n How much External CLK do you connect ?\n");
printf("MHz : ");
uOpClk = GetIntNum()*1000000;
}
//--------------------------------------------------------
//--------------------select Deadzone----------------------
// printf("\nSelect Deadzone\n");
// printf("0.Disable 1.Enable\n");
// printf("[0~1]:");
/// sEnDz = GetIntNum();
sEnDz = 0;
if(sEnDz== -1)
{
sEnDz= 0;
}
else
{
}
if(sEnDz == 1)
{
printf("\nSelect Deadzone Length\n");
printf("[0~255]: ");
sDzlen = GetIntNum();
if(sDzlen== -1)
{
sDzlen= 20;
}
else
{
}
}
else
{
sDzlen = 0;
}
//--------------------------------------------------------
//--------------------select Auto-reload---------------------
// printf("\nSelect Auto-reload\n");
// printf("0. One-shot 1. Auto-reload \n");
// printf("[0,1] :");
// sAutoreload = GetIntNum();
sAutoreload = 1;
if(sAutoreload == -1)
{
sAutoreload = 1;
}
else
{
}
//--------------------------------------------------------
//--------------------select Inverter enable------------------
// printf("\nSelect Inverter Enable");
// printf("0. Disable 1. Enblae");
// printf("[0.1] : ");
// sEnInverter = GetIntNum();
sEnInverter = 0;
if(sEnInverter == -1)
{
sEnInverter = 0;
}
else
{
}
//--------------------------------------------------------
//--------------------select interrupt enable------------------
// printf("\nSelect Interrupt Enable");
// printf("0. Disable 1. Enblae");
// printf("[0.1] : ");
// sEnInt = GetIntNum();
sEnInt = 0;
if(sEnInt == -1)
{
sEnInt = 1;
}
else
{
}
//--------------------------------------------------------
//--------------------select DMA enable---------------------
// printf("\nSelect DMA Enable");
// printf("0. Disable 1. Enblae");
// printf("[0.1] : ");
// sEnDMA = GetIntNum();
sEnDMA = 0;
if(sEnDMA== -1)
{
sEnDMA = 0;
}
else
{
}
if(sEnDMA == 1)
{
sEnDMA = sTimer+1;
}
//--------------------------------------------------------
//--------------------select TCNTB-----------------------
// printf("\nSelect TCNTB value : ");
// sTCNTB = GetIntNum();
sTCNTB = 400;
if(sTCNTB == -1)
{
sTCNTB = 2000;
}
else
{
}
//--------------------------------------------------------
//--------------------select TCMPB-----------------------
// printf("\nSelect TCMPB value : ");
// sTCMPB = GetIntNum();
sTCMPB = 200;
if(sTCMPB == -1)
{
sTCMPB = 1000;
}
else
{
}
//--------------------------------------------------------
//-----------------------Set SFRs--------------------------
uTemp0 = Inp32(rTCFG1);
uTemp0 = (uTemp0 & (~(0xf<<4*sTimer))& (~(1<<20))) |(sEnDMA<<20)|(sDivider<<4*sTimer);
Outp32(rTCFG1,uTemp0);
uTemp0 = Inp32(rTINT_CSTAT);
uTemp0 = (uTemp0 & (~(1<
switch(sTimer)
{
case 0:
uTemp0 = Inp32(rTCON);
uTemp0 = uTemp0 & (0xfffffffe);
Outp32(rTCON, uTemp0); // Timer0 stop
uTemp0 = Inp32(rTCFG0);
uTemp0 = (uTemp0 & (~(0xff00ff))) | ((sPrescaler-1)<<0) |(sDzlen<<16);
Outp32(rTCFG0, uTemp0);
Outp32(rTCNTB0, sTCNTB);
Outp32(rTCMPB0, sTCMPB);
uTemp1 = Inp32(rTCON);
uTemp1 = (uTemp1 & (~(0x1f))) |(sEnDz<<4)|(sAutoreload<<3)|(sEnInverter<<2)|(1<<1)|(0<<0);
Outp32(rTCON, uTemp1); //timer0 manual update
uTemp1 = (uTemp1 & (~(0x1f))) |(sEnDz<<4)|(sAutoreload<<3)|(sEnInverter<<2)|(0<<1)|(1<<0);
Outp32(rTCON, uTemp1); // timer0 start
break;
case 1:
uTemp0 = Inp32(rTCON);
uTemp0 = uTemp0 & (0xfffffeff);
Outp32(rTCON, uTemp0); // Timer1 stop
uTemp0 = Inp32(rTCFG0);
uTemp0 = (uTemp0 & (~(0xff00ff))) | ((sPrescaler-1)<<0) |(sDzlen<<16);
Outp32(rTCFG0, uTemp0);
Outp32(rTCNTB1, sTCNTB);
Outp32(rTCMPB1, sTCMPB);
uTemp1 = Inp32(rTCON);
uTemp1 = (uTemp1 & (~(0xf00))) | (sAutoreload<<11)|(sEnInverter<<10)|(1<<9)|(0<<8);
Outp32(rTCON,uTemp1); // timer1 manual update
uTemp1 = (uTemp1 & (~(0xf00))) | (sAutoreload<<11)|(sEnInverter<<10)|(0<<9)|(1<<8);
Outp32(rTCON,uTemp1); // timer1 start
break;
case 2:
uTemp0 = Inp32(rTCON);
uTemp0 = uTemp0 & (0xffffefff);
Outp32(rTCON, uTemp0); // Timer2 stop
uTemp2 = Inp32(rTCFG0);
uTemp2 = (uTemp2 & (~(0xffff00)))|((sPrescaler-1)<<8)|(sDzlen<<16);
Outp32(rTCFG0, uTemp2);
Outp32(rTCNTB2, sTCNTB);
Outp32(rTCMPB2, sTCMPB);
uTemp3 = Inp32(rTCON);
uTemp3 = (uTemp3 & (~(0xf000)))|(sAutoreload<<15)|(sEnInverter<<14)|(1<<13)|(0<<12);
Outp32(rTCON, uTemp3); // timer2 manual update
uTemp3 = (uTemp3 & (~(0xf000)))|(sAutoreload<<15)|(sEnInverter<<14)|(0<<13)|(1<<12);
Outp32(rTCON,uTemp3); // timer2 start
break;
case 3:
uTemp0 = Inp32(rTCON);
uTemp0 = uTemp0 & (0xfffeffff);
Outp32(rTCON, uTemp0); // Timer3 stop
uTemp2 = Inp32(rTCFG0);
uTemp2 = (uTemp2 & (~(0xffff00)))|((sPrescaler-1)<<8)|(sDzlen<<16);
Outp32(rTCFG0, uTemp2);
Outp32(rTCNTB3, sTCNTB);
Outp32(rTCMPB3, sTCMPB);
uTemp3 = Inp32(rTCON);
uTemp3 = (uTemp3 &(~(0xf0000)))|(sAutoreload<<19)|(sEnInverter<<18)|(1<<17)|(0<<16);
Outp32(rTCON, uTemp3); // timer3 manual update
uTemp3 = (uTemp3 &(~(0xf0000)))|(sAutoreload<<19)|(sEnInverter<<18)|(0<<17)|(1<<16);
Outp32(rTCON,uTemp3); // timer3 start
break;
case 4:
uTemp0 = Inp32(rTCON);
uTemp0 = uTemp0 & (0xffefffff);
Outp32(rTCON, uTemp0); // Timer4 stop
uTemp2 = Inp32(rTCFG0);
uTemp2 = (uTemp2 & (~(0xffff00)))|((sPrescaler-1)<<8)|(sDzlen<<16);
Outp32(rTCFG0, uTemp2);
Outp32(rTCNTB4, sTCNTB);
uTemp3 = Inp32(rTCON);
uTemp3 = (uTemp3 & (~(0xf00000)))|(sAutoreload<<22)|(1<<21)|(0<<20);
Outp32(rTCON,uTemp3); // timer4 manual update
uTemp3 = (uTemp3 & (~(0xf00000)))|(sAutoreload<<22)|(0<<21)|(1<<20);
Outp32(rTCON, uTemp3); // timer4 start
break;
}
if (sDivider == 5) // Using External TCLK
{
fTimerclk = (1/(float)uOpClk)*sTCNTB*1000;
fTimerfreq = 1/fTimerclk;
printf("Timer = %d\n",sTimer);
printf("OpCLK = %d \n",uOpClk);
printf("Timer Clock = %f msec , Timer Frequency = %f Khz\n\n",fTimerclk,fTimerfreq);
}
else
{
//(1/(PCLK/(Prescaler)/divider) * count(Max 4294967295) = Timer clock (Frequency)
fTimerclk = (1/(float)((float)uOpClk/((float)sPrescaler)/(1<<(sDivider))))*sTCNTB*1000;
fTimerfreq = 1/fTimerclk;
printf("Timer = %d\n",sTimer);
printf("OpCLK = %d , sPrescaler = %d, sDivider = %d dd= %d\n",uOpClk,sPrescaler,sDivider,(1<<(sDivider)));
printf("Timer Clock = %f msec , Timer Frequency = %f Khz\n\n",fTimerclk,fTimerfreq);
}
printf("Do you want to change other timers?\n");
printf("[0.no 1.yes]:");
sContinue = GetIntNum();
if (sContinue == 0)
break;
else
printf("\n");
}
}
No comments:
Post a Comment