Autor Tema: SPI 18f4550 no funciona el pin de salida...!  (Leído 2316 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado fernando busi

  • PIC10
  • *
  • Mensajes: 5
SPI 18f4550 no funciona el pin de salida...!
« en: 04 de Julio de 2008, 12:46:31 »
Hola amigos

Estoy trabajando con las tarjetas SD y MMC, las cuales ya puedo manejar, pero usando el protocolo SPI por software.

Ahora quiero hacerlo pero usando por hardware, cambie algunos comandos y lo probe en Proteus 7.2 y funciona perfectamente, pero cuando lo llevo a la realidad el pin de salida no envia ningun dato. El reloj funciona bien pero el da salida no.

hay algun fusible o algun detalle que deba mover para lograr el correctu funcionamiento de este..?

Gracias por su ayuda amigos.

Fernando.

Desconectado firepic

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1130
    • JC Servicios
Re: SPI 18f4550 no funciona el pin de salida...!
« Respuesta #1 en: 04 de Julio de 2008, 15:51:29 »
Para que se te pueda ayudar mejor, sería excelente que colocaras el esquemático y el código.
Saludos, nos leemos!  :mrgreen:
"Por la presunción solo se ocasiona una lucha, pero con los que consultan juntos hay sabiduría" (Proverbios 13:10).
Visita Mi Sitio Web

Desconectado Nocturno

  • Administrador
  • DsPIC33
  • *******
  • Mensajes: 18286
    • MicroPIC
Re: SPI 18f4550 no funciona el pin de salida...!
« Respuesta #2 en: 05 de Julio de 2008, 01:36:35 »
Esos pines comparten función con la EUSART y con la Int. Externa. El hecho de que te funcionen en Proteus no es garantía de que esté todo correcto.
A ver cómo configuras el SPI.

Desconectado PalitroqueZ

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 5474
    • Electrónica Didacta
Re: SPI 18f4550 no funciona el pin de salida...!
« Respuesta #3 en: 07 de Julio de 2008, 19:05:22 »
yo he inicializado Exitosamente una MMC (en protoboard) con este código:

mi driver (incompleto) usando spi por hardware a bajo nivel -nada de built-in -  :)

