资源简介
PCA9685的驱动,STM32F103开发平台,测试通过,硬件实现;
代码片段和文件信息
/***
Copyright (c) 2016
All rights reserved.
File name :pca9685.c
file identification :
Subject :Say something about the file
Current Version :V1.0.0
Author :Hacker
Date :
Instead Version :
Author :Hacker
Date :
***/
#include “PCA9685.h“
#include “math.h“
#include “delay.h“
#include “Arduino.h“
#include “usart.h“
#define HARDWARE_IIC
#define TEMP37 1365
#define TEMP60 4095
#ifndef HAC_STM32F103C8T6
#define I2C_Channel I2C1
#else
#define I2C_Channel I2C2
#endif
/* STM32 I2C 快速模式 */
#define I2C_Speed 100000
/* 这个地址只要与STM32外挂的I2C器件地址不一样即可 */
#define I2C1_OWN_ADDRESS7 0x0A
/* PCA9685写地址 */
#define I2C_Write_ADDRESS 0x80
/* PCA9685读地址 */
#define I2C_Read_ADDRESS 0x81
#define TurnOn() GPIO_ResetBits(GPIOAGPIO_Pin_5)
#define TurnOff() GPIO_SetBits(GPIOA GPIO_Pin_5)
static void DELAY_US(u32 n)
{
delay_us(n);
}
/**
* [map description]
* @param x [description]
* @param in_min [description]
* @param in_max [description]
* @param out_min [description]
* @param out_max [description]
* @return [description]
*/
long map(long x long in_min long in_max long out_min long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/*******************************************************************************
*
* 功能 :I2C I/O配置
* 参数 :无
*
******************************************************************************
*/
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOBENABLE);
#ifndef HAC_STM32F103C8T6
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
#else
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
#endif
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; // 开漏输出
GPIO_Init(GPIOB &GPIO_InitStructure);
}
/*******************************************************************************
*
* 功能 :I2C 工作模式配置
* 参数 :无
*
******************************************************************************
*/
void I2C_Mode_Configu(void)
{
I2C_InitTypeDef I2C_InitStructure;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 =I2C1_OWN_ADDRESS7;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;
I2C_Init(I2C_Channel &I2C_InitStructure);
I2C_Cmd(I2C_Channel ENABLE);
}
/***************************************************************************
评论
共有 条评论