☆ HC32F003 系列是一款 Low Pin Count、宽电压工作范围的 MCU。集成 12 位 1M sps 高精度SARADC 以及集成了比较器,多路 UART,SPI,I2C 等丰富的通讯外设,具有高整合度、高抗干扰、高可靠性的特点。HC32F003 内核采用 Cortex-M0+ 内核,配合成熟的 Keil μVision 调试开发软件,支持 C 语言及汇编语言,汇编指令。 ☆ 华大MCU单片机资料分享连接
HC32F003 HC32F005 HC32L110 HC32L136 HC32F030 HC32M140 HC32F146 HC32L150 HC32L156/****************************************************************************/ / \file main.c
**
** A detailed description is available at
** @link Sample Group Some description @endlink
**
** - 2016-02-16 1.0 XYZ First version for Device Driver Library of Module.
**
******************************************************************************/ /****************************************************************************** - Include files
/
#include “i2c.h”
#include “gpio.h”
/ - Local pre-processor symbols/macros (’#define’)
******************************************************************************/
/****************************************************************************** - Global variable definitions (declared in header file with ‘extern’)
******************************************************************************/
/****************************************************************************** - Local type definitions (‘typedef’)
******************************************************************************/
/****************************************************************************** - Local function prototypes (‘static’)
******************************************************************************/
/****************************************************************************** - Local variable definitions (‘static’) *
******************************************************************************/
/****************************************************************************** - Local pre-processor symbols/macros (’#define’)
*/
#define T1_PORT (3)
#define T1_PIN (3)
/ - Function implementation - global (‘extern’) and local (‘static’)
****************************************************************************/
/
** \brief Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/ uint8_t u8Senddata[10] = {0x12,0x34,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00};
uint8_t u8Recdata[10]={0x00};
boolean_t bIrq = 0;
uint8_t u8State = 0,u8SendCnt = 0,u8RecCnt = 0;
uint8_t Send** = 0,Comm_** = 0;
uint8_t data=0;
static void I2cCallBack(void)
{
bIrq = 1;
u8State = I2C_GetState();
if(0 == Send**)
{
if(0x08 == u8State)
{
I2C_ClearFunc(I2cStart_En);
I2C_WriteByte(I2c_SlaveWt);//从机地址发送OK
}
if(0x18 == u8State)
{
I2C_WriteByte(0x00);//写零地址
}
if(0x28 == u8State)
{
I2C_WriteByte(u8Senddata[u8SendCnt]);
u8SendCnt++;
}
if(u8SendCnt>10)
{
I2C_SetFunc(I2cStop_En);
Send** = 1;
Comm_** = 1;
}
}
else
{
if(0x08 == u8State)
{
I2C_ClearFunc(I2cStart_En);
I2C_WriteByte(I2c_SlaveWt);//从机地址发送OK
}
if(0x18 == u8State)
{
I2C_WriteByte(0x00);//读取0地址
}
if(0x28 == u8State)
{
I2C_SetFunc(I2cStart_En);
}
if(0x10 == u8State)
{
I2C_ClearFunc(I2cStart_En); I2C_WriteByte(I2c_SlaveRd); } if(0x40 == u8State) { I2C_SetFunc(I2cAck_En); } if(0x50 == u8State) { u8Recdata[u8RecCnt] = I2C_ReadByte(); u8RecCnt++; } if(u8RecCnt>10) { I2C_SetFunc(I2cStop_En); }}I2C_ClearIrq();- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
}
int32_t main(void)
{ stc_i2c_config_t stcI2cCfg;stc_clk_config_t stcCfg;DDL_ZERO_STRUCT(stcCfg);DDL_ZERO_STRUCT(stcI2cCfg);Gpio_InitIOExt(2,5,GpioDirOut,TRUE,FALSE,TRUE,FALSE); Gpio_InitIOExt(2,6,GpioDirOut,TRUE,FALSE,TRUE,FALSE);Gpio_SetFunc_I2CDAT_P25() Gpio_SetFunc_I2CCLK_P26()Clk_SetPeripheralGate(ClkPeripheralI2c,TRUE);stcI2cCfg.enFunc = I2cBaud_En;stcI2cCfg.u8Tm = 0x04;//100K=(4000000/(8*(4+1))stcI2cCfg.pfnI2cCb = I2cCallBack;stcI2cCfg.bTouchNvic = TRUE;I2C_DeInit(); I2C_Init(stcI2cCfg);I2C_SetFunc(I2cMode_En);I2C_SetFunc(I2cStart_En);while(1){- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
#if 1
if(1 == Comm_**)
{
Comm_** = 0;
delay1ms(100);
I2C_SetFunc(I2cStart_En);
}
#endif
} } /****************************************************************************** - EOF (not truncated)
******************************************************************************/
作者:虹芯侠客1
来源:CSDN
原文:https://blog.csdn.net/jetson024/article/details/84619088
版权声明:本文为博主原创**,转载请附上博文链接!
|