AD5791.rar

  • yisuoyanyuwhr
    了解作者
  • C/C++
    开发工具
  • 2.4KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-12-27 22:15
    上传日期
AD5791,驱动,stm32f407平台
AD5791.rar
  • AD5791
  • AD5791.h
    1.8KB
  • AD5791.c
    7.1KB
内容介绍
#include "AD5791.h" #include "spi.h" #include "delay.h" #include "uart.h" ////////////////////////////////////////////////////////////////////////////////// #define CLR_TYPE GPIOA #define CLR_GPIO GPIO_Pin_5 #define RST_TYPE GPIOA #define RST_GPIO GPIO_Pin_6 #define LDAC_TYPE GPIOG #define LDAC_GPIO GPIO_Pin_8 unsigned long int AD5791Registers[5]={0,0,0,0,0}; //Register Map and bit definition //DAC Register void AD5791HardwareReset() { GPIO_ResetBits(RST_TYPE, RST_GPIO); GPIO_SetBits(RST_TYPE, RST_GPIO); delay_us(10); } void AD5791HardwareClear() { GPIO_ResetBits(CLR_TYPE, RST_GPIO); GPIO_SetBits(CLR_TYPE, RST_GPIO); } void AD5791HardwareLoad() { GPIO_ResetBits(LDAC_TYPE, RST_GPIO); GPIO_SetBits(LDAC_TYPE, RST_GPIO); } void AD5791Initialization() { AD5791HardwareReset(); AD5791HardwareClear(); } void AD5791_Channel_ENABLE(u8 ch) { switch (ch) { case 1: AD5791_CS1_ENABLE(); break; case 2: AD5791_CS2_ENABLE(); break; case 3: AD5791_CS3_ENABLE(); break; case 4: AD5791_CS4_ENABLE(); break; case 5: AD5791_CS5_ENABLE(); break; case 6: AD5791_CS6_ENABLE(); break; case 7: AD5791_CS7_ENABLE(); break; case 8: AD5791_CS8_ENABLE(); break; default: break; } } void AD5791_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; // GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOB时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);//使能GPIOG时钟 GPIO_InitStructure.GPIO_Pin = CLR_GPIO; //CS1 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(CLR_TYPE, &GPIO_InitStructure); //GPIO_SetBits(CLR_TYPE, CLR_GPIO); GPIO_InitStructure.GPIO_Pin = LDAC_GPIO; //LDAC GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(LDAC_TYPE, &GPIO_InitStructure); //GPIO_SetBits(LDAC_TYPE, LDAC_GPIO); GPIO_InitStructure.GPIO_Pin = RST_GPIO; //RST GPIO_Init(RST_TYPE, &GPIO_InitStructure); //GPIO_SetBits(RST_TYPE, RST_GPIO); //这里是定义CS脚 GPIO_InitStructure.GPIO_Pin = CS1_GPIO; //CS1 GPIO_Init(CS1_TYPE, &GPIO_InitStructure); GPIO_SetBits(CS1_TYPE, CS1_GPIO); //printf("CS1\r\n"); GPIO_InitStructure.GPIO_Pin = CS2_GPIO; //CS2 GPIO_Init(CS2_TYPE, &GPIO_InitStructure); GPIO_SetBits(CS2_TYPE, CS2_GPIO); //printf("CS2\r\n"); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //CS3 GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_SetBits(GPIOC, GPIO_Pin_3); GPIO_InitStructure.GPIO_Pin = CS4_GPIO; //CS4 GPIO_Init(CS4_TYPE, &GPIO_InitStructure); GPIO_SetBits(CS4_TYPE, CS4_GPIO); GPIO_InitStructure.GPIO_Pin = CS5_GPIO; //CS5 GPIO_Init(CS5_TYPE, &GPIO_InitStructure); GPIO_SetBits(CS5_TYPE, CS5_GPIO); GPIO_InitStructure.GPIO_Pin = CS6_GPIO; //CS6 GPIO_Init(CS6_TYPE, &GPIO_InitStructure); GPIO_SetBits(CS6_TYPE, CS6_GPIO); GPIO_InitStructure.GPIO_Pin = CS7_GPIO; //CS7 GPIO_Init(CS7_TYPE, &GPIO_InitStructure); GPIO_SetBits(CS7_TYPE, CS7_GPIO); GPIO_InitStructure.GPIO_Pin = CS8_GPIO; //CS8 GPIO_Init(CS8_TYPE, &GPIO_InitStructure); GPIO_SetBits(CS8_TYPE, CS8_GPIO); SPI1_Init(); SPI1_SetSpeed(SPI_BaudRatePrescaler_256); // delay_ms(1); GPIO_ResetBits(LDAC_TYPE, LDAC_GPIO);/*拉低 AD5791的LDAC引脚*/ GPIO_SetBits(CLR_TYPE, CLR_GPIO);/*拉高 AD5791的CLR引脚*/ GPIO_SetBits(RST_TYPE, RST_GPIO);/*拉高 AD5791的RST引脚*/ // for(int i=1; i<9; i++) //AD5791_Channel_ABLE(i); //GPIO_SetBits(LDAC_TYPE, LDAC_GPIO);/*拉低 AD5791的LDAC引脚*/ printf("Hardware reset AD5791\r\n"); //AD5791HardwareReset(); } void AD5791_Channel_DISABLE(u8 ch) { switch (ch) { case 1: AD5791_CS1_DISABLE(); break; case 2: AD5791_CS2_DISABLE(); break; case 3: AD5791_CS3_DISABLE(); break; case 4: AD5791_CS4_DISABLE(); break; case 5: AD5791_CS5_DISABLE(); break; case 6: AD5791_CS6_DISABLE(); break; case 7: AD5791_CS7_DISABLE(); break; case 8: AD5791_CS8_DISABLE(); break; default: break; } } void AD5791_SetValue(u8 ch,u32 value) { AD5791_Channel_ENABLE(ch); SPI1_ReadWriteByte(0x20); SPI1_ReadWriteByte(0x02); SPI1_ReadWriteByte(0x52); AD5791_Channel_DISABLE(ch); /*写入DAC寄存器,其中DAC_data1的值会在串口接收等函数中进行运算更改*/ AD5791_Channel_ENABLE(ch); SPI1_ReadWriteByte(value>>16|0x10); SPI1_ReadWriteByte(value>>8); SPI1_ReadWriteByte(value); AD5791_Channel_DISABLE(ch); } void AD5791_SetStop(u8 ch) { AD5791_Channel_ENABLE(ch); SPI1_ReadWriteByte(0x20); SPI1_ReadWriteByte(0x02); SPI1_ReadWriteByte(0x5E); AD5791_Channel_DISABLE(ch); /*写入DAC寄存器,其中DAC_data1的值会在串口接收等函数中进行运算更改*/ } unsigned char ReadFromAD5791ViaSPI(u8 ch,const unsigned char RegisterStartAddress, const unsigned char NumberOfRegistersToRead, unsigned long int *DataBuffer, const unsigned char OffsetInBuffer) { unsigned char WriteBuf[3]; unsigned char ReadBuf[3]; unsigned char i; for(i=0; i< (NumberOfRegistersToRead+1); i++) { if (i < NumberOfRegistersToRead) { WriteBuf[0] = 0x80 | ((RegisterStartAddress+i)<<4) ; } else { WriteBuf[0] = NOP; } WriteBuf[1] = NOP; WriteBuf[2] = NOP; AD5791_ReadWriteBuffers(ch,WriteBuf, ReadBuf, 3); if (i>0) { DataBuffer[OffsetInBuffer + i -1] = ReadBuf[0]; DataBuffer[OffsetInBuffer + i -1] = (DataBuffer[OffsetInBuffer + i -1]<<8) + ReadBuf[1]; DataBuffer[OffsetInBuffer + i -1] = (DataBuffer[OffsetInBuffer + i -1]<<8) + ReadBuf[2]; } } //AD5791_Channel_DISABLE(ch); return 0; } u8 AD5791_ReadWriteBuffers(u8 ch,unsigned char* WriteBuffer, unsigned char *ReadBuffer, unsigned char NumberOfByte) { unsigned char WriteData, ReadData; unsigned char i, j; AD5791_Channel_ENABLE(ch); for(i=0; i<NumberOfByte; i++) { WriteData = *(WriteBuffer + i); ReadData = 0; SPI1_ReadWriteByte(WriteData); ReadData =SPI1_ReadWriteByte(0x00); *(ReadBuffer + i)= ReadData; } AD5791_Channel_DISABLE(ch); return 0; } unsigned char WriteToAD5791ViaSPI(u8 ch,const unsigned char RegisterStartAddress, const unsigned char NumberOfRegistersToWrite, unsigned long int *DataBuffer, const unsigned char OffsetInBuffer) { unsigned char WriteBuf[3]; unsigned char ReadBuf[3]; unsigned char i; for(i=0; i<NumberOfRegistersToWrite; i++) { WriteBuf[0] = ((RegisterStartAddress + i)<<4) | (DataBuffer[RegisterStartAddress + i]>>16); WriteBuf[1] = DataBuffer[RegisterStartAddress + i]>>8; WriteBuf[2] = DataBuffer[RegisterStartAddress + i]; AD5791_ReadWriteBuffers(ch,WriteBuf, ReadBuf, 3); } return 0; }
评论
    相关推荐