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@6:973d6a66dbf7, 2016-03-04 (annotated)
- Committer:
- sk398
- Date:
- Fri Mar 04 23:04:28 2016 +0000
- Revision:
- 6:973d6a66dbf7
- Parent:
- 5:eba9f639154c
Modified updateChannel to accept 2 chars instead of char *; ; Still having huge issue with printf statement, only working with this.
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 | 6:973d6a66dbf7 | 34 | int SD20::UpdateChannel(char channelReg, char channelSpeed) |
sk398 | 0:870a29ad1074 | 35 | { |
sk398 | 1:6557cf755742 | 36 | // Output new duty cycle to SD20, return 0 if successful, 1 if unsuccessful |
sk398 | 6:973d6a66dbf7 | 37 | updateDutyCycle[0] = channelReg; |
sk398 | 6:973d6a66dbf7 | 38 | updateDutyCycle[1] = channelSpeed; |
sk398 | 6:973d6a66dbf7 | 39 | |
sk398 | 6:973d6a66dbf7 | 40 | if(_bus -> write(SD20_ADDRESS,updateDutyCycle,2)) |
sk398 | 0:870a29ad1074 | 41 | { |
sk398 | 6:973d6a66dbf7 | 42 | // printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 43 | return 1; |
sk398 | 0:870a29ad1074 | 44 | } |
sk398 | 0:870a29ad1074 | 45 | else |
sk398 | 0:870a29ad1074 | 46 | { |
sk398 | 6:973d6a66dbf7 | 47 | // updateDutyCycle = *(updateDutyCycle+1) |
sk398 | 6:973d6a66dbf7 | 48 | printf("Reg: 0x%02x Data: 0x%02x\r\n",*updateDutyCycle,*(updateDutyCycle+1)); |
sk398 | 0:870a29ad1074 | 49 | return 0; |
sk398 | 0:870a29ad1074 | 50 | } |
sk398 | 0:870a29ad1074 | 51 | } |
sk398 | 0:870a29ad1074 | 52 | |
sk398 | 1:6557cf755742 | 53 | int SD20::StartStopChannel(uint8_t ServoChannel,bool Start) |
sk398 | 0:870a29ad1074 | 54 | { |
sk398 | 1:6557cf755742 | 55 | // Startup a new channel, return 0 if successful, 1 if unsuccessful |
sk398 | 0:870a29ad1074 | 56 | if(Start == CHANNEL_START) |
sk398 | 0:870a29ad1074 | 57 | { |
sk398 | 6:973d6a66dbf7 | 58 | char startDutyCycle[] = {ServoChannel,CHANNEL_START}; |
sk398 | 6:973d6a66dbf7 | 59 | if(_bus -> write(SD20_ADDRESS,startDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 60 | { |
sk398 | 6:973d6a66dbf7 | 61 | // printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 62 | return 1; |
sk398 | 0:870a29ad1074 | 63 | } |
sk398 | 0:870a29ad1074 | 64 | else |
sk398 | 0:870a29ad1074 | 65 | { |
sk398 | 5:eba9f639154c | 66 | // printf("Reg: 0x%02x Data: 0x%02x\r\n",*newDutyCycle,*(newDutyCycle+1)); |
sk398 | 0:870a29ad1074 | 67 | return 0; |
sk398 | 0:870a29ad1074 | 68 | } |
sk398 | 0:870a29ad1074 | 69 | } |
sk398 | 1:6557cf755742 | 70 | |
sk398 | 1:6557cf755742 | 71 | // Stop channel, return 0 if successful, 1 if unsuccessful |
sk398 | 0:870a29ad1074 | 72 | else |
sk398 | 0:870a29ad1074 | 73 | { |
sk398 | 6:973d6a66dbf7 | 74 | char stopDutyCycle[] = {ServoChannel,CHANNEL_STOP}; |
sk398 | 6:973d6a66dbf7 | 75 | if(_bus -> write(SD20_ADDRESS,stopDutyCycle,OUT_BUF_LEN)) |
sk398 | 0:870a29ad1074 | 76 | { |
sk398 | 6:973d6a66dbf7 | 77 | // printf("I2C Failed to write\r\n"); |
sk398 | 0:870a29ad1074 | 78 | return 1; |
sk398 | 0:870a29ad1074 | 79 | } |
sk398 | 0:870a29ad1074 | 80 | else |
sk398 | 0:870a29ad1074 | 81 | { |
sk398 | 5:eba9f639154c | 82 | // printf("Reg: 0x%02x Data: 0x%02x\r\n",*newDutyCycle,*(newDutyCycle+1)); |
sk398 | 0:870a29ad1074 | 83 | return 0; |
sk398 | 0:870a29ad1074 | 84 | } |
sk398 | 0:870a29ad1074 | 85 | } |
sk398 | 0:870a29ad1074 | 86 | } |
sk398 | 0:870a29ad1074 | 87 | |
sk398 | 1:6557cf755742 | 88 | int SD20::SetStandardMode() |
sk398 | 1:6557cf755742 | 89 | { |
sk398 | 1:6557cf755742 | 90 | char outputBuf[] = {STD_ETD_MODE_CTRL,STD_MODE}; |
sk398 | 1:6557cf755742 | 91 | if(_bus -> write(SD20_ADDRESS,outputBuf,OUT_BUF_LEN)) |
sk398 | 1:6557cf755742 | 92 | { |
sk398 | 6:973d6a66dbf7 | 93 | // printf("I2C Failed to write\r\n"); |
sk398 | 1:6557cf755742 | 94 | return 1; |
sk398 | 1:6557cf755742 | 95 | } |
sk398 | 1:6557cf755742 | 96 | else |
sk398 | 1:6557cf755742 | 97 | { |
sk398 | 5:eba9f639154c | 98 | // printf("0x%02x sent to Reg: 0x%02x\r\n",*(outputBuf+1),*outputBuf); |
sk398 | 1:6557cf755742 | 99 | return 0; |
sk398 | 1:6557cf755742 | 100 | } |
sk398 | 1:6557cf755742 | 101 | } |
sk398 | 1:6557cf755742 | 102 |