www.pudn.com > S3C44B0X_USB.rar > ctrl_set.c


#include "inc\44b0x.h" 
#include "DataType.h" 
 
#define	EXT_OSC_CLK	10000000 
 
unsigned int MCLK = 20000000; 
 
void ChangePllValue(int mdiv, int pdiv, int sdiv) 
{ 
	int i = 1;		 
	 
	rPLLCON = (mdiv<<12)|(pdiv<<4)|sdiv; 
	 
	while(sdiv--) 
		i *= 2;	 
	 
	MCLK = (EXT_OSC_CLK*(mdiv+8))/((pdiv+2)*i);		 
} 
 
 
/**************************************************************************** 
【功能说明】IO端口功能、方向设定 
****************************************************************************/ 
void PortInit(void) 
{ 
    //ADB V1.0 B/D Status 
    //USB D12SUSPD  
    //    PC0 
    //LED D0  D1  D2 
    //	  PC1 PC2 PC3 
    //KEY K0  K1  K2  K3 
    //	  PG4 PG5 PG6 PG7 
 
    //CAUTION:Follow the configuration order for setting the ports.  
    // 1) setting value  
    // 2) setting control register  
    // 3) configure pull-up resistor.   
	      
    //16bit data bus configuration   
    //PORT A GROUP 
    //rPCONA[9:0] 
    //|BIT9  |.......................................................|BIT0 
    //|ADDR24|ADDR23|ADDR22|ADDR21|ADDR20|ADDR19|ADDR18|ADDR17|ADDR16|ADDR0		       
    //|     0|     1|	  1|     1|     1|     1|     1|	 1|     1|    1 
    rPCONA=0x1ff;	 
 
    //PORT B GROUP 
    //rPCONB[10:0] 
    //|BIT10|....................................................|BIT0 
    //|nGCS5|nGCS4|nGCS3|nGCS2|nGCS1|nWBE3|nWBE2|nSRAS|nSCAS|SCLK|SCKE 
    //|    1|    1|    1|	 1|    1|    1|    1|	 1|    1|   1|   1 
    rPDATB=0x3ff; 
    rPCONB=0x3ff; 
     
    //PORT C GROUP 
    //BUSWIDTH=16 
    //PORT C GROUP 
    //rPCONC[31:0] 
    //|GPC15|....................................................................|    GPC0 
    //|BIT31|....................................................................|    BIT0 
    //|nCTS0|nRTS0|RXD1|TXD1|nCTS1|nRTS1|nEL|nDISP|VD4|VD6|VD5|VD7|LED2|LED1|LED0|D12SUSPD 
    //|   00|   00|  11|  11|   00|   00| 01|   01| 11| 11| 11| 11|  01|  01|  01|      01 
    //rPUPC[15:0] 
    //|    0|    0|   1|   1|    0|    0|  0|    0|  1|  1|  1|  1|   0|   0|   0|       0 
    rPDATC=0xffff;	//All I/O Is High 
    rPCONC=0x0f05ff55;	 
    rPUPC=0x30fe;	//PULL UP RESISTOR should be enabled to I/O 
 
    //PORT D GROUP 
    //rPCOND 
    //| BIT15|.........................|BIT0  
    //|VFRAME|VM|VLINE|VCLK|VD3|VD2|VD1|VD0 
    //|    10|10|   10|	 10| 10| 10| 10| 10   
    rPDATD=0xff; 
    rPCOND= 0x0; 
	rPUPD = 0x0; 
	//These pins must be set only after CPU's internal LCD controller is enable 
	 
	//rPCOND=0xaaaa;	 
    //rPUPD=0xff; 
 
    //PORT E GROUP 
    //rPCONE 
    //| BIT17 |..............................|BIT0 
    //|CODECLK|PE7|PE6|BEEP|PE4|PE3|RXD0|TXD0| PE0 
    //|     10| 01| 01|  01| 01| 01|  10|  10|  00 
    //|rPUPE[7:0] 
    //        |  0|  0|   0|  0|  0|   1|   1|   0 
    rPDATE=0x1ff;  	//All I/O Is High 
    rPCONE=0x25568;	//All NC is INPUT	 
    rPUPE=0x0df;	//PE8 do not have programmable pull-up resistor.   
 
    //PORT F GROUP 
    //rPCONF[21:0] 
    //| BIT21|....................................|  BIT0 
    //|IISCLK|PF7|IISDO|IISLRCK|PF4|PF3|PF2|IICSDA|IICSCL 
    //|   100|000|  100|    100| 00| 00| 00|    10|    10 
    //rPUPF[8:0] 
    //|     1|  0|    1|      1|  0|  0|  0|     1|     1 
    rPDATF=0x1ff;	//All I/O Is High 
    rPCONF=0x20900a;//All NC is INPUT 
    rPUPF=0x163; 
 
    //PORT G GROUP 
    //rPCONG[15:0] 
    //|BIT15|....................................|   BIT0 
    //| KEY3|KEY2|KEY1|KEY0|EXINT3|EXINT2|NET_INT|USB_INT 
    //|   11|  11|  11|  11|    11|    11|     11|     11 
    rPDATG=0xff; 
    rPCONG=0x00ff;	//KEY0~KEY3定义为I/O,采用查询方式 
    			//rPCONG=0xffff,KEY0~KEY3定义为中断,	 
    			 
    rPUPG=0x0;		//should be enabled   
     
    rSPUCR=0x7; 	//D15-D0 pull-up disable 
     
	rEXTINT=0x0;	//All EXTINT0-7 Low level interrupt 
	 
	//Define the Non_Cache area 
//	rNCACHBE0=(((Non_Cache_End)>>12)<<16)|((Non_Cache_Start)>>12);  
} 
 
	 
 
 
/*********************************************************/ 
#define RTC_YEAR0		(0x03)		//年 
#define RTC_MONTH0		(0x02)		//月 
#define RTC_DAY0		(0x0e)		//日 
#define RTC_DATE0		(0x06)		//星期SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7 
#define RTC_HOUR0		(0x08)		//小时 
#define RTC_MIN0		(0x08)		//分 
#define RTC_SEC0		(0x08)		//秒 
 
