Advertisement
irieelectronics

mcp3208 & SPI axoloti

Aug 23rd, 2015
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. /*
  2. MCP3208 script2 code
  3. by paul
  4.  
  5. Connect the MCP3208 CS pin to the NSS(PA4) or any other digital out pin of axoloti.
  6. In this example, the MCP3208 CS pin is connected to axoloti B0(GPIOB,0) pin.
  7.  
  8. If you use more then one spi device, it's importent to disable every other spi device by switching their cs pin.
  9. */
  10.  
  11. uint8_t *txbuf;
  12. uint8_t *rxbuf;
  13.  
  14. void setup(void){
  15.     static uint8_t _txbuf[8] __attribute__ ((section (".sram2")));
  16.     static uint8_t _rxbuf[8] __attribute__ ((section (".sram2")));
  17.     txbuf = _txbuf;
  18.     rxbuf = _rxbuf;
  19.  
  20.     palSetPadMode(GPIOA,0,PAL_MODE_OUTPUT_PUSHPULL);    // MCP3208
  21.     palWritePad(GPIOA,0,1);                             // pull high to disable first MCP3208
  22. }
  23.  
  24. void loop(void){
  25.     // txtbuf[0] = txtbuf[1] = txtbuf[2] = 0b00000000;
  26.     txbuf[2] = 0b00000000;
  27.    
  28.         for(int pin=0; pin<8; pin++){
  29.  
  30.             txbuf[0] = pin < 4 ? 0b01100000 : 0b11100000;
  31.      
  32.             if (pin % 4 == 0) { // pin == 0 || pin == 4
  33.                 txbuf[1] = 0b00000000;
  34.             } else if (pin % 4 == 1) { // pin == 1 || pin == 5
  35.                 txbuf[1] = 0b00000010;
  36.             } else if (pin % 4 == 2) { // pin == 2 || pin == 6
  37.                 txbuf[1] = 0b00000001;
  38.             } else {
  39.                 txbuf[1] = 0b00000011;
  40.             }
  41.            
  42.  
  43.         palWritePad(GPIOA,0,0);     // enable MCP3208
  44.         spiSelect(&SPID1);          // START SPI
  45.         spiSend(&SPID1,3,txbuf);    // send SPI data txbuf[]
  46.         spiReceive(&SPID1,3,rxbuf); // receive SPI data from MCP3208
  47.         spiUnselect(&SPID1);        // STOP SPI
  48.         palWritePad(GPIOA,0,1);     // disable MCP3208
  49.                
  50.         int z = (rxbuf[1] << 8| rxbuf[0]) << 16;
  51.  
  52.         if (pin == 0){
  53.             PExParameterChange(&parent->PExch[PARAM_INDEX_c0_value],z,0xFFFD);
  54.         }
  55.         else if (pin == 1){
  56.             PExParameterChange(&parent->PExch[PARAM_INDEX_c1_value],z,0xFFFD);
  57.         }
  58.         else if (pin == 2){
  59.             PExParameterChange(&parent->PExch[PARAM_INDEX_c2_value],z,0xFFFD);
  60.         }
  61.         else if (pin == 3){
  62.             PExParameterChange(&parent->PExch[PARAM_INDEX_c3_value],z,0xFFFD);
  63.         }
  64.         else if (pin == 4){
  65.             PExParameterChange(&parent->PExch[PARAM_INDEX_c4_value],z,0xFFFD);
  66.         }
  67.         else if (pin == 5){
  68.             PExParameterChange(&parent->PExch[PARAM_INDEX_c5_value],z,0xFFFD);
  69.         }
  70.         else if (pin == 6){
  71.             PExParameterChange(&parent->PExch[PARAM_INDEX_c6_value],z,0xFFFD);
  72.         }
  73.         else if (pin == 7){
  74.             PExParameterChange(&parent->PExch[PARAM_INDEX_c7_value],z,0xFFFD);
  75.         }  
  76.     }
  77.     chThdSleepMilliseconds(1);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement