This is a library for the Texas Instruments PGA112

Committer:
rvasquez6089
Date:
Mon Jan 21 19:00:25 2019 +0000
Revision:
0:4cb6d8393e9d
Not tested yet, but will soon. Should work just fine.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rvasquez6089 0:4cb6d8393e9d 1 #include "PGA112_Driver.h"
rvasquez6089 0:4cb6d8393e9d 2
rvasquez6089 0:4cb6d8393e9d 3
rvasquez6089 0:4cb6d8393e9d 4 PGA112::PGA112(SPI *SPI_BUS, DigitalOut *Chip_Select)
rvasquez6089 0:4cb6d8393e9d 5 {
rvasquez6089 0:4cb6d8393e9d 6 PGA = SPI_BUS;
rvasquez6089 0:4cb6d8393e9d 7 CS = Chip_Select;
rvasquez6089 0:4cb6d8393e9d 8 gain = 1;
rvasquez6089 0:4cb6d8393e9d 9 channel = 1;
rvasquez6089 0:4cb6d8393e9d 10 CS->write(1);
rvasquez6089 0:4cb6d8393e9d 11 }
rvasquez6089 0:4cb6d8393e9d 12
rvasquez6089 0:4cb6d8393e9d 13 void PGA112::set_gain(int gain_set)
rvasquez6089 0:4cb6d8393e9d 14 {
rvasquez6089 0:4cb6d8393e9d 15 char temp = 0x00;
rvasquez6089 0:4cb6d8393e9d 16 gain = gain_set;
rvasquez6089 0:4cb6d8393e9d 17
rvasquez6089 0:4cb6d8393e9d 18 switch(gain)
rvasquez6089 0:4cb6d8393e9d 19 {
rvasquez6089 0:4cb6d8393e9d 20 case 1:
rvasquez6089 0:4cb6d8393e9d 21 temp = GAIN_1<<4;
rvasquez6089 0:4cb6d8393e9d 22 break;
rvasquez6089 0:4cb6d8393e9d 23 case 2:
rvasquez6089 0:4cb6d8393e9d 24 temp = GAIN_2<<4;
rvasquez6089 0:4cb6d8393e9d 25 break;
rvasquez6089 0:4cb6d8393e9d 26 case 4:
rvasquez6089 0:4cb6d8393e9d 27 temp = GAIN_4<<4;
rvasquez6089 0:4cb6d8393e9d 28 break;
rvasquez6089 0:4cb6d8393e9d 29 case 8:
rvasquez6089 0:4cb6d8393e9d 30 temp = GAIN_8<<4;
rvasquez6089 0:4cb6d8393e9d 31 break;
rvasquez6089 0:4cb6d8393e9d 32 case 16:
rvasquez6089 0:4cb6d8393e9d 33 temp = GAIN_16<<4;
rvasquez6089 0:4cb6d8393e9d 34 break;
rvasquez6089 0:4cb6d8393e9d 35 case 32:
rvasquez6089 0:4cb6d8393e9d 36 temp = GAIN_32<<4;
rvasquez6089 0:4cb6d8393e9d 37 break;
rvasquez6089 0:4cb6d8393e9d 38 case 64:
rvasquez6089 0:4cb6d8393e9d 39 temp = GAIN_64<<4;
rvasquez6089 0:4cb6d8393e9d 40 break;
rvasquez6089 0:4cb6d8393e9d 41 case 128:
rvasquez6089 0:4cb6d8393e9d 42 temp = GAIN_128<<4;
rvasquez6089 0:4cb6d8393e9d 43 break;
rvasquez6089 0:4cb6d8393e9d 44 }
rvasquez6089 0:4cb6d8393e9d 45 switch(channel)
rvasquez6089 0:4cb6d8393e9d 46 {
rvasquez6089 0:4cb6d8393e9d 47 case 0:
rvasquez6089 0:4cb6d8393e9d 48 gain_chan = temp + VCAL_CH0;
rvasquez6089 0:4cb6d8393e9d 49 break;
rvasquez6089 0:4cb6d8393e9d 50 case 1:
rvasquez6089 0:4cb6d8393e9d 51 gain_chan = temp + CH_1;
rvasquez6089 0:4cb6d8393e9d 52 break;
rvasquez6089 0:4cb6d8393e9d 53 case 12:
rvasquez6089 0:4cb6d8393e9d 54 gain_chan = temp + CAL_1;
rvasquez6089 0:4cb6d8393e9d 55 break;
rvasquez6089 0:4cb6d8393e9d 56 case 13:
rvasquez6089 0:4cb6d8393e9d 57 gain_chan = temp + CAL_2;
rvasquez6089 0:4cb6d8393e9d 58 break;
rvasquez6089 0:4cb6d8393e9d 59 case 14:
rvasquez6089 0:4cb6d8393e9d 60 gain_chan = temp + CAL_3;
rvasquez6089 0:4cb6d8393e9d 61 break;
rvasquez6089 0:4cb6d8393e9d 62 case 15:
rvasquez6089 0:4cb6d8393e9d 63 gain_chan = temp + CAL_4;
rvasquez6089 0:4cb6d8393e9d 64 break;
rvasquez6089 0:4cb6d8393e9d 65 }
rvasquez6089 0:4cb6d8393e9d 66 CS->write(0);
rvasquez6089 0:4cb6d8393e9d 67 PGA->write(CMD_WRITE);
rvasquez6089 0:4cb6d8393e9d 68 PGA->write(gain_chan);
rvasquez6089 0:4cb6d8393e9d 69 CS->write(1);
rvasquez6089 0:4cb6d8393e9d 70 }
rvasquez6089 0:4cb6d8393e9d 71
rvasquez6089 0:4cb6d8393e9d 72 void PGA112::set_channel(int channel_set)
rvasquez6089 0:4cb6d8393e9d 73 {
rvasquez6089 0:4cb6d8393e9d 74 char temp = 0x00;
rvasquez6089 0:4cb6d8393e9d 75 channel = channel_set;
rvasquez6089 0:4cb6d8393e9d 76
rvasquez6089 0:4cb6d8393e9d 77 switch(gain)
rvasquez6089 0:4cb6d8393e9d 78 {
rvasquez6089 0:4cb6d8393e9d 79 case 1:
rvasquez6089 0:4cb6d8393e9d 80 temp = GAIN_1<<4;
rvasquez6089 0:4cb6d8393e9d 81 break;
rvasquez6089 0:4cb6d8393e9d 82 case 2:
rvasquez6089 0:4cb6d8393e9d 83 temp = GAIN_2<<4;
rvasquez6089 0:4cb6d8393e9d 84 break;
rvasquez6089 0:4cb6d8393e9d 85 case 4:
rvasquez6089 0:4cb6d8393e9d 86 temp = GAIN_4<<4;
rvasquez6089 0:4cb6d8393e9d 87 break;
rvasquez6089 0:4cb6d8393e9d 88 case 8:
rvasquez6089 0:4cb6d8393e9d 89 temp = GAIN_8<<4;
rvasquez6089 0:4cb6d8393e9d 90 break;
rvasquez6089 0:4cb6d8393e9d 91 case 16:
rvasquez6089 0:4cb6d8393e9d 92 temp = GAIN_16<<4;
rvasquez6089 0:4cb6d8393e9d 93 break;
rvasquez6089 0:4cb6d8393e9d 94 case 32:
rvasquez6089 0:4cb6d8393e9d 95 temp = GAIN_32<<4;
rvasquez6089 0:4cb6d8393e9d 96 break;
rvasquez6089 0:4cb6d8393e9d 97 case 64:
rvasquez6089 0:4cb6d8393e9d 98 temp = GAIN_64<<4;
rvasquez6089 0:4cb6d8393e9d 99 break;
rvasquez6089 0:4cb6d8393e9d 100 case 128:
rvasquez6089 0:4cb6d8393e9d 101 temp = GAIN_128<<4;
rvasquez6089 0:4cb6d8393e9d 102 break;
rvasquez6089 0:4cb6d8393e9d 103 }
rvasquez6089 0:4cb6d8393e9d 104 switch(channel)
rvasquez6089 0:4cb6d8393e9d 105 {
rvasquez6089 0:4cb6d8393e9d 106 case 0:
rvasquez6089 0:4cb6d8393e9d 107 gain_chan = temp + VCAL_CH0;
rvasquez6089 0:4cb6d8393e9d 108 break;
rvasquez6089 0:4cb6d8393e9d 109 case 1:
rvasquez6089 0:4cb6d8393e9d 110 gain_chan = temp + CH_1;
rvasquez6089 0:4cb6d8393e9d 111 break;
rvasquez6089 0:4cb6d8393e9d 112 case 12:
rvasquez6089 0:4cb6d8393e9d 113 gain_chan = temp + CAL_1;
rvasquez6089 0:4cb6d8393e9d 114 break;
rvasquez6089 0:4cb6d8393e9d 115 case 13:
rvasquez6089 0:4cb6d8393e9d 116 gain_chan = temp + CAL_2;
rvasquez6089 0:4cb6d8393e9d 117 break;
rvasquez6089 0:4cb6d8393e9d 118 case 14:
rvasquez6089 0:4cb6d8393e9d 119 gain_chan = temp + CAL_3;
rvasquez6089 0:4cb6d8393e9d 120 break;
rvasquez6089 0:4cb6d8393e9d 121 case 15:
rvasquez6089 0:4cb6d8393e9d 122 gain_chan = temp + CAL_4;
rvasquez6089 0:4cb6d8393e9d 123 break;
rvasquez6089 0:4cb6d8393e9d 124 }
rvasquez6089 0:4cb6d8393e9d 125 CS->write(0);
rvasquez6089 0:4cb6d8393e9d 126 PGA->write(CMD_WRITE);
rvasquez6089 0:4cb6d8393e9d 127 PGA->write(gain_chan);
rvasquez6089 0:4cb6d8393e9d 128 CS->write(1);
rvasquez6089 0:4cb6d8393e9d 129 }