S3C2440之PWM

#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); 	
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值