⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upsd_sound_studio.c

📁 增强型单片机UPSD33XX系列芯片做音频发生器例程。内含KEIL源码和PLD源码。
💻 C
字号:
 /* `=========================================================================`

                    ***************************************
                  ****   *                           *   ****
                            Title: UPSD_SOUND_STUDIO
                         File name: uPSD_Sound_Studio.c
                        Project name: UPSD_SOUND_STUDIO
                  ***                                    ****
                    ****************** * ******************
                  ****                                   ****
                              Author: Petr PFEIFER
                           MPG Prague, Czech Republic
                  ****   *                           *   ****
                    ***************************************

                 $Version:  0.016   Build: 2004-04-17,20:58:46

 
 
 
                                  Description:
                                  ============
                       uPSD Sound Studio  - Beta release

                                     Notes:
                                     ======
                                 -internal copy
                         - requires active src-control



                           ..........................
                          .                          .
                          .      ******************  .
                          .     **PPPPPPPPPPPPPPPP   .
                          .     *PPPP*******PP****   .
                          .    **PPP********PP***    .
                          .    ***PPP******PP****    .
                          .   *****PPP****PP****     .
                          .   *****PPP****PP****     .
                          .  PPPPPPPP****PP****  (R) .
                          .                          .
                           ..........................


                                  =-=-=-=-=-=
                           =-=-=-=-=-=-=-=-=-=-=-=-=
                =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                     Copyright (c) 2004 ST Microelectronics

             This code/file is provided as is and has no warranty,
     implied or otherwise.  You are free to use/modify any of the provided
    code at your own risk in your applications with the expressed limitation
        of liability (see below) so long as your product using the code
                 contains at least one uPSD products (device).

                            LIMITATION OF LIABILITY:
                            ========================
              NEITHER STMicroelectronics NOR ITS VENDORS OR AGENTS
      SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
       INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
       CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
         OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

                =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                           =-=-=-=-=-=-=-=-=-=-=-=-=
                                  =-=-=-=-=-=

                   For current information on uPSD products,
                please consult our pages on the World Wide Web:

                                 www.st.com/psm

                            - - - - - - - - - - - -

                     STMicroelectronics GROUP OF COMPANIES
    Australia - Brazil - China - Czech Republic - Finland - France - Germany
   Hong Kong - India - Italy - Japan - Malaysia - Malta - Morocco - Singapore
             Spain - Sweden - Switzerland - United Kingdom - U.S.A.

                               http://www.st.com


 `========================================================================` */


#pragma CODE
#pragma NOAREGS

#include "upsd3200.h"
#include "upsd_hardware.h"


static data unsigned int  uPSDSoundStudio_PlayAddress,uPSDSoundStudio_EndAddress,uPSDSoundStudio_FS,uPSDSoundStudio_DCNT;

static data unsigned char uPSDSoundStudio_PlayPage,uPSDSoundStudio_EndPage;

#define uPSDSoundStudio_ZeroLevel 128

static xdata PSD_REGS uPSD_CSIOPxx_reg _at_ PSD_REG_ADDR;
                                  // Define PSD registers at address "csiop" space




