chenzhufly 发表于 2020-3-16 13:26

“玩板”+ 共享单车控制板-时钟芯片PCF8563

<div class='showpostmsg'><p><span style="font-size:20px;">板子左边一个GPS,右边一个GPRS,难度稍微有点大,柿子还是要捡软的捏,先把时钟芯片PCF8563 搞定吧</span></p>

<p><span style="font-size:20px;"></span></p>

<p><span style="font-size:20px;">首先用stm32cube做好管脚配置,PCF8563使用的是I2C1接口</span></p>

<p><span style="font-size:20px;">然后从网上找了个PCF8563的驱动:</span></p>

<pre>
<code>#include "main.h"
#include "pcf8563.h"

I2C_HandleTypeDef *hi2c_pcf8563;
uint8_t Pcf8563Buffer;


void WriteBitToControlRegister(uint8_t ControlRegister, uint8_t BitNumber, uint8_t Value)
{
        uint8_t tmp;

        if(Value&gt;1) Value = 1;
        if(ControlRegister&gt;1) Value = 1;
        if(BitNumber&gt;7) Value = 7;

        HAL_I2C_Mem_Read(hi2c_pcf8563, PCF8563_ADDRESS, ControlRegister?PCF8563_REG_CONTROL_STATUS1:PCF8563_REG_CONTROL_STATUS2, 1, &amp;tmp, 1, PCF8563_I2C_TIMEOUT);
        tmp &amp;= ~(1&lt;&lt;BitNumber);
        tmp |= (Value&lt;&lt;BitNumber);
//        ControlRegister?(Value &amp;= 0b10101000):(Value &amp;= 0b00011111); // Put zeros where zero is needed
        HAL_I2C_Mem_Write(hi2c_pcf8563, PCF8563_ADDRESS, ControlRegister?PCF8563_REG_CONTROL_STATUS1:PCF8563_REG_CONTROL_STATUS2, 1, &amp;Value, 1, PCF8563_I2C_TIMEOUT);
}

void PCF8563_TEST1Enable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL1_TEST1, Enable);
}

void PCF8563_STOPEnable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL1_STOP, Enable);
}

void PCF8563_TESTCEnable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL1_TESTC, Enable);
}

void PCF8563_InterruptEnable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL2_TI_TP, Enable);
}

void PCF8563_AlarmFlagEnable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL2_AF, Enable);
}

void PCF8563_TimerFlagEnable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL2_TF, Enable);
}

void PCF8563_AlarmInterruptEnable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL2_AIE, Enable);
}

void PCF8563_TimerInterruptEnable(uint8_t Enable)
{
        WriteBitToControlRegister(PCF8563_REG_CONTROL_STATUS1, PCF8563_CONTROL2_TIE, Enable);
}

void PCF8563_ClkoutFrequency(CLKOUT_Freq Frequency)
{
        uint8_t tmp;

        if(Frequency&gt;3) Frequency = 3;

        HAL_I2C_Mem_Read(hi2c_pcf8563, PCF8563_ADDRESS, PCF8563_REG_CLKOUT, 1, &amp;tmp, 1, PCF8563_I2C_TIMEOUT);
        tmp &amp;= ~(3&lt;&lt;PCF8563_CLKOUT_CONTROL_FD0);
        tmp |= (Frequency&lt;&lt;PCF8563_CLKOUT_CONTROL_FD0);
        HAL_I2C_Mem_Write(hi2c_pcf8563, PCF8563_ADDRESS, PCF8563_REG_CLKOUT, 1, &amp;tmp, 1, PCF8563_I2C_TIMEOUT);
}

uint8_t bcd2dec(uint8_t BCD)
{
        return (((BCD &amp; 0xF0)&gt;&gt;4) *10) + (BCD &amp; 0xF);
}

uint8_t dec2bcd(uint8_t DEC)
{
        return ((DEC / 10)&lt;&lt;4) + (DEC % 10);
}

int dayofweek(int Day, int Month, int Year)
{
    int Y, C, M, N, D;
    M = 1 + (9 + Month) % 12;
    Y = Year - (M &gt; 10);
    C = Y / 100;
    D = Y % 100;
    N = ((13 * M - 1) / 5 + D + D / 4 + 6 * C + Day + 5) % 7;
    return (7 + N) % 7;
}

