#include "uart.h"
#include "stdlib.h"
//MS延时
void delay( unsigned int time)
{
unsigned int i,j;
for ( i=0; i<time; i++)
{
for(j=0;j<30000;j++)
{
}
}
}
void pwm_init(unsigned int tcnt,unsigned int tcmp)
{
/*
//中断配置
rSRCPND |= (0x1<<10); //TIMER0
rINTMOD &= ~(0x1<<10);
rINTMSK &= ~(0x1<<10);
rINTPND |= (0x1<<10);
pISR_TIMER0 = (U32)TIMER0_IRQ;
*/
//定时器参数配置
rTCON &= ~(0x1);//关闭定时器
rTCFG0 |= 0x7c;//125分频 124+1
rTCFG1 |= 0x2;//TIMER0 8分频
rTCNTB0 = tcnt;//TIMER0 0xc350 50000,0x3e8 1000,0x1f4 500
rTCMPB0 = tcmp;//25 50% 0x61a8 25000
rTCON |= 0x1<<1;//手动更新TIMER0->TCNT0
rTCON &= ~(0x1<<1);//关闭手动更新
rGPBCON |= 0x2;//定时器PWM输出引脚连接蜂鸣器
rTCON |= (0x1<<3|0x1<<2|0x1);
}
/*
//定时器中断函数
void __irq TIMER0_IRQ(void)
{
rSRCPND |= (0x1<<10);
rINTPND |= (0x1<<10);
uart_printf("timer0_irq");
}
*/
void choose_speak(void)
{
char dat;
static unsigned int *array = NULL;
static int flag;
dat = uart_getchar();
uart_putchar(dat);
}
/*
播放蜂鸣器
参数设置:vo ->低、中、高音的选择
cnt->dou/rui/mi/fa/so/la/xi
tim->每个频率响的时间
buzzer数组存的是频率的周期
voice 数组存的是低电平的占空比!
*/
void start_buzzer(int vo,int cnt,unsigned int time)
{
//close_buzzer();
static unsigned int buzzer[][8]={{191,170,152,143,127,114,101},//低0/1/2/3/4/5/6
{96,86,76,72,64,57,50},//中/7/8/9/10/11/12/13
{48,43,38,36,32,28,25}};//高14/15/16/17/18/19/20
static unsigned int voice[][8]={{160,140,120,110,100,90,80},
{80,70,60,56,48,41,36},
{38,34,30,28,24,22,20}};
pwm_init(buzzer[vo][cnt],voice[vo][cnt]);//重新配置定时器数据
delay(time);
close_buzzer();
}
//关闭定时器,不在输出PWM
void close_buzzer(void)
{
rTCON &= ~(0x1);
rGPBCON &= ~(0x3);
rGPBCON |= 0x1;
rGPBDAT &= ~(0x1);
}
S3C2440之PWM
最新推荐文章于 2023-07-26 08:33:54 发布