系統(tǒng)時鐘和定時器——PWM定時器
S3C2440 CPU核的工作電壓為1.2V時,主頻可以達到300MHz;工作電壓為1.3V時,主頻可以達到400MHz。開發(fā)板為12MHz,需要通過時鐘控制邏輯的PLL提高系統(tǒng)時鐘。
本文引用地址:http://m.butianyuan.cn/article/201611/316624.htmS3C2440有兩個PLL:MPLL和UPLL。UPLL專用于USB設(shè)備,MPLL用于設(shè)置FCLK、HCLK、PCLK。
其中 m=MDIV+8, p=PDIV+2, s=SDIV
CLKDIVN寄存器:用于設(shè)置FCLK、HCLK、PCLK 三者比例。各種時鐘比例對應(yīng)的寄存器設(shè)置如下圖所示:
程序分析如下:(這程序是趙春江老師的程序)
#include "2440addr.h"
#define U32 unsigned int
typedef unsigned char BOOL;
#define TRUE 1
#define FALSE 0
BOOL stop;
static void __irq Key3_ISR(void) /*暫停鍵,關(guān)閉蜂鳴器*/
{
rSRCPND = rSRCPND | (0x1<<2); /*定義EINT2*/
rINTPND = rINTPND | (0x1<<2);
rTCON &= ~0x8; /*禁止定時器自動重載,即關(guān)閉定時器0111*/
stop = TRUE;
}
void __irq Key2_ISR(void) /*重啟鍵,開啟蜂鳴器*/
{
rSRCPND = rSRCPND | 0x1; /*定義EINT0*/
rINTPND = rINTPND | 0x1;
stop = FALSE;
}
void delay(int a)
{
int k;
for(k=0;k
;
}
void Main(void)
{
int freq;
rGPBCON = 0x155556; /*B0為TOUT0,B5~B8為輸出,給LED 0001 0101 0101 0101 0101 0110*/
rGPBUP = 0x7ff; /*0111 1111 1111關(guān)閉上拉使能*/
rGPFCON = 0xaaaa; /*F口為EINT,給按鈕 1010 1010 1010 1010*/
/*按鈕的一些必要配置*/
rSRCPND = 0x0f; /*中斷設(shè)置*/
rINTMSK = ~0x0f;
rINTPND =0x0f;
rEXTINT0 = 0x2222; /*EINT0/EINT1均設(shè)置為下降沿觸發(fā)*/
freq = 2500;
rTCFG0 &= 0xFFFF00;
rTCFG0 |= 0x31; /*prescal 是49 3*16+1=49 timer0 and timer1*/
rTCFG1 &= ~0xF; /*低四位清零 divider value=1/2,因為PCLK為50MHz,所以50MHz/50/2=500kHz/
rTCNTB0 = 5000; /*定時器計數(shù)初始值*/
rTCMPB0 = freq; /*定時器比較值*/
rTCON &= ~0x1F;
rTCON |= 0xf; /*死區(qū)無效,自動裝載,電平反轉(zhuǎn),手動更新,定時器開啟*/
rTCON &= ~0x2 ; /*手動更新位清零,PWM開始工作*/
pISR_EINT0 = (U32)Key2_ISR;
pISR_EINT2 = (U32)Key3_ISR;
stop = FALSE;
rGPBDAT = ~0x60; /*兩個LED亮*/
while(1)
{
/*頻率遞增*/
for ( ; freq<4950 ; )
{
freq+=10;
rTCMPB0 = freq; /*重新賦值*/
delay(20000);
while (stop == TRUE) /*是否暫停*/
{
delay(1000);
if (stop ==FALSE) /*判斷是否重啟*/
{
rTCON &= ~0x1F;
rTCON |= 0xf;
rTCON &= ~0x2 ; /*恢復(fù)PWM功能*/
}
}
/*4個LED隨著頻率的高低,時滅時亮。燈亮的數(shù)目4-3-2-1*/
if(freq == 100)
rGPBDAT = ~0x560; /*0001 1110 0000 取反之后1110 0001 1111*/
if(freq == 1300)
rGPBDAT = ~0x160; /*1110 0000取反之后 0001 1111*/
if(freq == 2500)
rGPBDAT = ~0x60; /*0110 0000取反之后 1001 1111*/
if(freq == 3700)
rGPBDAT = ~0x20; /*0010 0000取反之后 1101 1111*/
if(freq == 4900)
rGPBDAT = ~0x0; /*0000取反之后 1111111111*/
}
/*頻率遞減*/
for( ; freq>50 ; )
{
freq-=10;
rTCMPB0 = freq;
delay(20000);
while (stop == TRUE)
{
delay(1000);
if (stop ==FALSE)
{
rTCON &= ~0x1F;
rTCON |= 0xf;
rTCON &= ~0x2 ;
}
}
if(freq == 100)
rGPBDAT = ~0x560;
if(freq == 1300)
rGPBDAT = ~0x160;
if(freq == 2500)
rGPBDAT = ~0x60;
if(freq == 3700)
rGPBDAT = ~0x20;
if(freq == 4900)
rGPBDAT = ~0x0;
}
}
}
TOUT輸出的波形是:
評論