Código: C
  1. #byte SSPCON1 = 0xFC6
  2. #byte SSPSTAT = 0xFC7
  3. #byte SSPBUF= 0xFC9
  4. #byte PIR1=0xF9E
  5.  
  6. int8  const CMD0 =0x40;    // comando reset  (GO_IDLE_STATE)
  7. int8  const CMD1 =0x41;    //    "     idle  (SEND_OP_COND)
  8. int8  const CMD9 =0x49;    //    "     CSD   (SEND_CSD)
  9. int8  const CMD10 =0x4A;   //    "     CID   (SEND_CID)
  10. int8  const CMD17 =0x51;   //    "     leer 1 sector  (READ_SINGLE_BLOCK)
  11. int8  const CMD24 =0x58;   //    "     escribir 1 sector (WRITE_BLOCK)
  12. int8  const CMD16 =0x50;   //    "     define ancho del bloque (SET_BLOCKLEN)
  13.  
  14. //int8 const cmd[]={0x40,0x41,0xff,0x4A,0x49,0x51,0x58,0x50};
  15. int16 const lTimeout=0x2fff;
  16. //int32 Direccion_alta=0, Direccion_baja=0;
  17.  
  18. #inline
  19. int iLeer_1Byte_SPI(){
  20.    SSPBUF=0xff;  // envia puros unos en dataout
  21.    while(!bit_test(PIR1,3)){};
  22.    // mientras SSPIF=0 la transmisión contínua
  23.    // si SSPIF=1, terminó la transmisión. SSPIF-> PIR1,3
  24.    bit_clear(PIR1,3); // borro el flag SSPIF
  25.    return(SSPBUF);
  26. }
  27. //--------------------------------------------------------------
  28. #inline
  29. int iEscribir_1Byte_SPI(int dato){
  30.    SSPBUF=dato;
  31.    if(SSPCON1 & 0x80){
  32.       return(0xff);
  33.    }
  34.    else{
  35.       while(!bit_test(PIR1,3)){};
  36.       // mientras SSPIF=0 la transmisión contínua
  37.       // si SSPIF=1, terminó la transmisión. SSPIF-> PIR1,3
  38.       bit_clear(PIR1,3); // borro el flag SSPIF
  39.    }
  40.    return(0);
  41. }
  42. //--------------------------------------------------------------
  43. #inline
  44. int iComando_MMC(int8 _cmd,int32 llDireccion){
  45.    int iRespuesta=0xff;
  46.    long lTiempo_fuera=0;
  47.  
  48.    output_low(MMC_CS);
  49.    iLeer_1Byte_SPI();
  50.  
  51.    iEscribir_1Byte_SPI(_cmd);
  52.    iEscribir_1Byte_SPI(make8(llDireccion,3));
  53.    iEscribir_1Byte_SPI(make8(llDireccion,2));
  54.    iEscribir_1Byte_SPI(make8(llDireccion,1));  
  55.    iEscribir_1Byte_SPI(make8(llDireccion,0));
  56.    iEscribir_1Byte_SPI(0x95);
  57.  
  58.    
  59.    
  60.    while(iRespuesta==0xff){
  61.       iRespuesta=iLeer_1Byte_SPI();
  62.       if(lTiempo_fuera++>500){ break;}
  63.    }
  64.    return(iRespuesta);
  65. }
  66. //------------------------------------------------------------------------
  67. #separate
  68. int1 bLongitud_Bloque_512_Bytes(){
  69.    if(!iComando_MMC(CMD16,512)){
  70.       return 0;
  71.    }else{
  72.       return 1;
  73.    }
  74. }
  75. //---------------------------------------------------------------------------
  76. #separate
  77. int8 iIniciar_MMC(){
  78.    int t;
  79.    long lTiempo_fuera=0;
  80.  
  81. //----------------------------------------------------
  82. // aqui se baja la velocidad a Fosc=8Mhz para tener 125Khz en el reloj
  83. // del SPI para iniciar la MMC
  84. //----------------------------------------------------
  85.    setup_oscillator(OSC_INTRC |OSC_8MHZ);
  86.    // cambio de oscilador primario = oscilador interno a 8 MHz
  87.    // IESO bit ignorado      
  88.    delay_cycles(10); // transición de 7 ciclos + adicionales
  89.    while(!IOFS){}  //espera estabilización del Fosc
  90.    // ¡¡comentar el while para la simulación en proteus!!
  91.    setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64 | SPI_XMIT_L_TO_H);
  92.    // SSPCON1=0b00110010;
  93.    // activa spi por hardware, maestro en Fosc/64,
  94.    // reloj idle en alto (CKP=1)
  95.    // SSPSTAT=0;
  96.    // transmisión ocurre en flanco bajada (CKE=0)
  97. //----------------------------------------------------  
  98.  
  99.    output_high(MMC_CS);
  100.    delay_ms(100); // una especie de power-up timer para la MMC
  101.    
  102.    for(t=0;t<12;t++){
  103.       output_toggle(LED_BICOLOR);
  104.       iLeer_1Byte_SPI();
  105.    }
  106.    
  107.    while(iComando_MMC(CMD0,0)!=1){
  108.      delay_us(100);
  109.      if(lTiempo_fuera++>lTimeout){ return(1);}
  110.    }
  111.    
  112.    lTiempo_fuera=0;
  113.    while(iComando_MMC(CMD1,0)!=0){
  114.      delay_us(100);
  115.      if(lTiempo_fuera++>lTimeout){ return(2);}
  116.    }
  117.    
  118.    t=0;
  119.    t=bLongitud_Bloque_512_Bytes();
  120. //----------------------------------------------------  
  121. // aqui se sube la velocidad a Fosc=48Mhz para tener 12Mhz en el reloj
  122. // del SPI para hacer el resto de las operaciones en la MMC
  123. // nota: lo hago después de setear longitud bloque, porque
  124. // en el simulador lo pedía así, Falta probar en físico
  125. //----------------------------------------------------
  126.    setup_oscillator(OSC_NORMAL);
  127.    // oscilador primario activo. IESO bit ignorado
  128.    delay_cycles(10); // transición de 7 ciclos + adicionales
  129.    setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_XMIT_L_TO_H);
  130.    // SSPCON1=0b00110000;
  131.    // activa spi por hardware, maestro en Fosc/4, reloj idle en alto (CKP=1)  
  132.    // SSPSTAT = 0;
  133.    // transmisión ocurre en flanco bajada (CKE=0
  134. //----------------------------------------------------
  135.  
  136.    if(!t){
  137.       output_high(MMC_CS);
  138.       return 0;
  139.    } else{
  140.       return 1;
  141.    }
  142. }
  143. //------------------------------------------------------------
  144. #separate
  145. int iLeer_CID(char *sCID){
  146.    int t;
  147.    long lTiempo_fuera=0;
  148.    
  149.    output_low(MMC_CS);
  150.    while(iComando_MMC(CMD10,0)!=0){
  151.       delay_us(100);
  152.       if(lTiempo_fuera++>lTimeout){
  153.          output_high(MMC_CS);
  154.          return(2);
  155.       }
  156.    }
  157.    while(iLeer_1Byte_SPI()!=0xFE){};
  158.    
  159.    for(t=0;t<18;t++){
  160.    output_toggle(LED_BICOLOR);
  161.       sCID[t]=iLeer_1Byte_SPI();
  162.    }
  163.    
  164.    output_high(MMC_CS);
  165.    iLeer_1Byte_SPI();
  166.    return 0;
  167. }
  168.  
  169. //----------------------------------------------------------------
  170. #separate
  171. int iLeer_CSD(char *sCSD){
  172.    int t;
  173.    long lTiempo_fuera=0;
  174.    
  175.    output_low(MMC_CS);
  176.    while(iComando_MMC(CMD9,0)!=0){
  177.       delay_us(100);
  178.       if(lTiempo_fuera++>lTimeout){
  179.          output_high(MMC_CS);
  180.          return(2);
  181.       }
  182.    }
  183.    while(iLeer_1Byte_SPI()!=0xFE){};
  184.    
  185.    for(t=0;t<18;t++){
  186.       output_toggle(LED_BICOLOR);
  187.       sCSD[t]=iLeer_1Byte_SPI();
  188.    }
  189.    
  190.    output_high(MMC_CS);
  191.    iLeer_1Byte_SPI();
  192.    return 0;
  193. }
  194.  
  195. #separate
  196. int8  iLeer_bloque(int32 lNumero_Bloque){
  197.    if(!iComando_MMC(CMD17,lNumero_Bloque)){
  198.    
  199.    return 0;
  200.    }else{
  201.       return 0xff;
  202.    }
  203.    //Direccion_baja=((llDireccion & 0x3F)<<9);
  204.    //Direccion_alta=((llDireccion & 0xFFC0)>>7);
  205.    
  206.    
  207.    
  208.  
  209. }


