javnson 发表于 2022-4-10 16:16

【沁恒CH582】 I2C暂存的继电器上位机控制继电器模块

<h2 cid="n134" mdtype="heading">i2c接口与继电器驱动</h2>

<p cid="n135" mdtype="paragraph">本次我们将要实现的功能是上位机发送继电器控制指令(单字节控制指令),24C256EEPROM用于保存当前的继电器状态,并且在保存之后驱动继电器处于正确的状态。</p>

<p cid="n152" mdtype="paragraph">接下来我们逐步完成。首先我们需要选定一个基础例程,在此基础上进行改动,我计划选用IIC例程进行开发。</p>

<h3 cid="n154" mdtype="heading">对于IIC例程做基础分析加工</h3>

<p cid="n157" mdtype="paragraph">首先测试是否能正确烧写例程。链接硬件如下:</p>

<p cid="n169" mdtype="paragraph">&nbsp;</p>

<p cid="n159" mdtype="paragraph">就是为开发板连上编程器和串口转换器,然后链接一个24C256的EEPROM。(目前手上最方便的就是这块测试板了)。</p>

<p cid="n161" mdtype="paragraph">然后编译例程,并烧录。</p>

<p cid="n163" mdtype="paragraph"><img alt="image-20220410141732107" data-local-refresh="true" onerror="onImageErrorFunc(event)" onload="onLoadedFuncForQuickAction(event)" referrerpolicy="no-referrer" src="file://G:\OneDriveSync\OneDrive%20-%20zju.edu.cn\_Notes\LocalPictureService\image-20220410141732107-16495714549841.png?lastModify=1649578377" /></p>

<p cid="n165" mdtype="paragraph">接下来的问题就是确定数据保存的位置和代码中对于数据定位的部分调整了。</p>

<p cid="n171" mdtype="paragraph">接下来我们先做一个简单的测试,我们使用单字节写入的模式对芯片的第一个地址空间和最后一个地址空间分别写入0x56和0x78,然后读出这两个地址空间的数据并发送到上位机中。</p>

<p cid="n173" mdtype="paragraph">首先是器件地址,根据数据手册中给出的描述10100A_1A_0\,b我们将A_1,A_0连接到低电平,然后我们可以得到器件地址为1010000\,B=0x50。</p>

<p cid="n175" mdtype="paragraph">接下来将需要向器件中输入16bits的地址,之后紧跟8bits的具体数据,最终发送停止位。</p>

<p cid="n179" mdtype="paragraph">按照我们的定义,我们将要发送<code> 0x00 0x00 0x56 </code>,<code> 0xFF 0xFF 0x78 </code>,我们知道256k的EEPROM的首地址应当为<code>0x0000</code>末地址为<code>0xFFFF</code>。那么按照这个逻辑可以得到对应的代码。</p>

<pre cid="n181" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;void send_one_byte(uint8_t data)
&nbsp;{
&nbsp; &nbsp;I2C_SendData(data);
&nbsp; &nbsp;while(I2C_GetFlagStatus(I2C_FLAG_TXE) != RESET);
&nbsp;}
&nbsp;void main()
&nbsp;{
&nbsp; &nbsp; &nbsp;// ... 初始化部分
&nbsp; &nbsp; &nbsp;
&nbsp;    I2C_GenerateSTART(ENABLE);
&nbsp;    while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
&nbsp;​
&nbsp;    I2C_Send7bitAddress(0x50, I2C_Direction_Transmitter);
&nbsp;    while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
&nbsp;​
&nbsp;    send_one_byte(0x00);
&nbsp;    send_one_byte(0x00);
&nbsp;    send_one_byte(0x56);
&nbsp;​
&nbsp;    while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
&nbsp;    I2C_GenerateSTOP(ENABLE);
&nbsp;​
&nbsp;    I2C_GenerateSTART(ENABLE);
&nbsp;    while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
&nbsp;​
&nbsp;    I2C_Send7bitAddress(0x50, I2C_Direction_Transmitter);
&nbsp;    while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
&nbsp;​
&nbsp;    send_one_byte(0xff);
&nbsp;    send_one_byte(0xff);
&nbsp;    send_one_byte(0x78);
&nbsp;​
&nbsp;    while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
&nbsp;    I2C_GenerateSTOP(ENABLE);
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;// ... 后续循环部分
&nbsp;}</pre>

