Autor Tema: librería MCP6S21  (Leído 1862 veces)

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

Desconectado vtasco

  • PIC12
  • **
  • Mensajes: 72
librería MCP6S21
« en: 07 de Julio de 2008, 18:28:49 »
Amigos: dejo acá la 'librería' que me hice para usar el MCP6S21. Funciona bien hasta el momento, también puse ahí la configuración del módulo SPI del dsPIC30f401 como máster, está la opción de ponerlo como slave, pero no lo he probado.
Si hay algo que pueda mejorarse, estoy muy interesado en saberlo.

Saludos a todos


Código: C
  1. //configura y usa modulo SPI
  2. //funciones:
  3. //- spi_init_slave(void): configura el módulo como esclavo, sin SS.
  4. //- spi_init_master(void): configura el módulo como maestro.
  5. //gain_pga1() setea ganancia del MCP6S21
  6.  
  7. #include "p30f4011.h"
  8. #define CS1 LATFbits.LATF0
  9. extern int *dptr;
  10.  
  11. void spi_init_master(void)
  12. {
  13.         CS1=1;                                  //para no escribir sobre el PGA
  14.         SPI1STATbits.SPIEN=0;   //deshabilita el módulo SPI
  15.         IFS0bits.SPI1IF=0;              //limpia la bandera de interupción
  16.         IEC0bits.SPI1IE=0;              //deshabilita la interrupción del módulo
  17.         IPC2bits.SPI1IP=0x2;    //prioridad de interrupcion Nº2 (ADC tiene 4)
  18.         SPI1CONbits.MSTEN=1;    //como maestro
  19.         SPI1CONbits.CKE=1;
  20.         SPI1CONbits.SPRE=0b000; //prescaler secundario 1
  21.         SPI1CONbits.PPRE=0b00;  //prescaler primario 1
  22.         SPI1STATbits.SPIROV=0;  //No receive overflow
  23.         SPI1STATbits.SPIEN=1;   //habilita el módulo SPI
  24. }
  25.  
  26. void spi_init_slave(void)/*sin SS habilitado*/
  27. {
  28.         SPI1BUF=0;                              //limpia el buffer del módulo
  29.         IFS0bits.SPI1IF=0;              //limpia la bandera de interupción
  30.         IEC0bits.SPI1IE=1;              //habilita la interrupción del módulo
  31.         IPC2bits.SPI1IP=0x3;    //prioridad de interrupcion Nº3 (ADC tiene 4)
  32.         SPI1CONbits.MSTEN=0;    //como slave
  33.         SPI1CONbits.SMP=0;              //debe sere deshabilitado en modo Slave
  34.         SPI1STATbits.SPIROV=0;  //No receive overflow
  35.         SPI1STATbits.SPIEN=0;   //habilita el módulo SPI
  36. }
  37.  
  38. int spi_out(int send)
  39. {
  40.  
  41.         SPI1BUF=send;
  42.         while(!SPI1STATbits.SPIRBF){}
  43.  
  44.         return SPI1BUF;
  45. }
  46.  
  47. void gain_pga1(int gain)
  48. {
  49.         switch(gain)
  50.         {
  51.                 case 0:
  52.                         spi_out(0b00100000);
  53.                         spi_out(0x0);   //dummy data                   
  54.                         break;
  55.                 case 1:
  56.                         spi_out(0b01000000);
  57.                         spi_out(0b00000000);
  58.                         break;
  59.                 case 2:
  60.                         spi_out(0b01000000);
  61.                         spi_out(0b00000001);
  62.                         break;
  63.                 case 4:
  64.                         spi_out(0b00000010);
  65.                         spi_out(0b00000010);
  66.                         break;
  67.                 case 5:
  68.                         spi_out(0b01000000);
  69.                         spi_out(0b00000011);
  70.                         break;
  71.                 case 8:
  72.                         spi_out(0b01000000);
  73.                         spi_out(0b00000100);
  74.                         RE8=1;delay_ms(5);RE8=0;
  75.                         break;
  76.                 case 10:
  77.                         spi_out(0b01000000);
  78.                         spi_out(0b00000101);
  79.                         RE8=1;RE8=0;
  80.                         break;
  81.                 case 16:
  82.                         spi_out(0b01000000);
  83.                         spi_out(0b00000110);
  84.                         break;
  85.                 case 32:
  86.                         spi_out(0b01000000);
  87.                         spi_out(0b00000111);
  88.                         break;
  89.         }
  90.         CS1=1;
  91. }

http://vtasco.googlepages.com/spi.c (para bajarla)