static void MAIN_INT_ISR (void) interrupt TF2_VECTOR using 3             // Timer2
/******************************************************************************
 Function   : static void MAIN_INT_ISR ()
 Parameters : (void)
 Description: The main Sound g. INTerrupt
 ******************************************************************************/
 {
  #pragma asm
              push   PSW
              push   ACC
              push   DPL
              push   DPH
              clr    TF2

              mov    DPTR,#uPSD_CSIOPxx_reg+0XE0                    ;PAGE REG.
              movx   A,@DPTR
              push   ACC
              mov    A,uPSDSoundStudio_PlayPage
              movx   @DPTR,A

              mov    A,uPSDSoundStudio_PlayAddress+0                ;HIGH Byte
              jz     PLAY_STOP                                    ; no sound !

              mov    DPL,uPSDSoundStudio_PlayAddress+1
              mov    DPH,uPSDSoundStudio_PlayAddress+0
              movx   A,@DPTR
              mov    PWM0,A
              inc    uPSDSoundStudio_PlayAddress+1
              mov    A,uPSDSoundStudio_PlayAddress+1
              jnz    PLAY_TESTA

              inc    uPSDSoundStudio_PlayAddress+0
              mov    A,uPSDSoundStudio_PlayAddress+0
              jnz    PLAY_TESTA
              orl    uPSDSoundStudio_PlayAddress+0,#0x80

              inc    uPSDSoundStudio_PlayPage
              anl    uPSDSoundStudio_PlayPage,#0x07
              mov    A,uPSDSoundStudio_PlayPage
              jnz    PLAY_TESTA
PLAY_STOP:    mov    uPSDSoundStudio_PlayAddress+0,#0                  ;STOP !
              mov    uPSDSoundStudio_PlayAddress+1,#0                  ;STOP !
              mov    uPSDSoundStudio_PlayPage,#0
              mov    PWM0,#uPSDSoundStudio_ZeroLevel
              mov    PWM1,#uPSDSoundStudio_ZeroLevel
              jmp    PLAY_END

PLAY_TESTA:   mov    A,uPSDSoundStudio_PlayAddress+1
              cjne   A,uPSDSoundStudio_EndAddress+1,PLAY_END
              mov    A,uPSDSoundStudio_PlayAddress+0
              cjne   A,uPSDSoundStudio_EndAddress+0,PLAY_END
              mov    A,uPSDSoundStudio_PlayPage
              cjne   A,uPSDSoundStudio_EndPage,PLAY_END
              jmp    PLAY_STOP                                    ; no sound !
PLAY_END:
              mov    A,uPSDSoundStudio_DCNT+0
              orl    A,uPSDSoundStudio_DCNT+1
              jz     PINT_END
              mov    A,uPSDSoundStudio_DCNT+1
              dec    uPSDSoundStudio_DCNT+1
              jnz    PINT_END
              dec    uPSDSoundStudio_DCNT+0
PINT_END:
              mov    DPTR,#uPSD_CSIOPxx_reg+0XE0                    ;PAGE REG.
              pop    ACC
              movx   @DPTR,A

              pop    DPH
              pop    DPL
              pop    ACC
              pop    PSW
              reti

  #pragma endasm
 }





void uPSDSoundStudio_Init(void)
/******************************************************************************
 Function   : void InitPlay()
 Parameters : (void)
 Description: Initializes Sound Studio
 ******************************************************************************/
 {
  EA = 0;

  PSCL0L = 0;
  PSCL0H = 0;

  PWM0 = 0;
  PWM1 = 0;
  PWMCON = 0x23;
  P4SFS  |= 0x18;                            //enable PWM4+0+1

  T2MOD  = 0;                                //cnt mode
  T2CON &= 0x30;
  T2CON |= 0x04;

  uPSDSoundStudio_FS = 100;
  RCAP2H =~(((FREQ_OSC/uPSDSoundStudio_FS/12)-1)>>8);
  RCAP2L =~ ((FREQ_OSC/uPSDSoundStudio_FS/12)-1);
  TL2 = 1;
  TH2 = 1;

  PWM0 = uPSDSoundStudio_ZeroLevel;
  PWM1 = uPSDSoundStudio_ZeroLevel;

  IP  = 0x20;                                //Timer2 INT has the highest priprity
  IPA = 0x00;                                //Timer2 INT has the highest priprity

  ET2 = 1;                                   // enable timer 2 interrupt

  uPSDSoundStudio_PlayAddress = 0;
  uPSDSoundStudio_PlayPage = 0;
  uPSDSoundStudio_EndAddress = 0;
  uPSDSoundStudio_EndPage = 0;
  EA = 1;
 }









void uPSDSoundStudio_NoSound(void)
/******************************************************************************
 Function   : void uPSDSoundStudio_NoSound()
 Parameters : (void)
 Description: Stops playing
 ******************************************************************************/
 {
  uPSDSoundStudio_PlayAddress = 0;
  uPSDSoundStudio_PlayPage = 0;
  uPSDSoundStudio_EndAddress = 0;
  uPSDSoundStudio_EndPage = 0;
  uPSDSoundStudio_FS   = 1000;
 }