<p cid="n183" mdtype="paragraph">接下来我们应该读取这两个地址的内容并发送到上位机。</p>

<p cid="n185" mdtype="paragraph">根据数据手册上的说明这款芯片的接收时序是这样的:</p>

<p cid="n187" mdtype="paragraph"><img alt="image-20220410144159159" data-local-refresh="true" onerror="onImageErrorFunc(event)" onload="onLoadedFuncForQuickAction(event)" referrerpolicy="no-referrer" src="file://G:\OneDriveSync\OneDrive%20-%20zju.edu.cn\_Notes\LocalPictureService\image-20220410144159159.png?lastModify=1649578377" /></p>

<p cid="n150" mdtype="paragraph">那么我们可以啰嗦地,写出逐个字节读取地代码。</p>

<pre cid="n199" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;    I2C_GenerateSTART(ENABLE);
&nbsp;    while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
&nbsp;​
&nbsp;    I2C_Send7bitAddress(0x50, I2C_Direction_Transmitter);
&nbsp; &nbsp;while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
&nbsp;​
&nbsp; &nbsp;send_one_byte(0xff);
&nbsp; &nbsp;send_one_byte(0xff);
&nbsp; &nbsp;send_one_byte(0x78);
&nbsp;​
&nbsp; &nbsp;while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
&nbsp; &nbsp;I2C_GenerateSTOP(ENABLE);
&nbsp;​
&nbsp; &nbsp;printf(&quot;Data has been sent!\r\n&quot;);
&nbsp;​
&nbsp; &nbsp;I2C_GenerateSTART(ENABLE);
&nbsp; &nbsp;while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
&nbsp;​
&nbsp; &nbsp;I2C_Send7bitAddress(0x50, I2C_Direction_Receiver);
&nbsp; &nbsp;while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
&nbsp;​
&nbsp; &nbsp;send_one_byte(0x00);
&nbsp; &nbsp;send_one_byte(0x00);
&nbsp;​
&nbsp; &nbsp;send_one_byte(0xff);
&nbsp; &nbsp;send_one_byte(0xff);
&nbsp;​
&nbsp; &nbsp;I2C_GenerateSTART(ENABLE); // 重复产生起始位
&nbsp; &nbsp;while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));
&nbsp; &nbsp;printf(&quot;Addr:0x0000, Data: 0x%x&quot;,I2C_ReceiveData());
&nbsp;​
&nbsp; &nbsp;while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));
&nbsp; &nbsp;printf(&quot;Addr:0xFFFF, Data: 0x%x&quot;,I2C_ReceiveData());
&nbsp;​</pre>

<p cid="n201" mdtype="paragraph">有了这一段代码可以验证是否数据被正确存入指定区域。</p>

<h3 cid="n208" mdtype="heading">UART接收中断</h3>

<p cid="n223" mdtype="paragraph">首先是初始化代码:</p>

<pre cid="n210" lang="c++" mdtype="fences" spellcheck="false">
&nbsp;GPIOA_SetBits(GPIO_Pin_9);
&nbsp;    GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU);
&nbsp;    GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);
&nbsp;    UART1_DefInit();
&nbsp;​
&nbsp; &nbsp;UART1_ByteTrigCfg( UART_1BYTE_TRIG );
&nbsp; &nbsp;trigB = 1;
&nbsp; &nbsp;UART1_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
&nbsp; &nbsp;PFIC_EnableIRQ( UART1_IRQn );
&nbsp;​</pre>

<p cid="n221" mdtype="paragraph">之后是中断响应函数:</p>

