S3C2440 測試程序(一)PWM控制蜂鳴器測試
while(1)
{
U8 idx;
Uart_Printf("nPlease select function : n");
for(i=0; CmdTip[i].fun!=0; i++)
Uart_Printf("%d : %sn", i, CmdTip[i].tip);
idx = Uart_GetIntNum_GJ() ;
if(idx
{
(*CmdTip[idx].fun)();
Delay(20);
Uart_Init( 0,115200 );
}
}
struct { void (*fun)(void); char *tip; }CmdTip[] = { { Temp_function, "Please input 1-11 to select test" } , { BUZZER_PWM_Test, "Test PWM" } , { RTC_Display, "RTC time display" } , { Test_Adc, "Test ADC" } , { KeyScan_Test, "Test interrupt and key scan" } , { Test_Touchpanel, "Test Touchpanel" } , { Lcd_TFT_Test, "Test TFT LCD" } , { Test_Iic, "Test IIC EEPROM" } , { PlayMusicTest, "UDA1341 play music" } , { RecordTest, "UDA1341 record voice" } , { Test_SDI, "Test SD Card" } , { Camera_Test, "Test CMOS Camera"}, { 0, 0} };
0.Temp_function
void Temp_function() { Uart_Printf("nPlease input 1-11 to select test!!!n"); }
注:該行為打醬油
1.BUZZER_PWM_Test
/****************************************************************************
蜂鳴器測試 ****************************************************************************/ void BUZZER_PWM_Test( void ) { U16 freq =800;// lci 1000 ; Uart_Printf( "nBUZZER TEST ( PWM Control )n" ); Uart_Printf( "Press +/- to increase/reduce the frequency of BUZZER !n" ) ; Uart_Printf( "Press 'ESC' key to Exit this program !nn" ); Buzzer_Freq_Set( freq ) ;
/***************************************************************
void Buzzer_Freq_Set( U32 freq ) { //使用Timer0 rGPBCON &= ~3; //set GPB0=10 as tout0, pwm output rGPBCON |= 2; rTCFG0 &= ~0xff; rTCFG0 |= 15; //prescaler = 15+1 rTCFG1 &= ~0xf; rTCFG1 |= 2; //Timer0 mux = 1/8 rTCNTB0 = (PCLK>>7)/freq; //FCLK = 400M HCLK = 100M PCLK = 50M rTCMPB0 = rTCNTB0>>1; // 50% rTCON &= ~0x1f; rTCON |= 0xb; // 0 , 1 0 1 1 //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0 rTCON &= ~2; //clear manual update bit } 如何才能得到精確的定時(shí)呢?那就要靠TCFG0和TCFG1這兩個(gè)寄存器來配置定時(shí)器的頻率,即要確定TCNTOn每遞減一個(gè)數(shù)所需要的時(shí)間,它們之間是倒數(shù)的關(guān)系。具體的計(jì)算公式為:
定時(shí)器輸出時(shí)鐘頻率=PCLK ÷ (prescaler+1) ÷ divider
其中prescaler值由TCFG0決定,divider值由TCFG1決定,而prescaler只能取0~255之間的整數(shù),divider只能取2、4、8和16。
比如已知PCLK為50MHz,而我們?nèi)rescaler為15,divider為8,則定時(shí)器頻率為50000000/(15+1)/8=390625HZ,則TCNTOn每遞減一個(gè)數(shù)所需時(shí)間為T=1000/390625MS
注:為什么程序里rTCNTB0 = (PCLK>>7)/freq;要移7位,因?yàn)橐?位就等于PCLK/0x80,即PCLK>>7等效于 50000000/(15+1)/8=390625HZ,設(shè)完成一次PWM所需要的時(shí)間為t,則t/T=TCNTOn=rTCNTB0,計(jì)算得t=1000/freq(ms) 用示波器驗(yàn)證:freq = 10時(shí),一個(gè)脈沖周期為100ms,高低電平各50ms,freq=800時(shí),一個(gè)脈沖周期為1.25ms
現(xiàn)在我們要得到0.5s的中斷的話,TCNTOn=500/T=390625/2=195312.5,即freq=2
rTCMPB0 = rTCNTB0>>1;又是怎么理解的呢?該句等效于rTCMPB0 = rTCNTB0/2.就是說計(jì)數(shù)一半了翻轉(zhuǎn)。 那么究竟是高變低還是低變高呢。我改成了rTCMPB0 = rTCNTB0>>2;就是除4了,用示波器一看,明白了,高電平占用了1/4,低電平占用了3/4,總結(jié)如下:高電平到了rTCMPB0 就翻轉(zhuǎn)成低電平直到下一個(gè)脈沖周期開始才翻轉(zhuǎn)回高電平。
rTCNTB0決定了PWM脈沖周期,rTCMPB0 決定占空比。
我們得到每遞減一個(gè)數(shù)所需要的時(shí)間后,根據(jù)PWM輸出波形要求,就可以計(jì)算出TCMPBN和TCNTBN的值,得到所需要的波形。 ******************************************************************/ while( 1 ) { U8 key = Uart_Getch(); if( key == '+' ) { if( freq < 2000 ) //lci 20000 freq += 10 ; Buzzer_Freq_Set( freq ) ; } if( key == '-' ) { if( freq > 11 ) freq -= 10 ; Buzzer_Freq_Set( freq ) ; } Uart_Printf( "tFreq = %dn", freq ) ; if( key == ESC_KEY ) { Buzzer_Stop() ;
/**********************************************
void Buzzer_Stop( void ) { rGPBCON &= ~3; //set GPB0 as output rGPBCON |= 1; rGPBDAT &= ~1; }
***********************************************/ return ; } }
}