The MSP430 library operates ad7708 through SPI

Source: Internet
Author: User
Tags chop filter register

The ad7708 is a 16-bit Σ-△type adconverter chip. in low-frequency applications, the ad7708 can be used as the complete front end of a single power supply. The ad7708 contains a PGA (Programmable Gain Amplifier) to enlarge the signal. The PGA range is 20-28, which is programmable, when the reference voltage is V, the voltage from 20mv to 2.56v can be measured. The ad7708 and ad7718 pin functions exactly the same, but the number of digits ad7718 is 24 bits. If you use 7718, you only need to change a fewProgram, Change the 16-bit part to 24-bit. The ad7708 is communicated through the SPI interface. The program uses the SPI program implemented in the previous article to communicate with the ad chip, which can be used as a detailed example of SPI usage.

  1. Hardware introduction:

    The hardware is mainly the SPI interface of MSP430 and the description of the ad7708 chip.

    MSP430 SPI interface: supports host mode and slave mode, and the polarity and phase are always adjustable. The polarity must be consistent when the adconverter chip communicates. For details about the SPI of MSP430, refer:MSP430 library <5> SPI Synchronous Serial Communication.

    The ad7718 has 28 external pins. Based on the nature, it is mainly divided into two parts: Simulation and number. Analog pins include analog input, reference voltage input, and analog power supply. The analog input pin can be configured as an 8-channel or 10-channel pseudo-differential input.

    The digital pin has four categories: SPI interface, data ready, General I/O port, and digital power supply. The four standard signal lines of the SPI interface are chip selection signal CS, serial clock input sclk, serial data input DIN, and serial data output dout. When ad7718 is connected to the SPI bus, it is a slave device, and the low-level signal is input from the CS pin to enable ad7718. Data-ready rdy is a low-level valid output pin. When valid data exists in the data register of the selected channel, the low level signal is output. After the data is read, the high level is output. The general I/O Ports of ad7718 are two one-port P1 and P2. They can be configured as both input and output. The MCU reads and writes the relevant registers in ad7718 through the SPI port to perform operations on P1 and P2. They extend the I/O interface capabilities of single-chip microcomputer.

    Both the analog power supply and Digital Power Supply of ad7718 can be powered by + 3 V or + 5 V respectively. But must be consistent, either with + 3 V or with + 5 V.

    Ad7708 and ad7718 are controlled and configured through a set of In-chip registers. The first of these registers is the communication register, which is used to control all operations of the converter. For all communications between these components, you must first write the communication register to specify the next operation to be performed. After power-on or reset, the device waits for the write communication register by default. The Status Register contains information about the operation conditions of the converter. The Status Register is a read-only register. The Mode register is used to configure the conversion mode, calibration, Chopper enabling/disabling, Reference Voltage selection, Channel configuration, and pseudo-differential aincom to simulate the buffer or no buffer during input operations. A mode register is a read/write register. The ADC control register is a read/write register used to select the active channel and encoding input range and bipolar/single polarity operations. The I/O control register is a read/write register used to configure two I/O Ports. The filter register is a read/write register used to update the data of the encoding converter. The ADC data register is a read-only register that contains the result of a data conversion on the selected channel. The ADC's offset register read/write register contains offset calibration data. There are five offset registers, one of each fully differential input channel. If this parameter is set to pseudo-differential input mode, the channels share the offset register. The ADC gain register is a read/write register that contains gain calibration data. There are five ADC gain registers, one of each fully differential input channel. When configured as a pseudo-differential input mode, the channel shares the gain register. The ADC contains the test registers used by the factory and the user shall not change the operating conditions of these registers. The ID register is a read-only register for silicon recognition purposes.

    I used the hardware connection method: 430 p3.0 connected to ad7708 CS end, P3.1-P3.2 connected to the corresponding ad chip SPI port; rdy signal is not connected; so, the program uses the query method, wait for the rdy bit of the Status Register to indicate that the conversion is complete.

    For more information about ad7708, see its datasheet. In addition, I have translated the register part of the Data Manual and the part of the program process.Attachment.

  2. Program Implementation:

    The first is the read/write register function of ad7708. Each operation of ad7708 starts with a write communication register. This step instructs the next operation, refer to ad7708-register in attachment (blog end)

    Write register:

     
    VoidAd7708writeregister (CharADDR,LongDat) {spiwritedata (ADDR );// Write the communication register to notify the next operation: Write the ADDR registerIf(Islong [ADDR])// If it is a 16-bit register, 7718 then 24-bit if you want to change the if statement{Spiwritedata (DAT> 8);} spiwritedata (0xff & dat );// Write low-level data}

    Register address. You can refer to datasheet or the part I translated. The islong character array indicates whether the corresponding register is 8-bit or 16-bit:

     
    CharIslong [16] = };

    Read register:

    LongAd7708readregister (CharADDR ){CharH = 0, L = 0;// High/low bytes of dataSpiwritedata (0x40 | ADDR );// Write the communication register to notify the next operation: Read the ADDR registerIf(Islong [ADDR]) {H = spiwritedata (0xff);} l = spiwritedata (0xff );Return((Unsigned int) H <8) | L ;}

    SPI explanation: 430 is the SPI Host module. When sending, another clock is sampled and received along the same time. Therefore, data can be read in the half cycle after each sending; therefore, when the spiwritedata function writes data, it returns the characters received at the same time. Sending 0xff provides a clock for reading the coming data. For details, refer to the precautions section in the previous article (just updated ).

    Read result data:

    LongAd7708readresultdata (){While(Ad7708readregister (0x00) & 0x80) = 0 );// Wait until the conversion is completedReturnAd7708readregister (0x04 );}

    Wait until the rdy bit of status changes to high (the AD data conversion is updated) to read the data register content.

    Calibration: The calibration process has a detailed flowchart in Datasheet. You can refer to the ad7708-register in Datasheet or the accessories. This sub-function only completes the calibration of one channel. The channel address has parameter input, convenient call:

    VoidAd7708cal (CharChannel) {adccon = (adccon & 0x0f) | (Channel <4); mode = (Mode & 0xf8) | 0x04;// Internal 0 CalibrationAd7708writeregister (0x02, adccon );// ADC control register, channelAd7708writeregister (0x01, mode );// Mode registerWhile(Ad7708readregister (0x01) & 0x07 )! = 0x01 );// Wait until the calibration is completedMode = (Mode & 0xf8) | 0x05;// Internal full scale CalibrationAd7708writeregister (0x01, mode );// Mode registerWhile(Ad7708readregister (0x01) & 0x07 )! = 0x01 );// Wait until the calibration is completed}

    Adccon is the content of the adccon register that was previously input by the program. mode is the content of the last input mode register recorded by the program. Because it takes time to read the serial port, in order to get a faster speed, the program records these two variables for use. For the channel address, see the documentation in Datasheet or the attachment.

    Initialization:

     
    VoidAd7708init (CharChop ){
    P3dir | = bit0;
    P3out & = ~ Bit0; // CS selected
         // Host mode, 115200,8-Bit Data bit, three-line mode, clock Mode 1 (for details, see SPI. c) Spimasterinit (115200,8, 3,1 ); // The clock is not accurate by 115200 (for details, see SPI. c) _ Eint (); // Open interrupt. The SPI read/write program must be interrupted.  Char Filter; adccon = 0x0f;If (Chop = 0) {filter = 0x03; // Set the filter register to the minimum value, which can be changed Mode = 0x91; // Chopping prohibited, 10 channels, no buffer, idle mode } Else {Filter = 0x0d; // Set the filter register to the minimum value, which can be changed Mode = 0x11; // Enable chopper, 10 channels, no buffer, idle mode } Ad7708writeregister (0x07,0x00 ); // Io register, NO = Ad7708writeregister (0x03, filter ); // Filter register Ad7708writeregister (0x02, 0x0f ); // ADC control register, 0-channel, single polarity Ad7708writeregister (0x01, mode ); // Mode register  If (Chop = 0) For ( Int I = 0; I <5; I ++ ){ // Calibration. Because there are only five offset registers, if there are more than five, the previous ones will be overwritten and only five ones will be calibrated. Ad7708cal (5);} _ dint ();}

    The chopper parameter is introduced in the initialization system. Other parameters are fixed: 10-channel pseudo-difference, single polarity, no buffer, and the shortest speed when the filter register is set to chopper or the chopper is disabled, you can modify it as needed. Enable interrupt after SPI initialization. The purpose is to write content to ad to initialize ad. After initialization, the function is disconnected. In order to make the library consistent after initialization, but after calling this function, you need to enable the interruption before using other functions of AD sampling.

    Sampling start: This program only supports the start of word sampling. If you need continuous mode, you can implement it yourself (easy to implement: you only need to change the value of the Register ):

    VoidAd7708start (CharChannel) {adccon = (adccon & 0x0f) | (Channel <4); mode = (Mode & 0xf8) | 0x02; ad7708writeregister (0x02, adccon ); ad7708writeregister (0x01, mode );}

    Based on the previous settings of the control register and mode register, change the current value and write it into the corresponding register.

    At this point, the program is partially completed and needs to be extended. You can add it on your own.

  3. instructions for use:

    Add ad7708.c to the file containing ad7708.h, and then use the functions provided by this program; for details, refer to the sample project and the main. c file.

      long  A;  void  main () { // stop watchdog timer to prevent time out reset  wdtctl = wdtpw + wdthold; clkinit (); ad7708init (0);  // enable chopper when chopping 1 is disabled  _ Eint ();  // open interrupt, the program needs to use the SPI interrupt; conflict, you can change the SPI function   while  (1)  // serial port test  {ad7708start (0); A = ad7708readresultdata ();  // read the ad sample result // Voltage Calculation Method: A * 1.024*2.5 (reference voltage) /65535  A = ad7708readregister (0);  // de-state value, this function does not need to use }

    ad7708 Library (simplified. You can add other functions as needed: after functions with read/write registers are available, it is relatively simple to add other functions.) You are welcome to discuss any shortcomings. Thank you.

Attachment: Library ad7708-register

Author:Give me a drink

Source: http://Engin.cnblogs.com/

The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. repost the text and indicate the source. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.