This is a driver to control the SD20 20 Channel I2C to Servo Driver Chip from www.robot-electronics.co.uk (http://www.robot-electronics.co.uk/htm/sd20tech.htm). This driver will allow a servo channel to be started up, stopped and the duty cycle altered in standard mode. No attempt to include the extended mode is included.
SD20.cpp@2:b6789ad2af2b, 2016-02-21 (annotated)
- Committer:
- sk398
- Date:
- Sun Feb 21 23:19:27 2016 +0000
- Revision:
- 2:b6789ad2af2b
- Parent:
- 1:6557cf755742
- Child:
- 3:447af15af7c7
Beginning commenting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 2:b6789ad2af2b | 1 | /* ##################################################################### |
sk398 | 2:b6789ad2af2b | 2 | SD20.cpp |
sk398 | 2:b6789ad2af2b | 3 | -------- |
sk398 | 2:b6789ad2af2b | 4 | |
sk398 | 2:b6789ad2af2b | 5 | Surface Ship, Group 5 |
sk398 | 2:b6789ad2af2b | 6 | --------------------- |
sk398 | 2:b6789ad2af2b | 7 | |
sk398 | 2:b6789ad2af2b | 8 | Written by: Steven Kay |
sk398 | 2:b6789ad2af2b | 9 | |
sk398 | 2:b6789ad2af2b | 10 | Date: February 2016 |
sk398 | 2:b6789ad2af2b | 11 | |
sk398 | 2:b6789ad2af2b | 12 | Function: This |
sk398 | 2:b6789ad2af2b | 13 | |
sk398 | 2:b6789ad2af2b | 14 | Version: 1.0 |
sk398 | 2:b6789ad2af2b | 15 | |
sk398 | 2:b6789ad2af2b | 16 | Version History |
sk398 | 2:b6789ad2af2b | 17 | --------------- |
sk398 | 2:b6789ad2af2b | 18 | |
sk398 | 2:b6789ad2af2b | 19 | 1.1 rgdfgdfgdfggdfgdg |
sk398 | 2:b6789ad2af2b | 20 | |
sk398 | 2:b6789ad2af2b | 21 | 1.0 gdgddfdddgd |
sk398 | 2:b6789ad2af2b | 22 | |
sk398 | 2:b6789ad2af2b | 23 | ##################################################################### */ |
sk398 | 2:b6789ad2af2b | 24 | |
sk398 | 0:870a29ad1074 | 25 | #include "mbed.h" |
sk398 | 0:870a29ad1074 | 26 | #include "SD20.h" |
sk398 | 0:870a29ad1074 | 27 | |
sk398 | 1:6557cf755742 | 28 | SD20::SD20(I2C *bus) |
sk398 | 0:870a29ad1074 | 29 | { |
sk398 | 0:870a29ad1074 | 30 | _bus = bus; |
sk398 | 1:6557cf755742 | 31 | _bus -> frequency(BUS_FREQ); |
sk398 | 0:870a29ad1074 | 32 | } |
sk398 | 0:870a29ad1074 | 33 | |
sk398 | 1:6557cf755742 | 34 | int SD20::UpdateChannel(char *newDutyCycle) |
sk398 | 0:870a29ad1074 | 35 | { |
sk398 | 1:6557cf755742 | 36 | // Output new duty cycle to SD20, return 0 if successful, 1 if unsuccessful |
sk398 | 1:6557cf755742 | 37 | if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 38 | { |
sk398 | 0:870a29ad1074 | 39 | printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 40 | return 1; |
sk398 | 0:870a29ad1074 | 41 | } |
sk398 | 0:870a29ad1074 | 42 | else |
sk398 | 0:870a29ad1074 | 43 | { |
sk398 | 1:6557cf755742 | 44 | printf("0x%02x sent to Reg: 0x%02x\r\n",*(newDutyCycle+1),*newDutyCycle); |
sk398 | 0:870a29ad1074 | 45 | return 0; |
sk398 | 0:870a29ad1074 | 46 | } |
sk398 | 0:870a29ad1074 | 47 | } |
sk398 | 0:870a29ad1074 | 48 | |
sk398 | 1:6557cf755742 | 49 | int SD20::StartStopChannel(uint8_t ServoChannel,bool Start) |
sk398 | 0:870a29ad1074 | 50 | { |
sk398 | 1:6557cf755742 | 51 | // Startup a new channel, return 0 if successful, 1 if unsuccessful |
sk398 | 0:870a29ad1074 | 52 | if(Start == CHANNEL_START) |
sk398 | 0:870a29ad1074 | 53 | { |
sk398 | 1:6557cf755742 | 54 | char newDutyCycle[] = {ServoChannel,CHANNEL_START}; |
sk398 | 1:6557cf755742 | 55 | if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 56 | { |
sk398 | 0:870a29ad1074 | 57 | printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 58 | return 1; |
sk398 | 0:870a29ad1074 | 59 | } |
sk398 | 0:870a29ad1074 | 60 | else |
sk398 | 0:870a29ad1074 | 61 | { |
sk398 | 1:6557cf755742 | 62 | printf("0x%02x sent to Reg: 0x%02x\r\n",*(newDutyCycle+1),*newDutyCycle); |
sk398 | 0:870a29ad1074 | 63 | return 0; |
sk398 | 0:870a29ad1074 | 64 | } |
sk398 | 0:870a29ad1074 | 65 | } |
sk398 | 1:6557cf755742 | 66 | |
sk398 | 1:6557cf755742 | 67 | // Stop channel, return 0 if successful, 1 if unsuccessful |
sk398 | 0:870a29ad1074 | 68 | else |
sk398 | 0:870a29ad1074 | 69 | { |
sk398 | 1:6557cf755742 | 70 | char newDutyCycle[] = {ServoChannel,CHANNEL_STOP}; |
sk398 | 1:6557cf755742 | 71 | if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 72 | { |
sk398 | 0:870a29ad1074 | 73 | printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 74 | return 1; |
sk398 | 0:870a29ad1074 | 75 | } |
sk398 | 0:870a29ad1074 | 76 | else |
sk398 | 0:870a29ad1074 | 77 | { |
sk398 | 1:6557cf755742 | 78 | printf("0x%02x sent to Reg: 0x%02x\r\n",*(newDutyCycle+1),*newDutyCycle); |
sk398 | 0:870a29ad1074 | 79 | return 0; |
sk398 | 0:870a29ad1074 | 80 | } |
sk398 | 0:870a29ad1074 | 81 | } |
sk398 | 0:870a29ad1074 | 82 | } |
sk398 | 0:870a29ad1074 | 83 | |
sk398 | 1:6557cf755742 | 84 | int SD20::SetStandardMode() |
sk398 | 1:6557cf755742 | 85 | { |
sk398 | 1:6557cf755742 | 86 | char outputBuf[] = {STD_ETD_MODE_CTRL,STD_MODE}; |
sk398 | 1:6557cf755742 | 87 | if(_bus -> write(SD20_ADDRESS,outputBuf,OUT_BUF_LEN)) |
sk398 | 1:6557cf755742 | 88 | { |
sk398 | 1:6557cf755742 | 89 | printf("I2C Failed to write\r\n"); |
sk398 | 1:6557cf755742 | 90 | return 1; |
sk398 | 1:6557cf755742 | 91 | } |
sk398 | 1:6557cf755742 | 92 | else |
sk398 | 1:6557cf755742 | 93 | { |
sk398 | 1:6557cf755742 | 94 | printf("0x%02x sent to Reg: 0x%02x\r\n",*(outputBuf+1),*outputBuf); |
sk398 | 1:6557cf755742 | 95 | return 0; |
sk398 | 1:6557cf755742 | 96 | } |
sk398 | 1:6557cf755742 | 97 | } |
sk398 | 1:6557cf755742 | 98 |