金牌会员
- 积分
- 2092
- 金钱
- 2092
- 注册时间
- 2017-2-11
- 在线时间
- 306 小时
|
10金钱
本帖最后由 jiangyy 于 2020-6-22 14:09 编辑
学习裸机背光PWM输出后,我找到一个有源蜂鸣器,接上去,选用的是PWM_OUT6(IO口是GPIO3_IO24)。下面是我配置有源蜂鸣器的底层:1.beep.c
#include "bsp_beep.h"
#include "cc.h"
#include "bsp_int.h"
struct beep_dev_struc beep_dev;
/* BEEP初始化 */
void beep_init(void)
{
unsigned char i = 0;
/* 1、IO初始化 */
IOMUXC_SetPinMux(IOMUXC_LCD_DATA19_PWM6_OUT, 0); /*复用为PWM6_OUT */
IOMUXC_SetPinConfig(IOMUXC_LCD_DATA19_PWM6_OUT, 0Xb090);
/* 2、PWM初始化
* PWM时钟源=66MHz,设置66分频,因此PWM时钟频率为1MHz
*
*/
PWM6->PWMCR = 0; /* 清零 */
PWM6->PWMCR |= (1 << 16) | (65 << 4) | (1 << 26);
pwm6_setperiod_value(2000); /* PWM频率为2Khz */
/* 3、设置默认站空比 */
beep_dev.pwm_duty = 50;
for(i = 0; i < 4; i++)
{
pwm6_setduty(beep_dev.pwm_duty);
}
/*4、使能FIFO空中断 */
PWM6->PWMIR = 1 << 0; /* 使能PWM1的FIFO空中断 */
GIC_EnableIRQ(PWM6_IRQn);
system_register_irqhandler(PWM6_IRQn, (system_irq_handler_t)pwm6_irqhandler, NULL);
PWM6->PWMSR = 0xff;
/* 5、打开PWM */
PWM6->PWMCR |= 1 << 0;
}
/* 设置PR寄存器 */
void pwm6_setperiod_value(unsigned int value)
{
unsigned int regvalue = 0;
if(value < 2)
regvalue = 2;
else
regvalue = value - 2;
PWM6->PWMPR = (regvalue & 0XFFFF);
}
/* 设置站空比duty=0~100 */
void pwm6_setduty(unsigned char duty)
{
unsigned short period = 0;
unsigned short sample = 0;
beep_dev.pwm_duty = duty;
period = PWM6->PWMPR + 2;
sample = (unsigned short)(period * beep_dev.pwm_duty / 100.0f);
PWM6->PWMSAR = (sample & 0xffff);
}
/* 中断处理函数 */
void pwm6_irqhandler(unsigned int gicciar, void *param)
{
if(PWM6->PWMSR & (1 << 3)) /* FIFO空中断 */
{
pwm6_setduty(beep_dev.pwm_duty);
PWM6->PWMSR |= 1 << 3; /* 中断标志位清零 */
}
}
/* 奉命器控制函数 */
void beep_switch(int status)
{
if(status == ON)
pwm6_setduty(50);
else if(status == OFF)
pwm6_setduty(0);
}
2.beep.h
#ifndef __BSP_BEEP_H
#define __BSP_BEEP_H
#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "MCIMX6Y2.h"
/* 背光信息 */
struct beep_dev_struc {
unsigned char pwm_duty; /* 0~100 */
};
extern struct beep_dev_struc beep_dev;
/* 函数声明 */
void beep_init(void);
void beep_switch(int status);
void pwm6_setperiod_value(unsigned int value);
void pwm6_setduty(unsigned char duty);
void pwm6_irqhandler(unsigned int gicciar, void *param);
#endif
3.main.c
int main(void)
{
// struct rtc_datetime rtcdate;
// char buf[30];
u8 state = OFF,i = 0;
int_init(); /* 初始化中断 */
imx6u_clkinit(); /* 初始化系统时钟 */
delay_init(); /* 延时初始化 */
uart_init(); /* 初始化串口 */
clk_enable(); /* 使能外设时钟 */
led_init(); /* 初始化LED */
epit1_init(0, 66000000/2); /* 初始化epit1,周期为500ms */
rtc_init(); /* 初始化时钟 */
beep_init(); /* 初始化奉命器 */
pwm6_setduty(50);
while(1)
{
i++;
state = !state;
delay_ms(200);
led_switch(LED_ERR,state);
printf("i = %d\r\n",i);
}
return 0;
}
现象描述:一上电就死机了,灯也不闪烁。屏蔽蜂鸣器初始化函数,灯能闪烁,求原因?!
|
|