Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of digipot by
digipot.cpp@0:57bdd27601ad, 2015-04-03 (annotated)
- Committer:
- Braintrust
- Date:
- Fri Apr 03 20:04:16 2015 +0000
- Revision:
- 0:57bdd27601ad
- Child:
- 1:0b0025664bb7
Final, With debugging code included
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Braintrust | 0:57bdd27601ad | 1 | //Digipot code with 2nd CAN Transceiver for testing |
| Braintrust | 0:57bdd27601ad | 2 | |
| Braintrust | 0:57bdd27601ad | 3 | #include "mbed.h" |
| Braintrust | 0:57bdd27601ad | 4 | |
| Braintrust | 0:57bdd27601ad | 5 | static int id = 0x04; //Module CAN ID, to be assigned by keypad team. |
| Braintrust | 0:57bdd27601ad | 6 | static double input_amp = 3.3; //input analog signal is 3.3V |
| Braintrust | 0:57bdd27601ad | 7 | |
| Braintrust | 0:57bdd27601ad | 8 | static double R1 = 10; //Opamp resistor between output and inverting-input |
| Braintrust | 0:57bdd27601ad | 9 | static double R2 = 5.6; //Opamp resistor between inverting-input and ground |
| Braintrust | 0:57bdd27601ad | 10 | static double gain = 1 + R2/R1; //Non-inverting opamp gain |
| Braintrust | 0:57bdd27601ad | 11 | |
| Braintrust | 0:57bdd27601ad | 12 | //Debugging Variables |
| Braintrust | 0:57bdd27601ad | 13 | DigitalOut led1(LED1); //debugging LED1 |
| Braintrust | 0:57bdd27601ad | 14 | Ticker ticker; //triggers debugging send |
| Braintrust | 0:57bdd27601ad | 15 | char counter = 0; //Output amplitude |
| Braintrust | 0:57bdd27601ad | 16 | CAN can2(p30, p29); //2nd debugging send transceiver |
| Braintrust | 0:57bdd27601ad | 17 | |
| Braintrust | 0:57bdd27601ad | 18 | //Values to be used in final implementation |
| Braintrust | 0:57bdd27601ad | 19 | DigitalOut led2(LED2); //debugging LED2 |
| Braintrust | 0:57bdd27601ad | 20 | DigitalOut CS(p8); // inverted chip select |
| Braintrust | 0:57bdd27601ad | 21 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
| Braintrust | 0:57bdd27601ad | 22 | CAN can1(p9, p10); //reading transceiver |
| Braintrust | 0:57bdd27601ad | 23 | |
| Braintrust | 0:57bdd27601ad | 24 | |
| Braintrust | 0:57bdd27601ad | 25 | void digipot (int OutputAmp) |
| Braintrust | 0:57bdd27601ad | 26 | { |
| Braintrust | 0:57bdd27601ad | 27 | CS=0; // Deselects the digipot |
| Braintrust | 0:57bdd27601ad | 28 | // Setup the spi for 8 bit data, high steady state clock, |
| Braintrust | 0:57bdd27601ad | 29 | // second edge capture, with a 1MHz clock rate |
| Braintrust | 0:57bdd27601ad | 30 | spi.format(8,3); |
| Braintrust | 0:57bdd27601ad | 31 | spi.frequency(1000000); |
| Braintrust | 0:57bdd27601ad | 32 | |
| Braintrust | 0:57bdd27601ad | 33 | uint8_t wiper = uint8_t(OutputAmp*(255/(input_amp * gain))); //Generates appropriate Wiper position |
| Braintrust | 0:57bdd27601ad | 34 | |
| Braintrust | 0:57bdd27601ad | 35 | spi.write(0x11); // Write data command, potentiometer zero |
| Braintrust | 0:57bdd27601ad | 36 | spi.write(wiper); // Writes wiper value |
| Braintrust | 0:57bdd27601ad | 37 | |
| Braintrust | 0:57bdd27601ad | 38 | CS=1; // Selects the digipot, and executes command |
| Braintrust | 0:57bdd27601ad | 39 | } |
| Braintrust | 0:57bdd27601ad | 40 | |
| Braintrust | 0:57bdd27601ad | 41 | void send() //For debugging |
| Braintrust | 0:57bdd27601ad | 42 | { |
| Braintrust | 0:57bdd27601ad | 43 | if(can2.write(CANMessage(id, &counter, 1))) { |
| Braintrust | 0:57bdd27601ad | 44 | counter++; |
| Braintrust | 0:57bdd27601ad | 45 | if(counter ==6) |
| Braintrust | 0:57bdd27601ad | 46 | { |
| Braintrust | 0:57bdd27601ad | 47 | counter = 0; //reset counter |
| Braintrust | 0:57bdd27601ad | 48 | } |
| Braintrust | 0:57bdd27601ad | 49 | } |
| Braintrust | 0:57bdd27601ad | 50 | led1 = !led1; |
| Braintrust | 0:57bdd27601ad | 51 | } |
| Braintrust | 0:57bdd27601ad | 52 | |
| Braintrust | 0:57bdd27601ad | 53 | int main() |
| Braintrust | 0:57bdd27601ad | 54 | { |
| Braintrust | 0:57bdd27601ad | 55 | ticker.attach(&send,10); //Debugging: Changes output amplitude every 10 seconds |
| Braintrust | 0:57bdd27601ad | 56 | |
| Braintrust | 0:57bdd27601ad | 57 | int OutputAmp = 0; //Default output amplitude value |
| Braintrust | 0:57bdd27601ad | 58 | digipot(OutputAmp); |
| Braintrust | 0:57bdd27601ad | 59 | |
| Braintrust | 0:57bdd27601ad | 60 | CANMessage msg; |
| Braintrust | 0:57bdd27601ad | 61 | |
| Braintrust | 0:57bdd27601ad | 62 | while(1) |
| Braintrust | 0:57bdd27601ad | 63 | { |
| Braintrust | 0:57bdd27601ad | 64 | if(can1.read(msg) && msg.id == id) |
| Braintrust | 0:57bdd27601ad | 65 | { |
| Braintrust | 0:57bdd27601ad | 66 | OutputAmp = msg.data[0]; //Input is an int 0,1,2,3,4,5 |
| Braintrust | 0:57bdd27601ad | 67 | digipot(OutputAmp); //set digipot value |
| Braintrust | 0:57bdd27601ad | 68 | led2 = !led2; |
| Braintrust | 0:57bdd27601ad | 69 | } |
| Braintrust | 0:57bdd27601ad | 70 | } |
| Braintrust | 0:57bdd27601ad | 71 | } |