void uPSDSoundStudio_PlayItemSpec(unsigned char ItemOrder, unsigned int UserData)
/******************************************************************************
 Function   : void uPSDSoundStudio_PlayItemSpec()
 Parameters : unsigned char ItemOrder, unsigned int UserData
 Description: Plays specified item with user data
              ItemOrder = 0   - silence of duration UserData in ms
                        <128  - Item order + waits for end of play
                        >=128 - Item order-128, no wait
 ******************************************************************************/
 {
  if (ItemOrder == 0)
   {
    uPSDSoundStudio_FS   = 1000;
    uPSDSoundStudio_DCNT = UserData;
   }
  else
   {
    uPSDSoundStudio_FS = ItemOrder & 0x7F;
    EA = 0;
    #pragma asm
              push   ACC
              push   DPH
              push   DPL

              mov    DPTR,#uPSD_CSIOPxx_reg+0XE0                    ;PAGE REG.
              clr    A
              movx   @DPTR,A                                          ;PAGE #0

              mov    DPTR,#0x8000
              movx   A,@DPTR
              cjne   A,#0x75,PLAY_INIT_ERROR                         ;wrong ID
              inc    DPTR
              movx   A,@DPTR
              cjne   A,#0x53,PLAY_INIT_ERROR                         ;wrong ID
              inc    DPTR
              movx   A,@DPTR
              cjne   A,#0x53,PLAY_INIT_ERROR                         ;wrong ID
              mov    DPL,#0x08
              jmp    PLAY_INIT_FIND

PLAY_INIT_ERROR:

              mov    uPSDSoundStudio_FS+0,#0
              mov    uPSDSoundStudio_FS+1,#0
              jmp    PLAY_INIT_END

PLAY_INIT_FINDLOOP:
              movx   A,@DPTR
              jz     PLAY_INIT_ERROR                        ;wrong item number
              inc    DPTR
              inc    DPTR
              inc    DPTR
              inc    DPTR
              inc    DPTR
              inc    DPTR
PLAY_INIT_FIND:
              djnz   uPSDSoundStudio_FS+1,PLAY_INIT_FINDLOOP

PLAY_INIT_FIND_END:
              movx   A,@DPTR
              cjne   A,#1,PLAY_INIT_ERROR       ;ONLY PURE WAV, MONO supported

              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_FS+1,A                               ;low
              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_FS+0,A                              ;high

              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_PlayAddress+1,A                      ;low
              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_PlayAddress+0,A                     ;high
              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_PlayPage,A

              inc    DPTR

              inc    DPTR
              inc    DPTR
              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_EndAddress+1,A
              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_EndAddress+0,A
              inc    DPTR
              movx   A,@DPTR
              mov    uPSDSoundStudio_EndPage,A

PLAY_INIT_END:
              pop    DPL
              pop    DPH
              pop    ACC
    #pragma endasm
    EA = 1;

    if (uPSDSoundStudio_FS>22050)
     {
      uPSDSoundStudio_FS = 22050;
     }
    else
    if (uPSDSoundStudio_FS<256)              //Error
     {
      uPSDSoundStudio_NoSound();
     }
   }

  T2CON  = 0x00;
  T2MOD  = 0;

  TL2    = 0;
  TH2    = 255;
  RCAP2H =~(((FREQ_OSC/uPSDSoundStudio_FS/12)-1)>>8);
  RCAP2L =~ ((FREQ_OSC/uPSDSoundStudio_FS/12)-1);
  T2CON |= 0x04;

  ET2 = 1;                                   //enable Timer2 int

  if (ItemOrder == 0)
   {
    while (uPSDSoundStudio_DCNT);
   }
  else
  if (ItemOrder < 128)                       //Wait for end of file
   {

    while (uPSDSoundStudio_PlayAddress | uPSDSoundStudio_PlayPage)
     {
     }
   }
 }




char uPSDSoundStudio_IsPlayActive(void)
/******************************************************************************
 Function   : char uPSDSoundStudio_IsPlayActive()
 Parameters : (void)
 Description: Returns actual state of playback
 ******************************************************************************/
 {
  if (uPSDSoundStudio_PlayAddress | uPSDSoundStudio_PlayPage)
   {
    return(1);                               //play
   }
  else
   {
    return(0);                               //stop
   }
 }




void uPSDSoundStudio_PlayItem(unsigned char ItemOrder)
/******************************************************************************
 Function   : void uPSDSoundStudio_PlayItemSpec()
 Parameters : unsigned char ItemOrder
 Description: Plays the specified WAV file item stored in FLASH
 ******************************************************************************/
 {
  uPSDSoundStudio_PlayItemSpec(ItemOrder,0);
 }





void uPSDSoundStudio_Silence(unsigned int DurationInMiliSeconds)
/******************************************************************************
 Function   : void uPSDSoundStudio_Silence()
 Parameters : DurationInMiliSeconds
 Description: Generates silence of DurationInMiliSeconds
 ******************************************************************************/
 {
  uPSDSoundStudio_PlayItemSpec(0,DurationInMiliSeconds);
 }




/* *************************************************************************
  ***                                                                      ***
  **                         *** End of File ***                            **
  ***                                                                      ***
   *************************************************************************  */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -