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

📄 main__.c

📁 This is the latest VS1053B chip interface routines combined with Elm Chan s FatFs library. Whole cod
💻 C
字号:
#define _ALTERNATE_PUTCHAR_
#define _ALTERNATE_GETCHAR_

#include <mega128.h>
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x20
	.EQU __sm_mask=0x1C
	.EQU __sm_powerdown=0x10
	.EQU __sm_powersave=0x18
	.EQU __sm_standby=0x14
	.EQU __sm_ext_standby=0x1C
	.EQU __sm_adc_noise_red=0x08
	.SET power_ctrl_reg=mcucr
	#endif
#include <stdio.h>
#include <ctype.h>
#include <spi.h>
#include <delay.h>
#include <string.h>
#include "main.h"
#include "mmc.h"
#include "diskio.h"
#include "delay.h"
#include "tff.h"
#include "vs1001.h"

/* Playback control code generated by G-sensor control task */
#define K_NEXT_A	6
#define	K_PREV_A	7
#define K_NEXT_F	1
#define	K_PREV_F	2
#define	K_PAUSE	        3
#define	K_PLAY	        4
#define	K_MODE	        5

FATFS fatfs;
FIL file1;

eeprom unsigned char vol_ee=5;

void putchar(char c)
{
while (!(UCSR1A & 0x20));
UDR1=c;
}

unsigned char getchar(void)
{
while (!(UCSR1A & 0x80));
return UDR1;
}

void init()
{
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0xFF;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=In Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=0 State3=P State2=1 State1=0 State0=1
PORTB=0x01011101;
 DDRB=0b01000111;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0xFF;
DDRC=0xDE;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0b01111111;
 DDRD=0b11101110;

// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0xFF;
DDRE=0x00;

// Port F initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTF=0xFF;
DDRF=0x00;

// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x00;

// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 38400
UCSR1A=0x00;
UCSR1B=0x18;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x13;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2*4000,000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x01;
}

static
UINT out_stream (		/* Number of bytes sent or stream status */
	const BYTE *p,		/* Pointer to the data block to be sent */
	UINT btf			/* >0: Number of bytes to be sent. 0: Sense stream status */
)
{
	UINT cnt = btf;


	if (cnt == 0)				/* Return stream status (0: not ready, 1: ready)*/
	return DREQ ? 1 : 0;                    /* If once it returned 1 to sense call, it must accept a byte at least */
                                                /* at subsequent transmission call, or f_forward will result FR_RW_ERROR. */

	ENABLE_XDCS();				/* Select decoder data port */
	do {
        WriteByte(*p++);
        } while (--cnt && DREQ);	/* Repeat while there is a data to be sent and the decoder is demanding data */
	DISABLE_XDCS();				/* Deselect decoder data port */

	return btf - cnt;			/* Return number of bytes sent */
}

/*-----------------------------------------------------------------------*/
/* Sample code using f_forward function                                  */
/*-----------------------------------------------------------------------*/

static
BYTE play_file (
    char *fn        /* Pointer to the audio file name to be played */
)
{
    FRESULT rc;
    FIL fil;
    UINT dmy;

    WORD voltime;
    BYTE tempvol;
    BYTE cmd;

    tempvol = vol_ee;
    voltime = 0;
    voltime = 0;
    cmd=0;

    vs_init(SOFT);

    /* Open the audio file in read only mode */
    rc = f_open(&fil, fn, FA_READ);

    printf("\r\n");
    printf("Playing: %s\r\n", fn);
    printf("SIZE: %9lu\r\n", fil.fsize);

    #ifdef IDv3
    printf("NAME: %s\r\n", titlename);
    printf("ARTIST: %s\r\n", artistname);
    printf("ALBUM: %s\r\n", playlist_name);
    #endif


    /* Repeat until the file pointer reaches end of the file */
    while (rc == FR_OK && fil.fptr < fil.fsize) {

        /* any other processes... */

                if (!(NEXT_F))
                {
                while (!(NEXT_F));
                cmd=K_NEXT_F;
                break;
                }

                if (!(PREV_F))
                {
                while (!(PREV_F));
                cmd=K_PREV_F;
                break;
                }

                if (!(NEXT_B))
                {
                while (!(NEXT_B));
                cmd=K_NEXT_A;
                break;
                }

                if (!(PREV_B))
                {
                while (!(PREV_B));
                cmd=K_PREV_A;
                break;
                }

                if (!(POWER_B))
                {
                while (!(POWER_B));
                vs_beep();
                POWEROFF();
                }

        if (!(VOL_DN))
        {
            if (tempvol < 255)
            {
                if (voltime == 300)
                {
                    voltime = 0;
                    tempvol ++;
                    vs_setvolume(tempvol);
                    vol_ee=tempvol;
                } else {voltime++;}
            }
        }

        if (!(VOL_UP))
        {
            if (tempvol > 0)
            {
                if (voltime == 300)
                {
                    voltime = 0;
                    tempvol --;
                    vs_setvolume(tempvol);
                    vol_ee=tempvol;
                } else {voltime++;}
            }
        }


        /* Fill output stream periodicaly or on-demand */
        rc = f_forward(&fil, out_stream, 512, &dmy);

    }

    f_close(&fil); // 怦

⌨️ 快捷键说明

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