![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
basic pca9685
Fork of ic2_test_bus by
main.cpp@1:dbe78e80d722, 2018-10-29 (annotated)
- Committer:
- richnash
- Date:
- Mon Oct 29 14:56:07 2018 +0000
- Revision:
- 1:dbe78e80d722
- Parent:
- 0:2db41a0c2f17
basic pca9685
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martinsimpson | 0:2db41a0c2f17 | 1 | #include "mbed.h" |
richnash | 1:dbe78e80d722 | 2 | #include"PCA9685.h" |
martinsimpson | 0:2db41a0c2f17 | 3 | |
richnash | 1:dbe78e80d722 | 4 | #define I2C_ADDR_PCA9685 (0xEE) // Device You Wish to Use Address - using i2c Address |
richnash | 1:dbe78e80d722 | 5 | |
martinsimpson | 0:2db41a0c2f17 | 6 | #define WRITE (0x00) // i2c Write bit |
martinsimpson | 0:2db41a0c2f17 | 7 | #define READ (0x01) // i2c Read bit |
martinsimpson | 0:2db41a0c2f17 | 8 | |
richnash | 1:dbe78e80d722 | 9 | // ----- NOTES ----- |
richnash | 1:dbe78e80d722 | 10 | // 4.7k resistor pull up to 3.3v on SDA and SDL lines |
richnash | 1:dbe78e80d722 | 11 | // SCL is green on our boards |
richnash | 1:dbe78e80d722 | 12 | // |
martinsimpson | 0:2db41a0c2f17 | 13 | |
martinsimpson | 0:2db41a0c2f17 | 14 | Serial pc(SERIAL_TX, SERIAL_RX); //Serial Class Pin Assignments see Serial.h |
martinsimpson | 0:2db41a0c2f17 | 15 | |
richnash | 1:dbe78e80d722 | 16 | |
richnash | 1:dbe78e80d722 | 17 | PCA9685 pwm( I2C_SDA, I2C_SCL ); |
richnash | 1:dbe78e80d722 | 18 | |
richnash | 1:dbe78e80d722 | 19 | |
richnash | 1:dbe78e80d722 | 20 | bool I2CScan( I2C *i2c, int Device_Adress ) |
richnash | 1:dbe78e80d722 | 21 | { |
richnash | 1:dbe78e80d722 | 22 | char ucdata_write[2]; |
richnash | 1:dbe78e80d722 | 23 | |
richnash | 1:dbe78e80d722 | 24 | ucdata_write[0]=0; |
richnash | 1:dbe78e80d722 | 25 | ucdata_write[1]=0; |
richnash | 1:dbe78e80d722 | 26 | |
richnash | 1:dbe78e80d722 | 27 | // try writing to the device |
richnash | 1:dbe78e80d722 | 28 | if (!i2c->write((Device_Adress|WRITE), ucdata_write, 1, 0))// Check for ACK from i2c Device NB I am 'ORing' the Write Bit |
richnash | 1:dbe78e80d722 | 29 | { |
richnash | 1:dbe78e80d722 | 30 | // all good |
richnash | 1:dbe78e80d722 | 31 | return true; |
richnash | 1:dbe78e80d722 | 32 | } |
richnash | 1:dbe78e80d722 | 33 | // no device found |
richnash | 1:dbe78e80d722 | 34 | return false; |
richnash | 1:dbe78e80d722 | 35 | } |
richnash | 1:dbe78e80d722 | 36 | |
martinsimpson | 0:2db41a0c2f17 | 37 | |
richnash | 1:dbe78e80d722 | 38 | int I2CScan( I2C *i2c ) |
richnash | 1:dbe78e80d722 | 39 | { |
richnash | 1:dbe78e80d722 | 40 | short count=0; |
richnash | 1:dbe78e80d722 | 41 | |
richnash | 1:dbe78e80d722 | 42 | // scan all channels |
richnash | 1:dbe78e80d722 | 43 | pc.printf("====================================================\n\r"); |
richnash | 1:dbe78e80d722 | 44 | pc.printf("I2CScan (ALL Channels)\n\r"); |
richnash | 1:dbe78e80d722 | 45 | for (int Device_Adress=0; Device_Adress<=0xFE; Device_Adress+=2)//Stepping in 2 Because Read/Write use LSB |
richnash | 1:dbe78e80d722 | 46 | { |
richnash | 1:dbe78e80d722 | 47 | if( I2CScan(i2c, Device_Adress) ) |
richnash | 1:dbe78e80d722 | 48 | { |
richnash | 1:dbe78e80d722 | 49 | pc.printf("I2CScan: %#4x\n\r", Device_Adress ); |
richnash | 1:dbe78e80d722 | 50 | count++; |
richnash | 1:dbe78e80d722 | 51 | } |
richnash | 1:dbe78e80d722 | 52 | } |
richnash | 1:dbe78e80d722 | 53 | pc.printf("%d Devices detected!\n\r",count); |
richnash | 1:dbe78e80d722 | 54 | pc.printf("====================================================\n\r\n\r"); |
richnash | 1:dbe78e80d722 | 55 | |
richnash | 1:dbe78e80d722 | 56 | return count; |
richnash | 1:dbe78e80d722 | 57 | } |
richnash | 1:dbe78e80d722 | 58 | |
richnash | 1:dbe78e80d722 | 59 | |
richnash | 1:dbe78e80d722 | 60 | |
richnash | 1:dbe78e80d722 | 61 | void PCA9685_initDriver() { |
richnash | 1:dbe78e80d722 | 62 | pwm.begin(); |
richnash | 1:dbe78e80d722 | 63 | pwm.setPWMFreq(1600); // max frequency |
richnash | 1:dbe78e80d722 | 64 | pwm.frequencyI2C(400000); //400kHz fast I2C comunication |
richnash | 1:dbe78e80d722 | 65 | } |
richnash | 1:dbe78e80d722 | 66 | |
martinsimpson | 0:2db41a0c2f17 | 67 | |
martinsimpson | 0:2db41a0c2f17 | 68 | int main() |
martinsimpson | 0:2db41a0c2f17 | 69 | { |
richnash | 1:dbe78e80d722 | 70 | I2C i2c(I2C_SDA, I2C_SCL); //I2C Class Pin Assignments see I2C.h |
martinsimpson | 0:2db41a0c2f17 | 71 | |
martinsimpson | 0:2db41a0c2f17 | 72 | unsigned int uifrequency=400000; //400KHz for i2c Max |
richnash | 1:dbe78e80d722 | 73 | unsigned int uibaudrate=115200; |
martinsimpson | 0:2db41a0c2f17 | 74 | |
richnash | 1:dbe78e80d722 | 75 | // debug |
richnash | 1:dbe78e80d722 | 76 | pc.baud(uibaudrate); |
richnash | 1:dbe78e80d722 | 77 | pc.printf("\n\r ---- I2C Scanner ---- \n\r"); |
martinsimpson | 0:2db41a0c2f17 | 78 | |
richnash | 1:dbe78e80d722 | 79 | // setup i2c |
richnash | 1:dbe78e80d722 | 80 | i2c.frequency(uifrequency); |
richnash | 1:dbe78e80d722 | 81 | pc.printf("I2C: %uKHz\n\r", uifrequency/1000); |
richnash | 1:dbe78e80d722 | 82 | |
richnash | 1:dbe78e80d722 | 83 | // scan |
richnash | 1:dbe78e80d722 | 84 | I2CScan(&i2c); |
richnash | 1:dbe78e80d722 | 85 | |
richnash | 1:dbe78e80d722 | 86 | // Init the PCA9685 on the default addr |
richnash | 1:dbe78e80d722 | 87 | PCA9685_initDriver(); |
richnash | 1:dbe78e80d722 | 88 | |
richnash | 1:dbe78e80d722 | 89 | // loop led's on channel 0 and 1 |
richnash | 1:dbe78e80d722 | 90 | while(1) |
martinsimpson | 0:2db41a0c2f17 | 91 | { |
richnash | 1:dbe78e80d722 | 92 | for (int i = 0; i < 4095; i=i+24) |
richnash | 1:dbe78e80d722 | 93 | { |
richnash | 1:dbe78e80d722 | 94 | pwm.setPWM_ALL(4095-i,0); |
richnash | 1:dbe78e80d722 | 95 | wait(0.0005); |
richnash | 1:dbe78e80d722 | 96 | } |
richnash | 1:dbe78e80d722 | 97 | for (int i = 4094; i > 0; i=i-24) |
richnash | 1:dbe78e80d722 | 98 | { |
richnash | 1:dbe78e80d722 | 99 | pwm.setPWM_ALL(4095-i,0); |
richnash | 1:dbe78e80d722 | 100 | wait(0.0005); |
richnash | 1:dbe78e80d722 | 101 | } |
richnash | 1:dbe78e80d722 | 102 | /* |
richnash | 1:dbe78e80d722 | 103 | pc.printf("1."); |
richnash | 1:dbe78e80d722 | 104 | for (int i = 0; i < 4095; i=i+8) |
richnash | 1:dbe78e80d722 | 105 | { |
richnash | 1:dbe78e80d722 | 106 | pwm.setPWM(1,4095-i,0); |
richnash | 1:dbe78e80d722 | 107 | wait(0.0005); |
richnash | 1:dbe78e80d722 | 108 | |
richnash | 1:dbe78e80d722 | 109 | pwm.setPWM(2,i,0); |
richnash | 1:dbe78e80d722 | 110 | wait(0.0005); |
richnash | 1:dbe78e80d722 | 111 | } |
richnash | 1:dbe78e80d722 | 112 | |
richnash | 1:dbe78e80d722 | 113 | pc.printf("2."); |
richnash | 1:dbe78e80d722 | 114 | for (int i = 4094; i > 0; i=i-8) |
richnash | 1:dbe78e80d722 | 115 | { |
richnash | 1:dbe78e80d722 | 116 | pwm.setPWM(1,4095-i,0); |
richnash | 1:dbe78e80d722 | 117 | wait(0.0005); |
richnash | 1:dbe78e80d722 | 118 | |
richnash | 1:dbe78e80d722 | 119 | pwm.setPWM(2,i,0); |
richnash | 1:dbe78e80d722 | 120 | wait(0.0005); |
richnash | 1:dbe78e80d722 | 121 | } |
richnash | 1:dbe78e80d722 | 122 | pc.printf("\n\r"); |
richnash | 1:dbe78e80d722 | 123 | */ |
martinsimpson | 0:2db41a0c2f17 | 124 | } |
martinsimpson | 0:2db41a0c2f17 | 125 | } |
richnash | 1:dbe78e80d722 | 126 |