Software driver for MCP6S22 PGA

Standard

Software driver for MCP6S22 ( SPI  Programmable Gain Amplifiers ) 

     h file first :

#ifndef __MCP6S22
#define __MCP6S22

//*************************************************************************************************
//                        Define output pins for MCU here
//
//               In main case :    RB3=CS, RB4=SI, RB5=SCK
//
//*************************************************************************************************
#define            MCP6S22_CS_PIN_INIT                            TRISBbits.TRISB3 = 0
#define             MCP6S22_CS                                     LATBbits.LATB3

#define            MCP6S22_SI_PIN_INIT                            TRISBbits.TRISB4 = 0
#define             MCP6S22_SI                                     LATBbits.LATB4

#define            MCP6S22_SCK_PIN_INIT                            TRISBbits.TRISB5 = 0
#define             MCP6S22_SCK                                     LATBbits.LATB5

//*************************************************************************************************
//                                 definitions for “data”
//*************************************************************************************************
#define gain1  0 // Gain of 1
#define gain2  1 // Gain of 2
#define gain4  2 // Gain of 4
#define gain5  3 // Gain of 5
#define gain8  4 // Gain of 8
#define gain10 5 // Gain of 10
#define gain16 6 // Gain of 16
#define gain32 7 // Gain of 32

#define channel_0 0 // channel 0
#define channel_1 1 // channel 1
#define channel_2 2 // channel 2
#define channel_3 3 // channel 3
#define channel_4 4 // channel 4
#define channel_5 5 // channel 5

//*************************************************************************************************
//                                 definitions for “instructions”
//*************************************************************************************************
#define PrgGain    64 // instruction for gain register
#define PrgChannel 65 // instruction for channel register
#define PrgShdn    32 // instruction to shutdown PGA

//*************************************************************************************************
//                            subrutines
//*************************************************************************************************
void Init_MCP6S22 (void);
void Send_2_Bytes_to_MCP6S22(uint8 Instruction, uint8 Data);

#endif

 Subrutines (C file):

//*************************************************************************************************
//                                    Init_MCP6S22
//*************************************************************************************************
void Init_MCP6S22 (void)
{
MCP6S22_CS_PIN_INIT    ;
MCP6S22_CS = 1;

MCP6S22_SI_PIN_INIT;
MCP6S22_SI = 1;

MCP6S22_SCK_PIN_INIT;
MCP6S22_SCK = 1;
}

//*************************************************************************************************
//                                    Send_2_Bytes_to_MCP6S22
//*************************************************************************************************
void Send_2_Bytes_to_MCP6S22(uint8 Instruction, uint8 Data)
{    uint8 t;
MCP6S22_CS = 0;
// 1 Instruction
for(t=0;t<8;t++)
{     MCP6S22_SCK = 0;

if((Instruction & 0x80)==0x80){ MCP6S22_SI = 1;}
else{ MCP6S22_SI = 0;}

Instruction <<=1;
Delay1ms(2);
MCP6S22_SCK = 1;
Delay1ms(2);
MCP6S22_SCK = 0;
Delay1ms(2);
}
// 2 Data
for(t=0;t<8;t++)
{     MCP6S22_SCK = 0;

if((Data & 0x80)==0x80){ MCP6S22_SI = 1;}
else{ MCP6S22_SI = 0;}

Data <<=1;
Delay1ms(2);
MCP6S22_SCK = 1;
Delay1ms(2);
MCP6S22_SCK = 0;
Delay1ms(2);
}

MCP6S22_CS = 1;
}

 

Exampe How to call subrutines :

    Init_MCP6S22();
Send_2_Bytes_to_MCP6S22(PrgGain, gain4);
Send_2_Bytes_to_MCP6S22(PrgChannel, channel_5);

 20141115_18440220141115_184440