به نام خدا سلام دوستان من یک برنامه WAVEPLAYER خوب از اینترنت برای CV پیداکردم ولی متاسفانه با M32 و LCD نتونستم جواب بگیرم ببینید شمامیتونید
این هم از برنامه:
این هم از برنامه:
/************************************************** ***
This program was produced by the
CodeWizardAVR V1.24.8e Standard
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 8/4/2007
Author : **********
Company : -----------------------
Comments:
Chip type : ATmega48
Clock frequency : 12.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 128
************************************************** ***/
#include <mega48.h>
#include <stdio.h>
#include <delay.h>
#include <string.h>
#include <spi.h>
#define XTAL 12000000
#define Root_Directory_x 0
#define Root_Directory_y 25088
#define Max_Songs 20
#define CS PORTB.2
char SD_init(void);
char command(char cmd, unsigned int x,unsigned int y, char crc);
char SD_read(unsigned int x,unsigned int y);
void Play_wav(void);
void Get_File_Sizes(int x,int y);
char received;
int x=0;
long int y=0;
int start_x[Max_Songs]={0};
int start_y[Max_Songs]={0};
char file=0; //# of songs
char file_pos=0; //which song will be played next
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char w;
w = UDR0;
printf("%c ",w); //echo
if(w=='+'
{
y+=512;
if (y==65536)
{
y=0;
x++;
printf("man x = %u, y = %u \n\r",x,y);
SD_read(x,y);
}
else
{
printf(" x = %u, y = %u \n\r",x,y);
SD_read(x,y);
}
}
else if (w == 'r' || w == '0'
{
scanf("%u,\r",&x);
printf("x=%u\n\r",x);
scanf("%u,\r",&y);
printf("y=%u\n\r",y);
SD_read(x,y);
}
}
// Pin change 0-7 interrupt service routine
interrupt [PCINT1] void pin_change_isr0(void)
{
delay_ms(200);
PCIFR=2; //clear any other pin_change flags that might have occured when pin changed back
Play_wav(); //to its original state
}
void main(void)
{
// Crystal Oscillator division factor: 1
CLKPR=0x80;
//CLKPR=0x00;
DDRB=0x2E;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 46.875 kHz
// Mode: Ph. correct PWM top=00FFh
// OC1A output: Inverted
TCCR1A=0xC1;
SMCR=01; //sleep enable
PCICR=0x02; //PIN change Interrupt on PINC.0
PCMSK1=0x01;
PCIFR=0x02;
CS=1;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART0 Mode: Asynchronous
// USART Baud rate: 115200 (Double Speed Mode)
UCSR0A=0x02;
UCSR0B=0x98;
UCSR0C=0x06;
UBRR0L=0x0C;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2*3000.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x01;
SD_init();
Get_File_Sizes(Root_Directory_x,Root_Directory_y);
while (1)
{
printf("Ready for input\n\r"
;
#asm("sei"
;
#asm("sleep"
;
};
}
void Play_wav(void)
{
int i;
int cnt=0;
unsigned long pos=0;
char buf[4]={0};
CS = 0; // enable SD
delay_ms(10);//wait for cs to be stable
received=(command(0x52,start_x[file_pos],start_y[file_pos],0xff)); //multi read mode
received=spi(0xFF);
h1:
if (received != 0)
{
received= spi(0xFF);
goto h1;
}
// wait for 0xFE - start of any transmission
received = spi(0xFF);
while(received != 0xfe)
{
received = spi(0xFF);
};
PCIFR=2;
#asm("sei"
; //enable interrupts
spi(0xff);
spi(0xff);
spi(0xff);
spi(0xff);
for(i=0;i<4;i++)
{
buf[i]=spi(0xff); //skip to data
cnt++;
}
pos=0;
pos=(unsigned long)buf[3]<<24;
pos|=(unsigned long)buf[2]<<16;
pos|=(unsigned long)buf[1]<<8;
pos|=(unsigned long)buf[0];
pos+=8;
pos/=512;
cnt+=4;
for(i=0;i<44;i++)
{
spi(0xff); //skip to data
cnt++;
}
OCR1AL=0xff-spi(0xff); //load first value
cnt++;
#asm("cli"
;
TCCR1B=0x09; // 9 = fast PWM 46875 Khz start clock
while(pos>0)
{
while(TCNT1L>6);
OCR1AL=0xff-spi(0xff); //load new data
cnt++; //keep track of how many bytes long the file is
if (cnt==512) //get end of block bytes
{
cnt=0;
received = spi(0xFF);
pos--;
while(received != 0xfe) //at the end of every 512 bytes, the SD card sends some a few bytes
{ //that end with 0xFE
received = spi(0xFF);
};
}
if (PCIFR.1==1)
{
putsf("Done Playing\n\r"
;
goto Done;
}
};
PCIFR=2; //clear pin_change flag PINC.0
TCCR1B=0;
Done:
spi(0x4C); //Stop sending data
h2:
if (received != 0)
{
received= spi(0xFF);
goto h2;
}
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF); // at the end, send 2 dummy bytes
spi(0xFF);
CS = 1; // disable SD
file_pos++;
if ((file_pos+1)>file)
{
file_pos=0;
}
}
char SD_init(void)
{
char i;
CS=0;
spi(0x4C); //Stop sending data if AVR was reset, the SD card might still think it's in multi-read mode
h2:
if (received != 0)
{
received= spi(0xFF);
goto h2;
}
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF); // at the end, send 2 dummy bytes
spi(0xFF);
CS = 1; // disable SD
mmcerror1:
CS = 1; // disable SD
// start SD in SPI mode
for(i=0; i < 10; i++)
{
spi(0xFF); // send 10*8=80 clock pulses
delay_us(100);
}
CS = 0; // enable SD
received=command(0x40,0,0,0x95);
if (received != 1)
{
goto mmcerror1; // reset MMC/ put in idle mode
}
st: // if there is no MMC, prg. loops here
if (command(0x41,0,0,0xFF) !=0)
goto st; //bring out of idle state
CS=1; // disable MMC
return 1;
}
char command(char cmd, unsigned int x,unsigned int y, char crc)
{
spi(0xFF);
spi(cmd);
spi((char)(x >> 8));
spi((char)x);
spi((char)(y >> 8));
spi((char)y);
spi(crc);
spi(0xFF);
return (spi(0xFF)); // return the last received character
}
char SD_read(unsigned int x1,unsigned int y1)
{
int i;
char k;
CS = 0; // enable SD
delay_ms(10); //wait for cs to be stable
//Get Response
received=(command(0x51,x1,y1,0xff));
h1:
if (received != 0)
{
received= spi(0xFF);
goto h1;
}
// wait for 0xFE - start of any transmission
received = spi(0xFF);
while(received != 0xfe)
{
received = spi(0xFF);
};
for(i=0;i<512;i++)
{
k=spi(0xFF);
if (k>=32&&k<=127)
{
printf("%c",k);
}
else
{
printf("-%x-",k);
}
}
printf("\n\r"
;
// at the end, send 2 dummy bytes
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF);
spi(0xFF);
CS = 1; // disable SD
return 0;
}
void Get_File_Sizes(int x,int y)
{
char buf[32]={0};
char cnt,i;
int v;
long int Cluster=0;
CS = 0; // enable SD
delay_ms(10); //wait for cs to be stable
received=(command(0x51,x,y,0xff)); //read Real Root Directory
h1: if (received != 0)
{
received= spi(0xFF);
goto h1;
}
// wait for 0xFE - start of any transmission
received = spi(0xFF);
while(received != 0xfe)
{
received = spi(0xFF);
};
for(v=0,cnt=0;v<16;v++) //512/32 = 16
{
for(i=0; i < 32; i++) //get first file entry
{
buf[i]=spi(0xFF);
delay_us(100); //wait for spi to return value
}
if(buf[cnt]==0) //end of entries
{
goto Done;
}
Cluster=(int)buf[27]<<8; //location of first cluster in FAT
Cluster|=(int)buf[26];
Cluster*=(2048); // 4*512 (sectors/cluster*bytes/sector)
if(buf[11]==32) //File Entry
{
file++; //how many files there are
start_x[v]=0;
Cluster+=37376; //absolute offset to start of file
if((Cluster/65536)>=1) //0,37376 is the start of the FAT area
{
start_x[v]=Cluster/65536;
Cluster=Cluster-(start_x[v]*65536);
}
start_y[v]=Cluster;
printf("File %u is located at (%u,%u)\r",v,start_x[v],start_y[v]);
}
if(buf[11]==8) //Volume Label
{
v--; //don't want to keep info for Volume Name
}
else if (buf[11]==0x0f) //LFN, we want to skip these
{
v--; //by doing this, we can skip over LFN's while cnt is pointing
//to the next entry
}
}
Done:
printf("There are %u Wav Files\n\r",file);
for(v=0;v<496;v++) //skip through rest of the 512 bytes
{
spi(0xff);
}
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF);
CS = 1; // disable SD
}
This program was produced by the
CodeWizardAVR V1.24.8e Standard
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 8/4/2007
Author : **********
Company : -----------------------
Comments:
Chip type : ATmega48
Clock frequency : 12.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 128
************************************************** ***/
#include <mega48.h>
#include <stdio.h>
#include <delay.h>
#include <string.h>
#include <spi.h>
#define XTAL 12000000
#define Root_Directory_x 0
#define Root_Directory_y 25088
#define Max_Songs 20
#define CS PORTB.2
char SD_init(void);
char command(char cmd, unsigned int x,unsigned int y, char crc);
char SD_read(unsigned int x,unsigned int y);
void Play_wav(void);
void Get_File_Sizes(int x,int y);
char received;
int x=0;
long int y=0;
int start_x[Max_Songs]={0};
int start_y[Max_Songs]={0};
char file=0; //# of songs
char file_pos=0; //which song will be played next
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char w;
w = UDR0;
printf("%c ",w); //echo
if(w=='+'

{
y+=512;
if (y==65536)
{
y=0;
x++;
printf("man x = %u, y = %u \n\r",x,y);
SD_read(x,y);
}
else
{
printf(" x = %u, y = %u \n\r",x,y);
SD_read(x,y);
}
}
else if (w == 'r' || w == '0'

{
scanf("%u,\r",&x);
printf("x=%u\n\r",x);
scanf("%u,\r",&y);
printf("y=%u\n\r",y);
SD_read(x,y);
}
}
// Pin change 0-7 interrupt service routine
interrupt [PCINT1] void pin_change_isr0(void)
{
delay_ms(200);
PCIFR=2; //clear any other pin_change flags that might have occured when pin changed back
Play_wav(); //to its original state
}
void main(void)
{
// Crystal Oscillator division factor: 1
CLKPR=0x80;
//CLKPR=0x00;
DDRB=0x2E;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 46.875 kHz
// Mode: Ph. correct PWM top=00FFh
// OC1A output: Inverted
TCCR1A=0xC1;
SMCR=01; //sleep enable
PCICR=0x02; //PIN change Interrupt on PINC.0
PCMSK1=0x01;
PCIFR=0x02;
CS=1;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART0 Mode: Asynchronous
// USART Baud rate: 115200 (Double Speed Mode)
UCSR0A=0x02;
UCSR0B=0x98;
UCSR0C=0x06;
UBRR0L=0x0C;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2*3000.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x01;
SD_init();
Get_File_Sizes(Root_Directory_x,Root_Directory_y);
while (1)
{
printf("Ready for input\n\r"

#asm("sei"

#asm("sleep"

};
}
void Play_wav(void)
{
int i;
int cnt=0;
unsigned long pos=0;
char buf[4]={0};
CS = 0; // enable SD
delay_ms(10);//wait for cs to be stable
received=(command(0x52,start_x[file_pos],start_y[file_pos],0xff)); //multi read mode
received=spi(0xFF);
h1:
if (received != 0)
{
received= spi(0xFF);
goto h1;
}
// wait for 0xFE - start of any transmission
received = spi(0xFF);
while(received != 0xfe)
{
received = spi(0xFF);
};
PCIFR=2;
#asm("sei"

spi(0xff);
spi(0xff);
spi(0xff);
spi(0xff);
for(i=0;i<4;i++)
{
buf[i]=spi(0xff); //skip to data
cnt++;
}
pos=0;
pos=(unsigned long)buf[3]<<24;
pos|=(unsigned long)buf[2]<<16;
pos|=(unsigned long)buf[1]<<8;
pos|=(unsigned long)buf[0];
pos+=8;
pos/=512;
cnt+=4;
for(i=0;i<44;i++)
{
spi(0xff); //skip to data
cnt++;
}
OCR1AL=0xff-spi(0xff); //load first value
cnt++;
#asm("cli"

TCCR1B=0x09; // 9 = fast PWM 46875 Khz start clock
while(pos>0)
{
while(TCNT1L>6);
OCR1AL=0xff-spi(0xff); //load new data
cnt++; //keep track of how many bytes long the file is
if (cnt==512) //get end of block bytes
{
cnt=0;
received = spi(0xFF);
pos--;
while(received != 0xfe) //at the end of every 512 bytes, the SD card sends some a few bytes
{ //that end with 0xFE
received = spi(0xFF);
};
}
if (PCIFR.1==1)
{
putsf("Done Playing\n\r"

goto Done;
}
};
PCIFR=2; //clear pin_change flag PINC.0
TCCR1B=0;
Done:
spi(0x4C); //Stop sending data
h2:
if (received != 0)
{
received= spi(0xFF);
goto h2;
}
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF); // at the end, send 2 dummy bytes
spi(0xFF);
CS = 1; // disable SD
file_pos++;
if ((file_pos+1)>file)
{
file_pos=0;
}
}
char SD_init(void)
{
char i;
CS=0;
spi(0x4C); //Stop sending data if AVR was reset, the SD card might still think it's in multi-read mode
h2:
if (received != 0)
{
received= spi(0xFF);
goto h2;
}
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF); // at the end, send 2 dummy bytes
spi(0xFF);
CS = 1; // disable SD
mmcerror1:
CS = 1; // disable SD
// start SD in SPI mode
for(i=0; i < 10; i++)
{
spi(0xFF); // send 10*8=80 clock pulses
delay_us(100);
}
CS = 0; // enable SD
received=command(0x40,0,0,0x95);
if (received != 1)
{
goto mmcerror1; // reset MMC/ put in idle mode
}
st: // if there is no MMC, prg. loops here
if (command(0x41,0,0,0xFF) !=0)
goto st; //bring out of idle state
CS=1; // disable MMC
return 1;
}
char command(char cmd, unsigned int x,unsigned int y, char crc)
{
spi(0xFF);
spi(cmd);
spi((char)(x >> 8));
spi((char)x);
spi((char)(y >> 8));
spi((char)y);
spi(crc);
spi(0xFF);
return (spi(0xFF)); // return the last received character
}
char SD_read(unsigned int x1,unsigned int y1)
{
int i;
char k;
CS = 0; // enable SD
delay_ms(10); //wait for cs to be stable
//Get Response
received=(command(0x51,x1,y1,0xff));
h1:
if (received != 0)
{
received= spi(0xFF);
goto h1;
}
// wait for 0xFE - start of any transmission
received = spi(0xFF);
while(received != 0xfe)
{
received = spi(0xFF);
};
for(i=0;i<512;i++)
{
k=spi(0xFF);
if (k>=32&&k<=127)
{
printf("%c",k);
}
else
{
printf("-%x-",k);
}
}
printf("\n\r"

// at the end, send 2 dummy bytes
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF);
spi(0xFF);
CS = 1; // disable SD
return 0;
}
void Get_File_Sizes(int x,int y)
{
char buf[32]={0};
char cnt,i;
int v;
long int Cluster=0;
CS = 0; // enable SD
delay_ms(10); //wait for cs to be stable
received=(command(0x51,x,y,0xff)); //read Real Root Directory
h1: if (received != 0)
{
received= spi(0xFF);
goto h1;
}
// wait for 0xFE - start of any transmission
received = spi(0xFF);
while(received != 0xfe)
{
received = spi(0xFF);
};
for(v=0,cnt=0;v<16;v++) //512/32 = 16
{
for(i=0; i < 32; i++) //get first file entry
{
buf[i]=spi(0xFF);
delay_us(100); //wait for spi to return value
}
if(buf[cnt]==0) //end of entries
{
goto Done;
}
Cluster=(int)buf[27]<<8; //location of first cluster in FAT
Cluster|=(int)buf[26];
Cluster*=(2048); // 4*512 (sectors/cluster*bytes/sector)
if(buf[11]==32) //File Entry
{
file++; //how many files there are
start_x[v]=0;
Cluster+=37376; //absolute offset to start of file
if((Cluster/65536)>=1) //0,37376 is the start of the FAT area
{
start_x[v]=Cluster/65536;
Cluster=Cluster-(start_x[v]*65536);
}
start_y[v]=Cluster;
printf("File %u is located at (%u,%u)\r",v,start_x[v],start_y[v]);
}
if(buf[11]==8) //Volume Label
{
v--; //don't want to keep info for Volume Name
}
else if (buf[11]==0x0f) //LFN, we want to skip these
{
v--; //by doing this, we can skip over LFN's while cnt is pointing
//to the next entry
}
}
Done:
printf("There are %u Wav Files\n\r",file);
for(v=0;v<496;v++) //skip through rest of the 512 bytes
{
spi(0xff);
}
spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF);
CS = 1; // disable SD
}
دیدگاه