类比半导体ADX824驱动

#ifndef _ADX824_H_
#define _ADX824_H_

#define ADX82X_PWDN_HIGH        
#define ADX82X_PWDN_LOW            
#define ADX82X_RESET_HIGH        
#define ADX82X_RESET_LOW        

#define ADX82X_DRDY_READ        

#define    SPI_NSS_L                        
#define    SPI_NSS_H                        

#define CMD_WAKEUP                    0x00
#define CMD_STANDBY                    0x02
#define CMD_SYNC                        0x04
#define    CMD_RESET                        0X06
#define CMD_RDATAC                    0X10
#define CMD_SDATAC                    0x11
#define    CMD_RDATA                        0x12
#define    CMD_RREG                        0x20
#define CMD_WREG                        0x40
#define CMD_OFSCAL                    0x60
#define    CMD_GANCAL                    0x61

#define REG_ID_CFG                    0X00
#define    CONFIG0                            0X01
#define    CONFIG1                            0X02
#define    HPF0                                0X03
#define    HPF1                                0X04
#define    OFC0                                0X05            
#define    OFC1                                0X06        
#define    OFC2                                0X07    
#define    FSC0                                0X08    
#define    FSC1                                0X09    
#define    FSC2                                0X0A    

//config0 01h
#define Pulse_sync_mode                        0x00
#define Continuous_sync_mode            0x80
#define    Low_power_mode                        0x00                    
#define    High_resolution_mode            0x40    
#define DR_250sps                                    0x00
#define    DR_500sps                                    0x08    
#define    DR_1000sps                                0x10
#define    DR_2000sps                                0x18
#define    DR_4000sps                                0x20
#define Linear_phase                            0x00
#define Minimum_phase                            0x04
#define    FILTR_Reserved                        0x00
#define    FILTR_Sinc                                0x01
#define    FILTR_Sinc_LPF                        0x02
#define    FILTR_Sinc_LPF_HPF                0x03

//config1 02h
#define MUX_AINP1_N1                            0X00
#define MUX_AINP2_N2                            0X10
#define MUX_Shout_400_res                    0X20
#define MUX_Shout_AIN1_CONN_AIN2    0X30
#define MUX_EXT_Shout_AINN2                0X40
#define PGA_CHOP_DISABLE                    0X00
#define PGA_CHOP_ENABLE                        0X08
#define PGA_GAIN_1                                0x00
#define PGA_GAIN_2                                0x01
#define PGA_GAIN_4                                0x02
#define PGA_GAIN_8                                0x03
#define PGA_GAIN_16                                0x04
#define PGA_GAIN_32                                0x05
#define PGA_GAIN_64                                0x06

void ADX824_Init(void);
void ADX824_WriteCMD(uint8_t CMD);
void ADX824_SetReg(uint8_t regAddess,uint8_t *sbuff,uint8_t size);
void ADX824_ReadReg(uint8_t regAddess,uint8_t *rbuff,uint8_t size);
uint32_t ADX824_ReadValue(void);

#endif

类比半导体ADX82X 驱动

#include "main.h"
#include "main_thread.h"
#include "set.h"
#include "ADX824.H"


/*****************************************
1 delay(TCSSC) > 40ns
2 delay(TSCLKDLY) > (24 * (1 /fclk)) 
3 delay(TSCSC)  > (24 * (1 /fclk))
4 Send the CMD_RDATAC before using ADX824_ReadValue
*****************************************/


/*
Function:       ADX824_Init
Description:    ADX824 GPIO and SPI initialization  
Input :         null
return:         null
*/
void ADX824_Init(void)
{
    // RESET/PWDN/RESET definition
    GPIO_Init();
    ADX82X_PWDN_HIGH;
    ADX82X_RESET_HIGH;
    // SPI INIT   CPOL = LOW/CPHA = 1EDGE/BaudRate 2Mhz
    ADX824_SPI_Init();
}

/*
Function:       ADX824_WriteCMD
Description:    ADX824 Write cmd 
Input :         CMD
return:         null
*/
void ADX824_WriteCMD(uint8_t CMD)
{
    uint8_t r;
    SPI_NSS_L;
    delay(TCSSC);
    ADX824_SPI_TransmitReceive_ReadSetREG(&CMD,&r,1);
    delay(TSCSC);
    SPI_NSS_H;
}

/*
Function:       ADX824_SetReg
Description:    ADX824 Write register
Input :         regAddess : Need to write the first address of the register
                                sbuff : Write reg value
                                size:        Write the number of registers
return:         null
*/
void ADX824_SetReg(uint8_t regAddess,uint8_t *sbuff,uint8_t size)
{
    uint8_t p[3],r[20],i;
    
    p[0] = CMD_SDATAC;
    p[1] = CMD_WREG | regAddess;
    p[2] = size - 1;
    SPI_NSS_L;
    delay(TCSSC);
    for(i = 0;i < 3;i ++)
    {
        ADX824_SPI_TransmitReceive_ReadSetREG(&p[i],&r[i],1);
        delay(TSCLKDLY);
    }
    for(i = 0;i < size;i ++)
    {
        ADX824_SPI_TransmitReceive_ReadSetREG(&sbuff[i],&r[i],1);
        delay(TSCLKDLY);
    }
    delay(TSCSC);
    SPI_NSS_H;
}

/*
Function:       ADX824_ReadReg
Description:    ADX824 read register
Input :         regAddess : Need to read the first address of the register
                                sbuff : Read register save buff
                                size:        read the number of registers
return:         null
*/
void ADX824_ReadReg(uint8_t regAddess,uint8_t *rbuff,uint8_t size)
{
    uint8_t p[3],r[20],i;
    
    p[0] = CMD_SDATAC;
    p[1] = CMD_RREG | regAddess;
    p[2] = size - 1;
    SPI_NSS_L;
    delay(TCSSC);
    for(i = 0;i < 3;i ++)
    {
        ADX824_SPI_TransmitReceive_ReadSetREG(&p[i],&r[i],1);
        delay(TSCLKDLY);
    }
    for(i = 0;i < size;i ++)
    {
        ADX824_SPI_TransmitReceive_ReadSetREG(&r[i],&rbuff[i],1);
        delay(TSCLKDLY);
    }
    delay(TSCSC);
    SPI_NSS_H;
}

/*
Function:       ADX824_ReadValue
Description:    ADX824 read value
Input :         null
return:         read value
*/
uint32_t ADX824_ReadValue(void)
{
    uint8_t p[4],r[4];
    uint32_t  value;
    SPI_NSS_L;
    ADX824_SPI_TransmitReceive_ReadValue(p,r,4);
    SPI_NSS_H;
    value = (uint32_t)(r[0] << 24) + (uint32_t)(r[1] << 16) + (uint32_t)(r[2] << 8) + (uint32_t)(r[3]);
    return value;
}

void ADX824_Test(void)
{
    uint32_t Value;
    uint8_t config0;
    ADX824_Init();
    config0 = Pulse_sync_mode | High_resolution_mode | DR_500sps | Linear_phase | FILTR_Sinc_LPF;
    ADX824_SetReg(CONFIG0,&config0,1);
    ADX824_WriteCMD(CMD_RDATAC);
    while(1)
    {
        if(ADX82X_DRDY_READ == 0)
        {
            Value = ADX824_ReadValue();
        }
    }
}

 

posted @ 2023-09-12 15:19  xuyuntong  阅读(64)  评论(0编辑  收藏  举报