本帖最后由 zhaor 于 2011-11-17 17:42 编辑
蜂鸣器不是板子上的,是用的类似贺年卡上的。
这样设置的
开机串口首先输出这样的信息:
+------------------------------------------------------------------------+
| PWM Driver Sample Code |
| |
+------------------------------------------------------------------------+
This sample code will use PWM0 to drive Buzzer .
I/O configuration:
GPA12 <--> Buzzer
Please Select Test Item
0 : PWM Timer Waveform Test
1 : Exit
如果输入0,则提示:
PWM Timer Waveform Test. Waveform output(PWM0) to Buzzer
Select Test Item
1: Do (256Hz)
2: Re (287Hz)
3: Mi (322Hz)
4: Fa (341Hz)
5: Sol(2000Hz)
6: La (4000Hz)
7: Si (8000Hz)
0: Exit
假设这时如果输入2,则蜂鸣器输出287Hz的声音。蜂鸣器一端接在R12任意一侧即可,另一端接地。这个时候L6也闪烁。
程序是在例程的基础上修改而成的。
- /*---------------------------------------------------------------------------------------------------------*/
- #include "config.h"
- /*---------------------------------------------------------------------------------------------------------*/
- /* Global variables */
- /*---------------------------------------------------------------------------------------------------------*/
- uint8_t volatile g_u8PWMCount = 1;
- uint16_t g_u16Frequency; //PWM频率输出值
- static uint32_t s_u32Pulse = 0;
- /*************************************************************************************
- ** Function name: GPIO_Configuration
- ** Descriptions: GPIO配置
- *************************************************************************************/
- void GPIO_Configuration(void)
- {
- DrvGPIO_Open( E_GPB, 14, E_IO_INPUT ); //按键端口设置为输入
- DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* PWM Timer Callback function */
- /*---------------------------------------------------------------------------------------------------------*/
- void DRVPWM_PwmIRQHandler()
- {
- if (s_u32Pulse == 1 * g_u16Frequency /10)
- {
- /*--------------------------------------------------------------------------------------*/
- /* Stop PWM Timer 0 (Recommended procedure method 2) */
- /* Set PWM Timer counter as 0, When interrupt request happen, disable PWM Timer */
- /*--------------------------------------------------------------------------------------*/
- DrvPWM_SetTimerCounter(DRVPWM_TIMER2, 0); //中断中关闭PWM。
- }
- if (s_u32Pulse == 1 * g_u16Frequency /10 + 1)
- {
- g_u8PWMCount = 0;
- }
- s_u32Pulse++;
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* Main Function */
- /*---------------------------------------------------------------------------------------------------------*/
- int32_t main (void)
- {
- S_DRVPWM_TIME_DATA_T sPt;
- uint8_t u8Item;
- uint8_t u8Timer;
- int32_t i32Loop = 1;
- int32_t i32TestLoop = 1;
- /************配置系统时钟***************************/
- UNLOCKREG();
- SYSCLK->PWRCON.XTL12M_EN = 1;
- /* Waiting for 12M Xtal stalble */
- DrvSYS_Delay(5000);
- /*Descriptions: USART初始化 */
- UART_Configuration();
- /*Descriptions: GPIO初始化 */
- GPIO_Configuration();
- /* Enable PWM clock */
- DrvPWM_Open(); //打开PWM
- /* Set PWM pins */
- DrvGPIO_InitFunction(E_FUNC_PWM01);
- // DrvGPIO_InitFunction(E_FUNC_PWM23);
- // DrvGPIO_InitFunction(E_FUNC_PWM45);
- // DrvGPIO_InitFunction(E_FUNC_PWM67);
- /************配置系统时钟和解锁***************************/
- UNLOCKREG();
- DrvSYS_SelectHCLKSource(0);
- LOCKREG();
- /************关闭蜂鸣器***************************/
- (DrvGPIO_ClrBit(E_GPB,10)); //关闭蜂鸣器
- /************串口输出信息***************************/
- printf("+------------------------------------------------------------------------+\n");
- printf("| PWM Driver Sample Code |\n");
- printf("| |\n");
- printf("+------------------------------------------------------------------------+\n");
- printf(" This sample code will use PWM0 to drive Buzzer .\n");
- printf(" I/O configuration:\n");
- printf(" GPA12 <--> Buzzer\n");
- while (i32Loop)
- {
- printf("\n Please Select Test Item\n");
- printf(" 0 : PWM Timer Waveform Test\n");
- printf(" 1 : Exit\n\n");
- u8Item = getchar();
- switch (u8Item)
- {
- case '0':
- {
- uint8_t u8ItemOK;
- printf("\nPWM Timer Waveform Test. Waveform output(PWM0) to Buzzer\n");
- i32TestLoop = 1;
- printf("Select Test Item\n");
- printf(" 1: Do (256Hz)\n");
- printf(" 2: Re (287Hz)\n");
- printf(" 3: Mi (322Hz)\n");
- printf(" 4: Fa (341Hz)\n");
- printf(" 5: Sol(2000Hz) \n");
- printf(" 6: La (4000Hz)\n");
- printf(" 7: Si (8000Hz)\n");
- printf(" 0: Exit\n");
- while (i32TestLoop)
- {
- u8ItemOK = 1;
- u8Item = getchar();
- switch (u8Item)
- {
- case '1':
- g_u16Frequency = 256;
- break;
- case '2':
- g_u16Frequency = 287;
- break;
- case '3':
- g_u16Frequency = 322;
- break;
- case '4':
- g_u16Frequency = 341;
- break;
- case '5':
- g_u16Frequency = 2000;
- break;
- case '6':
- g_u16Frequency = 4000;
- break;
- case '7':
- g_u16Frequency = 8000;
- break;
- case '0':
- i32TestLoop = 0;
- break;
- default:
- u8ItemOK = 0;
- break;
- }
- if (i32TestLoop && u8ItemOK)
- {
- s_u32Pulse = 0;
- g_u8PWMCount = 1;
- /* PWM Timer property */
- sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; /*自动重载模式*/
- sPt.u32Frequency = g_u16Frequency; //PWM 频率
- sPt.u8HighPulseRatio = 50; /* 高脉冲宽度时间所占周期的百分比 50 : 100 */
- sPt.i32Inverter = 0; /*反向打开1或者关闭0*/
- u8Timer = DRVPWM_TIMER0; /*PWM设定0、1、2、3*/
- /* Select PWM engine clock */
- DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK);
- /* Set PWM Timer0 Configuration */
- DrvPWM_SetTimerClk(u8Timer, &sPt);
- /* Enable Output for PWM Timer0 */
- DrvPWM_SetTimerIO(u8Timer, 1);
- /* Enable Interrupt Sources of PWM Timer0 and install call back function */
- DrvPWM_EnableInt(u8Timer, 0, DRVPWM_PwmIRQHandler);
- /* Enable the PWM Timer 0 */
- DrvPWM_Enable(u8Timer, 1);
- while (g_u8PWMCount);
- /*--------------------------------------------------------------------------------------*/
- /* Stop PWM Timer0 (Recommended procedure method 2) */
- /* Set PWM Timer counter as 0, When interrupt request happen, disable PWM Timer */
- /* Set PWM Timer counter as 0 in Call back function */
- /*--------------------------------------------------------------------------------------*/
- /* Disable the PWM Timer 0 */
- DrvPWM_Enable(u8Timer, 0);
- }
- }
- break;
- }
- case '1':
- {
- i32Loop = 0;
- break;
- }
- }
- }
- DrvPWM_Close();
- return 0;
- }
文件
|