[SOLVED !] Sd card again :((


Closed Thread
Results 1 to 6 of 6

Thread: Sd card again :((4400 days old

  1. #1
    birey
    Guest birey's Avatar

    0 Not allowed!

    Default Sd card again :((

    Hello I am a new comer! )
    I am dealing with interfacing an sd card to pic16f877a by using spi interface.In fact I am not too openminded to use c ), instead I use pics assembly.(Maybe the reason is: I like to do a hard job). Anyway for a while I read lots of stuff material including the sundisk's product manual and wrote a code but I haven't succeeded to get a proper CMD0 response when I reset the card. sometimes it passes the cmd0 and cmd1 successfully but the code always gives error after the cmd24 command when I tried to get the data response token. In addition I get a silly data response token like 0x00 or 0x0E or 0x0C. I have analysed lots of code examples and as you can notice my code is similar to them. but I can't find any solution.I used the same configuration as the link: http://www.captain.at/electronics/pic-mmc/ used.
    Another problem that I confused is: when I send a cmdxx command and send 0xFF to get the response, how can I know that the data that I received is the response.
    Another point that I wonder is when I look to the data out pin of the card by a oscilloscobe probe I observed a periodic noise. is it normal? I will post it later...

    Thanks alot for your attention, an patience....

    here is my code: (written in mplab 7.52)

    title sd card
    #define _version "1.00"

    LIST R=DEC ; Set default radix to decimal

    ; Include relevant header files for register and bit definitions
    ifdef __16F877A
    INCLUDE "p16f877a.inc"
    endif

    ; *******************************************
    ; Constant Macro definitions
    ; *******************************************

    ; *******************************************
    ; Various variable definitions
    ; *******************************************

    ; Context information for the ISR
    _w equ 0x20 ; Memory to store w register
    _status equ 0x21 ; Memory to store the status register
    _pclath equ 0x22 ; Memory to store the pclath register
    rs equ 0x04
    en equ 0x05
    CS equ 0x02
    ; Variables for the main code. We start allocating from address
    ; 0x23 on Bank0. Make sure to properly initialize all variables
    CBLOCK 0x23
    spi_data
    count
    txready
    txdata
    address0
    address1
    address2
    address3
    counter
    command
    errornum
    crc
    ENDC

    ; Configuration registers.
    ifdef __16F84
    __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON
    else
    ifdef __16F877A
    __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON & _DEBUG_OFF & _LVP_OFF & _BODEN_OFF
    endif

    ; reset vector
    org 0x00
    goto main

    ; interrupt vector
    org 0x04
    goto Int1

    ; main program segment
    org 0x10

    main
    ; Goto Bank 1 and set proper direction for Port B bits
    bcf INTCON,GIE
    bsf STATUS, RP0 ; Switch to bank 1 for the TRISB register
    bcf PIE1,TXIE
    movlw 0FH ;
    movwf TRISA ;
    movlw 0D0H ;1101-0000
    movwf TRISC ;
    clrf TRISB ;
    movlw 0x07 ;prescaler 256
    movwf OPTION_REG
    ;initilization of ADC
    movlw 40H
    movwf ADCON1
    ;initilization of serial port
    movlw 0AH ;Baud rate is ~113,2 KBAUD
    movwf SPBRG
    movlw 06H ;High baud rate,tx disabled for now
    movwf TXSTA
    movlw 40H ;initilization of SPI port
    movwf SSPSTAT
    bcf STATUS, RP0 ; Back to Bank0
    movlw 22h ;initilization of SPI port
    movwf SSPCON ;
    movlw 0FFH
    movwf PORTC
    ;enabling of serial port
    bsf RCSTA,SPEN
    ;initilization of ADC but it is not used for now
    movlw 81H
    movwf ADCON0
    bsf STATUS, RP0
    bsf TXSTA,TXEN
    bsf PIE1,RCIE
    bcf STATUS, RP0
    bcf PIR1,ADIF
    bsf RCSTA,CREN
    bsf INTCON,PEIE
    bsf INTCON,GIE

    movlw '!'
    movwf txdata
    call uart_send
    ;initialize SD card
    bsf PORTC,CS
    movlw 0CH ;send 0xFF for 12 times
    movwf count
    sd_synch
    movlw 0FFH
    movwf spi_data
    call spi_send
    decf count
    btfss STATUS,Z
    goto sd_synch
    CMD0
    bcf PORTC,CS
    movlw 40H
    movwf command
    clrf address3
    clrf address2
    clrf address1
    clrf address0
    movlw 95H
    movwf crc
    call CMD
    clrf counter
    ;getting the response
    cmd0resp
    movlw 0FFH
    movwf spi_data
    call spi_send
    incf counter
    movlw 09H
    subwf counter,0
    btfss STATUS,Z
    goto $+4
    movlw 30H
    movwf errornum
    goto cmd_error
    movf spi_data,w
    sublw 01H
    btfss STATUS,Z
    goto cmd0resp
    bsf PORTC,2
    CMD1
    bcf PORTC,CS
    movlw 41H
    movwf command
    clrf address3
    clrf address2
    clrf address1
    clrf address0
    movlw 0FFH
    movwf crc
    call CMD
    clrf counter
    cmd1resp
    movlw 0FFH
    movwf spi_data
    call spi_send
    incf counter
    movlw 09H
    subwf counter,0
    btfss STATUS,Z
    goto $+4
    movlw 31H
    movwf errornum
    goto cmd_error
    movf spi_data,w
    btfss STATUS,Z
    goto cmd1resp
    bsf PORTC,2

    clrf address0
    clrf address1
    clrf address2
    clrf address3
    CMD24
    bcf PORTC,CS
    movlw 58H
    movwf command
    clrf address3
    clrf address2
    clrf address1
    clrf address0
    movlw 0FFH
    movwf crc
    call CMD
    clrf counter
    cmd24resp
    movlw 0FFH
    movwf spi_data
    call spi_send
    incf counter
    movlw 09H
    subwf counter,0
    btfss STATUS,Z
    goto $+4
    movlw 'W' ;means I have a response error
    movwf errornum
    goto cmd_error
    movf spi_data,w
    btfss STATUS,Z
    goto cmd24resp
    movlw 0FFH
    movwf spi_data
    call spi_send
    ;start token
    movlw 0FEH
    movwf spi_data
    call spi_send
    movlw 0FFH
    movwf counter
    movlw 02H
    movwf command ;I used command because it is available and not used for now
    data2send ;send M for 512 times
    movlw 'M'
    movwf spi_data
    call spi_send
    movf counter,w
    btfsc STATUS,Z
    goto $+3
    decf counter
    goto data2send
    movlw 0FFH
    movwf counter
    decf command
    btfss STATUS,Z
    goto data2send
    ;2 byte CRC
    movlw 0FFH
    movwf spi_data
    call spi_send
    movlw 0FFH
    movwf spi_data
    call spi_send
    ;getting the response
    movlw 0FFH
    movwf spi_data
    call spi_send
    movlw 0FH
    andwf spi_data,0
    xorlw 0FH
    btfsc STATUS,Z
    goto $-7
    movlw 0FH
    andwf spi_data,0
    sublw 05H
    btfsc STATUS,Z
    goto $+4
    movlw 'w' ;means I have a data response token error
    movwf errornum
    goto cmd_error
    movlw 0FFH
    movwf spi_data
    call spi_send
    movf spi_data,w
    btfsc STATUS,Z
    goto $-5
    bsf PORTC,2
    exit
    movlw 'o'
    movwf txdata
    call uart_send
    movlw 'k'
    movwf txdata
    call uart_send
    nop
    nop
    nop
    nop
    goto $-1
    ; print which cmd is unsuccessful
    cmd_error
    bsf PORTC,2
    movlw 0DH
    movwf txdata
    call uart_send
    movlw 0AH
    movwf txdata
    call uart_send
    movlw 'C'
    movwf txdata
    call uart_send
    movlw 'M'
    movwf txdata
    call uart_send
    movlw 'D'
    movwf txdata
    call uart_send
    movf errornum,w
    movwf txdata
    call uart_send
    movlw ' '
    movwf txdata
    call uart_send
    movlw 'f'
    movwf txdata
    call uart_send
    movlw 'a'
    movwf txdata
    call uart_send
    movlw 'i'
    movwf txdata
    call uart_send
    movlw 'l'
    movwf txdata
    call uart_send
    movlw 'e'
    movwf txdata
    call uart_send
    movlw 'd'
    movwf txdata
    call uart_send
    movlw '!'
    movwf txdata
    call uart_send
    ; swapf spi_data,0
    ; andlw 0FH
    ; movwf txdata
    ; call uart_send
    ; movf spi_data,w
    ; andlw 0FH
    ; addlw 30H
    ; movwf txdata
    ; call uart_send
    movlw 0DH
    movwf txdata
    call uart_send
    movlw 0AH
    movwf txdata
    call uart_send
    nop
    goto $-1
    ;**************************************
    ;SUBPROGRAMS
    ;**************************************
    wait
    clrf TMR0
    wait1
    movlw 70H
    subwf TMR0,w
    btfss STATUS,Z
    goto wait1
    return

    ;sd card reset and initilization
    CMD ;CMD -Sd card commands
    ; movlw 0FFH
    ; movwf spi_data
    ; call spi_send
    ; bcf PORTC,2 ;CS=0

    movf command,w
    movwf spi_data
    call spi_send

    movf address3,w
    movwf spi_data
    call spi_send

    movf address2,w
    movwf spi_data
    call spi_send

    movf address1,w
    movwf spi_data
    call spi_send

    movf address0,w
    movwf spi_data
    call spi_send

    movf crc,w
    movwf spi_data
    call spi_send

    return

    ;send data to spi
    spi_send
    movf spi_data,w
    movwf SSPBUF
    bsf STATUS,RP0
    bcf STATUS,RP1
    btfss SSPSTAT,BF
    goto $-1
    bcf STATUS,RP0
    bcf STATUS,RP1
    movf SSPBUF,w
    movwf spi_data
    return

    ;send data to sarial port
    uart_send
    bsf STATUS, RP0
    movf TXSTA,w
    bcf STATUS, RP0
    movwf txready
    btfss txready,TRMT
    goto $-5
    movf txdata,w
    movwf TXREG
    return
    ;*****************************************
    ;INTERRUPT
    ;*****************************************
    ; Interrupt Handler Routine
    Int1
    ; First save registers before processing interrupt
    movwf _w ; save the w-register
    movf STATUS, w ; get the status register into w-register
    ; (This sets RP0)
    bcf STATUS, RP0 ; Re-Select bank 0 for the status register save
    bcf STATUS, RP1 ; Both RP0 and RP1 are undetermined and need to be set
    movwf _status ; save the status register
    movf PCLATH, w ; Save the high byte of PC
    movwf _pclath ;
    clrf PCLATH
    REC
    btfss PIR1,RCIF
    goto interrupt_exit
    movf RCREG,w
    movwf TXREG
    goto interrupt_exit

    interrupt_exit
    ; Restore the context registers
    movf _pclath, w ; Restore back the high byte of PC
    movwf PCLATH
    movf _status, w ; Restore back the status register
    movwf STATUS
    swapf _w, f ; Load w with stored value without changing flags
    swapf _w, w

    ; Finished restoring context, return from interrupt
    retfie

    Prog_End
    end ; End of program
    ;////////////////////////////////

Attention

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Please consider supporting us by disabling your ad blocker.


Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues


Thank you for your attention.

  • #2
    birey
    Guest birey's Avatar

    0 Not allowed!

    Default

    Here I get the oscilloscope output:
    ch1 is the data_out of the card and ch2 is the sck

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #3
    Mark Rodgers
    Guest Mark Rodgers's Avatar

    0 Not allowed!

    Default

    Hello Birey,
    I think you are on the wrong forum, this is a forum for the Proton BASIC compiler and I am afraid I for one have no idea what your code is doing as it is ASM, others may not be quite as stupid as me and will be able to help, but you might find more help on a different forum?

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #4
    Fanatical Contributor Tim's Avatar
    Join Date
    Jan 2003
    Posts
    7,682
    Thumbs Up
    Received: 47
    Given: 95
    Total Downloaded
    1.32 GB

    0 Not allowed!

    Default

    I'll give you a tip that's especially handy when writing asm.

    Comment your code.
    Tim

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #5
    Aranyi
    Guest Aranyi's Avatar

    0 Not allowed!

    Default Re: Sd card again :((

    Hi Guys

    im currently trying to implement an SD card reader to a self designed processor board that I use to plug into either a breadboard for prototyping or a dedicated mother board, for instance a stepper motor driver board, I am new to SD card interfacing with the pic, the software is not a problem, but on all the SD card interfacing schematics the SCL/SDA lines are pulled low, the problem I have is that I also have 2 512K I2C eeproms on the processor board, which I use for calibration data/graphics/menu text and so on, the eeproms require the SCL/SDA line to be pulled high, has anyone had any conflicts interfacing both eeprom and SD card because of this, or can I simply leave out the pull down resisitors from the SD interface ?

    Regards

    G

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #6
    ADLIN SYSTEMS
    Guest ADLIN SYSTEMS's Avatar

    0 Not allowed!

    Default Re: Sd card again :((

    I think you are getting mixed up here the sd card runs on spi not I2C. Become a registered member and you may get some real good support here.
    AD

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

    Thread Information

    Users Browsing this Thread

    There are currently 1 users browsing this thread. (0 members and 1 guests)

       

    Similar Threads

    1. Sd card
      By ADLIN SYSTEMS in forum The Lounge
      Replies: 8
      Last Post: 28th May 2015, 21:36
    2. [SOLVED !] SD Card
      By ADLIN SYSTEMS in forum The Lounge
      Replies: 4
      Last Post: 29th November 2012, 09:10
    3. [SOLVED !] SD card
      By ADLIN SYSTEMS in forum SWright's Feedback Forum
      Replies: 2
      Last Post: 15th April 2009, 07:37
    4. Sd Card
      By Jezsmith in forum Proton Plus Compiler v3
      Replies: 4
      Last Post: 18th July 2008, 21:51
    5. [SOLVED !] SD Card
      By ADLIN SYSTEMS in forum Wish List / Product Feedback
      Replies: 0
      Last Post: 21st May 2008, 22:40

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts