- MCP4922 :12 位双通道 DAC
时序
引脚
电压输出
寄存器
程序
#include "MCP4922.H"
//========================================================================
// 描述: SPI初始化.
// 参数: none.
// 返回: none.
//========================================================================
void MCP4922_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //结构定义
GPIO_InitStructure.Pin = GPIO_Pin_0|GPIO_Pin_1 |GPIO_Pin_2|GPIO_Pin_3 ; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
GPIO_InitStructure.Mode = GPIO_OUT_PP; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
GPIO_Inilize(GPIO_P2,&GPIO_InitStructure); //初始化
// SOFTSPI_SCK_MODE;
// SOFTSPI_MISO_MODE;
// SOFTSPI_MOSI_MODE;
MCP4922_CS = 1;
MCP4922_LD = 1;
SOFTSPI_SCK_PIN = 0; //set clock to low initial state
}
//========================================================================
// 描述: SPI写入一个字节后读取一个字节数据.
// 参数: none.
// 返回: none.
//========================================================================
u8 softspi_wr_data(u16 dat)
{
u16 datavalue=0,i,wr_data;
wr_data = dat;
#if SOFTSPI_CPHA==0 && SOFTSPI_CPOL==0
for(i=0;i<16;i++){
if(wr_data&0x8000){
SOFTSPI_MOSI_PIN = 1;
}else{
SOFTSPI_MOSI_PIN = 0;
}
wr_data <<=1;
SOFTSPI_SCK_PIN = 1;
datavalue <<=1;
if(SOFTSPI_MISO_PIN == 1){
datavalue |= 0x01;
}
SOFTSPI_SCK_PIN = 0;
//delay(1);
}
#elif SOFTSPI_CPHA==0 && SOFTSPI_CPOL==1
for(i=0;i<16;i++){
if(wr_data&0x8000){
SOFTSPI_MOSI_PIN = 1;
}else{
SOFTSPI_MOSI_PIN = 0;
}
wr_data <<=1;
SOFTSPI_SCK_PIN = 0;
datavalue <<=1;
if(SOFTSPI_MISO_PIN == 1){
datavalue |= 0x01;
}
SOFTSPI_SCK_PIN = 1;
//delay(1);
}
#elif SOFTSPI_CPHA==1 && SOFTSPI_CPOL==0
for(i=0;i<16;i++){
SOFTSPI_SCK_PIN = 1;
if(wr_data&0x8000){
SOFTSPI_MOSI_PIN = 1;
}else{
SOFTSPI_MOSI_PIN = 0;
}
wr_data <<=1;
SOFTSPI_SCK_PIN = 0;
datavalue <<=1;
if(SOFTSPI_MISO_PIN == 1){
datavalue |= 0x01;
}
//delay(1);
}
#elif SOFTSPI_CPHA==1 && SOFTSPI_CPOL==1
for(i=0;i<16;i++){
SOFTSPI_SCK_PIN = 0;
if(wr_data&0x80){
SOFTSPI_MOSI_PIN = 1;
}else{
SOFTSPI_MOSI_PIN = 0;
}
wr_data <<=1;
SOFTSPI_SCK_PIN = 1;
datavalue <<=1;
if(SOFTSPI_MISO_PIN == 1){
datavalue |= 0x01;
}
SOFTSPI_SCK_PIN = 1;
// delay(1);
}
#endif
return datavalue;
}
//========================================================================
// 描述: SPI写一个字节.
// 参数: none.
// 返回: none.
//========================================================================
void softspi_write_byte(u8 out)
{
softspi_wr_data(out);
}
//========================================================================
// 描述: SPI读一个字节.
// 参数: none.
// 返回: none.
//========================================================================
u8 softspi_read_byte(void)
{
return softspi_wr_data(0xff);;
}
#define MCP4922_VREF 3.3
void MCP4922_SET(u8 channel,u8 gain,float dac_out)
{
u16 dat = 0x1000; //打开输出
u16 d_dat=0;
if(channel == 1) //选择DACB
dat |= 0x8000;
if(gain == 1) //默认2x
{
dat |= 0x2000;
d_dat = dac_out / MCP4922_VREF * 4096;
}
else
{
d_dat = dac_out / MCP4922_VREF * 4096 / 2;
}
//d_dat = 4095-50;
dat |= d_dat;
MCP4922_CS = 0;
MCP4922_LD = 1;
softspi_wr_data(dat);
MCP4922_LD = 0;
MCP4922_CS = 1;
}
#ifndef __MCP4922_H_
#define __MCP4922_H_
#include "config.h"
#include "STC8A_Delay.h"
#include "STC8A_GPIO.h"
sbit MCP4922_CS = P2^3;
sbit MCP4922_SCLK = P2^2;
sbit MCP4922_SDI = P2^1;
sbit MCP4922_LD = P2^0;
#ifndef SOFTSPI_SCK_PIN
#define SOFTSPI_SCK_PIN P22
#endif
#ifndef SOFTSPI_MISO_PIN
#define SOFTSPI_MISO_PIN P21
#endif
#ifndef SOFTSPI_MOSI_PIN
#define SOFTSPI_MOSI_PIN P21
#endif
#define SOFTSPI_CPHA 0
#define SOFTSPI_CPOL 0
void MCP4922_init();//初始化
u8 softspi_wr_data(u16 dat);
//void softspi_write_byte(u8 out);
//u8 softspi_read_byte(void);
void MCP4922_SET(u8 channel,u8 gain,float dac_out);
#endif
void main(void)
{
GPIO_config();
UART_config();
Hmi_Init();
MCP4922_init();
EA = 1;
while(1)
{
instructionProcessing();
MCP4922_SET(0,1,3.2);
MCP4922_SET(1,1,1.0);
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于