⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm32f10x_i2c.lst

📁 STM32利用正交编码器实现电机的控制
💻 LST
📖 第 1 页 / 共 4 页
字号:
    209              else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
    210              {
    211                /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
    212                result = (u16)(pclk1clock / (I2C_InitStruct->I2C_ClockSpeed * 25));
    213                /* Set DUTY bit */
    214                result |= I2C_DutyCycle_16_9;
    215              }
    216              /* Test if CCR value is under 0x1*/
    217              if ((result & CCR_CCR_Set) == 0)
    218              {
    219                /* Set minimum allowed value */
    220                result |= (u16)0x0001;  
    221              }
    222              /* Set speed value and set F/S bit for fast mode */
    223              tmpreg |= result | CCR_FS_Set;
    224              /* Set Maximum Rise Time: ((300/(1000000000/pclk1clock))+1 */
    225              I2Cx->TRISE = (u16)(((freqrange * 300) / 1000) + 1);  
    226            }
    227            /* Write to I2Cx CCR */
    228            I2Cx->CCR = tmpreg;
    229          
    230            /* Enable I2Cx */
    231            I2C_Cmd(I2Cx, ENABLE);
    232          
    233          /*---------------------------- I2Cx CR1 Configuration ------------------------*/
    234            /* Get the I2Cx CR1 value */
    235            tmpreg = I2Cx->CR1;
    236            /* Clear ACK, SMBTYPE and  SMBUS bits */
    237            tmpreg &= CR1_CLEAR_Mask;
    238            /* Configure I2Cx: mode and acknowledgement */
    239            /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
    240            /* Set ACK bit according to I2C_Ack value */
    241            tmpreg |= (u16)((u32)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
    242            /* Write to I2Cx CR1 */
    243            I2Cx->CR1 = tmpreg;
    244          
    245          /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
    246            /* Set I2Cx Own Address1 and acknowledged address */
    247            I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
    248          }
    249          
    250          /*******************************************************************************
    251          * Function Name  : I2C_StructInit
    252          * Description    : Fills each I2C_InitStruct member with its default value.
    253          * Input          : - I2C_InitStruct: pointer to an I2C_InitTypeDef structure
    254          *                    which will be initialized.
    255          * Output         : None
    256          * Return         : None
    257          *******************************************************************************/
    258          void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
    259          {
    260          /*---------------- Reset I2C init structure parameters values ----------------*/
    261            /* Initialize the I2C_Mode member */
    262            I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
    263          
    264            /* Initialize the I2C_DutyCycle member */
    265            I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
    266          
    267            /* Initialize the I2C_OwnAddress1 member */
    268            I2C_InitStruct->I2C_OwnAddress1 = 0;
    269          
    270            /* Initialize the I2C_Ack member */
    271            I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
    272          
    273            /* Initialize the I2C_AcknowledgedAddress member */
    274            I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    275          
    276            /* initialize the I2C_ClockSpeed member */
    277            I2C_InitStruct->I2C_ClockSpeed = 5000;
    278          }
    279          
    280          /*******************************************************************************
    281          * Function Name  : I2C_Cmd
    282          * Description    : Enables or disables the specified I2C peripheral.
    283          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
    284          *                  - NewState: new state of the I2Cx peripheral. This parameter
    285          *                    can be: ENABLE or DISABLE.
    286          * Output         : None
    287          * Return         : None
    288          *******************************************************************************/
    289          void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
    290          {
    291            /* Check the parameters */
    292            assert(IS_FUNCTIONAL_STATE(NewState));
    293          
    294            if (NewState != DISABLE)
    295            {
    296              /* Enable the selected I2C peripheral */
    297              I2Cx->CR1 |= CR1_PE_Set;
    298            }
    299            else
    300            {
    301              /* Disable the selected I2C peripheral */
    302              I2Cx->CR1 &= CR1_PE_Reset;
    303            }
    304          }
    305          
    306          /*******************************************************************************
    307          * Function Name  : I2C_DMACmd
    308          * Description    : Enables or disables the specified I2C DMA requests.
    309          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
    310          *                  - NewState: new state of the I2C DMA transfer.
    311          *                    This parameter can be: ENABLE or DISABLE.
    312          * Output         : None
    313          * Return         : None
    314          *******************************************************************************/
    315          void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
    316          {
    317            /* Check the parameters */
    318            assert(IS_FUNCTIONAL_STATE(NewState));
    319          
    320            if (NewState != DISABLE)
    321            {
    322              /* Enable the selected I2C DMA requests */
    323              I2Cx->CR2 |= CR2_DMAEN_Set;
    324            }
    325            else
    326            {
    327              /* Disable the selected I2C DMA requests */
    328              I2Cx->CR2 &= CR2_DMAEN_Reset;
    329            }
    330          }
    331          
    332          /*******************************************************************************
    333          * Function Name  : I2C_DMALastTransferCmd
    334          * Description    : Specifies that the next DMA transfer is the last one.
    335          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
    336          *                  - NewState: new state of the I2C DMA last transfer.
    337          *                    This parameter can be: ENABLE or DISABLE.
    338          * Output         : None
    339          * Return         : None
    340          *******************************************************************************/
    341          void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
    342          {
    343            /* Check the parameters */
    344            assert(IS_FUNCTIONAL_STATE(NewState));
    345          
    346            if (NewState != DISABLE)
    347            {
    348              /* Next DMA end of transfer is the last transfer */
    349              I2Cx->CR2 |= CR2_LAST_Set;
    350            }
    351            else
    352            {
    353              /* Next DMA end of transfer is not the last transfer */
    354              I2Cx->CR2 &= CR2_LAST_Reset;
    355            }
    356          }
    357          
    358          /*******************************************************************************
    359          * Function Name  : I2C_GenerateSTART
    360          * Description    : Generates I2Cx communication START condition.
    361          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
    362          *                  - NewState: new state of the I2C START condition generation.
    363          *                    This parameter can be: ENABLE or DISABLE.
    364          * Output         : None
    365          * Return         : None.
    366          *******************************************************************************/
    367          void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
    368          {
    369            /* Check the parameters */
    370            assert(IS_FUNCTIONAL_STATE(NewState));
    371          
    372            if (NewState != DISABLE)
    373            {
    374              /* Generate a START condition */
    375              I2Cx->CR1 |= CR1_START_Set;
    376            }
    377            else
    378            {
    379              /* Disable the START condition generation */
    380              I2Cx->CR1 &= CR1_START_Reset;
    381            }
    382          }
    383          
    384          /*******************************************************************************
    385          * Function Name  : I2C_GenerateSTOP
    386          * Description    : Generates I2Cx communication STOP condition.
    387          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
    388          *                  - NewState: new state of the I2C STOP condition generation.
    389          *                    This parameter can be: ENABLE or DISABLE.
    390          * Output         : None
    391          * Return         : None.
    392          *******************************************************************************/
    393          void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
    394          {
    395            /* Check the parameters */
    396            assert(IS_FUNCTIONAL_STATE(NewState));
    397          
    398            if (NewState != DISABLE)
    399            {
    400              /* Generate a STOP condition */
    401              I2Cx->CR1 |= CR1_STOP_Set;
    402            }
    403            else
    404            {
    405              /* Disable the STOP condition generation */
    406              I2Cx->CR1 &= CR1_STOP_Reset;
    407            }
    408          }
    409          
    410          /*******************************************************************************
    411          * Function Name  : I2C_AcknowledgeConfig
    412          * Description    : Enables or disables the specified I2C acknowledge feature.
    413          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
    414          *                  - NewState: new state of the I2C Acknowledgement.
    415          *                    This parameter can be: ENABLE or DISABLE.
    416          * Output         : None
    417          * Return         : None.
    418          *******************************************************************************/
    419          void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
    420          {
    421            /* Check the parameters */
    422            assert(IS_FUNCTIONAL_STATE(NewState));
    423          
    424            if (NewState != DISABLE)
    425            {
    426              /* Enable the acknowledgement */
    427              I2Cx->CR1 |= CR1_ACK_Set;
    428            }
    429            else
    430            {
    431              /* Disable the acknowledgement */
    432              I2Cx->CR1 &= CR1_ACK_Reset;
    433            }
    434          }
    435          
    436          /*******************************************************************************
    437          * Function Name  : I2C_OwnAddress2Config
    438          * Description    : Configures the specified I2C own address2.
    439          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
    440          *                  - Address: specifies the 7bit I2C own address2.
    441          * Output         : None
    442          * Return         : None.
    443          *******************************************************************************/
    444          void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, u8 Address)
    445          {
    446            u16 tmpreg = 0;
    447          
    448            /* Get the old register value */
    449            tmpreg = I2Cx->OAR2;
    450            /* Reset I2Cx Own address2 bit [7:1] */
    451            tmpreg &= OAR2_ADD2_Reset;
    452            /* Set I2Cx Own address2 */
    453            tmpreg |= (u16)(Address & (u16)0x00FE);
    454            /* Store the new register value */
    455            I2Cx->OAR2 = tmpreg;
    456          }
    457          
    458          /*******************************************************************************
    459          * Function Name  : I2C_DualAddressCmd
    460          * Description    : Enables or disables the specified I2C dual addressing mode.
    461          * Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -