【原创】基于NIOS II的ADS1256 SPI驱动

/*********************************说明**********************************/
            
在SOPC里面SPI设置如下:
            
     
 另外在SOPC里面设置两个IO:
      1.ads1256_rst  :1位,输出端口。对应ads1256的rst管脚
      2.ads1256_drdy :1位,输入端口。对应ads1256的DRDY管脚

      需要说明的是,笔者使用对ads1256的片选引脚一直置低,所以也就没有连接到FPGA。

      环境:
            QUARTUS 8.1
      器件:
            EP2C8Q208C8N

       /**************************头文件*****************************/

 1/*
 2 *  Copyright (C) 2009, Electric & Electronic Innovation Center of Sci. & Tech. HUST
 3 *  All Rights Reserved.
 4 *  
 5 *  File name:              ads1256.h
 6 *  File description:       Header file of the ads1256
 7 *  Operating environment:  NIOS II processer,clock 75MHz
 8 *          
 9 *  This version:           1.0
10 *  Author:                 lwpo2008(lwpo2008@yahoo.com.cn)
11 *  Previous Author:        Billy
12 *  Complete date:          2009-07-30
13 *  
14*/

15#ifndef ADS1256_H_
16#define ADS1256_H_
17/*********************************************************************************************************
18 * headers
19*********************************************************************************************************/

20#include "altera_avalon_spi_regs.h"
21#include "altera_avalon_spi.h"
22#include "system.h"
23#include "altera_avalon_pio_regs.h"
24#include "priv/alt_busy_sleep.h"
25/*********************************************************************************************************
26 * define
27*********************************************************************************************************/

28
29//ADS1256 Command Definitions
30//#define ADS1256_WAKEUP          0x00    //Completes SYNC and Exits Standby Mode
31#define ADS1256_RDATA           0x01    //Read Data
32#define ADS1256_RDATAC          0x03    //Read Data Continuously
33#define ADS1256_SDATAC          0x0f    //Stop Read Data Continuously
34#define ADS1256_RREG            0x10    //Read from REG rrr 0001 rrrr (1xh) 0000 nnnn
35#define ADS1256_WREG            0x50    //Write to REG rrr 0101 rrrr (5xh) 0000 nnnn
36#define ADS1256_SELFCAL         0xf0    //Offset and Gain Self-Calibration
37#define ADS1256_SELFOCAL        0xf1    //Offset Self-Calibration
38#define ADS1256_SELFGCAL        0xf2    //Gain Self-Calibration
39#define ADS1256_SYSOCAL         0xf3    //System Offset Calibration
40#define ADS1256_SYSGCAL         0xf4    //System Gain Calibration
41#define ADS1256_SYNC            0xfc    //Synchronize the A/D Conversion
42#define ADS1256_STANDBY         0xfd    //Begin Standby Mode
43#define ADS1256_RESET           0xfe    //Reset to Power-Up Values
44#define ADS1256_WAKEUP          0xff    //Completes SYNC and Exits Standby Mode
45
46
47
48//ADS1256 REGISTER MAP
49#define ADS1256_STATUS          0x00    //STATUS REGISTER (ADDRESS 00h)
50#define ADS1256_MUX             0x01    //Input Multiplexer Control Register (Address 01h)
51#define ADS1256_ADCON           0x02    //A/D Control Register (Address 02h)
52#define ADS1256_DRATE           0x03    //A/D Data Rate (Address 03h)
53#define ADS1256_IO              0x04    //GPIO Control Register (Address 04H)
54#define ADS1256_OFC0            0x05    //Offset Calibration Byte 0, least significant byte (Address 05h)
55#define ADS1256_OFC1            0x06    //Offset Calibration Byte 1 (Address 06h)
56#define ADS1256_OFC2            0x07    //Offset Calibration Byte 2, most significant byte (Address 07h)
57#define ADS1256_FSC0            0x08    //Full.scale Calibration Byte 0, least significant byte (Address 08h)
58#define ADS1256_FSC1            0x09    //Full.scale Calibration Byte 1 (Address 09h)
59#define ADS1256_FSC2            0x0a    //Full.scale Calibration Byte 2, most significant byte (Address 0Ah)
60
61
62/*********************************************************************************************************
63 * function declaration
64*********************************************************************************************************/

