微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > ARM技术讨论 > 玩转蓝牙4.0系列教程(第一季)-第四课 CC2540的音乐盒

玩转蓝牙4.0系列教程(第一季)-第四课 CC2540的音乐盒

时间:10-02 整理:3721RD 点击:

要产生音频脉冲,只要算出某一音频的周期(1/频率),然后将此周期除以2,即得出半周期的时间。利用定时器计时这个半周期时间,每当计时到后就将输出脉冲的I/O 反相,然后重复计时此半周期时间再对I/O 反相,就可在I/O 脚上得到此频率的脉冲。

  硬件电路:



首先配置P1_6为输出引脚

P1SEL &= ~0X60;

P1DIR |= 0X60;





然后配置定时器3中断和第二课第三小节一样,我就不多说了

void T3_Init(void)//系统默认为16M

{


  T3IE = 1;               //开总中断和T3中断

  T3CTL|=0XE8;            //128分频,128/16000000*N=0.5S,N=62500

  T3CTL &= ~0X03;         //自动重装,选择自由模式 00->0xff  62500/256=244(次)

  T3CTL |=0X10;           //启动

  EA = 1;


}


播放的音乐,祝你平安

另有指示灯一闪一闪指示

源程序为:

/**************************************************

** 本程序只供学习使用,未经作者许可,不得用于其它任何用途

** 蓝牙4.0底板开发板

** 作者: shizhe

** 创始时间:2014-05-08

** 修改人:shizhe

** 修改时间:2014-05-08

** 修改人:shizhe

** 修改时间:2014-05-08

** 描述:  

**  音乐播放祝你平安…

** 版权所有,盗版必究。

** Copyright(C) 联控智能 2014-2020

** All rights reserved

*************************************************/

#include<ioCC2540.h>



#define LED1 P1_0

#define SPEAK P1_6

unsigned char Count;

unsigned char  SONG[] ={      //祝你平安

0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,

0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,

0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,

0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,

0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,

0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,

0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,

0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,

0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,

0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,

0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,

0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,

0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,

0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,


};

void Gpio_Init()

{

  P1SEL &= ~0X61;

  P1DIR |= 0X61;

}

void Time3_Init()

{

  T3IE = 1;               //开总中断和T3中断

  T3CTL|=0X18;            //128分频,128/16000000*N=0.5S,N=62500

  T3CTL &= ~0X03;         //自动重装,选择自由模式 00->0xff  62500/256=244(次)

  T3CTL |=0X10;           //启动

  EA = 1;

}


#pragma vector = T3_VECTOR //定时器T1

__interrupt void T3_ISR(void)

{

  static unsigned int js =0;

  IRCON = 0x00;             //清中断标志, 也可由硬件自动完成

  if(js++ >900){

    js =0;

    Count++;

  }

}

/*-------------------------------------------------

功能:1MS延时子程序

-------------------------------------------------*/

void Delay_xMs(unsigned int x)

{

    unsigned int i,j;

    for( i =0;i < x;i++ )

    {

        for( j =0;j<3;j++ );

    }

}


/*-------------------------------------------------

功能:歌曲播放子程序i为播放哪一段曲目

-------------------------------------------------*/

void Play_Song(unsigned char i)

{

unsigned char Temp1,Temp2;

unsigned int Addr;

Count = 0;      //中断计数器清0

Addr = i * 217;

while(1)

{

  Temp1 = SONG[Addr++];

     if ( Temp1 == 0xFF )          //休止符

     {

      T3CTL &=~0X10;           //停止

      Delay_xMs(100);

     }

     else if ( Temp1 == 0x00 )   //歌曲结束符

     {

      return;

     }

     else

     {

      Temp2 = SONG[Addr++];

     T3CTL |=0X10;           //启动

     while(1)

     {

       SPEAK = ~SPEAK;


       Delay_xMs(Temp1);

       if ( Temp2 == Count )

       {

        Count = 0;

        LED1 = ~LED1;

        break;

       }

      }

     }

}

}

/*-------------------------------------------------

功能:主程序

-------------------------------------------------*/

void main()

{

Gpio_Init();  //引脚初始化

Time3_Init();   //定时器0中断初始化

while(1)

{

  Play_Song(0);  //播放//祝你平安

}

}




你好小编 请问你这个程序可以加在BLE协议栈中吗?

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top