Autor Tema: MMC card SPI and USART with 18F AN1003 C18  (Leído 3524 veces)

0 Usuarios y 2 Visitantes están viendo este tema.

adbensi

  • Visitante
MMC card SPI and USART with 18F AN1003 C18
« en: 26 de Noviembre de 2006, 00:51:50 »
Hello, great full for you visit,

 I have difficult to make one project with firmware AN1003 to use MMC card memory and USART comunication, because the SPI port use the USART pins.

 It ís possible to use other library / firmeware with ANSI - C18/C30 or other microcontroler compatible to make this project? Please, help me, i need one sample code to know how to make it, i am studding it yet.

Best regards,
Alexandre.

Desconectado navaismo

  • Jubilado Todopic
  • PIC24H
  • ****
  • Mensajes: 1973
Re: MMC card SPI and USART with 18F AN1003 C18
« Respuesta #1 en: 26 de Noviembre de 2006, 03:23:09 »
Maybe this can help you.

Its an example from the Mikrobasic's help

The following code tests MMC library routines. First, we fill the buffer with 512 “M” characters and write it to sector 56; then, we repeat the sequence with character “E” at sector 56. Finally, we read the sectors 55 and 56 to check if the write was successful.

Código: FreeBasic
  1. program mmc_test
  2.  
  3. dim tmp as byte
  4. dim i as word
  5. dim data as byte[512]
  6.  
  7. main:
  8.  
  9.   Usart_Init(9600)
  10.  
  11.   ' Initialize ports
  12.   tmp = Mmc_Init(PORTC, 2)
  13.  
  14.   ' Fill the buffer with the "M" character
  15.   for i = 0 to 512
  16.     data[i] = "M"
  17.   next i
  18.  
  19.   ' Write it to MMC card, sector 55
  20.   tmp = Mmc_Write_Sector(55, data)
  21.  
  22.   ' Fill the buffer with the "E" character
  23.   for i = 0 to 512
  24.     data[i] = "E"
  25.   next i
  26.  
  27.   ' Write it to MMC card, sector 56
  28.   tmp = Mmc_Write_Sector(56, data)
  29.  
  30.   '** Verify: **
  31.  
  32.   ' Read from sector 55
  33.   tmp = Mmc_Read_Sector(55, data)
  34.  
  35.   ' Send 512 bytes from buffer to USART
  36.   if tmp = 0 then
  37.     for i = 0 to 512
  38.       Usart_Write(data[i])
  39.     next i
  40.   end if
  41.  
  42.   ' Read from sector 56
  43.   tmp = Mmc_Read_Sector(56, data)
  44.  
  45.   ' Send 512 bytes from buffer to USART
  46.   if tmp = 0 then
  47.     for i = 0 to 512
  48.       Usart_Write(data[i])
  49.     next i
  50.   end if
  51.  
  52. end.

The next program tests MMC FAT routines. First, we create 5 different files in the root of MMC card, and fill with some information. Then, we read the files and send them via USART for a check.

Código: FreeBasic
  1. program MMC_FAT_Test
  2.  
  3. const FAT_ERROR as string[20] = "FAT16 not found"
  4. dim filename as string[14]
  5. dim tmp, character, j as byte
  6. dim size, i as longint
  7. dim aux as string[5]
  8. dim msg as string[100]
  9.  
  10.  
  11. main:
  12.  
  13.   ' Set up USART for the read of the files
  14.   Usart_Init(19200)
  15.  
  16.   tmp = Mmc_Fat_Init(PORTC, 2)  ' Try to locate the FAT
  17.  
  18.   if tmp <> 0 then
  19.     for tmp = 0 to Strlen(FAT_ERROR) - 1
  20.       Usart_Write(FAT_ERROR[tmp])
  21.     next tmp
  22.   end if
  23.  
  24.   j = 1
  25.  
  26.   '** Write test, 5 files **
  27.  
  28.   for j = 1 to 5                 ' We want 5 files on the MMC card
  29.     filename = "MYFILE0xTXT"     ' File names, e.g. "MYFILE01.TXT", "MYFILE02.TXT"
  30.     filename[7] = j + 48         ' Set number 1, 2, 3, 4, or 5
  31.  
  32.     Mmc_Fat_Assign(filename, 1)  ' Create the file, if not found
  33.     Mmc_Fat_Rewrite            ' Clear the file and prepare for writing
  34.  
  35.     ' Form the text to be written
  36.     aux = "  "
  37.     aux[0] = j + 48
  38.     msg = "This is a test file, no." + aux
  39.  
  40.     Mmc_Fat_Write(msg)  ' Write data to the assigned file
  41.  
  42.   next j
  43.  
  44.   '** Append test **
  45.  
  46.   ' Now let's add more data to the same files
  47.   for j = 1 to 5
  48.     filename = "MYFILE0xTXT"
  49.     filename[7] = j + 48
  50.  
  51.     Mmc_Fat_Assign(filename, 1)  ' Find the file and "assign" it
  52.     Mmc_Fat_Append             ' Prepare for appending
  53.  
  54.     ' Form the text to be written
  55.     aux = " "
  56.     aux[0] = j + 48
  57.     msg = "Append test, try " + aux
  58.  
  59.     Mmc_Set_File_Date(2005,5,j,12,47,12)  ' Test the date function
  60.  
  61.     Mmc_Fat_Write(msg)   ' Write data to the assigned file
  62.  
  63.     '** Read test **
  64.  
  65.     Mmc_Fat_Reset(size)  ' Take the size of the file
  66.  
  67.     ' Send whole file to USART, char by char
  68.     for i = 1 to size
  69.       Mmc_Fat_Read(character)
  70.       Usart_Write(character)
  71.     next i
  72.  
  73.   next j
  74.  
  75. end.

"Mi tercer deseo, que otra cosa podría ser, poner un final a todo el sufrimiento, que sólo con mi propia extinción el planeta podrá ser aliviado. Pobre de mí, Si tuviera tres deseos hoy, la elección no sería dificil para mí, Sólo tres deseos serían suficientes, No tendría uso para más."

"He sido un hombre que busca y aun lo sigo siendo, pero ya no busco en las estrellas y en los libros, sino en las enseñanzas de mi sangre."

adbensi

  • Visitante
Re: MMC card SPI and USART with 18F AN1003 C18
« Respuesta #2 en: 26 de Noviembre de 2006, 09:53:04 »
Hello navaismo!!  :-/

You this using the serial communication USART to record data in the MMC card, this is very interesting.

Can to use TX USART to write in MMC, and RX USART to get the data to record from other PIC :D

I try to run this code, on page 231. Thanks a lott!! This compiler is more easy of C18 and different.

Best regards,
Alexandre.