当前位置:网站首页>Realize IIC data / instruction interaction with micro batg135
Realize IIC data / instruction interaction with micro batg135
2022-07-04 06:23:00 【Tears like eating hot pot】
This routine is used in a project IIC Realization 3 Direct data interaction of Taichung micro , And the wechat IIC Routines tend to be simple , There are many problems in the implementation , To really achieve IIC, Need to rewrite .
First of all, we need to initialize the micro as a slave :
void IICA0_Init(void)
{
CGC->PER0 |= CGC_PER0_IICA0EN_Msk; /* enables input clock supply */
IICA->IICCTL00 = _00_IICA_OPERATION_STOP; /* stop operation */
INTC_DisableIRQ(IICA_IRQn); /* disable INTIICA interrupt flag */
INTC_ClearPendingIRQ(IICA_IRQn); /* clear INTIICA interrupt flag */#ifdef IICA_STANDARD_MODE /* Max rate: 100Kbps */
IICA->IICCTL01 = _00_IICA_OPERATE_STANDARD; /* operates in standard mode */
IICA->IICWL0 = _4B_IICA_LOW_WIDTH;
IICA->IICWH0 = _53_IICA_HIGH_WIDTH;
#endif
#ifdef IICA_FAST_MODE /* Max rate: 400Kbps */
IICA->IICCTL01 = _08_IICA_OPERATE_FAST; /* operates in fast mode */
IICA->IICCTL01 |= _04_IICA_FILTER_ON; /* filter on */
IICA->IICWL0 = _14_IICA_LOW_WIDTH;
IICA->IICWH0 = _12_IICA_HIGH_WIDTH;
#endif
IICA->IICCTL01 |= _01_IICA_FCLK_2_SELECTED;
IICA->SVA0 = _98_IICA0_SLAVEADDRESS;
IICA->IICF0 |= _02_IICA_WITHOUT_DETECTION; /* enable generation of a start condition without detecting a stop
condition */
IICA->IICF0 |= _01_IICA_RESERVATION_DISABLE; /* disable communication reservation */
IICA->IICCTL00 |= _08_IICA_INTERRUPT_REQUEST_NINTH; /* interrupt request is generated at the ninth clock's falling edge */
IICA->IICCTL00 |= _00_IICA_ACKOWNLEDGMENT_DISABLE; /* enable acknowledgment */
IICA->IICCTL00 |= _80_IICA_OPERATION_ENABLE; /* enable operation */
IICA->IICCTL00 |= _40_IICA_OPERATION_STANDBY; /* this exits from the current communications and sets standby mode */
INTC_EnableIRQ(IICA_IRQn); /* enable INTIICA interrupt flag *//* Set SCLA0, SDAA0 pin */
SCLA0_PORT_SETTING();
SDAA0_PORT_SETTING();
}
among : According to the... Of Zhongwei GPIO Oral IIC Pin mapping
#define SCLA0_PORT_SETTING() do{ \
PORT->SCLA0PCFG = 0x13; /* allocate SCLA0 to P30 */ \
PORT->P3 &= ~(1 << 0); /* P30 output low level */ \
PORT->PM3 &= ~(1 << 0); /* P30 is N-ch open-drain output mode */ \
PORT->PMC3 &= ~(1 << 0); /* P30 digital function */ \
}while(0)/* ToDo: You can allocate the SDA00 to any desired pins with SDA00PCFG register */
#define SDAA0_PORT_SETTING() do{ \
PORT->SDAA0PCFG = 0x17; /* allocate SDAA0 to P50 */ \
PORT->P5 &= ~(1 << 0); /* P50 output low level */ \
PORT->PM5 &= ~(1 << 0); /* P50 is used as SDAA0 inout */ \
PORT->PMC5 &= ~(1 << 0); /* P50 digital function */ \
}while(0)
Next, micro enterprise as a slave , Realize the sending and receiving of data :
void IIC_Rec_Fuction(void)
{
static uint8_t times = 0;
static uint16_t interval = 0;
uint8_t csc = 0;
if (0 == times)
{
times = 1;
g_iica0_rx_len = 200;
g_iica0_rx_cnt = 0U;
g_iica0_rx_end = 0;
g_iica0_slave_status_flag = 0U;
IICA->IICA0 = SLVADDR_98U; /* slave address */
interval = gMillisecondTick;
}
if (1 == times)
{
if (((gMillisecondTick - interval) & 0xFFFF) > 10) //10ms
{
if ((g_iica0_rx_end == 1) && (RxBuffer2[0] == 0x55) && (RxBuffer2[1] == 0xAA) && (RxBuffer2[2] != 0x00))
{
for(uint16_t i = 0; i < (RxBuffer2[2] + 1);i++ )
{
csc ^= RxBuffer2[i];
}
if(csc == RxBuffer2[RxBuffer2[2] + 1])
{
Sys_Data.CmdData = (RxBuffer2[4]<<8) + RxBuffer2[5]; /* Get function instructions */
memcpy(Sys_Data.Rec_Data,RxBuffer2,sizeof(RxBuffer2)); /* Get the received data */
//Sys_Data.Rec_Data Is an array of structures :200 Bytes uint8_t Rec_Data[200]; /* Receive array */, There is no problem with the data received here, so you can enter
/*IIC Receiving function processing */
Rev_Process_Fuction();
Send_Process_Connect_Upload(); /*IIC Send data thread */
g_iica0_rx_len = 200;
g_iica0_rx_cnt = 0U;
g_iica0_rx_end = 0;
g_iica0_slave_status_flag = 0U;
IICA->IICA0 = SLVADDR_98U; /* slave address */
}
}
interval = gMillisecondTick;
}
}else
{
g_iica0_rx_len = 200;
g_iica0_rx_cnt = 0U;
g_iica0_rx_end = 0;
g_iica0_slave_status_flag = 0U;
IICA->IICA0 = SLVADDR_98U; /* slave address */
}
}
among : It should be noted that , Remember to modify the interrupt function officially given by wechat :
uint8_t RxBuffer2[200]; /* Receive array */
uint16_t RxCounter2 = 0; /* Receive count *//***********************************************************************************************************************
* Function Name: iica0_slavehandler
* @brief This function is IICA0 slave handler.
* @param None
* @return None
***********************************************************************************************************************/
static void iica0_slavehandler(void)
{
/* Control for stop condition */
if (IICA->IICS0 & IICA_IICS0_SPD_Msk)
{
/* Get stop condition */
IICA->IICCTL00 &= ~IICA_IICCTL00_SPIE_Msk; /* SPIE0 = 0: disable */
if(g_iica0_slave_status_flag & _80_IICA_ADDRESS_COMPLETE)
{
if (g_iica0_slave_status_flag & 0x04U) /* send flag */
{
iica0_callback_slave_sendend();
}
if (g_iica0_slave_status_flag & 0x02U) /* receive flag */
{
iica0_callback_slave_receiveend();
}
}
g_iica0_slave_status_flag = 1U;
}
else
{
if ((g_iica0_slave_status_flag & _80_IICA_ADDRESS_COMPLETE) == 0U)
{
if (IICA->IICS0 & IICA_IICS0_COI_Msk)
{
IICA->IICCTL00 |= IICA_IICCTL00_SPIE_Msk; /* SPIE0 = 1: enable */
g_iica0_slave_status_flag = _80_IICA_ADDRESS_COMPLETE;if (IICA->IICS0 & IICA_IICS0_TRC_Msk) /* TRC0 == 1 */
{
IICA->IICCTL00 |= IICA_IICCTL00_WTIM_Msk; /* WTIM0 = 1: interrupt request is generated at the ninth clock's falling edge */if (g_iica0_tx_cnt > 0U)
{
IICA->IICA0 = *gp_iica0_tx_address;
gp_iica0_tx_address++;
g_iica0_tx_cnt--;
}
else
{
iica0_callback_slave_sendend();
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
else
{
IICA->IICCTL00 |= IICA_IICCTL00_ACKE_Msk; /* ACKE0 = 1U: enable acknowledgment */
IICA->IICCTL00 &= ~IICA_IICCTL00_WTIM_Msk; /* WTIM0 = 0U: interrupt request is generated at the eighth clock's falling edge */
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
else
{
iica0_callback_slave_error(MD_ERROR);
}
}
else
{
if (IICA->IICS0 & IICA_IICS0_TRC_Msk) /* TRC0 == 1 */
{
g_iica0_slave_status_flag |= 4U; /* send flag */
if ((0U == (IICA->IICS0 & IICA_IICS0_ACKD_Msk)) && (g_iica0_tx_cnt != 0U))
{
iica0_callback_slave_error(MD_NACK);
}
else
{
if (g_iica0_tx_cnt > 0U)
{
IICA->IICA0 = *gp_iica0_tx_address;
gp_iica0_tx_address++;
g_iica0_tx_cnt--;
}
else
{
iica0_callback_slave_sendend();
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
}
else
{
g_iica0_slave_status_flag |= 2U; /* receive flag */
if (g_iica0_rx_cnt < g_iica0_rx_len)
{
RxBuffer2[g_iica0_rx_cnt] = IICA->IICA0;
g_iica0_rx_cnt++;if (g_iica0_rx_cnt == g_iica0_rx_len)
{
IICA->IICCTL00 |= IICA_IICCTL00_WTIM_Msk; /* WTIM0 = 1: interrupt request is generated at the ninth clock's falling edge */
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
iica0_callback_slave_receiveend();
}
else
{
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
else
{
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
}
}
}
/*
*********************************************************************************
* Letter Count name :Send_Process_Connect_Upload
* Functional specifications : Send data to Bluetooth MCU
* shape ginseng :void
* return return value :void
*********************************************************************************
*/
void Send_Process_Connect_Upload(void)
{
uint16_t value_size = 0;
//uint16_t i = 0;
//uint8_t tmpbuf[10]={0};
static uint8_t value[200] = {0};
/* Judge whether serial port sending is executed immediately */
if((Sys_Data.Com_Stas & COM_SEND_RDY) !=COM_SEND_RDY)
{
return;
}
/* Serial port group packet storage value Value*/
// value_size = SendPacekData(Sys_Data.SendBuf,Sys_Data.SendLen,value);
IICA0_SlaveSend(SLVADDR_98U,value,value_size);
while(g_iica0_tx_end == 0);
#if 0
/* send data */
printf_log("\r\n send_process :");
for(i=0;i<(value_size);i++)
{
memset(tmpbuf,0,10);
u8_T_array(value[i],tmpbuf);
printf_log((const char *)tmpbuf);
printf_log(" ");
}
printf_log("\r\n value_size = %d",value_size);
#endif
//Sys_Data.Com_Stas = 0; /* Command status reset */
// Sys_Data.CmdData = 0; /* Command reset */
// Sys_Data.SendLen = 0; /* The data length is 0*/
// memset(Sys_Data.CmdPara,0,sizeof(Sys_Data.CmdPara)); /* Command packet reset */
}
边栏推荐
- Webrtc quickly set up video call and video conference
- 《ClickHouse原理解析与应用实践》读书笔记(4)
- AWT常用组件、FileDialog文件选择框
- 测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
- FRP intranet penetration, reverse proxy
- AWT common components, FileDialog file selection box
- 27-31. Dependency transitivity, principle
- buuctf-pwn write-ups (8)
- 70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
- Matlab remainder
猜你喜欢
Arcpy uses the updatelayer function to change the symbol system of the layer
JSON web token -- comparison between JWT and traditional session login authentication
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
Arcpy 利用updatelayer函数改变图层的符号系统
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
分布式CAP理论
JS arguments parameter usage and explanation
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
Experience weekly report no. 102 (July 4, 2022)
746. Climb stairs with minimum cost
随机推荐
手动对list进行分页(参数list ,当前页,页面大小)
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
对List进行排序工具类,可以对字符串排序
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Operator < <> > fool test case
C實現貪吃蛇小遊戲
4G wireless all network solar hydrological equipment power monitoring system bms110
如何实现视频平台会员多账号登录
C语言练习题(递归)
C language exercises (recursion)
How to get the parent node of all nodes in El tree
Vant --- detailed explanation and use of list component in vant
How to realize multi account login of video platform members
运算符<< >>傻瓜式测试用例
buuctf-pwn write-ups (8)
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
QT releases multilingual International Translation
Leakage detection relay jy82-2p
How to expand all collapse panels