<pre cid="n224" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;__INTERRUPT
&nbsp;__HIGH_CODE
&nbsp;void UART1_IRQHandler( void )
&nbsp;{
&nbsp; &nbsp;UINT8V i;
&nbsp;​
&nbsp; &nbsp;switch ( UART1_GetITFlag() )
&nbsp;{
&nbsp; &nbsp; &nbsp;case UART_II_LINE_STAT : &nbsp; &nbsp; &nbsp; &nbsp;// 线路状态错误
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;UART1_GetLinSTA();
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp;}
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_RECV_RDY : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 数据达到设置触发点
&nbsp; &nbsp; &nbsp; &nbsp;for ( i = 0; i != trigB; i++ )
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RxBuff = UART1_RecvByte();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UART1_SendByte( RxBuff );
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_RECV_TOUT : &nbsp; &nbsp; &nbsp; &nbsp; // 接收超时,暂时一帧数据接收完成
&nbsp; &nbsp; &nbsp; &nbsp;i = UART1_RecvString( RxBuff );
&nbsp; &nbsp; &nbsp; &nbsp;UART1_SendString( RxBuff, i );
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_THR_EMPTY : &nbsp; &nbsp; &nbsp; &nbsp; // 发送缓存区空,可继续发送
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_MODEM_CHG : &nbsp; &nbsp; &nbsp; &nbsp; // 只支持串口0
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;default :
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;}
&nbsp;​
&nbsp;}</pre>

<p cid="n226" mdtype="paragraph">这样我们就得到了一个ECHO例程。</p>

<p cid="n213" mdtype="paragraph">&nbsp;</p>

<h3 cid="n209" mdtype="heading">继电器模块</h3>

<p cid="n206" mdtype="paragraph">我们假设输入A-D表示吸合1-4继电器,那么就是在GPIOA1-GPIO4执行输出。E-H表示关闭GPIOA1-GPIOA4。我们可以魔改一下上面的程序。</p>

<p cid="n215" mdtype="paragraph">将GPIO 设为低电流推挽输出,<code>GPIO_ModeOut_PP_5mA</code>。</p>

<pre cid="n217" lang="c++" mdtype="fences" spellcheck="false">
&nbsp;__INTERRUPT
&nbsp;__HIGH_CODE
&nbsp;void UART1_IRQHandler( void )
&nbsp;{
&nbsp; &nbsp;UINT8V i;
&nbsp;​
&nbsp; &nbsp;switch ( UART1_GetITFlag() )
&nbsp;{
&nbsp; &nbsp; &nbsp;case UART_II_LINE_STAT : &nbsp; &nbsp; &nbsp; &nbsp;// 线路状态错误
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;UART1_GetLinSTA();
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp;}
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_RECV_RDY : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 数据达到设置触发点
&nbsp; &nbsp; &nbsp; &nbsp;for ( i = 0; i != trigB; i++ )
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RxBuff = UART1_RecvByte();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UART1_SendByte( RxBuff );
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch(RxBuff)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;A&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;B&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;C&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_3);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;D&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_4);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;E&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;F&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;G&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_3);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;H&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_4);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//UART1_SendByte(RxBuff);
&nbsp;​
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_RECV_TOUT : &nbsp; &nbsp; &nbsp; &nbsp; // 接收超时,暂时一帧数据接收完成
&nbsp; &nbsp; &nbsp; &nbsp;i = UART1_RecvString( RxBuff );
&nbsp; &nbsp; &nbsp; &nbsp;UART1_SendString( RxBuff, i );
&nbsp; &nbsp; &nbsp; &nbsp;switch(RxBuff)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;A&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;B&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;C&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_3);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;D&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_SetBits(GPIO_Pin_4);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;E&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;F&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;G&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_3);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;H&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIOA_ResetBits(GPIO_Pin_4);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_THR_EMPTY : &nbsp; &nbsp; &nbsp; &nbsp; // 发送缓存区空,可继续发送
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_MODEM_CHG : &nbsp; &nbsp; &nbsp; &nbsp; // 只支持串口0
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;default :
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;}
&nbsp;​
&nbsp;}</pre>

<p cid="n193" mdtype="paragraph">利用万用表可以测量得到这种情况下,低电平为0V,高电平为5.12V.</p>

