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

相关推荐
hairenjing11233 小时前
使用 Mac 数据恢复从 iPhoto 图库中恢复照片
windows·stm32·嵌入式硬件·macos·word
模拟IC攻城狮5 小时前
华为海思招聘-芯片与器件设计工程师-模拟芯片方向- 机试题-真题套题题目——共8套(每套四十题)
嵌入式硬件·华为·硬件架构·芯片
IT B业生5 小时前
51单片机教程(六)- LED流水灯
单片机·嵌入式硬件·51单片机
一枝小雨5 小时前
51单片机学习心得2(基于STC89C52):串口通信(UART)
单片机·嵌入式硬件·51单片机
IT B业生6 小时前
51单片机教程(一)- 开发环境搭建
单片机·嵌入式硬件·51单片机
海绵波波1078 小时前
Webserver(4.8)UDP、广播、组播
单片机·网络协议·udp
好想有猫猫8 小时前
【51单片机】串口通信原理 + 使用
c语言·单片机·嵌入式硬件·51单片机·1024程序员节
云卓科技8 小时前
无人车之路径规划篇
人工智能·嵌入式硬件·算法·自动驾驶
stm 学习ing9 小时前
C语言 循环高级
c语言·开发语言·单片机·嵌入式硬件·算法·嵌入式实时数据库
w微信1501350781210 小时前
小华一级 代理商 HC32F005C6PA-TSSOP20 HC32F005系列
c语言·arm开发·单片机·嵌入式硬件