【单片机】DS2431芯片,读写128个字节,程序

ds2431p+t&r

stm32读写程序:

部分程序:

cpp 复制代码
#include "sys.h"
#include "delay.h"
#include "usart.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sys.h"
#include "DS2431.h"

unsigned char serialNb[8];
unsigned char read_data[129] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                111, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                11, 2, 3, 4, 5, 6, 7, 0xff};

unsigned char write_data[129] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 111, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 11, 2, 3, 4, 5, 6, 7, 0xff};
unsigned char status;
unsigned char debug = 0; /* 0: release, 1: debug Õý³£¸ø0 */

u8 tim_data_finish_status = 0;
u8 start_tim = 0;
u8 rev_data_uart_buffer[600];
int rev_data_uart_buffer_cnt = 0;

int rev_counter_deal = 0;//´¦ÀíµÄʱºòµÄ¼ÆÊýÆ÷
u8 rev_data_tmp = 0; //´¦ÀíµÄʱºòµÄÁÙʱ±äÁ¿
u8 rev_sm_cnt = 0; //´¦ÀíµÄʱºòµÄ¼ÆÊýÆ÷
u8 check_ifg = 0; //´¦ÀíµÄʱºòÊÇ·ñÐèÒª´¦Àí

u8 retry = 3;//ÖØÊÔ´ÎÊý
u8 success_all = 0;//ÊÇ·ñ³É¹¦

int main(void) {
    int idx = 0;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                       /* ÉèÖÃNVICÖжϷÖ×é2:2Î>>ÇÀÕ¼ÓÅÏȼ¶£¬2Î>>ÏìÓ¦ÓÅÏȼ¶ */
    delay_init();                                                           /* ÑÓʱº¯Êý³õʼ>>¯ */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);              /* ¹Ø±Õjtag£¬Ê¹ÄÜSWD£¬¿ÉÒÔÓÃSWDģʽµ÷ÊÔ */
    delay_ms(500);                                                        /* µÈ´ýÎȶ¨ */
    uart_init(115200);                                                    /* uart */

    printf("************************************************ \r\n");
    printf("************************************************ \r\n");
    if (debug == 0) {
        while (1) {
            status = DS2431_Init();
            printf("DS2431_Init status: %d\r\n", status);
            if (DS2431_ReadRom(serialNb) == 0) {
                printf("ÒѾ­¶ÁÈ¡µ½ÐòÁÐÂë: %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
                       serialNb[7],
                       serialNb[6],
                       serialNb[5],
                       serialNb[4],
                       serialNb[3],
                       serialNb[2],
                       serialNb[1],
                       serialNb[0]);
                /* Check serial number CRC */
                if (crc8(serialNb, 7) != serialNb[7]) {
                    printf("CRCУÑé²>>ͨ¹ý£¬Ð¾Æ¬ÓÐÎÊÌâ.\r\n");
                } else {
                    printf("CRC8 УÑéͨ¹ý£¬Æ÷¼þ´¦ÓÚÕý³£¹¤×÷ÖС£ \r\n");
                    break;
                }
            } else {
                printf("Ã>>ÓжÁÈ¡µ½ÐòÁÐÂ룬ÇëÁ¬½ÓÆ÷¼þ¡£ \r\n");
            }
            delay_ms(1000);
        }
    }


    delay_ms(100);
    printf("************************************************ \r\n");
    printf("************************************************ \r\n");
    printf("¶ÁÈ¡128¸ö×Ö½ÚÖС£¡£¡£ \r\n");
    if (debug == 0) {
        status = DS2431_ReadMemory(0, 128, read_data);        /* ´ÓµØÖ·0¿ªÊ¼¶ÁÈ¡£¬¶ÁÈ¡128¸ö×Ö½Ú */
    } else {
        status = 0;                                             /* 0: success */
    }
    /* ´òÓ¡³ö¶ÁÈ¡µ½µÄÊý¾Ý */
    if (status == 0) {
        printf("¶ÁÈ¡µ½µÄÊý¾ÝΪ(16¿é£¬Ã¿¿é8¸ö×Ö½Ú)£º \r\n");
        for (idx = 0; idx < 128; idx++) {
            printf("%02X ", read_data[idx]);
            if ((idx + 1) % 8 == 0) {
                printf(" ");
            }
            if ((idx + 1) % 16 == 0) {
                printf("\r\n");
            }
        }
        printf("\r\n");
    } else {
        printf("¶Áȡʧ°Ü£¬´íÎóÂëΪ%d¡£ \r\n", status);
    }
    printf("************************************************ \r\n");
    printf("************************************************ \r\n");

    //¶ÁÈ¡ÆäËûµØÖ·µÄÊý¾Ý
    memset(read_data, 0, sizeof(read_data));
    DS2431_ReadMemory(0x80, 8, read_data);
    printf("µØÖ·0080h Êý¾Ý0x%02X (±£>>¤Ò³0)\r\n", read_data[0]);
    printf("µØÖ·0081h Êý¾Ý0x%02X (±£>>¤Ò³1)\r\n", read_data[1]);
    printf("µØÖ·0082h Êý¾Ý0x%02X (±£>>¤Ò³2)\r\n", read_data[2]);
    printf("µØÖ·0083h Êý¾Ý0x%02X (±£>>¤Ò³3)\r\n", read_data[3]);
    printf("µØÖ·0084h Êý¾Ý0x%02X (¸´ÖƱ£>>¤)\r\n", read_data[4]);
    printf("µØÖ·0085h Êý¾Ý0x%02X (¹¤³§)\r\n", read_data[5]);
    printf("µØÖ·0086h Êý¾Ý0x%02X (ÓÃ>>§1)\r\n", read_data[6]);
    printf("µØÖ·0087h Êý¾Ý0x%02X (ÓÃ>>§2)\r\n", read_data[7]);
    printf("************************************************ \r\n");
    printf("************************************************ \r\n");


    delay_ms(100);
    printf("************************************************ \r\n");
    printf("************************************************ \r\n");
    printf("дÈë128¸ö×Ö½ÚÖС£¡£¡£ \r\n");
    for (idx = 0; idx < 16; idx++) {
        status = DS2431_WriteMemory(idx, write_data + idx * 8);
        if (status == 0) {
            printf("ds2431µÄµÚ%02d¸ö¿éдÈë³É¹¦£¬Ã¿¸ö¿éÓÐ8¸ö×Ö½Ú¡£ \r\n", idx);
        } else {
            printf("ds2431µÄµÚ%02d¸ö¿éдÈëʧ°Ü£¬´íÎóÂëΪ%d¡£ \r\n", idx, status);
        }
    }


    delay_ms(100);
    printf("************************************************ \r\n");
    printf("************************************************ \r\n");
    printf("¶ÁÈ¡128¸ö×Ö½ÚÖС£¡£¡£ \r\n");
    if (debug == 0) {
        status = DS2431_ReadMemory(0, 128, read_data);        /* ´ÓµØÖ·0¿ªÊ¼¶ÁÈ¡£¬¶ÁÈ¡128¸ö×Ö½Ú */
    } else {
        status = 0;                                             /* 0: success */
    }
    /* ´òÓ¡³ö¶ÁÈ¡µ½µÄÊý¾Ý */
    if (status == 0) {
        printf("¶ÁÈ¡µ½µÄÊý¾ÝΪ(16¿é£¬Ã¿¿é8¸ö×Ö½Ú)£º \r\n");
        for (idx = 0; idx < 128; idx++) {
            printf("%02X ", read_data[idx]);
            if ((idx + 1) % 8 == 0) {
                printf(" ");
            }
            if ((idx + 1) % 16 == 0) {
                printf("\r\n");
            }
        }
        printf("\r\n");
    } else {
        printf("¶Áȡʧ°Ü£¬´íÎóÂëΪ%d¡£ \r\n", status);
    }
    printf("************************************************ \r\n");
    printf("************************************************ \r\n");

    while (1) {

    }
}


