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@4:5ccaaacec14f, 2016-02-25 (annotated)
- Committer:
- sk398
- Date:
- Thu Feb 25 20:04:50 2016 +0000
- Revision:
- 4:5ccaaacec14f
- Parent:
- 1:6557cf755742
sdsda
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:870a29ad1074 | 1 | #include "mbed.h" |
sk398 | 0:870a29ad1074 | 2 | #include "SD20.h" |
sk398 | 0:870a29ad1074 | 3 | |
sk398 | 1:6557cf755742 | 4 | SD20::SD20(I2C *bus) |
sk398 | 0:870a29ad1074 | 5 | { |
sk398 | 0:870a29ad1074 | 6 | _bus = bus; |
sk398 | 1:6557cf755742 | 7 | _bus -> frequency(BUS_FREQ); |
sk398 | 0:870a29ad1074 | 8 | } |
sk398 | 0:870a29ad1074 | 9 | |
sk398 | 1:6557cf755742 | 10 | int SD20::UpdateChannel(char *newDutyCycle) |
sk398 | 0:870a29ad1074 | 11 | { |
sk398 | 1:6557cf755742 | 12 | // Output new duty cycle to SD20, return 0 if successful, 1 if unsuccessful |
sk398 | 1:6557cf755742 | 13 | if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 14 | { |
sk398 | 0:870a29ad1074 | 15 | printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 16 | return 1; |
sk398 | 0:870a29ad1074 | 17 | } |
sk398 | 0:870a29ad1074 | 18 | else |
sk398 | 0:870a29ad1074 | 19 | { |
sk398 | 4:5ccaaacec14f | 20 | printf("Reg: 0x%02x Data: 0x%02x\r\n",*newDutyCycle,*(newDutyCycle+1)); |
sk398 | 0:870a29ad1074 | 21 | return 0; |
sk398 | 0:870a29ad1074 | 22 | } |
sk398 | 0:870a29ad1074 | 23 | } |
sk398 | 0:870a29ad1074 | 24 | |
sk398 | 1:6557cf755742 | 25 | int SD20::StartStopChannel(uint8_t ServoChannel,bool Start) |
sk398 | 0:870a29ad1074 | 26 | { |
sk398 | 1:6557cf755742 | 27 | // Startup a new channel, return 0 if successful, 1 if unsuccessful |
sk398 | 0:870a29ad1074 | 28 | if(Start == CHANNEL_START) |
sk398 | 0:870a29ad1074 | 29 | { |
sk398 | 1:6557cf755742 | 30 | char newDutyCycle[] = {ServoChannel,CHANNEL_START}; |
sk398 | 1:6557cf755742 | 31 | if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 32 | { |
sk398 | 0:870a29ad1074 | 33 | printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 34 | return 1; |
sk398 | 0:870a29ad1074 | 35 | } |
sk398 | 0:870a29ad1074 | 36 | else |
sk398 | 0:870a29ad1074 | 37 | { |
sk398 | 4:5ccaaacec14f | 38 | printf("Reg: 0x%02x Data: 0x%02x\r\n",*newDutyCycle,*(newDutyCycle+1)); |
sk398 | 0:870a29ad1074 | 39 | return 0; |
sk398 | 0:870a29ad1074 | 40 | } |
sk398 | 0:870a29ad1074 | 41 | } |
sk398 | 1:6557cf755742 | 42 | |
sk398 | 1:6557cf755742 | 43 | // Stop channel, return 0 if successful, 1 if unsuccessful |
sk398 | 0:870a29ad1074 | 44 | else |
sk398 | 0:870a29ad1074 | 45 | { |
sk398 | 1:6557cf755742 | 46 | char newDutyCycle[] = {ServoChannel,CHANNEL_STOP}; |
sk398 | 1:6557cf755742 | 47 | if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 48 | { |
sk398 | 0:870a29ad1074 | 49 | printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 50 | return 1; |
sk398 | 0:870a29ad1074 | 51 | } |
sk398 | 0:870a29ad1074 | 52 | else |
sk398 | 0:870a29ad1074 | 53 | { |
sk398 | 4:5ccaaacec14f | 54 | printf("Reg: 0x%02x Data: 0x%02x\r\n",*newDutyCycle,*(newDutyCycle+1)); |
sk398 | 0:870a29ad1074 | 55 | return 0; |
sk398 | 0:870a29ad1074 | 56 | } |
sk398 | 0:870a29ad1074 | 57 | } |
sk398 | 0:870a29ad1074 | 58 | } |
sk398 | 0:870a29ad1074 | 59 | |
sk398 | 1:6557cf755742 | 60 | int SD20::SetStandardMode() |
sk398 | 1:6557cf755742 | 61 | { |
sk398 | 1:6557cf755742 | 62 | char outputBuf[] = {STD_ETD_MODE_CTRL,STD_MODE}; |
sk398 | 1:6557cf755742 | 63 | if(_bus -> write(SD20_ADDRESS,outputBuf,OUT_BUF_LEN)) |
sk398 | 1:6557cf755742 | 64 | { |
sk398 | 1:6557cf755742 | 65 | printf("I2C Failed to write\r\n"); |
sk398 | 1:6557cf755742 | 66 | return 1; |
sk398 | 1:6557cf755742 | 67 | } |
sk398 | 1:6557cf755742 | 68 | else |
sk398 | 1:6557cf755742 | 69 | { |
sk398 | 1:6557cf755742 | 70 | printf("0x%02x sent to Reg: 0x%02x\r\n",*(outputBuf+1),*outputBuf); |
sk398 | 1:6557cf755742 | 71 | return 0; |
sk398 | 1:6557cf755742 | 72 | } |
sk398 | 1:6557cf755742 | 73 | } |
sk398 | 1:6557cf755742 | 74 |