<h3 cid="n195" mdtype="heading">缝合!</h3>

<p cid="n228" mdtype="paragraph">将以上代码,缝合到一起,让我们每次的继电器操作结果能够保存在EEPROM中。可以用下面的代码实现:</p>

<p cid="n233" mdtype="paragraph">首先是初始化继电器初始状态:</p>

<pre cid="n229" lang="C++" mdtype="fences" spellcheck="false">
&nbsp; &nbsp;uint8_t state;
&nbsp;​
&nbsp; &nbsp;I2C_GenerateSTART(ENABLE);
&nbsp; &nbsp;while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
&nbsp;​
&nbsp; &nbsp;I2C_Send7bitAddress(0x50, I2C_Direction_Receiver);
&nbsp; &nbsp;while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
&nbsp;​
&nbsp; &nbsp;send_one_byte(0x00);
&nbsp; &nbsp;send_one_byte(0x00);
&nbsp;​
&nbsp; &nbsp;I2C_GenerateSTART(ENABLE); // 重复产生起始位
&nbsp; &nbsp;while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));
&nbsp; &nbsp;state = I2C_ReceiveData();
&nbsp; &nbsp;printf(&quot;Addr:0x0000, Data: 0x%x&quot;,state); // 初始化</pre>

<p cid="n231" mdtype="paragraph">接下来是更新中断响应函数:</p>

