tag 标签: 驱动代码

相关博文
  • 热度 29
    2015-2-5 15:42
    5759 次阅读|
    1 个评论
    本驱动代码适用于ADI公司的AD779X系列ADC芯片,所有与硬件平台相关的代码都最大化的提取了出来,达到了最优化的移植性,本文档及代码旨在分享个人的开发工作,减少工程师们的开发周期,提高大家的工作效率。 测试时硬件平台我使用的是一块cortex-M3的单片机来和AD779X通信,具体型号为NXP公司的LPC1752单片机,原理图如下: 由上图可以看到AD779X的基准芯片采用ADI公司的ADR34XX系列芯片,AD779X与单片机的管脚连接有SCLK/CS/DI/DOUT共四个管脚,即AD779X的数字串行通信引脚。 A779X芯片的驱动代码整理后已经高度模块化,移植起来非常方便,整个移植过程只需要修改三个非常简单的地方即可使用,加快开发进度: 管脚定义及功能设置(与单片机相连的管脚定义,输入输出设置) /***********PIN define**************************/ #define      AD779x_DIN    (16) #define      AD779x_CS      (17) #define      AD779x_SCLK  (18) #define      AD779x_DOUTRDY (110) /******************************************************** *FUNCTION:Set pin function on MCU **********************************************************/ void McuGpioSet(void) 管脚写1或写0功能函数 /******************************************************* *FUNCTION:     AD779X PIN state set *PARAMETER:  PIN---serial pin on AD779X,may nex value: *                                    AD779X_DIN/AD779X_CS/AD779X_SCLK/AD779X_DOUTRDY *                           Data---set value:0/1 *******************************************************/ void AD779x_PIN_SET(unsigned int PIN, unsigned char Data) {                   if(Data)                             LPC_GPIO2-FIOSET |= PIN;                   else                             LPC_GPIO2-FIOCLR |=PIN; } 管脚电平状态读取函数 /****************************************************** *FUNCITON:AD779X PIN STATE READ *PARAMETER:PIN---serial pin,may next value: *                                    AD779X_DOUTRDY *******************************************************/ unsigned char   AD779X_PIN_READ(unsigned int PIN) { unsigned char VarDat;   if(LPC_GPIO2-FIOPIN PIN)            VarDat=1; else            VarDat=0;   return VarDat; } 从上面描述可以看出,芯片AD779X的移植非常简单高效,为项目开发节约时间,提高工作效率,一下为高度模块化的子模块函数,与平台无关,移植时不用修改: /****************************************************** *FUNCTION:    AD779X serial pin initial *******************************************************/ void AD779xInitialization(void)   /************************************************* *FUNCTION:    Serial interface reset                 *************************************************/ void AD779xSoftwareReset(void)   /************************************************** *FUNCTION: write data in registers *PARAMETER:RegisterAddress---register address,may next value: *                                            COMMUNICATIONS, *                                            IO, *                                            MODE, *                                            CONFIGURATION, *                                            OFFSET, *                                            FULLSCALE *                          RegisterBuffer---register data buffer point *                          CHIP---chip choose,may next value: *                                            AD7794 *                                            AD7795 *                                            AD7796 *                                            AD7797 **************************************************/ void AD779xWriteRegister(unsigned char RegisterAddress, unsigned long int *RegisterBuffer,unsigned char CHIP)   /************************************************************************ *FUNCTION:read data from register *PARAMETER:RegisterAddress---register address , may next value: *                                            STATUS, *                                            ID, *                                            MODE, *                                            CONFIGURATION, *                                            OFFSET, *                                            FULLSCALE, *                                            DATA *                          RegisterBuffer---data buffer point *                          CHIP---chip choose,may next value: *                                            AD7794 *                                            AD7795 *                                            AD7796 *                                            AD7797 *************************************************************************/ void AD779xReadRegister(unsigned char RegisterAddress, unsigned long int *RegisterBuffer,unsigned char CHIP)   /************************************************************************** *FUNCTION:Read result for single conversion mode *PARAMETER: DataIndex---the sotre place in DataBuffer *                         DataBuffer---data store buffer *                          CHIP---which type,may next value: *                                   AD7792/AD7793/AD7794/AD7795/AD7796/AD7797 **************************************************************************/ void AD779xReadResultForSingleConversion(unsigned char DataIndex, unsigned long int *DataBuffer,unsigned char CHIP)   /************************************************************************** *FUNCTION:Read result for continuous conversion mode *PARAMETER: StartIndex---the first store place in DataBuffer *                          NumberOfData---data numbers *                          DataBuffer---data store buffer *                          CHIP---which type,may next value: *                                   AD7792/AD7793/AD7794/AD7795/AD7796/AD7797 **************************************************************************/ void AD779xReadResultForContinuousConversion(unsigned char StartIndex, unsigned char NumberOfData, unsigned long int *DataBuffer,unsigned char CHIP)   /************************************************************************** *FUNCTION:Read result for continuous read mode *PARAMETER: StartIndex---the first store place in DataBuffer *                          NumberOfData---data numbers *                          DataBuffer---data store buffer *                          CHIP---which type,may next value: *                                   AD7792/AD7793/AD7794/AD7795/AD7796/AD7797 **************************************************************************/ void AD779xReadResultForContinuousRead(unsigned char StartIndex, unsigned char NumberOfData, unsigned long int *DataBuffer,unsigned char CHIP)   /************************************************************************* *FUNCITON:Exit continous read mode *PARAMETER: CHIP---which type,may next value: *                                   AD7792/AD7793/AD7794/AD7795/AD7796/AD7797 **************************************************************************/ void AD779xExitContinuousRead(unsigned char CHIP)            以上为该芯片驱动代码所有的子模块函数,在主函数中就可以调用这些子函数配置寄存器和读取ADC的值,下面代码即使用连续转换方式(Continuous Conversion)读取通道2的数据,然后将读取的数据通过UART接口打印输出,该代码在实际的硬件平台调试通过:    /********************************************************************************************************* ** Function name:       main ** Descriptions:         ** input parameters:    无 ** output parameters:   无 ** Returned value:      无 *********************************************************************************************************/ int  main (void) {            SystemInit();                                                       /*MCU system initial*/          uart0Init ();                                                                                 /*uart initial*/            printf("General Platform v1.0!\n");            myDelay(1000);            AD779xInitialization();          AD779xSoftwareReset();          NopDely(100*50);   // read ID register          AD779xRegisters =WRITE_ENABLE|READ|RS_ID|CREAD_DISABLE;          AD779xWriteRegister(COMMUNICATIONS, AD779xRegisters,AD7794);          AD779xReadRegister(ID, AD779xRegisters,AD7794);            printf("RESET ID REGISTER=0x%04X\n",AD779xRegisters );   // read STATUS register          AD779xRegisters =WRITE_ENABLE|READ|RS_STATUS|CREAD_DISABLE;          AD779xWriteRegister(COMMUNICATIONS, AD779xRegisters,AD7794);          AD779xReadRegister(STATUS, AD779xRegisters,AD7794);            printf("RESET STATUS REGISTER=0x%04X\n",AD779xRegisters );     //WRITE MODE REGISTER          AD779xRegisters =WRITE_ENABLE|WRITE|RS_MODE|CREAD_DISABLE;          AD779xWriteRegister(COMMUNICATIONS, AD779xRegisters,AD7794); AD779xRegisters =MD_CONTINUOUS_CONVERSION|PSW_ENABLE|AMP_CM_ENABLE|CLK_INTERNAL_OUTPUT_ENABLE|CHOP_DISABLE|FILTER_UPDATE_RATE_242;          AD779xWriteRegister(MODE, AD779xRegisters,AD7794);   //WRITE CONFIGURATION               AD779xRegisters =WRITE_ENABLE|WRITE|RS_CONFIGURATION|CREAD_DISABLE;          AD779xWriteRegister(COMMUNICATIONS, AD779xRegisters,AD7794); AD779xRegisters =VBIAS_DISABLE|BO_DISABLE|BIPOLAR|BOOST_DISABLE|GAIN_1|REFSEL_EXTERNAL_REF1|REF_DETECT_DIASBLE|BUF_ENABLE|CHANNEL_AIN2;          AD779xWriteRegister(CONFIGURATION, AD779xRegisters,AD7794);          while(1)          {                      AD779xReadResultForContinuousConversion(0, 1, AD779xData,AD7794);                             printf("convertion data1:0x%03X,data2:0x%03X\n",AD779xData ,AD779xData );                    myDelay(1000);          } } http://item.taobao.com/item.htm?id=43307575672;
相关资源