找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 867|回复: 0
打印 上一主题 下一主题
收起左侧

stm8s的i2c

[复制链接]
跳转到指定楼层
楼主
ID:382184 发表于 2018-8-2 15:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/**
  ******************************************************************************
  * @file I2C_10bitsCommunication\main_master_10bit.c
  * @brief This file contains the main function for I2C configured in master 10 bit addressing mode.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.0.1
  * @date 09/22/2008
  ******************************************************************************
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2008 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm8s_lib.h"
/* Evalboard I/Os configuration */
#define LEDS_PORT (GPIOH)
#define LED1_PIN  (GPIO_PIN_3)
#define LED2_PIN  (GPIO_PIN_2)
#define LED3_PIN  (GPIO_PIN_1)
#define LED4_PIN  (GPIO_PIN_0)
/**
  * @addtogroup I2C_10bitsCommunication
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define I2C_SLAVE_ADDRESS10 ((u16)0x0330)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 HEADER_ADDRESS_Write = (((I2C_SLAVE_ADDRESS10 & 0xFF00) >> 7) | 0xF0);
/* Private function prototypes -----------------------------------------------*/
void Delay (u16 nCount);
/* Private functions ---------------------------------------------------------*/
void Delay(u16 nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}
/* Public functions ----------------------------------------------------------*/
/**
  * @brief How to perform  a communication between 2 I2C in 10bit addressing mode, fast mode (Master transmitter)
  * @par Examples description
  * - Send a buffer to a second 10bit addressed I2C device.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * - GPIO_Init
  * - GPIO_WriteHigh
  * - GPIO_WriteReverse  
  * - I2C_GenerateSTART
  * - I2C_GenerateSTOP
  * - I2C_DeInit
  * - I2C_Send7bitAddress
  * - I2C_SendData
  * - I2C_CheckEvent
  * - I2C_ClearFlag
  */
void main(void)
{
  u8 Tx_Idx = 0;
  u8 tmpbuff[4] = {0x00, 0x55, 0xAA, 0xFF};
  
  /* Initialize I/Os in Output Mode */
  GPIO_Init(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN), GPIO_MODE_OUT_PP_LOW_FAST);
  /* Optional: put here as example only */
  I2C_DeInit();

  /* Initialize the I2C */
  I2C_Init(200000, 0x0102, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_10BIT, 16);
  /* All LEDs are ON per default */
  GPIO_WriteHigh(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN));
  /*----- Transmission Phase -----*/
  /* Send START condition */
  I2C_GenerateSTART(ENABLE);
  /* Test on EV5 and clear it */
  while (!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT));
  /* Send Header to Slave for write */
  I2C_SendData(HEADER_ADDRESS_Write);
  /* Test on EV9 and clear it */
  while (!I2C_CheckEvent(I2C_EVENT_MASTER_HEADER_ACKED));
  /* Send slave Address for write */
  I2C_Send7bitAddress(0x30, I2C_DIRECTION_TX);
  /* Test on EV6 and clear it */
  while (!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));
  I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
  /* Send data */
  Tx_Idx = 0;
  while (Tx_Idx < 4)
  {
    /* Send  data */
    I2C_SendData(tmpbuff[Tx_Idx++]);
    /* Test on EV8 and clear it */
    while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  }
  /* Send STOP Condition */
  I2C_GenerateSTOP(ENABLE);
  /* Toggle all LEDs when I2C communication is terminated */
  while (1)
  {
    GPIO_WriteReverse(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN));
    Delay((u16)60000);
  }
}
/**
  * @brief Reports the name of the source file and the source line number where
  * the assert error has occurred.
  * User can add his own implementation to report the file name and line number.
  * ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  */
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
  /* Add your own code to manage an assert error */
  /* Infinite loop */
  while (1)
  {
  }
}
/**
  * @}
  */
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表