Subversion Repositories HomeAutomation

Rev

Rev 1703 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. /*-----------------------------------------------
  3.  * Inclusions
  4.  * ---------------------------------------------*/
  5. #include <avr/io.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #include <config.h>
  10. #include "PCA9634.h"
  11. #include <drivers/mcu/TWI_Master.h>
  12.  
  13. /*-----------------------------------------------
  14.  * Function Implementations
  15.  * ---------------------------------------------*/
  16.  
  17. uint8_t pca9634_address;
  18. uint8_t pca9634_LEDOUT0=0;
  19. uint8_t pca9634_LEDOUT1=0;
  20.  
  21. /*---------------------------------------------------------------------------*/
  22.  
  23. void pca9634_init(uint8_t address)
  24. {
  25.     unsigned char msgBuf[5];
  26.     TWI_Master_Initialise();
  27.     pca9634_address = address;
  28.     /* init, MODE1.SLEEP shall be set to normal mode */
  29.     uint8_t data = PCA9634_MODE1_SLEEP|PCA9634_MODE1_SUB1|PCA9634_MODE1_SUB2|PCA9634_MODE1_SUB3|PCA9634_MODE1_ALL;
  30.  
  31.     msgBuf[0] = (pca9634_address<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  32.     msgBuf[1] = PCA9634_REG_MODE1;
  33.     msgBuf[2] = data;
  34.     TWI_Start_Read_Write( msgBuf, 3 );
  35.     while ( TWI_Transceiver_Busy() );
  36. }
  37.  
  38. /*---------------------------------------------------------------------------*/
  39.  
  40. void pca9634_setDuty(uint8_t id, uint8_t step)
  41. {
  42.     /* set register PWM[id] to step */
  43.     if (id < 8)
  44.     {
  45.         unsigned char msgBuf[5];
  46.         msgBuf[0] = (pca9634_address<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  47.         msgBuf[1] = PCA9634_REG_PWM0+id;
  48.         msgBuf[2] = step;
  49.         TWI_Start_Read_Write( msgBuf, 3 );
  50.         while ( TWI_Transceiver_Busy() );
  51.     }
  52. }
  53.  
  54. /*---------------------------------------------------------------------------*/
  55.  
  56. void pca9634_setOpMode(uint8_t id, pca9634_OpMode_t mode)
  57. {
  58.     if (id < 8)
  59.     {
  60.         unsigned char msgBuf[5];
  61.         /*  */
  62.         uint8_t reg;
  63.         uint8_t shift = (id&0b11)<<1;
  64.         uint8_t data;
  65.         if (id < 4)
  66.         {
  67.             reg = PCA9634_REG_LEDOUT0;
  68.             pca9634_LEDOUT0 = pca9634_LEDOUT0|(mode<<shift);
  69.             data = pca9634_LEDOUT0;
  70.         }
  71.         else if (id < 8)
  72.         {
  73.             reg = PCA9634_REG_LEDOUT1;
  74.             pca9634_LEDOUT1 = pca9634_LEDOUT1|(mode<<shift);
  75.             data = pca9634_LEDOUT1;
  76.         }
  77.    
  78.         msgBuf[0] = (pca9634_address<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  79.         msgBuf[1] = reg;
  80.         msgBuf[2] = data;
  81.         TWI_Start_Read_Write( msgBuf, 3 );
  82.         while ( TWI_Transceiver_Busy() );
  83.     }
  84. }
  85.  
  86.