高精度数模转换器DAC9881驱动程序
DAC9881.h
#ifndef __DAC9881_H
#define __DAC9881_H
#include "stm32f4xx.h"
#include "stm32f4xx_sys.h"
#include "delay.h"
#define CS_DAC_LOW() PBout(12) = 0
#define CS_DAC_HIGH() PBout(12) = 1
#define SCL_DAC_LOW() PBout(13) = 0
#define SCL_DAC_HIGH() PBout(13) = 1
#define SDO_DAC_LOW() PBin(14) = 0
#define SDO_DAC_HIGH() PBin(14) = 1
#define LDAC_DAC_LOW() PBout(11) = 0
#define LDAC_DAC_HIGH() PBout(11) = 1
#define SDI_DAC_LOW() PBout(15)=0
#define SDI_DAC_HIGH() PBout(15)=1
void DAC9881_Init(void);
void WriteToDAC9881(u8 count, u8 *buf);
#endif
DAC9881.c
#include "DAC9881.h"
#include "usart.h"
void DAC9881_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_1|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void WriteToDAC9881(u8 count, u8 *buf)
{
u8 ValueToWrite = 0;
u8 i = 0;
u8 j = 0;
LDAC_DAC_LOW();
delay_us(1);
CS_DAC_LOW();
delay_us(1);
for(i=0;i<count;i++)
{
ValueToWrite = *(buf + i);
for(j=0; j<8; j++)
{
delay_us(1);
SCL_DAC_LOW();
if(0x80 == (ValueToWrite & 0x80))
{
SDI_DAC_HIGH();
}
else
{
SDI_DAC_LOW();
}
delay_us(1);
SCL_DAC_HIGH();
ValueToWrite <<= 1;
}
}
CS_DAC_HIGH();
}