📄 main.c
字号:
#include "..\inc\option.h"
#include "..\inc\def.h"
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\rtc.h"
#include "..\inc\Pwm.h"
#include "..\inc\timer.h"
void Isr_Init(void);
void HaltUndef(void);
void HaltSwi(void);
void HaltPabort(void);
void HaltDabort(void);
void Rtc_Tick(void);
void Rtc_display(void);
char *date[8] = {"","SUN","MON","TUE","WED","THU","FRI","SAT"};
volatile unsigned int sec_tick;
int year,month,day,weekday,hour,min,sec;
#define READ_COUNT2 (rTCNTO2 & 0xffff)
#define MaxBeepFreq 50000
#define MinBeepFreq 20
extern int Uart_GetIntNum(void);
void SetBeepPwm(unsigned short Freq, unsigned char HiRatio);
void StopBeepPwm(void);
void __irq TIMER2_int(void);
//volatile int isWdtInt;
unsigned int pwmcnt=0;
/****************************************
* PWM (BEEP) test *
****************************************/
void Test_Pwm(void)
{
int beginf, endf;
int i, j;
Uart_Printf("[pwm (beep) Test]\n");
Uart_Printf("input begin f= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
beginf = Uart_GetIntNum();
while (beginf<MinBeepFreq ||beginf>=MaxBeepFreq)
{
Uart_Printf("ERROR !!!! input begin f= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
beginf = Uart_GetIntNum();
}
Uart_Printf("input end f= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
endf = Uart_GetIntNum();
while (endf<MinBeepFreq ||endf>=MaxBeepFreq ||endf < beginf )
{
Uart_Printf("ERROR !!!! input end f> beginf (%d--%d) : ",MinBeepFreq,MaxBeepFreq);
endf = Uart_GetIntNum();
}
Uart_Printf("beginf= %d ; endf=%d : ", beginf, endf );
rINTCON = 5;
rINTMOD = 0;
rINTMSK=~(BIT_GLOBAL|BIT_TIMER2);
pISR_TIMER2= (unsigned int)TIMER2_int; //(unsigned)
rPDATE = 0x157; //Beep = 10
rPCONE = 0x596b; // tout2
rPUPE = 0xff;
for (i=beginf; i<endf; i++)
{
pwmcnt=0;
SetBeepPwm( i, 50);
for (pwmcnt=0,j=0; pwmcnt<5;)
{
Uart_Printf("j=%d\n",j++);
}
for (j=0; j<5;)
{
while (READ_COUNT2>=1)
{
}
j++;
}
}
StopBeepPwm();
}
void SetBeepPwm(unsigned short Freq, unsigned char HiRatio)
{
if(Freq>MaxBeepFreq)
Freq = MaxBeepFreq;
if(HiRatio>100)
HiRatio = 100;
rTCON &= 0xffff0fff; // clear manual update bit, stop Timer2
rTCFG0 &= 0xffff00ff; // set Timer 2&3 prescaler 0
rTCFG1 &= 0xfffff0ff; // set Timer 2 MUX 1/16
rTCFG1 |= 0x00000300;
rTCNTB2 = MCLK/(Freq*16); //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
rTCMPB2 = (rTCNTB2*(100-HiRatio))/100; //if set inverter on, when TCNT2<=TCMP2, TOUT is low, TCNT2>TCMP2, TOUT is high
rTCON |= 0x00002000; // manual update
rTCON &= 0xffff0fff; // clear manal update bit
rTCON |= 0x0000d000; // auto reload, inverter on, start Timer 2
}
void StopBeepPwm(void)
{
rTCON &= ~0x1000;
}
void __irq TIMER2_int(void)
{
rI_ISPC=BIT_TIMER2;
pwmcnt++;
return ;
}
void Display_Rtc(void)
{
int year;
int month,day,weekday,hour,min,sec;
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
// Uart_Printf("This test should be excuted once RTC test(Alarm) for RTC initialization\n");
while(1) //避免读入期间时间跳变引起时间错误
{
if(rBCDYEAR == 0x99)
year = 0x1999;
else
year = 0x2000 + rBCDYEAR;
month=rBCDMON;
day=rBCDDAY;
weekday=rBCDDATE;
hour=rBCDHOUR;
min=rBCDMIN;
sec=rBCDSEC;
if(sec!=0)
break;
}
Uart_Printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%4x,%2x,%2x,%s,%2x:%2x:%2x",year,month,day,date[weekday],hour,min,sec);
rRTCCON = 0x0; // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
}
void Rtc_display(void)
{
rI_ISPC=BIT_TICK;
if(rBCDYEAR == 0x99)
year = 0x1999;
else
year = 0x2000 + rBCDYEAR;
month=rBCDMON;
day=rBCDDAY;
weekday=rBCDDATE;
hour=rBCDHOUR;
min=rBCDMIN;
sec=rBCDSEC;
Uart_Printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%4x,%2x,%2x,%s,%2x:%2x:%2x",year,month,day,date[weekday],hour,min,sec);
}
void DisplayRtc(void)
{
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
// Rtc_Init();
while(1)
{
if(rBCDYEAR == 0x99)
year = 0x1999;
else
year = 0x2000 + rBCDYEAR;
month=rBCDMON;
day=rBCDDAY;
weekday=rBCDDATE;
hour=rBCDHOUR;
min=rBCDMIN;
sec=rBCDSEC;
if(sec!=0)
break;
}
Uart_Printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%4x,%2x,%2x,%s,%2x:%2x:%2x",year,month,day,date[weekday],hour,min,sec);
rRTCCON = 0x00; // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
}
volatile int isRtcInt;
void Rtc_Int(void)
{
rI_ISPC=BIT_RTC;
//rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
Uart_Printf("RTC Alarm Interrupt O.K.\n");
isRtcInt=1;
}
int Test_Rtc_Alarm(void)
{
Uart_Printf("[RTC Alarm Test for S3C44B0X]\n");
Uart_Printf("Please check the VDDRTC connector or connect the VDD to Bt1+ !!!\n");
Rtc_Init();
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
rALMYEAR=rBCDYEAR ;
rALMMON =rBCDMON;
rALMDAY =rBCDDAY ;
rALMHOUR=rBCDHOUR ;
rALMMIN =rBCDMIN ;
if(rBCDSEC<0X30)
{
rALMSEC =0x30;}
else
{
rALMSEC =0x59;}
/* rALMYEAR=TESTYEAR2 ;
rALMMON =TESTMONTH2;
rALMDAY =TESTDAY2 ;
rALMHOUR=TESTHOUR2 ;
rALMMIN =TESTMIN2 ;
rALMSEC =TESTSEC2+9; */
isRtcInt=0;
pISR_RTC=(unsigned int)Rtc_Int;
rRTCALM=0x7f;
rINTMSK=~(BIT_GLOBAL|BIT_RTC);
while(isRtcInt==0);
rINTMSK|=BIT_GLOBAL;
rRTCCON = 0x0; // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
return 1;
}
void Rtc_Init(void)
{
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
rBCDYEAR = TESTYEAR;
rBCDMON = TESTMONTH;
rBCDDAY = TESTDAY; // SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
rBCDDATE = TESTDATE;
rBCDHOUR = TESTHOUR;
rBCDMIN = TESTMIN;
rBCDSEC = TESTSEC;
}
void Test_Rtc_Tick(void)
{
Uart_Printf("[RTC Tick interrupt test for S3C44B0X]\n");
Uart_Printf("Please check the VDDRTC connector or connect the VDD to Bt1+ !!!\n");
Uart_Printf("Typing any key to exit!!!\n");
pISR_TICK=(unsigned)Rtc_Tick;
sec_tick=1;
rINTMSK=~(BIT_GLOBAL|BIT_TICK);
rRTCCON=0x0; //R/W disable(for power consumption), 1/32768, Normal(merge), No reset
rTICINT = 127+(1<<7); //START
Uart_Getch();
rINTMSK |= (BIT_GLOBAL | BIT_TICK);
rRTCCON=0x0; //END
}
void Rtc_Tick(void)
{
rI_ISPC=BIT_TICK;
Uart_Printf("\b\b\b\b\b\b\b%03d sec",sec_tick++);
}
void * function[][2]=
{
(void *)Display_Rtc, "RTC(display) ",
(void *)Test_Rtc_Alarm, "RTC(Alarm) ",
(void *)Test_Rtc_Tick, "RTC Tick ",
0,0
};
int main(void)
{
char aa;
//int i;
rSYSCFG=SYSCFG_8KB;//define in option.h
#if (PLLON==1)
ChangePllValue(PLL_M,PLL_P,PLL_S);//declare in 44blib.h ,despcribe in 44blib.c
#endif
Isr_Init();
Port_Init();//from 44blib.c
Uart_Init(0,115200);//from 44blib.c
Uart_Select(0);// from 44blib.c
Delay(0); //calibrate Delay()
Led_Display(0x07);
Delay(1000); //calibrate Delay()
Led_Display(0);
Delay(5000); //calibrate Delay()
Led_Display(0x07);// from 44blib.c
Uart_Printf("\n startledshow \n");// from 44blib.c
Uart_Printf("\n press y to continue or any key to exit \n");
Rtc_Init();
/*******************
aa= Uart_Getch();
if((aa=='Y')||(aa=='y'))
{while(1)
{
i=0;
Uart_Printf("\n");
while(1)
{ //display menu
Uart_Printf("%2d:%s",i+1,function[i][1]);
i++;
if((int)(function[i][0])==0)
{
Uart_Printf("\n");
break;
}
if((i%4)==0)
Uart_Printf("\n");
}
Uart_Printf("\nSelect the function to test?");
i=Uart_GetIntNum();
i--;
Uart_Printf("\n");
if(i>=0 && (i<(sizeof(function)/8)) )
( (void (*)(void)) (function[i][0]) )();
}
}
}
*****************************/
//while(1)
//{
Delay(5000); //calibrate Delay()
Led_Display(1);
Delay(1000); //calibrate Delay()
Led_Display(3);
Delay(5000); //calibrate Delay()
Led_Display(4);
Delay(2000);
// DisplayRtc();
Delay(1000);
Display_Rtc();
Delay(1000);
Rtc_display();
Delay(5000);
Test_Pwm();
aa= Uart_Getch();
Uart_Printf("\n press Y/y to continue ? \n ");
if((aa=='Y')||(aa=='y'))
Test_Pwm();
//DisplayRtc();
// Delay(50000);
//}
}
/************************
void * function[][2]=
{
(void *)Display_Rtc, "RTC(display) ",
(void *)Test_Rtc_Alarm, "RTC(Alarm) ",
(void *)Test_Rtc_Tick, "RTC Tick ",
0,0
};
void Main(void)
{
char aa;
int i;
rSYSCFG=SYSCFG_8KB;//define in option.h
#if (PLLON==1)
ChangePllValue(PLL_M,PLL_P,PLL_S);//declare in 44blib.h ,despcribe in 44blib.c
#endif
Isr_Init();
Port_Init();
Uart_Init(0,115200);
Led_Display(0xf);
Delay(0);
//Beep(0x1);
Uart_Select(0); //Select UART0//
Uart_Printf("\n\n*************************************************************************");
//Beep(0x0);
Uart_Printf("\n* 立泰电子工作室 *");
Uart_Printf("\n* -S3C44B0X功能部件:RTC测试- *");
Uart_Printf("\n* Version 1.11 *");
Uart_Printf("\n* Email:rao_dali@263.net *");
Uart_Printf("\n* UART Config--COM:115.2kbps,8Bit,NP,UART0 *");
Uart_Printf("\n*------------------Begin to Start RTC test,OK? (Y/N)--------------------*");
Led_Display(0x0);
Rtc_Init();
aa= Uart_Getch();
if((aa=='Y')||(aa=='y'))
while(1)
{
i=0;
Uart_Printf("\n");
while(1)
{ //display menu
Uart_Printf("%2d:%s",i+1,function[i][1]);
i++;
if((int)(function[i][0])==0)
{
Uart_Printf("\n");
break;
}
if((i%4)==0)
Uart_Printf("\n");
}
Uart_Printf("\nSelect the function to test?");
i=Uart_GetIntNum();
i--;
Uart_Printf("\n");
if(i>=0 && (i<(sizeof(function)/8)) )
( (void (*)(void)) (function[i][0]) )();
}
}
*****************/
void Isr_Init(void)
{
U32 i;
pISR_UNDEF=(unsigned)HaltUndef;//#define pISR_UNDEF (*(unsigned *)(_ISR_STARTADDRESS+0x4));#define _ISR_STARTADDRESS 0xc7fff00
pISR_SWI =(unsigned)HaltSwi;//
pISR_PABORT=(unsigned)HaltPabort;//
pISR_DABORT=(unsigned)HaltDabort;//
for(i=_RAM_STARTADDRESS;i<(_RAM_STARTADDRESS+0x20);i+=4)//#define _RAM_STARTADDRESS 0xc000000
{
*((volatile unsigned *)i)=0xEA000000+0x1FFE;
}
rINTCON=0x5; //中断控制寄存器Non-vectored interrupt mode,IRQ interrupt enable,FIQ disable define from 44.b
rINTMOD=0x0; //中断模式寄存器 All=IRQ mode
rINTMSK|=BIT_GLOBAL|BIT_EINT3; //中断屏蔽寄存器 All interrupt is masked.Global位与EINT3都置1 1=mask
}
U32 pIrqStart, pIrqFinish, pIrqHandler;
void HaltUndef(void)
{
Uart_Printf("Undefined instruction exception!!!\n");
while(1);
}
void HaltSwi(void)
{
Uart_Printf("SWI exception!!!\n");
while(1);
}
void HaltPabort(void)
{
Uart_Printf("Pabort exception!!!\n");
while(1);
}
void HaltDabort(void)
{
Uart_Printf("Dabort exception!!!\n");
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -