1457|0

305

帖子

0

TA的资源

一粒金砂(高级)

楼主
 

【抢购NXP LPC 1788学习开发板】自己再次分享全部新I2C例程 [复制链接]

自己在nxp系列的小成就,使用库函数写代码
官方店  https://openmcu.taobao.com

* @file  pca9532.c
* @brief Contains all functions support for PCA9532 IC  to drive 16 LEDs
* @version 1.0
* @date  02. June. 2011
* @author NXP MCU SW Application Team
*
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*

#include "pca9532.h"
#include "lpc177x_8x_i2c.h"
#include "lpc177x_8x_pinsel.h"
#define BUFSIZE   (20)
//PCA9532 link to I2C0 only
#define PCA9532_I2C  I2C_0

/*********************************************************************//**
* @brief   Initialize Pca9532
* @param[in] i2cClockFreq I2C clock frequency that Pca9532 operate
* @return   None
**********************************************************************/
void Pca9532_Init(uint32_t i2cClockFreq)
{
// Config Pin for I2C_SDA and I2C_SCL of I2C0
// It's because the PCA9532 IC is linked to LPC177x_8x by I2C0 clearly
PINSEL_ConfigPin (0, 27, 1);
PINSEL_ConfigPin (0, 28, 1);
I2C_Init(PCA9532_I2C, i2cClockFreq);
/* Enable I2C1 operation */
I2C_Cmd(PCA9532_I2C, ENABLE);
return;
}
/*********************************************************************//**
* @brief   Close Pca9532
* @param[in] None
* @return   None
**********************************************************************/
void Pca9532_DeInit(void)
{
/* Enable I2C1 operation */
I2C_Cmd(PCA9532_I2C, DISABLE);
return;
}
/*********************************************************************//**
* @brief   Control led output
* @param[in] settings Pointer that point to PCA configuration struct
* @return   Status, could be:
*     - PCA9532_RETFUNC_OK: success
*     - PCA9532_RETFUNC_FAILED_OP: fail
**********************************************************************/
int Pca9532_LedOutputControl(pca9532_Configure_st_t* settings)
{
uint32_t psc0 = 0, psc1 = 0, pwm0 = 0, pwm1 = 0, ls0 = 0, ls1 = 0, ls2 = 0, ls3 = 0;
int cnt = 0;
I2C_M_SETUP_Type i2cData;
uint8_t i2cBuf[BUFSIZE];
// Check if the LED level is in range. Unless set it to default
for (cnt = 0; cnt < NUMBER_OF_ONCHIP_LEDS; cnt++)
{
  settings->led_settings[cnt] = ((settings->led_settings[cnt] >= PCA9532_LED_MAX_LEVEL)
           ||(settings->led_settings[cnt] < 0)) ?
           PCA9532_LED_LEVEL_DEFAULT : settings->led_settings[cnt];
}
if (settings->led_blinking_freq_0 == 0)
{
  psc0 = 0;
}
else
{
  switch(settings->led_freq0_unit)
  {
   default:
   case PCA9532_CALCULATING_TIME_IN_SECOND:
    psc0 = (settings->led_blinking_freq_0 * PCA9532_PRESCALER_FACTOR) - 1;
    break;
   case PCA9532_CALCULATING_TIME_IN_HERTZ:
    psc0 = (uint32_t) ((PCA9532_PRESCALER_FACTOR / settings->led_blinking_freq_0) - 1);
    break;
  }
}
pwm0 = settings->duty_cycle_0 * PCA9532_PWM_FACTOR / PERCENT_FACTOR;
if (settings->led_blinking_freq_0 == 0)
{
  psc1 = 0;
}
else
{
  switch(settings->led_freq1_unit)
  {
   default:
   case PCA9532_CALCULATING_TIME_IN_SECOND:
    psc1 = (settings->led_blinking_freq_1 * PCA9532_PRESCALER_FACTOR) - 1;
    break;
   case PCA9532_CALCULATING_TIME_IN_HERTZ:
    psc1 = (uint32_t) ((PCA9532_PRESCALER_FACTOR / settings->led_blinking_freq_1) - 1);
    break;
  }
}
pwm1 = settings->duty_cycle_1 * PCA9532_PWM_FACTOR / PERCENT_FACTOR;
ls0 = 0;
ls1 = 0;
ls2 = 0;
ls3 = 0;
for(cnt = 0; cnt < 4; cnt++)
{
  ls0 |= (settings->led_settings[cnt] & PCA9532_LED_LEVEL_MASK) << (cnt * PCA9532_LED_LEVEL_NUM_BITS);
  ls1 |= (settings->led_settings[cnt + 4] & PCA9532_LED_LEVEL_MASK) << (cnt * PCA9532_LED_LEVEL_NUM_BITS);
  ls2 |= (settings->led_settings[cnt + 8] & PCA9532_LED_LEVEL_MASK) << (cnt * PCA9532_LED_LEVEL_NUM_BITS);
  ls3 |= (settings->led_settings[cnt + 12] & PCA9532_LED_LEVEL_MASK) << (cnt * PCA9532_LED_LEVEL_NUM_BITS);
}
i2cBuf[0] = 0x12;      /* Control Register - start with Frequency Prescale 0 */
i2cBuf[1] = psc0 & BYTE_BITMASK;  /* Value of Frequency Prescale 0 */
i2cBuf[2] = pwm0 & BYTE_BITMASK;  /* Value of PWM Register 0 */
i2cBuf[3] = psc1 & BYTE_BITMASK;  /* Value of Frequency Prescale 1 */
i2cBuf[4] = pwm1 & BYTE_BITMASK;  /* Value of PWM Register 1 */
i2cBuf[5] = ls0 & BYTE_BITMASK;   /* Value of LED Selector 0 */
i2cBuf[6] = ls1 & BYTE_BITMASK;   /* Value of LED Selector 1 */
i2cBuf[7] = ls2 & BYTE_BITMASK;   /* Value of LED Selector 2 */
i2cBuf[8] = ls3 & BYTE_BITMASK;   /* Value of LED Selector 3 */

i2cData.sl_addr7bit = PCA9532_I2CADDR >> 1;
i2cData.tx_data = i2cBuf;
i2cData.tx_length = 9;
i2cData.rx_data = NULL;
i2cData.rx_length = 0;
i2cData.retransmissions_max = 3;
if (I2C_MasterTransferData(PCA9532_I2C, &i2cData, I2C_TRANSFER_POLLING) == SUCCESS)
{
  return PCA9532_RETFUNC_OK;
}
else
{
  return PCA9532_RETFUNC_FAILED_OP;
}
}
/*********************************************************************************
**                            End Of File
*********************************************************************************/

最新悍马.jpg (85.83 KB, 下载次数: 0)

最新悍马.jpg

(4)I2C例程.rar

6.98 MB, 下载次数: 10

此帖出自信息发布论坛
点赞 关注
个人签名http://openmcu.taobao.com]77[/url]
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表