【单片机】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封装:

相关推荐
析木不会编程2 小时前
【小白51单片机专用教程】protues仿真独立按键控制LED
单片机·嵌入式硬件·51单片机
枯无穷肉6 小时前
stm32制作CAN适配器4--WinUsb的使用
stm32·单片机·嵌入式硬件
不过四级不改名6776 小时前
基于HAL库的stm32的can收发实验
stm32·单片机·嵌入式硬件
嵌入式大圣7 小时前
单片机UDP数据透传
单片机·嵌入式硬件·udp
云山工作室7 小时前
基于单片机的视力保护及身姿矫正器设计(论文+源码)
stm32·单片机·嵌入式硬件·毕业设计·毕设
嵌入式-老费7 小时前
基于海思soc的智能产品开发(mcu读保护的设置)
单片机·嵌入式硬件
qq_397562319 小时前
MPU6050 , 设置内部低通滤波器,对于输出数据的影响。(简单实验)
单片机
liyinuo20179 小时前
嵌入式(单片机方向)面试题总结
嵌入式硬件·设计模式·面试·设计规范
艺术家天选9 小时前
STM32点亮LED灯
stm32·单片机·嵌入式硬件
向阳逐梦9 小时前
基于STM32F4单片机实现ROS机器人主板
stm32·单片机·机器人