沁恒微CH32X035芯片IIC主机配置程序

/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2023/04/06
 * Description        : Main program body.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for 
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

/*
 *@Note
 *7-bit addressing mode, master/slave mode, transceiver routine:
 *I2C1_SCL(PA10)\I2C1_SDA(PA11).
 *This routine demonstrates that Master sends and Slave receives.
 *Note: The two boards download the Master and Slave programs respectively,
 *and power on at the same time.During the I2C communication process, 
 *the pins are open drain outputs. 
 *    Hardware connection:PA10 -- PA10
 *                        PA11 -- PA11
 *
 */
#include "all.h"
#include "debug.h"

/* I2C Mode Definition */
#define HOST_MODE   0
#define SLAVE_MODE   1

/* I2C Communication Mode Selection */
#define I2C_MODE   HOST_MODE
//#define I2C_MODE   SLAVE_MODE

/* Global define */
#define Size   6
//#define RXAdderss   0x55
#define RXAdderss   0x55<<1
#define TxAdderss   0x02

/* Global Variable */
u8 TxData[Size] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
u8 RxData[5][Size];

/*********************************************************************
 * @fn      IIC_Init
 *
 * @brief   Initializes the IIC peripheral.
 *
 * @return  none
 */
//change by luo
#if 1
void IIC_Init(u32 bound, u16 address)
{
	GPIO_InitTypeDef GPIO_InitStructure={0};
	I2C_InitTypeDef I2C_InitTSturcture={0};

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );
    //-------------add by luo, gpio remap--------------
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    //GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
    //GPIO_PinRemapConfig(GPIO_PartialRemap3_I2C1, ENABLE);//C17-SCL C16-SDA
    GPIO_PinRemapConfig(GPIO_PartialRemap2_I2C1, ENABLE);//C16-SCL C17-SDA
    //-----------------------------------------------
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_17;//
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOC, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_16;//
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOC, &GPIO_InitStructure );

	I2C_InitTSturcture.I2C_ClockSpeed = bound;
	I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
	I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9;
	I2C_InitTSturcture.I2C_OwnAddress1 = address;
	I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
	I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_Init( I2C1, &I2C_InitTSturcture );

	I2C_Cmd( I2C1, ENABLE );

#if (I2C_MODE == HOST_MODE)
	I2C_AcknowledgeConfig( I2C1, ENABLE );

#endif
}
#endif

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
    u8 i = 0;
	u8 j = 0;
	u8 p = 0;
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init( 921600 );
    printf( "SystemClk:%d\r\n", SystemCoreClock );
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );

#if (I2C_MODE == HOST_MODE)
    printf("IIC Host mode\r\n");
	IIC_Init( 80000, TxAdderss);

	for( j =0; j < 5; j++)
	{
        myDebug("->\n");
        while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );//wait hight level,should connecte up resistance
        myDebug("->\n");
        I2C_GenerateSTART( I2C1, ENABLE );//send start
        myDebug("->\n");
        while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );
        myDebug("->\n");
        I2C_Send7bitAddress( I2C1, RXAdderss, I2C_Direction_Transmitter );
        myDebug("->\n");
        while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );
        myDebug("->\n");
        for( i=0; i< 6;i++ )
        {
                myDebug("->\n");
                while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) );
                I2C_SendData( I2C1, TxData[i] );
                myDebug("->\n");
        }
        myDebug("->\n");
        while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
        myDebug("->\n");
        I2C_GenerateSTOP( I2C1, ENABLE );
        Delay_Ms(1000);
	}

#elif (I2C_MODE == SLAVE_MODE)
	printf("IIC Slave mode\r\n");
	IIC_Init( 80000, RXAdderss);

	for( p =0; p < 5; p++)
	{
	
    i = 0;
	while( !I2C_CheckEvent( I2C1, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ) );
    while( i < 6 )
    {
        if( I2C_GetFlagStatus( I2C1, I2C_FLAG_RXNE ) !=  RESET )
        {
            RxData[p][i] = I2C_ReceiveData( I2C1 );
            i++;
        }
    }
    while( !I2C_CheckEvent( I2C1, I2C_EVENT_SLAVE_STOP_DETECTED ) );
        I2C1->CTLR1 &= I2C1->CTLR1;
    }
	    printf( "RxData:\r\n" );
	 for(p=0; p<5; p++)
   {
        for( i = 0; i < 6; i++ )
		{
           printf( "%02x ", RxData[p][i] );
		}
		   printf( "\r\n ");
	 }
	 
 
#endif

	while(1);
}

从机SDA,SCL要接上拉电阻,注意从机地址被右移动一位了,传入时先左移一位。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值