摘要
这篇文章通过定时器产生PWM波,控制LED实现呼吸灯的效果,并且通过三路PWM分别控制RGB三色灯,实现了颜色渐变。最后通过调制PWM频率,使用蜂鸣器实现了音乐播放。
PWM简介
Pulse Width Modulation 翻译成中文是 脉冲宽度调制,简单的可以理解为PWM就是控制方波的频率和占空比
RGB灯渐变源代码
/*
接线方法:
P0.4 -- 红灯
P0.5 -- 绿灯
P0.6 -- 蓝灯
*/
#include <ioCC2530.h>
//定义控制灯的端口
#define LED1 P1_0 //定义LED1为P1_0口控制
#define LED2 P1_5 //定义LED2为P1_5口控制
#define LED3 P1_3 //定义LED3为P1_3口控制
#define LED4 P1_4 //定义LED4为P1_4口控制
#define SW1 P1_2 //定义SW1为P1_2端口
#define SW2 P0_1 //定义SW2为P0_1端口
#define SRCLK P0_4
#define RCLK P0_5
#define SER P0_6
#define LEDS0 P0_7
#define LEDS1 P1_7
#define LEDS2 P0_0
/****************************
//延时
*****************************/
char pause = 0;
void Delay_mS(unsigned int n)
{
unsigned int tt,i = 0;
for(i = 0; i < n; i ++)
for(tt = 0; tt<1040; tt++);
while(pause);
}
void Delay_uS(unsigned int n)
{
unsigned int i = 0;
for(;i < n; i++);
}
/****************************
//初始化函数
*****************************/
void Initial(void)
{
CLKCONCMD = 0x80; //32MHz 外部晶振
P0DIR = 0xFF;
P0SEL = 0xFF; //设置P0为外设功能
P2DIR = 0xC0; //设置Timer外设引脚的优先级
//配置工作模式和分频系数
T1CTL = 0x01;
T1CCTL2 = 0x24; //设置T1输出的通道2,Channel 2
T1CCTL3 = 0x24; //设置T1输出的通道3,Channel 3
T1CCTL4 = 0x24; //设置T1输出的通道4,Channel 4
T1CC2L = 0x10;
T1CC2H = 0x00;
T1CC3L = 0x10;
T1CC3H = 0x00;
T1CC4L = 0x10;
T1CC4H = 0x00;
}
/***************************
//主函数
***************************/
void main(void)
{
unsigned short r = 60000;
unsigned short g = 300;
unsigned short b = 300;
int stage = 1;
Initial(); //调用初始化函数
while(1)
{
if(stage == 1)
{
r -= 300;
g += 300;
if(g == 60000)
{
stage = 2;
}
}
else if(stage == 2)
{
g -= 300;
b += 300;
if(b == 60000)
{
stage = 3;
}
}
else if(stage == 3)
{
b -= 300;
r += 300;
if(r == 60000)
{
stage = 1;
}
}
T1CC2L = r;
T1CC2H = r>>8;
T1CC3L = g;
T1CC3H = g>>8;
T1CC4L = b;
T1CC4H = b>>8;
Delay_mS(10);
}
}
音符与频率对应关系
播放音乐源代码
/*
接线方法:
P0.6连接蜂鸣器
*/
#include <ioCC2530.h>
//定义控制灯的端口
#define LED1 P1_0 //定义LED1为P1_0口控制
#define LED2 P1_5 //定义LED2为P1_5口控制
#define LED3 P1_3 //定义LED3为P1_3口控制
#define LED4 P1_4 //定义LED4为P1_4口控制
#define SW1 P1_2 //定义SW1为P1_2端口
#define SW2 P0_1 //定义SW2为P0_1端口
#define SRCLK P0_4
#define RCLK P0_5
#define SER P0_6
#define LEDS0 P0_7
#define LEDS1 P1_7
#define LEDS2 P0_0
/****************************
//延时
*****************************/
char pause = 0;
void Delay_mS(unsigned int n)
{
unsigned int tt,i = 0;
for(i = 0; i < n; i ++)
for(tt = 0; tt<1040; tt++);
while(pause);
}
void Delay_uS(unsigned int n)
{
unsigned int i = 0;
for(;i < n; i++);
}
/****************************
//初始化函数
*****************************/
void Initial(void)
{
CLKCONCMD = 0x88;
P0DIR = 0xFF;
P1DIR = 0xFB;//将P12设为输入,其他设为输出 (0xFB = 11111011)
LED1 = 0;//关闭LED1
LED2 = 0;//关闭LED2
LED3 = 0;//关闭LED3
LED4 = 0;//关闭LED4
P0SEL |= 0x40; //设置P0.6为外设功能
//配置工作模式和分频系数
T1CTL = 0x0E;
T1CCTL4 = 0x24; //设置T1通道4比较输出功能
T1CC4L = 0xA; //设置T1CC4,PWM输出时比较值值
T1CC4H = 0x00;
}
void PWM_Play(int tune, int time)
{
if(tune == 0)
{
P0SEL = 0x00;
P0_6 = 0;
Delay_mS(time);
P0SEL |= 0x40;
return;
}
T1CNTL = 0;
T1CNTH = 0;
T1CC0L = 125000/tune;
T1CC0H = (125000/tune) >> 8;
Delay_mS(time);
}
void birth()
{
PWM_Play(784,500);
PWM_Play(0, 25);
PWM_Play(784,500);
PWM_Play(880,1000);
PWM_Play(784,1000);
PWM_Play(1046,1000);
PWM_Play(988,2000);
PWM_Play(0, 300);
PWM_Play(784,500);
PWM_Play(0, 25);
PWM_Play(784,500);
PWM_Play(880,1000);
PWM_Play(784,1000);
PWM_Play(1175,1000);
PWM_Play(1046,2000);
PWM_Play(0, 1000);
}
void mamahao()
{
PWM_Play(880,1500);
PWM_Play(784,500);
PWM_Play(659,1000);
PWM_Play(784,1000);
PWM_Play(1046,1000);
PWM_Play(880,500);
PWM_Play(784,500);
PWM_Play(880,2000);
PWM_Play(0, 1000);
}
/***************************
//主函数
***************************/
void main(void)
{
Initial(); //调用初始化函数
PWM_Play(0, 100);
while(1)
{
birth();
mamahao();
}
}