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

📄 t0int.c

📁 完整的wav文件播放程序采用lpc2148芯片
💻 C
字号:
// Header:	  Main Interrupt Routine
// File Name: T0Int.c
// Author:	  Matthias Hertel
// Date:	  21-Oct-2004

#include <LPC213x.H>          /* LPC21xx definitions  */
#include "modes.h"
#include "riffwave.h"

#define T0_RlFreq  1874       /* Timer 0 Reload Frequency in Peripheral Clocks  */

/* Data for Record&Block handling */
#define FIRSTBLOCK 1          /* The 1st free block - application is in Block 0 */
//#define LASTBLOCK  7          /* The last block                                 */
#define LASTBLOCK  2          /* The last block                                 */
//#define BLOCKSIZE  0x10000L   /* Size of each block - 64KB                      */
#define BLOCKSIZE  0x8000L   /* Size of each block - 32KB                      */
#define SIZE_OF_BOOTLOADER 0x3000;  /* Size of the Bootloader that is in the last flash Sector */

enum modes mode;              /* Keeps the state that the player is in          */
                                                                                
unsigned int timeval = 0;     /* Global timer - overflows every 10 sec          */
signed short *p_record;       /* pointer to the wave sample                     */
unsigned int eot;             /* end of track                                   */
unsigned int blockindex;      /* current Block                                  */
extern signed int volume;     /* Volume - Range: 0-1023;                        */

unsigned char print_info = 0; /* flag that instructs the main loop to print track information */

struct wav_hdr* p_wav_hdr;    /* Wave file header                               */


/* 
 * Timer Counter 0 Interrupt executes in 8Khz frequency 
 */
void tc0 (void) __irq __ram {
  int sample;

  static unsigned short DA_val = 0;
  static unsigned short DA_record = 0;
  if (timeval++ >= 40000l){     /* 5 sec frames */
    timeval =0;               
    mode = NEXT;
  }
  switch (mode) {
    /* State: Playing */
    case PLAY:
      sample = *p_record++;
      sample *= volume;
	  DA_val = ((sample >> 15) + 0x8000) & 0xFFFF;
      //DA_val |= 0x10000;     /* Bias */
      DACR = DA_val;
      if (p_record >= eot) {
        IOCLR1 =  0x00FF0000;                          /* Turn off LEDs */
	    IOSET1 =  (0x01 <<  16) & 0xFF0000;            /* Turn on LED */            
        mode = PAUSE;                                  /* Next time the tc0 routine is entered, it playing is paused */
      }
      break;
    /* State: Pause - wait until interval counter overflows*/
    case PAUSE:
      if (timeval == 0)
        mode = NEXT;                                   /* After 10 seconds are up, switch to next record block */
      break;
    /* State: Next Track */
    case NEXT:
	    if (blockindex < LASTBLOCK) { blockindex++; } else { blockindex = FIRSTBLOCK; }  /* goto first block when last was played */
    /* State: Previous Track */
    case PREV:
      if (mode != NEXT)                                
        if (blockindex > FIRSTBLOCK) { blockindex--; } else { blockindex = LASTBLOCK; }  /* goto last block when first was played */
      
      {
      unsigned int blockaddress = (blockindex * BLOCKSIZE);                              /* calculate the absolute memory address of the block*/
        p_wav_hdr = (void*) blockaddress;                                                /* point the Wave/RIFF structure to the new block */
        p_record = (signed short*)(void*) blockaddress + 0x24;                           /* Set the sample pointer after the wave file header */
        
        if (p_wav_hdr->length < BLOCKSIZE){                                              
          eot = (unsigned int)(blockaddress + p_wav_hdr->length);                        /* Get the end of the file from either the header or */
        } else {
          eot = (unsigned int)(blockaddress + BLOCKSIZE) -1;                            /* set to the maximum (block boundary)                */
        } 
        if (blockindex == LASTBLOCK)                                                    /* don't play the content of the bootloader */
        eot -= SIZE_OF_BOOTLOADER;
      }

	  IOCLR1 =  0x00FF0000;                                     /* Turn off LEDs */
	  IOSET1 = (0x01 <<  (blockindex + 16)) & 0xFF0000;         /* Turn on LED */

      print_info = 1;                                           /* indicate that track information is printed in the main loop */
      mode = PLAY;                                              /* Next time the tc0 routine is entered, it starts playing */
      break;
  }
  T0IR        = 1;                            // Clear interrupt flag
  VICVectAddr = 0;                            // Acknowledge Interrupt
  
}


/* Setup the Timer Counter 0 Interrupt */
void init_timer (void) {
  T0MR0 = T0_RlFreq;                          // 0.125uSec = 1875-1 counts
  T0MCR = 3;                                  // Interrupt and Reset on MR0
  T0TCR = 1;                                  // Timer0 Enable
  VICVectAddr0 = (unsigned long)tc0;          // set interrupt vector in 0
  VICVectCntl0 = 0x20 | 4;                    // use it for Timer 0 Interrupt
  VICIntEnable = 0x00000010;                  // Enable Timer0 Interrupt
  mode = NEXT;
  blockindex = FIRSTBLOCK - 1l;
}

⌨️ 快捷键说明

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