2410beep.c
资源名称:arm_exam.rar [点击查看]
上传用户:mhstny
上传日期:2022-08-05
资源大小:793k
文件大小:2k
源码类别:
微处理器开发
开发平台:
Unix_Linux
- #include "2410addr.h"
- #include "2410lib.h"
- #include "def.h"
- void Buzzer_Freq_Set( U32 freq );
- void Buzzer_Stop( void );
- void BUZZER_PWM_Test( void );
- void xmain(void)
- {
- ChangeClockDivider(1, 1);
- ChangeMPllValue(0xa1,0x3,0x1);
- Port_Init();
- Uart_Select(0);
- Uart_Init(0, 115200);
- BUZZER_PWM_Test();
- while(1);
- }
- /****************************************************************************
- 【功能说明】蜂鸣器PWM测试
- ****************************************************************************/
- void BUZZER_PWM_Test( void )
- {
- U16 freq = 2000 ;
- 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 ) ;
- while( 1 )
- {
- U8 key = Uart_Getch();
- if( key == '+' )
- {
- if( freq < 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() ;
- return ;
- }
- }
- }
- void Buzzer_Freq_Set( U32 freq )
- {
- rGPBCON = rGPBCON & ~(3<<0)|(1<<1);//set GPB0 as tout0, pwm output
- rTCFG0 = rTCFG0 & ~0xff|15; //prescaler = 15
- rTCFG1 = rTCFG1 & ~0xf|2;//divider = 1/8
- rTCNTB0 = (PCLK>>7)/freq;//rTCNTB0=PCLK/{(prescaler+1) * divider *freq}
- rTCMPB0 = rTCNTB0>>1; //占空比50%
- //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
- rTCON = rTCON & ~0x1f|(0<<4)|(1<<3)|(0<<2)|(1<<1)|(1);
- rTCON &= ~(1<<1); //clear manual update bit
- }
- void Buzzer_Stop( void )
- {
- rGPBCON |= 1;
- rGPBCON = rGPBCON & ~3|1; //set GPB0 as output
- rGPBDAT &= ~1;//output 0
- }