programa ejemplo:

Código: C
  1. //*****************************************************************************
  2. //
  3. // nota1:
  4. // en vez de usar a BF (SSPSTAT,0) para preguntar si completó la transmisión
  5. // spi, pregunto por SSPIF (PIR1,3) porque así lo recomienda la
  6. // datasheet errata 18f4550 DS80220G-pagina8
  7. //
  8. // nota2: la recomendación aplica a las revisiones ID: 0010
  9. //*****************************************************************************
  10. #include <18F4550.h>
  11. #fuses XTPLL,NOMCLR,NOWDT,NOPROTECT,NOLVP,NODEBUG,NOPBADEN,USBDIV,PLL1,CPUDIV1,NOVREGEN
  12. #use delay(clock=48M)
  13. #HEXCOMMENT DESCOMENTAR while(!IOFS){}  //espera estabilización del Fosc
  14.  
  15. #use rs232(baud=9600, xmit=PIN_C0, rcv=PIN_C1,errors)
  16.  
  17. #use fast_io(A)
  18. #use fast_io(B)
  19. #use fast_io(C)
  20. #use fast_io(D)
  21. #use fast_io(E)
  22.  
  23. // definiciones
  24. #define LCD_DB4   PIN_A1
  25. #define LCD_DB5   PIN_A2
  26. #define LCD_DB6   PIN_A3
  27. #define LCD_DB7   PIN_A4
  28. #define LCD_RS     PIN_A5
  29. //#define LCD_RW    PIN_B0
  30. #define LCD_E      PIN_E0
  31. #define LED_BICOLOR      PIN_E1
  32.  
  33. #define MMC_CLK   PIN_B1
  34. #define MMC_DI    PIN_B0  // configurar RB0 como entrada en el trisb
  35. #define MMC_DO    PIN_C7
  36. #define MMC_CS    PIN_C2
  37.  
  38. #byte POSTINC0 = 0xFEE
  39. #byte FSR0H = 0xFEA
  40. #byte OSCCON=0xFD3
  41. #bit IOFS=OSCCON.2
  42.  
  43.  
  44. #include "mi_driver_MMC.c"
  45. #include "flex_lcd_c.c"
  46.  
  47. void limpiar_la_ram(){
  48. #asm
  49.       lfsr  0,0     //FSR0=0,banco0=0
  50. next: clrf  POSTINC0
  51.       btfss FSR0H,3  // 0x800 = 1000:00000000 , banco7=0x800-1
  52.       bra   next
  53.       nop
  54. #endasm
  55. }
  56.  
  57. void main(void){
  58.    int res=0xff,t;
  59.    
  60.    output_a(0);
  61.    output_b(0);
  62.    output_c(0);
  63.    output_d(0);
  64.    output_e(0);
  65.  
  66.    set_tris_a(1);
  67.    set_tris_b(0b11110001);
  68.    set_tris_c(1);
  69.    set_tris_d(0);
  70.    set_tris_e(0);
  71.    
  72.    port_b_pullups(false);   // voy a usar pull-up externos
  73.    setup_adc(ADC_OFF);
  74.    disable_interrupts(global);
  75.  
  76.    limpiar_la_ram();
  77.    lcd_init();
  78.    lcd_putc("Iniciando MMC\n");
  79.    res=iIniciar_MMC();
  80.    if(!res){
  81.       lcd_putc("MMC OK ;)");
  82.    }else{
  83.       lcd_putc("Fallo MMC :(");
  84.    }
  85. }
  86.  
  87. //****************************************************************

Slalen aquí está el código que te dije, se me había olvidado subirlo.

La propiedad privada es la mayor garantía de libertad.
Friedrich August von Hayek

Desconectado ivan braga

  • PIC12
  • **
  • Mensajes: 77
Re: SPI 18f4550 no funciona el pin de salida...!
« Respuesta #4 en: 07 de Julio de 2008, 20:08:38 »
Olá Fernando,

No site abaixo encontrei muita informação, que me ajudou bastante a compreender a comunicação SPI por hardware para tajetas MMC.

Espero que te ajude.

Ivan

http://www.ccsinfo.com/forum/viewtopic.php?t=31532&postdays=0&postorder=asc&highlight=mmc+protues&start=0&sid=27b8eaecc0fc6445ad3f87d0625d3f5e
"Mesmo o objeto mais inanimado tem movimento suficiente para ficar na sua frente e provocar um acidente."
"Incluso el objeto más inanimado tiene suficiente movimiento para interponerse en su camino y provocar un accidente."
"Even the most inanimate object has enough movement to stand in your way and cause an accident."


 

anything