void USART1_IRQHandler(void)                                          /* ´®¿Ú1ÖжϷþÎñ³ÌÐò */
{
    u8 Res;
    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)      /* ½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ) */
    {
        Res = USART_ReceiveData(USART1);                      /* ¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý */

    }
}

全部程序

ds2431p+t&r 是这个封装:

TSOC封装:

相关推荐
网易独家音乐人Mike Zhou5 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
zy张起灵5 小时前
48v72v-100v转12v 10A大功率转换电源方案CSM3100SK
经验分享·嵌入式硬件·硬件工程
lantiandianzi12 小时前
基于单片机的多功能跑步机控制系统
单片机·嵌入式硬件
哔哥哔特商务网12 小时前
高集成的MCU方案已成电机应用趋势?
单片机·嵌入式硬件
跟着杰哥学嵌入式12 小时前
单片机进阶硬件部分_day2_项目实践
单片机·嵌入式硬件
电子科技圈13 小时前
IAR与鸿轩科技共同推进汽车未来
科技·嵌入式硬件·mcu·汽车
东芝、铠侠总代1361006839314 小时前
浅谈TLP184小型平面光耦
单片机·嵌入式硬件·物联网·平面
lantiandianzi14 小时前
基于单片机中医药柜管理系统的设计
单片机·嵌入式硬件
嵌入式知识大讲堂14 小时前
HDMI数据传输三种使用场景
单片机
黑客呀15 小时前
[系统安全]Rootkit基础
stm32·单片机·系统安全