一、MAX535
max535是低功耗、13位、电压输出DAC,采用串行接口和µMAX封装。MAX535通过单个+5V/3.3V电源工作,仅需要280µA电流工作。
MAX535/MAX5351适用于广泛的应用,包括工业过程控制。其他功能包括软件关机和开机复位。
DAC寄存器可以独立更新,也可以与输入寄存器同时更新。输入是TTL/CMOS逻辑兼容,采用了缓冲施密特触发器允许直接与光电耦合器接口。
二、驱动代码
采用模拟SPI方式实现对MAX535的控制。
头文件:
/** FILE : max535.h* DESCRIPTION : This file is MAX535/MAX5351 spi header* Author : JackWang*/
#ifndef __MAX535__
#define __MAX535__//------------------- Typedef --------------------------//
typedef struct{int (* initialize)(void);int (* write)(unsigned short Dignum);
}MAX535_T;//------------------- Extern --------------------------//
extern MAX535_T max535;#endif
实现文件:
/** FILE : max535.c* DESCRIPTION : This file is MAX535/MAX5351 spi driver.* Author : JackWang*///--------------------------- Include ---------------------------//
#include "max535.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"//------------------- Define ----------------------------//
#define MAX535_CS_L GPIO_ResetBits(GPIOH, GPIO_Pin_7)
#define MAX535_CS_H GPIO_SetBits(GPIOH, GPIO_Pin_7)#define MAX535_DIN_L GPIO_ResetBits(GPIOH, GPIO_Pin_6)
#define MAX535_DIN_H GPIO_SetBits(GPIOH, GPIO_Pin_6)#define MAX535_CLK_L GPIO_ResetBits(GPIOH, GPIO_Pin_9)
#define MAX535_CLK_H GPIO_SetBits(GPIOH, GPIO_Pin_9)//---------------- Function Prototype ------------------//
static int initialize_max535(void);
static int write(unsigned short Dignum);//-----------------Variable-----------------------------//
MAX535_T max535 = {.initialize = initialize_max535,.write = write
};/*!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!*/static int
initialize_max535(void)
{GPIO_InitTypeDef GPIO_uInitStructure;//PH7-->MAX535_CS; PH6-->MAX535_DIN; PH9-->MAX535_SCLKRCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH,ENABLE);GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_9 | GPIO_Pin_6;GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_OUT;GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;;GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOH, &GPIO_uInitStructure);MAX535_CS_L;MAX535_CS_H;MAX535_DIN_L;MAX535_CLK_L;return 0;
}/*!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!*/
static int
write(unsigned short Dignum)
{unsigned short Dig = 0;unsigned char i = 0;int time;MAX535_CS_L;MAX535_CLK_H;for (int idx = 0; idx < 16; ++idx) {Dig = Dignum & 0x8000;if (Dig) { MAX535_DIN_H; }else { MAX535_DIN_L; }for (int delayTime = 0; delayTime < 100; ++delayTime );//for delay onlyMAX535_CLK_L;Dignum <<= 1;for (int delayTime = 0; delayTime < 100; ++delayTime );//for delay onlyMAX535_CLK_H;}MAX535_CLK_L;MAX535_CS_H;for (int delayTime = 0; delayTime < 1000; ++delayTime );//for delay onlyreturn 0;
}