void PCF8563_SetDateTime(RTCDateTime *DateTime)
{
        uint8_t tmp;

        if(DateTime-&gt;Second &gt; 59) DateTime-&gt;Second = 59;
        if(DateTime-&gt;Minute &gt; 59) DateTime-&gt;Minute = 59;
        if(DateTime-&gt;Hour &gt; 23) DateTime-&gt;Hour = 23;
        if(DateTime-&gt;Day &gt; 31) DateTime-&gt;Day = 31;
        if(DateTime-&gt;Month &gt; 12) DateTime-&gt;Month = 12;
        if(DateTime-&gt;Year&gt; 2099) DateTime-&gt;Year = 2099;

        tmp = dec2bcd((DateTime-&gt;Second) &amp; 0x7F);
        tmp = dec2bcd(DateTime-&gt;Minute);
        tmp = dec2bcd(DateTime-&gt;Hour);
        tmp = dec2bcd(DateTime-&gt;Day);
        tmp = dayofweek(DateTime-&gt;Day, DateTime-&gt;Month, DateTime-&gt;Year);
        tmp = dec2bcd(DateTime-&gt;Month);
        tmp = dec2bcd(DateTime-&gt;Year - 2000);

        HAL_I2C_Mem_Write(hi2c_pcf8563, PCF8563_ADDRESS, PCF8563_REG_TIME, 1, tmp, 7, PCF8563_I2C_TIMEOUT);
}

void PCF8563_CalculateDateTime(RTCDateTime *DateTime)
{
        DateTime-&gt;Second = bcd2dec((Pcf8563Buffer) &amp; 0x7F);
        DateTime-&gt;Minute = bcd2dec(Pcf8563Buffer);
        DateTime-&gt;Hour = bcd2dec(Pcf8563Buffer);
        DateTime-&gt;Day = Pcf8563Buffer;
        DateTime-&gt;DayOfWeek = bcd2dec(Pcf8563Buffer + 1); // too keep weekdays in 1-7 format
        DateTime-&gt;Month = bcd2dec(Pcf8563Buffer &amp; 0x1F);
        DateTime-&gt;Year = 2000 + bcd2dec(Pcf8563Buffer);
}

#ifdef PCF8563_USE_DMA
void PCF8563_ReceiveDateTimeDMA(void)
{
        HAL_I2C_Mem_Read_DMA(hi2c_pcf8563, PCF8563_ADDRESS, PCF8563_REG_TIME, 1, Pcf8563Buffer, 7);
}
#else
void PCF8563_GetDateTime(RTCDateTime *DateTime)
{
        HAL_I2C_Mem_Read(hi2c_pcf8563, PCF8563_ADDRESS, PCF8563_REG_TIME, 1, Pcf8563Buffer, 7, PCF8563_I2C_TIMEOUT);

PCF8563_CalculateDateTime(DateTime);
}
#endif

void PCF8563_Init(I2C_HandleTypeDef *hi2c)
{
        hi2c_pcf8563 = hi2c;

        PCF8563_ClkoutFrequency(CLKOUT_FREQ_1HZ);
        PCF8563_STOPEnable(0);
}</code></pre>

<p>&nbsp;</p>

<p><span style="font-size:20px;">写了个简单的测试代码:</span></p>

<pre>
<code>int main(void)
{
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */


/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
PCF8563_Init(&amp;hi2c1);
       
r.Hour = 12;
r.Minute = 30;
r.Second = 0;
PCF8563_SetDateTime(&amp;r);

printf("====pcf8563 test====\n\r");
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
               
        PCF8563_GetDateTime(&amp;r);
        MessageSize = sprintf(Message, "%02d:%02d:%02d\n\r", r.Hour, r.Minute, r.Second);
        HAL_UART_Transmit(&amp;huart2, Message, MessageSize, 1000);

        LED1_Toggle;
    HAL_Delay(1000);
               
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}</code></pre>

<p>&nbsp;</p>

<p></p>

<p>&nbsp;</p>

<p><span style="color:#3498db;"><span style="font-size:20px;">SIM800C目前也是有点反应,能够收到和响应AT命令,还在摸索中。。</span></span></p>

<p>&nbsp;</p>

<p><br />
<br />
<b><font color="#5E7384">此内容由EEWORLD论坛网友<font size="3">chenzhufly</font>原创,如需转载或用于商业用途需征得作者同意并注明出处</font></b></p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

threetigher 发表于 2020-3-16 13:40

<p>lz测试一下卡,看看是否还有流量</p>

threetigher 发表于 2020-3-16 13:41

<p>有网友说3个脚是升压电路,这里还有点问号</p>

threetigher 发表于 2020-3-16 13:42

<p>点赞支持!</p>

threetigher 发表于 2020-3-16 20:54

<p>AT有反应了?666</p>
页: [1]
查看完整版本: “玩板”+ 共享单车控制板-时钟芯片PCF8563