Guest User

Untitled

a guest
Apr 9th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.54 KB | None | 0 0
  1. #define ADDRESS 0x15EUL
  2.  
  3. #define INTENSITY_RED 0x11U
  4. #define INTENSITY_RGB 0x14U
  5. #define CURRENT_RED 0x21U
  6. #define DMXOFF 0x30U
  7. #define DMXON 0x31U
  8. #define DMXSLOT 0x32U
  9. #define DMX8BIT 0x33U
  10. #define DMX16BIT 0x34U
  11. #define OFFTIME_RED 0x41U
  12. #define WALKTIME 0x50U
  13. #define DIMMINGLEVEL 0x60U
  14. #define FADERATE 0x61U
  15. #define _CHANGE_ADDRESS 0x70U
  16.  
  17. #define READ_INTENSITY_RED 0x81U
  18. #define READ_CURRENT_RED 0x84U
  19. #define READ_OFFTIME_RED 0x87U
  20. #define READ_WALKTIME 0x8AU
  21. #define READ_DIMMINGLEVEL 0x8BU
  22. #define READ_FADERATE 0x8CU
  23. #define DIRECTACCESS_READ 0x90U // read twice
  24. #define DIRECTACCESS_MOVE 0x91U
  25. #define DIRECTACCESS_AND 0x92U
  26. #define DIRECTACCESS_OR 0x93U
  27.  
  28. #define SAVEPARAMETERS 0xA0U
  29.  
  30. #define BCCUMODID 0x50030008U
  31. #define CHIPID 0x40010004U
  32. #define REDINTS 0x500300A0U // BCCU_CH5
  33. #define REDINT 0x500300A4U
  34. #define BLUEINTS 0x50030078U
  35. #define STARTWALK 0x50030018U
  36.  
  37. #include <Wire.h>
  38.  
  39. unsigned int c[2] = {0};
  40. unsigned int d[4] = {0};
  41. unsigned int on = 0;
  42. unsigned int message = 0;
  43. unsigned long redcurr = 0;
  44. unsigned long redoff = 0;
  45. unsigned long redint = 0x00;
  46. unsigned long fadetime = 0x00;
  47. unsigned long walk = 0x00;
  48. unsigned long brightness = 1;
  49.  
  50. void setup() {
  51. Serial.begin(9600);
  52. Wire.begin();
  53.  
  54. while (on != 1) // Wait for shield to respond
  55. {
  56. I2CDMX (ADDRESS, DMXOFF); // disable DMX
  57. I2CWRITE2BYTES (ADDRESS, FADERATE, 0x0000); // Immediate fade
  58. I2CWRITE2BYTES (ADDRESS, DIMMINGLEVEL, 0x0000); // 0% brightness level
  59. on = I2CREAD(ADDRESS, READ_DIMMINGLEVEL); // Request for brightness level
  60. if (message == 1 && on == 0) // If message received and brightness level = 9%
  61. {
  62. message = 0;
  63. Serial.println("ON");
  64. on = 1; // break out of loop
  65. }
  66. }
  67.  
  68. while (redcurr != 0x15 || redoff != 0x38 || brightness != 0)
  69. {
  70. I2CWRITE6BYTES (ADDRESS, INTENSITY_RGB, 0x0000, 0x000, 0x0000); // Off Light
  71. // Ensure that parameters are set up correctly. Read back and check. If wrong, write and read again.
  72. redcurr = I2CREAD (ADDRESS, READ_CURRENT_RED); // Read the red current intensity
  73. redoff = I2CREAD (ADDRESS, READ_OFFTIME_RED); // Read the off-time of the red channel
  74. brightness = I2CREAD (ADDRESS, READ_DIMMINGLEVEL); // Read the dimming level
  75.  
  76. I2CWRITE2BYTES (ADDRESS, OFFTIME_RED, 0x38); // Set off-time of red channel to 0x38
  77. I2CWRITE2BYTES (ADDRESS, CURRENT_RED, 0x15); // Set current intensity of red channel to 0x15
  78. I2CWRITE2BYTES (ADDRESS, DIMMINGLEVEL, 0x0000);
  79. }
  80.  
  81. delay(100);
  82. I2CWRITE2BYTES (ADDRESS, FADERATE, 0x0014); // Fade Rate --> 0.7s
  83. I2CWRITE2BYTES (ADDRESS, WALKTIME, 0x000F);
  84. I2CWRITE6BYTES (ADDRESS, INTENSITY_RGB, 0x0555, 0x0555, 0x0555); // White Light
  85. I2CWRITE2BYTES (ADDRESS, DIMMINGLEVEL, 0x0FFF); // Maximum Brightness
  86.  
  87. delay(2000); // wait 2 sec
  88. // change lamp colour to red
  89. I2CWRITE2BYTES (ADDRESS, INTENSITY_RED, 0x0FFF); // change red colour intensity to 0xFFF
  90. delay(1000);
  91. I2CWRITE2BYTES (ADDRESS, INTENSITY_RED, 0x0000);
  92. delay(1000);
  93.  
  94. delay(1000); // Read back values from slave
  95. Serial.print("Red Int: ");
  96. redint = I2CREAD (ADDRESS, READ_INTENSITY_RED); // request from shield red colour intensity
  97. Serial.print("Red Curr: ");
  98. redcurr = I2CREAD (ADDRESS, READ_CURRENT_RED); // request from shield peak current reference of red channel
  99. Serial.print("Red PWM: ");
  100. redoff = I2CREAD (ADDRESS, READ_OFFTIME_RED); // request from shield off-time of red channel
  101. Serial.print("Walk: ");
  102. walk = I2CREAD (ADDRESS, READ_WALKTIME); // request from shield walk-time
  103. Serial.print("Brightness: ");
  104. brightness = I2CREAD (ADDRESS, READ_DIMMINGLEVEL); // request from shield brightness level
  105. Serial.print("FadeTime: ");
  106. fadetime = I2CREAD (ADDRESS, READ_FADERATE); // request from shield fade rate
  107. I2CWRITE2BYTES (ADDRESS, WALKTIME, 48); // set walk-time 480ms
  108. }
  109.  
  110. // the loop routine runs over and over again forever:
  111. void loop() {
  112.  
  113. // change lamp colour to red
  114. I2CWRITE2BYTES (ADDRESS, INTENSITY_RED, 0x0FFF); // RED
  115. delay(500); // wait 500ms
  116. I2CWRITE2BYTES (ADDRESS, INTENSITY_RED, 0x0000); // OFF
  117. delay(500); // wait 500ms
  118. }
  119. /*
  120. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  121. int Command - Defined I2C Commands i.e. INTENSITY_RED, INTENSITY_GREEN, INTENSITY_BLUE
  122. unsigned int Data - 16bit data to be written to slave
  123. Parameters (OUT): None
  124. Return Value: None
  125. Description: This function will write 2 bytes of word to the I2C bus line
  126. */
  127.  
  128. void I2CWRITE2BYTES (int Address, int Command, unsigned int Data)
  129. {
  130. unsigned int upperByte, lowerByte; // Separate 4 byte data into 2 byte values
  131. lowerByte = Data;
  132. upperByte = Data >> 8;
  133.  
  134. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF); // Putting address into correct format
  135. unsigned int upperSLAD = Address >> 8;
  136. upperSLAD |= 0x79; // First 5 bits 11110 and last bit '1' for a write
  137.  
  138. Wire.beginTransmission(byte(upperSLAD)); // Start I2C transmission
  139. Wire.write(byte(lowerSLAD)); // address lower 8 bits of i2c address
  140. Wire.write(byte(Command)); // write command
  141. Wire.write(byte(upperByte)); // write data
  142. Wire.write(byte(lowerByte));
  143. Wire.endTransmission(true);
  144.  
  145. }
  146. /*
  147. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  148. int Command - Defined I2C Commands i.e. INTENSITY_RGB, CURRENT_RGB
  149. unsigned int DataOne, unsigned int DataTwo, unsigned int DataThree - Three 16bit data to be written to slave
  150. Parameters (OUT): None
  151. Return Value: None
  152. Description: This function will write 6 bytes of word to the I2C bus line
  153. */
  154.  
  155. void I2CWRITE6BYTES (unsigned int Address, unsigned int Command, unsigned int DataOne, unsigned int DataTwo, unsigned int DataThree) // DataOne: Red, DataTwo: Green, DataThree: Blue
  156. {
  157. unsigned int upperByte, lowerByte; // Split each Data parameter into upper and lower 8 bytes because I2C format sends 8 bytes of data each time
  158. lowerByte = DataOne;
  159. upperByte = DataOne >> 8;
  160.  
  161. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF);
  162. unsigned int upperSLAD = Address >> 8;
  163. upperSLAD |= 0x79; // First 5 bits 11110 and last bit '1' for a write
  164.  
  165. Wire.beginTransmission(byte(upperSLAD)); // Red
  166. Wire.write(byte(lowerSLAD));
  167. Wire.write(byte(Command));
  168. Wire.write(byte(upperByte));
  169. Wire.write(byte(lowerByte));
  170. lowerByte = DataTwo;
  171. upperByte = DataTwo >> 8;
  172. Wire.write(byte(upperByte));
  173. Wire.write(byte(lowerByte));
  174. lowerByte = DataThree;
  175. upperByte = DataThree >> 8;
  176. Wire.write(byte(upperByte));
  177. Wire.write(byte(lowerByte));
  178. Wire.endTransmission(true);
  179.  
  180. }
  181.  
  182. /*
  183. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  184. int Command - Defined I2C Commands i.e. DMX16Bit
  185. unsigned int DataOne, unsigned int DataTwo, unsigned int DataThree, usigned int DataFour, unsigned int DataFive - Three 16bit data to be written to slave
  186. Parameters (OUT): None
  187. Return Value: None
  188. Description: This function will write 12 bytes of word to the I2C bus line
  189. */
  190.  
  191. void I2CWRITE12BYTES (unsigned int Address, unsigned int Command, unsigned int DataOne, unsigned int DataTwo, unsigned int DataThree, unsigned int DataFour, unsigned int DataFive, unsigned int DataSix) // DataOne: Red, DataTwo: Green, DataThree: Blue
  192. {
  193. unsigned int upperByte, lowerByte;
  194. lowerByte = DataOne;
  195. upperByte = DataOne >> 8;
  196.  
  197. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF);
  198. unsigned int upperSLAD = Address >> 8;
  199. upperSLAD |= 0x79; // First 5 bits 11110 and last bit '1' for a write
  200.  
  201. Wire.beginTransmission(byte(upperSLAD));
  202. Wire.write(byte(lowerSLAD));
  203. Wire.write(byte(Command)); // write command
  204. Wire.write(byte(upperByte)); // write 2 bytes
  205. Wire.write(byte(lowerByte));
  206.  
  207. lowerByte = DataTwo;
  208. upperByte = DataTwo >> 8;
  209. Wire.write(byte(upperByte)); // write next two bytes
  210. Wire.write(byte(lowerByte));
  211.  
  212. lowerByte = DataThree;
  213. upperByte = DataThree >> 8;
  214. Wire.write(byte(upperByte));
  215. Wire.write(byte(lowerByte));
  216.  
  217. lowerByte = DataFour;
  218. upperByte = DataFour >> 8;
  219. Wire.write(byte(upperByte));
  220. Wire.write(byte(lowerByte));
  221.  
  222. lowerByte = DataFive;
  223. upperByte = DataFive >> 8;
  224. Wire.write(byte(upperByte));
  225. Wire.write(byte(lowerByte));
  226.  
  227. lowerByte = DataSix;
  228. upperByte = DataSix >> 8;
  229. Wire.write(byte(upperByte));
  230. Wire.write(byte(lowerByte));
  231. Wire.endTransmission(true);
  232.  
  233. }
  234.  
  235. /*
  236. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  237. int Command - Defined read I2C Commands i.e. READ_INTENSITY_RED, READ_INTENSITY_GREEN, READ_INTENSITY_BLUE
  238. Parameters (OUT): None
  239. Return Value: Requested data from Shield will be sent back
  240. Description: This function will request 2 bytes of word from the shield
  241. */
  242.  
  243. unsigned int I2CREAD (unsigned int Address, unsigned int Command) // Returns data sent by slave
  244. {
  245. int i = 0;
  246. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF);
  247. unsigned int upperSLAD = Address >> 8;
  248. upperSLAD |= 0x79;
  249.  
  250. Wire.beginTransmission(byte(upperSLAD)); // Red
  251. Wire.write(byte(lowerSLAD));
  252. Wire.write(byte(Command));
  253. Wire.endTransmission(false); // false for Repeated Start
  254.  
  255. Wire.beginTransmission(byte(upperSLAD));
  256. Wire.write(byte(lowerSLAD));
  257. Wire.requestFrom(upperSLAD, 2, true);
  258. unsigned int data = 0;
  259.  
  260. while(Wire.available()) // slave may send less than requested. Print out received data byte
  261. {
  262. message = 1;
  263. c[i] = Wire.read(); // receive a byte as character
  264. i++;
  265.  
  266. }
  267. Wire.endTransmission(true);
  268.  
  269. data = c[1]; // write data to serial monitor. c[1] is higher byte
  270. data = (data << 8) | c[0]; // shift left and combine with lower byte
  271. Serial.print("0x");
  272. if (data < 0x1000)
  273. Serial.print("0");
  274. Serial.println(data, HEX);
  275. return data;
  276. }
  277.  
  278. /*
  279. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  280. int Command - DIRECTACCESS_READ
  281. Parameters (OUT): None
  282. Return Value: Requested data from the Shield will be returned
  283. Description: This function will request 4 bytes of data from shield.
  284. */
  285.  
  286. unsigned long I2CREAD_DIRECTACCESS (unsigned int Address, unsigned int Command, unsigned long registerAddress)
  287. {
  288. int i = 0;
  289. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF); // sending command + address
  290. unsigned int upperSLAD = Address >> 8;
  291. upperSLAD |= 0x79; // First 5 bits 11110 and last bit '1' for a write
  292.  
  293. Wire.beginTransmission(byte(upperSLAD));
  294. Wire.write(byte(lowerSLAD));
  295. Wire.write(byte(Command));
  296.  
  297. unsigned int firstByte, secondByte, thirdByte, fourthByte;
  298. firstByte = registerAddress >> 24; // top byte
  299. secondByte = registerAddress >> 16;
  300. thirdByte = registerAddress >> 8;
  301. fourthByte = registerAddress; // bottom byte
  302.  
  303. Wire.write(byte(firstByte));
  304. Wire.write(byte(secondByte));
  305. Wire.write(byte(thirdByte));
  306. Wire.write(byte(fourthByte));
  307.  
  308. Wire.endTransmission(false); // false for Repeated Start
  309.  
  310. Wire.beginTransmission(byte(upperSLAD)); // request for read
  311. Wire.write(byte(lowerSLAD));
  312. Wire.requestFrom(upperSLAD, 4, true);
  313. unsigned long data = 0;
  314.  
  315. while(Wire.available()) // slave may send less than requested. Print out received data byte
  316. {
  317. d[i] = 0;
  318. d[i] = Wire.read(); // receive a byte as character
  319. i++;
  320. }
  321.  
  322. Wire.endTransmission(true);
  323.  
  324. data = d[3]; // combining into one variable. Highest byte received first
  325. data = (data << 8) | d[2];
  326. data = (data << 8) | d[1];
  327. data = (data << 8) | d[0];
  328. Serial.print("0x");
  329. if (data < 0x10000000)
  330. Serial.print("0");
  331. Serial.println(data, HEX);
  332. return data;
  333. }
  334. /*
  335. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  336. int Command - Defined I2C Commands i.e. DIRECTACCESS_OR, DIRECTACCESS_AND, DIRECTACCESS_MOVE
  337. unsigned long registerAddress - address of target register
  338. unsigned long Data - 32 bits data to be written to register
  339. Parameters (OUT): None
  340. Return Value: None
  341. Description: This function will write 4 bytes of data to specified register
  342. */
  343. void I2CWRITE_DIRECTACCESS (unsigned int Address, unsigned int Command, unsigned long registerAddress, unsigned long Data) // For accessing registers directly
  344. {
  345. int i = 0;
  346. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF); // sending command + address
  347. unsigned int upperSLAD = Address >> 8;
  348. upperSLAD |= 0x79; // First 5 bits 11110 and last bit '1' for a write
  349.  
  350. Wire.beginTransmission(byte(upperSLAD));
  351. Wire.write(byte(lowerSLAD));
  352. Wire.write(byte(Command));
  353.  
  354. unsigned int firstByte, secondByte, thirdByte, fourthByte; // Send address of register first
  355. firstByte = registerAddress >> 24; // top byte
  356. secondByte = registerAddress >> 16;
  357. thirdByte = registerAddress >> 8;
  358. fourthByte = registerAddress; // bottom byte
  359.  
  360. Wire.write(byte(firstByte));
  361. Wire.write(byte(secondByte));
  362. Wire.write(byte(thirdByte));
  363. Wire.write(byte(fourthByte));
  364.  
  365. firstByte = Data >> 24; // top byte
  366. secondByte = Data >> 16;
  367. thirdByte = Data >> 8;
  368. fourthByte = Data; // bottom byte
  369.  
  370. Wire.write(byte(firstByte)); // send 4 bytes of data
  371. Wire.write(byte(secondByte));
  372. Wire.write(byte(thirdByte));
  373. Wire.write(byte(fourthByte));
  374. Wire.endTransmission(true);
  375.  
  376. }
  377. /*
  378. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  379. unsigned int newAddress - Address the shield should change to
  380. Parameters (OUT): None
  381. Return Value: None
  382. Description: This function will change the I2C address of the slave
  383. */
  384.  
  385. void CHANGEADDRESS (unsigned int Address, unsigned int newAddress)
  386. {
  387. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF);
  388. unsigned int upperSLAD = Address >> 8;
  389. upperSLAD |= 0x79; // First 5 bits 11110 and last bit '1' for a write
  390.  
  391. Wire.beginTransmission(byte(upperSLAD)); // Red
  392. Wire.write(byte(lowerSLAD));
  393. Wire.write(byte(0x70)); // Command to change address
  394. lowerSLAD = (unsigned int) (newAddress & 0x00FF);
  395. upperSLAD = newAddress >> 7; // Split address into 2 bytes
  396. upperSLAD |= 0xF0; // 10 bit addressing: First 5 bits have to be 11110.
  397. upperSLAD &= 0xFE;
  398. Wire.write(byte(upperSLAD));
  399. Wire.write(byte(lowerSLAD));
  400. Wire.endTransmission(true);
  401. }
  402.  
  403. /*
  404. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  405. unsigned int Command - DMXON, DMXOFF
  406. Parameters (OUT): None
  407. Return Value: None
  408. Description: This function will enable or disable DMX512 control on shield
  409. */
  410.  
  411. void I2CDMX (unsigned int Address, unsigned int Command) // Switch off / on the DMX
  412. {
  413. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF); // Putting address into correct format
  414. unsigned int upperSLAD = Address >> 8;
  415. upperSLAD |= 0x79;
  416.  
  417. Wire.beginTransmission(byte(upperSLAD)); // Start I2C transmission
  418. Wire.write(byte(lowerSLAD));
  419. Wire.write(byte(Command));
  420. Wire.endTransmission(true);
  421.  
  422. }
  423. /*
  424. Parameters (IN): int Address - Address of RGB LED Shield, Default 0x15E
  425. Parameters (OUT): None
  426. Return Value: None
  427. Description: This function will request the shield to save configurations to flash memory
  428. */
  429.  
  430. void I2CSAVEPARAM (unsigned int Address)
  431. {
  432. int i = 0;
  433. unsigned int lowerSLAD = (unsigned int) (Address & 0x00FF);
  434. unsigned int upperSLAD = Address >> 8;
  435. upperSLAD |= 0x79;
  436.  
  437. Wire.beginTransmission(byte(upperSLAD));
  438. Wire.write(byte(lowerSLAD));
  439. Wire.write(byte(SAVEPARAMETERS)); // write SAVEPARAMETERS command
  440. Wire.endTransmission(false); // false for Repeated Start
  441.  
  442. Wire.beginTransmission(byte(upperSLAD));
  443. Wire.write(byte(lowerSLAD)); // write to address lower 8 bits of slave address
  444. Wire.requestFrom(upperSLAD, 2, true); // send READ request with upper slave address
  445. unsigned int data = 0;
  446.  
  447. while(Wire.available()) // slave may send less than requested. Print out received data byte
  448. {
  449. message = 1;
  450. c[i] = Wire.read(); // receive a byte as character3
  451. i++;
  452. }
  453. Wire.endTransmission(true); // STOP condition
  454.  
  455. data = c[1]; // print the data on serial monitor
  456. data = (data << 8) | c[0];
  457. Serial.print("0x");
  458. if (data < 0x1000)
  459. Serial.print("0");
  460. Serial.println(data, HEX);
  461.  
  462. }
Advertisement
Add Comment
Please, Sign In to add comment