Darwin1234
Newbie level 3
Hello,
i am implementing a simple wav player that play samples that are saved on SD card. i am using r2r 8bit DAC as output..my problem is i get a trash sounds instead of a music. definitely i check the r2r circuit and working correctly. but i am confuse why i got a trash sounds?
i actually check the output using serial communication and it gives me a hex value like 7F 7F 7F 7F with out an 0x prefixed like 0x7F 0x7F 0x7F 0x7F. do you think that's the problem???
here are my full code:
thanks in advance!
i am implementing a simple wav player that play samples that are saved on SD card. i am using r2r 8bit DAC as output..my problem is i get a trash sounds instead of a music. definitely i check the r2r circuit and working correctly. but i am confuse why i got a trash sounds?
i actually check the output using serial communication and it gives me a hex value like 7F 7F 7F 7F with out an 0x prefixed like 0x7F 0x7F 0x7F 0x7F. do you think that's the problem???
here are my full code:
Code dot - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 unsigned char mmc_buf[512]; void uart_init(void) { // Setup the UART baud rate UBRR0H = (BAUD_RATE_UBRR >> 8); UBRR0L = BAUD_RATE_UBRR; // Enable the UART receiver and transceiver UCSR0B = _BV(RXEN0) | _BV(TXEN0); // Set the frame format : 8 bits data, 1 bit stop UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); } void uart_write(uint8_t byte) { while (!(UCSR0A & (1 << UDRE0))); UDR0 = byte; } int uart_stream_write(char c, FILE *stream) { if (c == '\n') uart_stream_write('\r', stream); uart_write(c); return 0; } static FILE uart_stdout = FDEV_SETUP_STREAM(uart_stream_write, NULL, _FDEV_SETUP_WRITE); /***************************************************************************** * MMC functions ******************************************************************************/ #define MMC_CMD_SIZE 6 uint8_t mmc_cmd[MMC_CMD_SIZE]; #define MMC_TIMEOUT 0xFF #define MMC_BLOCK_SIZE 512 #define DDR_CS DDRB #define PORT_CS PORTB #define DD_CS DDB2 #define SET_CS() PORT_CS |= _BV(DD_CS); #define CLEAR_CS() PORT_CS &= ~_BV(DD_CS); uint8_t mmc_response(uint8_t response) { uint16_t count = 0xFFF; uint8_t result; while(count > 0) { result = spi_receive_byte(); if (result == response) break; count--; } if (count == 0) return 1; /* Failure, loop was exited due to timeout */ return 0; /* Normal, loop was exited before timeout */ } void mmc_init(void) { uint8_t i; uint8_t byte; /* Init the CS output */ DDR_CS |= _BV(DD_CS); SET_CS(); { /* Init the card in SPI mode by sending 80 clks */ for(i = 0; i < 10; i++) byte = spi_receive_byte(); } CLEAR_CS(); printf("send CMD0\n"); /* Send CMD0 GO_IDLE_STATE */ mmc_cmd[0] = 0x40; mmc_cmd[1] = 0x00; mmc_cmd[2] = 0x00; mmc_cmd[3] = 0x00; mmc_cmd[4] = 0x00; mmc_cmd[5] = 0x95; spi_send(mmc_cmd, MMC_CMD_SIZE); if (mmc_response(0x01)) { printf("CMD0 timeout\n"); return; } printf("send dummy clocks\n"); /* Send some dummy clocks after GO_IDLE_STATE */ SET_CS(); spi_receive_byte(); CLEAR_CS(); printf("send CMD1\n"); /* Send CMD1 SEND_OP_COND until response is 0 */ i = MMC_TIMEOUT; do { mmc_cmd[0] = 0x41; mmc_cmd[1] = 0x00; mmc_cmd[2] = 0x00; mmc_cmd[3] = 0x00; mmc_cmd[4] = 0x00; mmc_cmd[5] = 0xFF; spi_send(mmc_cmd, MMC_CMD_SIZE); i--; } while((mmc_response(0x00) != 0) && (i > 0)); if (i == 0) { printf("CMD1 timeout\n"); return; } printf("send dummy clocks\n"); /* Send some dummy clocks after GO_IDLE_STATE */ SET_CS(); spi_receive_byte(); CLEAR_CS(); printf("send CMD16\n"); /* Send CMD16 SET_BLOCKLEN to set the block length */ mmc_cmd[0] = 0x50; mmc_cmd[1] = 0x00; /* 4 bytes from here is the block length */ /* LSB is first */ /* 00 00 00 10 set to 16 bytes */ /* 00 00 02 00 set to 512 bytes */ mmc_cmd[2] = 0x00; /* high block length bits - 512 bytes */ mmc_cmd[3] = 0x02; /* low block length bits */ mmc_cmd[4] = 0x00; mmc_cmd[5] = 0xFF; /* checksum is no longer required but we always send 0xFF */ spi_send(mmc_cmd, MMC_CMD_SIZE); if ((mmc_response(0x00)) == 1) { printf("CMD16 timeout\n"); } SET_CS(); spi_receive_byte(); printf("mmc_init end\n"); } void mmc_read_block(uint16_t block_number, uint8_t* block_address) { uint16_t checksum; uint16_t varh, varl; varl = ((block_number & 0x003F) << 9); varh = ((block_number & 0xFFC0) >> 7); printf("read block %i\n", block_number); CLEAR_CS(); { /* send MMC CMD17(READ_SINGLE_BLOCK) to read the data from MMC card */ mmc_cmd[0] = 0x51; /* high block address bits, varh HIGH and LOW */ mmc_cmd[1] = varh >> 0x08; mmc_cmd[2] = varh & 0xFF; /* low block address bits, varl HIGH and LOW */ mmc_cmd[3] = varl >> 0x08; mmc_cmd[4] = varl & 0xFF; /* checksum is no longer required but we always send 0xFF */ mmc_cmd[5] = 0xFF; spi_send(mmc_cmd, MMC_CMD_SIZE); /* if mmc_response returns 1 then we failed to get a 0x00 response */ if ((mmc_response(0x00)) == 1) { printf("read block timeout\n"); return; } /* wait for data token */ if ((mmc_response(0xFE)) == 1) { printf("data token missing\n"); return; } printf("receive block\n"); /* Get the block of data based on the length */ spi_receive(block_address, MMC_BLOCK_SIZE); /* CRC bytes that are not needed */ checksum = spi_receive_byte(); checksum = checksum << 0x08 | spi_receive_byte(); } SET_CS(); spi_receive_byte(); printf("block received\n"); } void audio_playback(uint8_t *block_address, uint16_t length) { uint16_t i, j; uint8_t byte; DDRD = 255; for(j = 0; j < 4521984; j++) { PORTD = block_address[i * 16 + j]; if(j >= 4521984){ j=0; } if(i >= 512){ i = 0; } _delay_us(64); } } /***************************************************************************** * Main program ******************************************************************************/ uint8_t mmc_block[MMC_BLOCK_SIZE]; int main (void) { _delay_us(125); /* Init the UART */ uart_init(); stdout = &uart_stdout; /* Init the SPI */ printf("spi_init\n"); spi_init(); /* Init the MMC */ printf("mmc_init\n"); mmc_init(); /* Reset the MMC buffer */ memset(mmc_block, 0xCA, MMC_BLOCK_SIZE); /* Read the first block */ mmc_read_block(645, mmc_block); audio_playback(mmc_block, MMC_BLOCK_SIZE); /* Infinite loop */ for(;;); return(0); }
thanks in advance!