65void InitAds1256();
66int ReadFromAds1256();
67
68#endif /*ADS1256_H_*/
69
70


      /***************************源文件****************************/ 

 1/*
 2 *  Copyright (C) 2009, Electric & Electronic Innovation Center of Sci. & Tech. HUST
 3 *  All Rights Reserved.
 4 *  
 5 *  File name:              ads1256.c
 6 *  File description:       Source file of the ads1256
 7 *  Operating environment:  NIOS II processer,clock 75MHz
 8 *          
 9 *  This version:           1.0
10 *  Author:                 lwpo2008(lwpo2008@yahoo.com.cn)
11 *  Previous Author:        Billy
12 *  Complete date:          2009-07-30
13 *  
14*/

15/*********************************************************************************************************
16 * headers
17*********************************************************************************************************/

18#include "ads1256.h"
19/*********************************************************************************************************
20 * control variable
21*********************************************************************************************************/

22unsigned char wrdata[16]={ADS1256_WREG|ADS1256_DRATE,   0x00,0x03,
23                          ADS1256_WREG |ADS1256_STATUS, 0x00,0x03,
24                          ADS1256_WREG |ADS1256_MUX,    0x00,0x54,
25                          ADS1256_WREG |ADS1256_ADCON,  0x00,0x00,
26                          ADS1256_WREG |ADS1256_IO,     0x00,0x0f,
27                          ADS1256_SELFCAL}
;
28
29/*********************************************************************************************************
30* Function:             void InitAds1256()
31* Description:          Initialize the spi
32* Input:                void
33* Output:               void
34* Use other resources:  none
35* Level:                  Hardware ★★★
36* Author:               lwpo2008(lwpo2008@yahoo.com.cn)
37* Previous Author:      none
38* Date:                 2009-07-30
39*********************************************************************************************************/

40void InitAds1256()
41{
42    IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_BASE,0x00);          //Initialize the spi control reg
43    IOWR_ALTERA_AVALON_PIO_DATA(ADS1256_RST_BASE,0);        //reset ads1256
44    alt_busy_sleep(1);
45    IOWR_ALTERA_AVALON_PIO_DATA(ADS1256_RST_BASE,1); 
46    
47    while(IORD_ALTERA_AVALON_PIO_DATA(ADS1256_DRDY_BASE)==1);
48    alt_avalon_spi_command(SPI_BASE,0,16,wrdata,0,NULL,0);
49}

50/*********************************************************************************************************
51* Function:             int ReadFromAds1256()
52* Description:          Read the value from ads1256
53* Input:                void
54* Output:               int 32 bit
55* Use other resources:  Spi core
56* Level:                  Hardware ★★★
57* Author:               lwpo2008(lwpo2008@yahoo.com.cn)
58* Previous Author:      none
59* Date:                 2009-07-30
60*********************************************************************************************************/

61int ReadFromAds1256()
62{
63    int adResult=0;
64    unsigned char wrToReadData[1]={ADS1256_RDATA};
65    unsigned char rddata[3];
66    unsigned char r;
67    
68    while(IORD_ALTERA_AVALON_PIO_DATA(ADS1256_DRDY_BASE)==1);
69    alt_avalon_spi_command(SPI_BASE,0,1,wrToReadData,3,rddata,0);
70    
71    for(r=0; r<3; r++
72    {   
73        adResult = adResult << 8;
74        adResult = adResult | rddata[r];
75    }

76    adResult = adResult << 8;
77    
78    adResult = (float)adResult / 0x800000*5000/256;
79    
80    return adResult;
81}

82
83
原文地址:https://www.cnblogs.com/oneseven/p/1543916.html