#define RTC_YEAR1		(0x03)		//年 
#define RTC_MONTH1		(0x03)		//月 
#define RTC_DAY1		(0x0e)		//日 
#define RTC_DATE1		(0x06)		//星期-SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7 
#define RTC_HOUR1		(0x08)		//小时 
#define RTC_MIN1		(0x08)		//分 
#define RTC_SEC1		(0x08)		//秒 
 
#define	RTC_RW_EN()	rRTCCON = 1		//|= 1 
#define	RTC_RW_DS()	rRTCCON &= 0xfe 
 
void RtcSetDay(TIME_STRUC *time) 
{ 
	RTC_RW_EN();				//RTC读写使能,选择BCD时钟、计数器,无复位,1/32768		 
	rBCDYEAR = time->year;;		//年 
    rBCDMON  = time->month;		//月 
    rBCDDAY  = time->day;		//日	 
	RTC_RW_DS(); 
} 
 
void RtcSetWeek(TIME_STRUC *time) 
{ 
	RTC_RW_EN(); 
	rBCDDATE = time->weekday; 
	RTC_RW_DS(); 
} 
 
void RtcSetTime(TIME_STRUC *time) 
{ 
	RTC_RW_EN();				//RTC读写使能,选择BCD时钟、计数器,无复位,1/32768		 
	rBCDHOUR = time->hour;		//小时 
    rBCDMIN  = time->min;		//分 
    rBCDSEC  = time->sec;		//秒 
	RTC_RW_DS(); 
} 
 
 
void RtcInit(TIME_STRUC *time) 
{ 
    RTC_RW_EN();				//RTC读写使能,选择BCD时钟、计数器,无复位,1/32768 
    rBCDYEAR = time->year;;		//年 
    rBCDMON  = time->month;		//月 
    rBCDDAY  = time->day;		//日 
    rBCDDATE = time->weekday;	//星期 
    rBCDHOUR = time->hour;		//小时 
    rBCDMIN  = time->min;		//分 
    rBCDSEC  = time->sec;		//秒 
     
    rTICNT = 0x80+127;		//enable tick timer interrupt, set tick timer interrupt time = (127+1)/128 second	 
    RTC_RW_DS();	 
} 
 
void RtcAlarm(TIME_STRUC *time)   
{ 
	rALMYEAR = time->year;		//年 
	rALMMON  = time->month;		//月 
	rALMDAY  = time->day;		//日 
	rALMHOUR = time->hour;		//小时 
	rALMMIN  = time->min;		//分 
	rALMSEC  = time->sec; 		//秒 
 
	rRTCALM = 0x7f;		//全局警报使能控制,年、月、日、星期、时分秒警报使能控制 
} 
 
void RtcGetTime(TIME_STRUC *time) 
{ 
	U8 m; 
 
	RTC_RW_EN();		//RTC读写使能 
	time->year = 2000 + rBCDYEAR; 
	time->weekday = rBCDDATE; 
 
	m = rBCDMON;	 
	time->month = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制 
 
	m = rBCDDAY;	 
	time->day = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制	 
 
	m = rBCDHOUR;	 
	time->hour = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制 
	 
	m = rBCDMIN;	 
	time->min = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制 
 
	m = rBCDSEC;			 
	time->sec = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制 
	 
    RTC_RW_DS();		//RTC读写禁止(降低功率消耗),选择BCD时钟、计数器,无复位,1/32768     
 
} 
 
void InitSysTimer(U8 Freq) 
{		 
	rTCON   &= 0xf0ffffff;			//clear manual update bit, stop Timer 5 	 
	rTCFG0  &= 0xff00ffff;			//定时器4/5的预分频为16 
	rTCFG0  |= (16-1)<<16; 
	rTCFG1  &= 0xff0fffff;			//定时器5的MUX为1/8 
	rTCFG1  |= 2<<20;	 
	rTCNTB5  = MCLK/(Freq*16*8);	//定时器5的重装值,在ENABLE之前设定	 
	rTCON   |= 0x02000000;			//定时器5的MANUAL UPDATE BIT设为1 
	rTCON   &= 0xf0ffffff;			//MANUAL UPDATE BIT清零 
	rTCON   |= 0x05000000;			//定时器5 start, 设为INTERVAL模式 
} 
	 
#define	MaxBeepFreq	20000 
#define	MinBeepFreq	20 
void SetBeepPwm(U16 Freq, U8 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 5 
}