<pre cid="n237" lang="c++" mdtype="fences" spellcheck="false">
&nbsp;__INTERRUPT
&nbsp;__HIGH_CODE
&nbsp;void UART1_IRQHandler( void )
&nbsp;{
&nbsp; &nbsp;UINT8V i;
&nbsp;​
&nbsp; &nbsp;switch ( UART1_GetITFlag() )
&nbsp;{
&nbsp; &nbsp; &nbsp;case UART_II_LINE_STAT : &nbsp; &nbsp; &nbsp; &nbsp;// 线路状态错误
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;UART1_GetLinSTA();
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp;}
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_RECV_RDY : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 数据达到设置触发点
&nbsp; &nbsp; &nbsp; &nbsp;for ( i = 0; i != trigB; i++ )
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RxBuff = UART1_RecvByte();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UART1_SendByte( RxBuff );
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch(RxBuff)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;A&#39;:case &#39;B&#39;:case &#39;C&#39;:case &#39;D&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state |= 1&lt;&lt;(RxBuff - &#39;A&#39;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;   break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;E&#39;:case &#39;F&#39;:case &#39;G&#39;:case &#39;H&#39;:
&nbsp;                  state &amp;= 0xFF^1&lt;&lt;(RxBuff - &#39;A&#39;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_RECV_TOUT : &nbsp; &nbsp; &nbsp; &nbsp; // 接收超时,暂时一帧数据接收完成
&nbsp; &nbsp; &nbsp; &nbsp;i = UART1_RecvString( RxBuff );
&nbsp; &nbsp; &nbsp; &nbsp;UART1_SendString( RxBuff, i );
&nbsp;      switch(RxBuff)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;A&#39;:case &#39;B&#39;:case &#39;C&#39;:case &#39;D&#39;:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state |= 1&lt;&lt;(RxBuff - &#39;A&#39;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;   break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &#39;E&#39;:case &#39;F&#39;:case &#39;G&#39;:case &#39;H&#39;:
&nbsp;                  state &amp;= 0xFF^1&lt;&lt;(RxBuff - &#39;A&#39;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_THR_EMPTY : &nbsp; &nbsp; &nbsp; &nbsp; // 发送缓存区空,可继续发送
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;case UART_II_MODEM_CHG : &nbsp; &nbsp; &nbsp; &nbsp; // 只支持串口0
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;​
&nbsp; &nbsp; &nbsp;default :
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;}
&nbsp;    R32_PA_OUT = state &lt;&lt; 1; // 从A1开始
&nbsp;}</pre>

<p cid="n239" mdtype="paragraph">至此我们就实现了一个可以由上位机控制的继电器组了!虽然我们只是用万用表测量了一下输出情况,但是已经能够起到我们预想中的效果了。</p>

Jacktang 发表于 2022-4-10 16:43

<p>文中图片没上传上来</p>

javnson 发表于 2022-4-12 11:15

<p>补一张硬件连接图,手头东西比较简陋,笑死,但是好使就行!&nbsp;&nbsp;</p>

lugl4313820 发表于 2022-4-13 18:58

<p>将GPIO 设为低电流推挽输出,</p>

<ul>
        <li>GPIO_ModeOut_PP_5mA,</li>
</ul>

<p>如果你要想推动继电器的话,建议你这里设置为20mA,我原来用这个推动固态继电器,推动不了。</p>

<p>还有我网页版看不到你的图片。你是上传world文档,还是直接编辑的。有空的话,修正一下,谢谢。</p>

javnson 发表于 2022-4-14 23:41

lugl4313820 发表于 2022-4-13 18:58
将GPIO 设为低电流推挽输出,


        GPIO_ModeOut_PP_5mA,


如果你要想推动继电器的话,建议你这里设 ...

<p>现在一般继电器模块之前都有光耦的,所以功率问题不大,5mA驱动光耦是足够的。可能是我没有交代清楚,抱歉。</p>

lugl4313820 发表于 2022-4-15 06:45

javnson 发表于 2022-4-14 23:41
现在一般继电器模块之前都有光耦的,所以功率问题不大,5mA驱动光耦是足够的。可能是我没有交代清楚,抱 ...

<p>我的是直接驱动固态继电器,所以对电流可能要求高一些,曾经排查好些时间,提示一下而。</p>

lugl4313820 发表于 2022-4-15 06:45

<p>其实你可以买个几块钱的继电器模块,这样展示就更加具有说服力。</p>

javnson 发表于 2022-4-15 23:29

<p>确实,只是现在物流实在让人崩溃,哈哈哈哈哈哈哈哈哈,等以后都会有的!</p>

javnson 发表于 2022-4-15 23:33

lugl4313820 发表于 2022-4-15 06:45
我的是直接驱动固态继电器,所以对电流可能要求高一些,曾经排查好些时间,提示一下而。

<p>谢谢呀~哈哈哈哈哈,看来都是踩了不少坑的兄弟</p>

lugl4313820 发表于 2022-4-15 23:37

javnson 发表于 2022-4-15 23:33
谢谢呀~哈哈哈哈哈,看来都是踩了不少坑的兄弟

<p>还没有休息呀,我也是刚刚调完程序,发了帖,后面两天没空调了。</p>

lugl4313820 发表于 2022-4-15 23:38

<p>其实数据不多的话,可以放在flash里,停电是保存。</p>

lugl4313820 发表于 2022-4-15 23:38

<p>不知道这个芯片有没有PVD,用这个中断来触发保存数据。stm32是有这个功能的。</p>

lugl4313820 发表于 2022-4-15 23:42

<p>可以用内部电池电压的检测来触发储存,电压低于某个值时保存状态。上电时取出状态。</p>

javnson 发表于 2022-4-18 09:19

lugl4313820 发表于 2022-4-15 23:42
可以用内部电池电压的检测来触发储存,电压低于某个值时保存状态。上电时取出状态。

<p>用IIC在平时工作过程中还有一个好处,就是可以指定初始条件,焊一个有初始条件要求的IIC芯片上去,要是不满足初始条件直接罢工,哈哈哈哈哈,不过您说的直接保存在本地FLASH的方法确实是一个更加简便的方法!</p>

<p>这款芯片也提供了一个FLASH操作的库,很方便的,之后工程应用的时候我去试试</p>

lugl4313820 发表于 2022-4-18 10:32

javnson 发表于 2022-4-18 09:19
用IIC在平时工作过程中还有一个好处,就是可以指定初始条件,焊一个有初始条件要求的IIC芯片上去,要是不 ...

<p>牛了,有想法呀。。。不交钱不给开机。。。这想法好!</p>
页: [1]
查看完整版本: 【沁恒CH582】 I2C暂存的继电器上位机控制继电器模块