![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IVSC Project
DigiPot.cpp@4:262764d24e4d, 2013-06-07 (annotated)
- Committer:
- kevinkent
- Date:
- Fri Jun 07 19:59:29 2013 +0000
- Revision:
- 4:262764d24e4d
- Parent:
- 2:d0778c36d28d
Version 1.2
; Added MSG[0] = 0x06 tow write one pot chain with a single message
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevinkent | 1:82f2ef52759e | 1 | // |
kevinkent | 1:82f2ef52759e | 2 | #include "DigiPot.h" |
kevinkent | 1:82f2ef52759e | 3 | #include "MCP4651.h" |
kevinkent | 1:82f2ef52759e | 4 | |
kevinkent | 2:d0778c36d28d | 5 | Serial dbg1(USBTX,USBRX); |
kevinkent | 2:d0778c36d28d | 6 | |
kevinkent | 1:82f2ef52759e | 7 | // Enable pins for PCA9 |
kevinkent | 1:82f2ef52759e | 8 | BusOut Pbus(p14,p13,p12,p11); |
kevinkent | 1:82f2ef52759e | 9 | |
kevinkent | 4:262764d24e4d | 10 | // This is the pins used for I2C and the base address of the chip |
kevinkent | 1:82f2ef52759e | 11 | |
kevinkent | 1:82f2ef52759e | 12 | MCP4651 Chip1(p9, p10, 0x1); |
kevinkent | 1:82f2ef52759e | 13 | MCP4651 Chip2(p9, p10, 0x2); |
kevinkent | 1:82f2ef52759e | 14 | MCP4651 Chip3(p9, p10, 0x3); |
kevinkent | 1:82f2ef52759e | 15 | MCP4651 Chip4(p9, p10, 0x5); |
kevinkent | 1:82f2ef52759e | 16 | MCP4651 Chip5(p9, p10, 0x6); |
kevinkent | 1:82f2ef52759e | 17 | MCP4651 Chip6(p9, p10, 0x7); |
kevinkent | 1:82f2ef52759e | 18 | |
kevinkent | 1:82f2ef52759e | 19 | |
kevinkent | 1:82f2ef52759e | 20 | // The MCP4651 go to mid scale on reset |
kevinkent | 1:82f2ef52759e | 21 | // Use the general call commands to set all pots to zero |
kevinkent | 1:82f2ef52759e | 22 | // Set the lowest values pot to approximately 10k |
kevinkent | 1:82f2ef52759e | 23 | |
kevinkent | 1:82f2ef52759e | 24 | int PotReset(void) { |
kevinkent | 1:82f2ef52759e | 25 | char wip0[2] = {0x80, 0x0}; |
kevinkent | 1:82f2ef52759e | 26 | char wip1[2] = {0x90, 0x0}; |
kevinkent | 1:82f2ef52759e | 27 | I2C GenCall(p9,p10); |
kevinkent | 1:82f2ef52759e | 28 | |
kevinkent | 1:82f2ef52759e | 29 | // Reset R1 & R2 |
kevinkent | 1:82f2ef52759e | 30 | Pbus = 0x1; |
kevinkent | 1:82f2ef52759e | 31 | GenCall.write(0x0, wip0, 2); |
kevinkent | 1:82f2ef52759e | 32 | GenCall.write(0x0, wip1, 2); |
kevinkent | 1:82f2ef52759e | 33 | Chip1.SetValue(0,50); // About 10k on 50k pot |
kevinkent | 1:82f2ef52759e | 34 | Chip4.SetValue(0,50); |
kevinkent | 1:82f2ef52759e | 35 | |
kevinkent | 1:82f2ef52759e | 36 | // Reset R3 & R4 |
kevinkent | 1:82f2ef52759e | 37 | Pbus = 0x2; |
kevinkent | 1:82f2ef52759e | 38 | GenCall.write(0x0, wip0, 2); |
kevinkent | 1:82f2ef52759e | 39 | GenCall.write(0x0, wip1, 2); |
kevinkent | 1:82f2ef52759e | 40 | Chip1.SetValue(0,50); |
kevinkent | 1:82f2ef52759e | 41 | Chip4.SetValue(0,50); |
kevinkent | 1:82f2ef52759e | 42 | |
kevinkent | 1:82f2ef52759e | 43 | // Reset R5 & R6 |
kevinkent | 1:82f2ef52759e | 44 | Pbus = 0x4; |
kevinkent | 1:82f2ef52759e | 45 | GenCall.write(0x0, wip0, 2); |
kevinkent | 1:82f2ef52759e | 46 | GenCall.write(0x0, wip1, 2); |
kevinkent | 1:82f2ef52759e | 47 | Chip1.SetValue(0,50); |
kevinkent | 1:82f2ef52759e | 48 | Chip4.SetValue(0,50); |
kevinkent | 1:82f2ef52759e | 49 | |
kevinkent | 1:82f2ef52759e | 50 | // Pbus = 0x8; /* Not Used at this time */ |
kevinkent | 1:82f2ef52759e | 51 | Pbus = 0; |
kevinkent | 1:82f2ef52759e | 52 | return(0); |
kevinkent | 1:82f2ef52759e | 53 | } // End PotReset |
kevinkent | 1:82f2ef52759e | 54 | |
kevinkent | 1:82f2ef52759e | 55 | // Decode the HID report and set the pots |
kevinkent | 1:82f2ef52759e | 56 | int SetResist( uint8_t *data) { |
kevinkent | 1:82f2ef52759e | 57 | int wiper = 0, value = 0; |
kevinkent | 2:d0778c36d28d | 58 | dbg1.printf("data[0] = %d\n",data[0]); |
kevinkent | 2:d0778c36d28d | 59 | dbg1.printf("data[1] = %d\n",data[1]); |
kevinkent | 2:d0778c36d28d | 60 | dbg1.printf("data[2] = %d\n",data[2]); |
kevinkent | 2:d0778c36d28d | 61 | dbg1.printf("data[3] = %d\n",data[3]); |
kevinkent | 2:d0778c36d28d | 62 | dbg1.printf("data[4] = %d\n",data[4]); |
kevinkent | 2:d0778c36d28d | 63 | dbg1.printf("data[5] = %d\n",data[5]); |
kevinkent | 4:262764d24e4d | 64 | if (data[0] != 3) return(1); // should not be here |
kevinkent | 1:82f2ef52759e | 65 | if (data[1] == 0) return(1); // Zero = bus disable |
kevinkent | 1:82f2ef52759e | 66 | if (data[3] == 0) return(1); // Zero disallowed |
kevinkent | 1:82f2ef52759e | 67 | Pbus = data[1]; |
kevinkent | 4:262764d24e4d | 68 | if(data[4] > 0) wiper = (data[4] - 1); //Wiper on Device is zero based |
kevinkent | 4:262764d24e4d | 69 | value = data[5]; // Pot value |
kevinkent | 1:82f2ef52759e | 70 | |
kevinkent | 1:82f2ef52759e | 71 | switch (data[3]) { |
kevinkent | 1:82f2ef52759e | 72 | case 1: |
kevinkent | 1:82f2ef52759e | 73 | Chip1.SetValue(wiper, value); |
kevinkent | 2:d0778c36d28d | 74 | dbg1.printf("Set Pbus = %d, Chip = %d, Pot = %d, Value = %d\n", data[1], data[3], wiper, value); |
kevinkent | 1:82f2ef52759e | 75 | break; |
kevinkent | 1:82f2ef52759e | 76 | case 2: |
kevinkent | 1:82f2ef52759e | 77 | Chip2.SetValue(wiper, value); |
kevinkent | 2:d0778c36d28d | 78 | dbg1.printf("Set Pbus = %d, Chip = %d, Pot = %d, Value = %d\n", data[1], data[3], wiper, value); |
kevinkent | 1:82f2ef52759e | 79 | break; |
kevinkent | 1:82f2ef52759e | 80 | case 3: |
kevinkent | 1:82f2ef52759e | 81 | Chip3.SetValue(wiper, value); |
kevinkent | 2:d0778c36d28d | 82 | dbg1.printf("Set Pbus = %d, Chip = %d, Pot = %d, Value = %d\n", data[1], data[3], wiper, value); |
kevinkent | 1:82f2ef52759e | 83 | break; |
kevinkent | 1:82f2ef52759e | 84 | case 4: |
kevinkent | 1:82f2ef52759e | 85 | Chip4.SetValue(wiper, value); |
kevinkent | 2:d0778c36d28d | 86 | dbg1.printf("Set Pbus = %d, Chip = %d, Pot = %d, Value = %d\n", data[1], data[3], wiper, value); |
kevinkent | 1:82f2ef52759e | 87 | break; |
kevinkent | 1:82f2ef52759e | 88 | case 5: |
kevinkent | 1:82f2ef52759e | 89 | Chip5.SetValue(wiper, value); |
kevinkent | 2:d0778c36d28d | 90 | dbg1.printf("Set Pbus = %d, Chip = %d, Pot = %d, Value = %d\n", data[1], data[3], wiper, value); |
kevinkent | 1:82f2ef52759e | 91 | break; |
kevinkent | 1:82f2ef52759e | 92 | case 6: |
kevinkent | 1:82f2ef52759e | 93 | Chip6.SetValue(wiper, value); |
kevinkent | 2:d0778c36d28d | 94 | dbg1.printf("Set Pbus = %d, Chip = %d, Pot = %d, Value = %d\n", data[1], data[3], wiper, value); |
kevinkent | 1:82f2ef52759e | 95 | break; |
kevinkent | 1:82f2ef52759e | 96 | default: |
kevinkent | 1:82f2ef52759e | 97 | break; |
kevinkent | 1:82f2ef52759e | 98 | } |
kevinkent | 2:d0778c36d28d | 99 | dbg1.printf("Resistor Set Done\n"); |
kevinkent | 1:82f2ef52759e | 100 | data[0] = 0; |
kevinkent | 1:82f2ef52759e | 101 | Pbus = 0; |
kevinkent | 1:82f2ef52759e | 102 | return(0); |
kevinkent | 1:82f2ef52759e | 103 | } // End SetResist |
kevinkent | 1:82f2ef52759e | 104 | |
kevinkent | 4:262764d24e4d | 105 | // Decode the HID report and set the pots |
kevinkent | 4:262764d24e4d | 106 | // This sets all pots in one string from the same USB message |
kevinkent | 4:262764d24e4d | 107 | // The HID report is the same format as SetResist except: |
kevinkent | 4:262764d24e4d | 108 | // the Chip#, Pot#, Value must be set for all 6 pots in the string. |
kevinkent | 4:262764d24e4d | 109 | // Only the Value is used and Chip# must == The top chip in the string. |
kevinkent | 4:262764d24e4d | 110 | int SetResistChain( uint8_t *data) { |
kevinkent | 4:262764d24e4d | 111 | |
kevinkent | 4:262764d24e4d | 112 | dbg1.printf("Starting string set\n"); |
kevinkent | 4:262764d24e4d | 113 | dbg1.printf("data[0] = %d\n",data[0]); |
kevinkent | 4:262764d24e4d | 114 | |
kevinkent | 4:262764d24e4d | 115 | |
kevinkent | 4:262764d24e4d | 116 | if (data[0] != 6) return(1); |
kevinkent | 4:262764d24e4d | 117 | if (data[1] == 0) return(1); // Zero = bus disable |
kevinkent | 4:262764d24e4d | 118 | if (data[3] == 0) return(1); // Zero = disallowed chip # |
kevinkent | 4:262764d24e4d | 119 | Pbus = data[1]; |
kevinkent | 4:262764d24e4d | 120 | dbg1.printf("Set Pbus = %d\n", data[1]); |
kevinkent | 4:262764d24e4d | 121 | if (data[3] == 1) { |
kevinkent | 4:262764d24e4d | 122 | Chip1.SetValue(0, data[5]); |
kevinkent | 4:262764d24e4d | 123 | Chip1.SetValue(1, data[8]); |
kevinkent | 4:262764d24e4d | 124 | dbg1.printf("Chip1: Wip0 = %d, Wip1 = %d\n", data[5], data[8]); |
kevinkent | 4:262764d24e4d | 125 | |
kevinkent | 4:262764d24e4d | 126 | Chip2.SetValue(0, data[11]); |
kevinkent | 4:262764d24e4d | 127 | Chip2.SetValue(1, data[14]); |
kevinkent | 4:262764d24e4d | 128 | dbg1.printf("Chip2: Wip0 = %d, Wip1 = %d\n", data[11], data[14]); |
kevinkent | 4:262764d24e4d | 129 | |
kevinkent | 4:262764d24e4d | 130 | Chip3.SetValue(0, data[17]); |
kevinkent | 4:262764d24e4d | 131 | Chip3.SetValue(1, data[20]); |
kevinkent | 4:262764d24e4d | 132 | dbg1.printf("Chip3: Wip0 = %d, Wip1 = %d\n", data[17], data[20]); |
kevinkent | 4:262764d24e4d | 133 | } |
kevinkent | 4:262764d24e4d | 134 | |
kevinkent | 4:262764d24e4d | 135 | if (data[3] == 4) { |
kevinkent | 4:262764d24e4d | 136 | Chip4.SetValue(0, data[5]); |
kevinkent | 4:262764d24e4d | 137 | Chip4.SetValue(1, data[8]); |
kevinkent | 4:262764d24e4d | 138 | dbg1.printf("Chip4: Wip0 = %d, Wip1 = %d\n", data[5], data[8]); |
kevinkent | 4:262764d24e4d | 139 | |
kevinkent | 4:262764d24e4d | 140 | Chip5.SetValue(0, data[11]); |
kevinkent | 4:262764d24e4d | 141 | Chip5.SetValue(1, data[14]); |
kevinkent | 4:262764d24e4d | 142 | dbg1.printf("Chip5: Wip0 = %d, Wip1 = %d\n", data[11], data[14]); |
kevinkent | 4:262764d24e4d | 143 | |
kevinkent | 4:262764d24e4d | 144 | Chip6.SetValue(0, data[17]); |
kevinkent | 4:262764d24e4d | 145 | Chip6.SetValue(1, data[20]); |
kevinkent | 4:262764d24e4d | 146 | dbg1.printf("Chip6: Wip0 = %d, Wip1 = %d\n", data[17], data[20]); |
kevinkent | 4:262764d24e4d | 147 | } |
kevinkent | 4:262764d24e4d | 148 | dbg1.printf("Resistor Chain Set Done\n"); |
kevinkent | 4:262764d24e4d | 149 | data[0] = 0; |
kevinkent | 4:262764d24e4d | 150 | Pbus = 0; |
kevinkent | 4:262764d24e4d | 151 | return(0); |
kevinkent | 4:262764d24e4d | 152 | } // End SetResistChain |
kevinkent | 4:262764d24e4d | 153 |