Thông báo

Collapse
No announcement yet.

Giao tiếp spi với adc

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Giao tiếp spi với adc

    mình đang giao tiếp spi giưa atmega16 vơi ad7730 nhưng mình gặp rắc rối là khi ghi lệnh ra cổng spi thì ko thấy chân DRY của adc xuống mức thấp.
    mình đã mô phỏng qua ISIS thì thấy dạng sóng đúng với timing của adc.sau day la code của mình.mong mọi ngườ giúp mình.
    #include <mega16.h>
    #include <delay.h>
    #include <spi.h>
    #define led1 PORTC.7
    #define led2 PORTC.6
    #define led3 PORTC.5//chan quet anod cua 5led 7doan
    #define led4 PORTC.4
    #define led5 PORTC.3
    #define CS PORTB.4 chon chip
    #define DRY PIND.2
    #define SYN PORTD.5
    #define RS PORTD.6 chan reset
    #define SINGLEW 0x00
    #define SINGLER 0b010000 // BIT(4);
    #define STARTCONT 0b100000 // BIT(5);
    #define STOPCONT 0b110000 // BIT(4) | BIT(5);

    #define COMM_REG 0x00
    #define STAT_REG 0x00
    #define DATA_REG 0x01
    #define MODE_REG 0x02
    #define FILT_REG 0x03
    #define DAC_REG 0x04
    #define OFFS_REG 0x05
    #define GAIN_REG 0x06
    #define TEST_REG 0x07

    // Status Reg.
    #define NOTREADY 0b10000000 // BIT(7);
    #define NOTSETTLE 0b01000000 // BIT(6);
    #define NOREF 0b00010000 // BIT(4);

    // Mode Reg.(High)
    #define IDLE_MODE 0b00000000
    #define CONT_MODE 0b00100000
    #define SING_MODE 0b01000000
    #define STBY_MODE 0b01100000
    #define IZERO_MODE 0b10000000
    #define IFULL_MODE 0b10100000
    #define SZERO_MODE 0b11000000
    #define SFULL_MODE 0b11100000

    #define BIPOLAR 0x00
    #define UNIPOLAR 0b10000 ///bit(4);

    #define BITLEN16 0x00
    #define BITLEN24 0x01 // BIT(0);


    // Mode Reg.(Low)
    #define REF2_5V 0x00
    #define REF5V 0b10000000 // BIT(7);

    #define RANGE10 0b000000 // 0x00 << 4;
    #define RANGE20 0b010000
    #define RANGE40 0b100000
    #define RANGE80 0b110000

    #define CLKDIS 0b1000 // BIT(3);
    #define BURNOUT 0b0100 // BIT(2);
    #define AIN1 0x00
    #define AIN2 0x01
    #define AIN1G 0x02
    #define AING 0x03

    // Filter Reg.(High)
    #define OUT_400_H 0b00010000 // BIT(0);
    #define OUT_400_L 0b00000000 // BIT(0);

    // Filter Reg.(Mid)
    #define SKIP_MODE 0b10 // BIT(1);
    #define FASTSTEP_MODE 0b01 // BIT(0);

    // Filter Reg.(Low)
    #define AC_MODE 0b100000 // BIT(5);
    #define CHOP_MODE 0b010000 // BIT(4);

    unsigned long DATA;
    unsigned int a,b,c,d,e,cnt,sodem;


    void giaima(unsigned long cnt)
    {
    switch (cnt)
    {
    case 0:
    PORTA=0x03;
    break;
    case 1:
    PORTA=0x9f;
    break;
    case 2:
    PORTA=0x25;
    break;
    case 3:
    PORTA=0x0d;
    break;
    case 4:
    PORTA=0x99;
    break;
    case 5:
    PORTA=0x49;
    break;
    case 6:
    PORTA=0x41;
    break;
    case 7:
    PORTA=0x1f;
    break;
    case 8:
    PORTA=0x01;
    break;
    case 9:
    PORTA=0x09;
    break;
    }
    }


    void wait_adc_rdy()
    {
    while(PIND.2==1);

    };

    unsigned long Read_ADC (void)
    {
    unsigned long AI;
    unsigned long ADC = 0;
    signed char i;


    CS = 0; // CS High
    for (i= 2; i>= 0; i--) // Read 3 Byte
    ADC |=(unsigned long) spi(0)<< (signed char)(i*8);
    AI = ADC; // Save ADC Data
    return (AI);
    CS = 1; // CS High
    }

    //-----------------------------------

    //-----chuong trinh con--------

    interrupt [TIM2_OVF] void timer2_ovf_isr(void)
    {
    TCCR2=0x00;
    a=(sodem/10000);
    b=(sodem-a*10000)/1000;
    c=(sodem-a*10000-b*1000)/100;
    d=(sodem-a*10000-b*1000-c*100)/10;
    e=(sodem-a*10000-b*1000-c*100-d*10);
    cnt=a;
    giaima(cnt);
    led2=1;
    led3=1;
    led4=1;
    led5=1;
    if(a==0) led1=1;
    if(a>=1) led1=0;



    delay_ms(3);
    PORTA=0xff;
    //-----------------------
    cnt=b;
    giaima(cnt);
    led1=1;
    led3=1;
    led4=1;
    led5=1;
    if((a==0)&&(b==0)) led2=1;
    if((a==0)&&(b>=1)) led2=0;
    if(a>=1) led2=0;
    delay_ms(3);
    PORTA=0xff;
    //---------------------
    cnt=c;
    giaima(cnt);
    led1=1;
    led2=1;
    led4=1;
    led5=1;
    if((a==0)&&(b==0)&&(c==0)) led3=1;
    if((a==0)&&(b==0)&&(c>=1)) led3=0;
    if((a==0)&&(b>=1)) led3=0;
    if(a>=1) led3=0;
    delay_ms(3);
    PORTA=0xff;
    //-------------------
    cnt=d;
    giaima(cnt);
    led1=1;
    led2=1;
    led3=1;
    led5=1;
    if((a==0)&&(b==0)&&(c==0)&&(d==0)) led4=1;
    if((a==0)&&(b==0)&&(c==0)&&(d>=1)) led4=0;
    if((a==0)&&(b==0)&&(c>=1)) led4=0;
    if((a==0)&&(b>=1)) led4=0;
    if(a>=1) led4=0;
    delay_ms(3);
    PORTA=0xff;
    cnt=e;
    giaima(cnt);
    led1=1;
    led2=1;
    led3=1;
    led4=1;
    led5=0;
    delay_ms(3);
    PORTA=0xff;

    TCCR2=0x06;
    }

    void main()
    {
    sodem=0;
    PORTA=0xFF;
    DDRA=0xFF;
    PORTB=0xFF;
    DDRB=0xB8;
    PORTC=0xff;
    DDRC=0xff;
    PORTD=0xFF;
    DDRD=0xFB;

    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;
    TCCR1A=0x00;
    TCCR1B=0x07;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    ASSR=0x00;
    TCCR2=0x06;
    OCR2=0x00;
    MCUCR=0x00;
    MCUCSR=0x00;
    TIMSK=0x40;
    ACSR=0x80;
    SFIOR=0x00;
    SPCR=0x54;
    SPSR=0x00;
    delay_ms(50);
    sodem=0;
    PORTB.5=0;
    CS=1;
    RS=0;
    delay_ms(5);
    RS=1;
    SYN=1;
    CS=0;
    spi(0x03);// MODE REGISTER
    spi(0x80); //
    spi(0x00); //filter register
    spi(0x10);
    ///////////
    spi(0x02);// mode register
    spi(0xb1);
    spi(0x80);
    wait_adc_rdy();
    #asm("sei")

    while (1)
    {


    DATA= Read_ADC ();
    sodem=DATA/200;

    };
    };
    đây là link dataheet ad7730
    http://www.analog.com/static/importe...7730_7730L.pdf

Về tác giả

Collapse

thanhtung86 Tìm hiểu thêm về thanhtung86

Bài viết mới nhất

Collapse

Đang tải...
X