יום רביעי, 29 ביוני 2011

תוכנה להפעלת AD0804

        rd equ P1.0           ;Read signal P1.0
        wr equ P1.1           ;Write signal P1.1
        cs equ P1.2           ;Chip Select P1.2
        intr equ P1.3         ;INTR signal P1.3

        adc_port equ P2       ;ADC data pins P2
        adc_val equ 30H       ;ADC read value stored here

        org 100H
start:                    ;Start of Program
        acall conv            ;Start ADC conversion
        acall read            ;Read converted value
        mov P3,adc_val        ;Move the value to Port 3
        sjmp start            ;Do it again

conv:                     ;Start of Conversion
        clr cs                ;Make CS low
        clr wr                ;Make WR Low
        nop
        setb wr               ;Make WR High
        setb cs               ;Make CS high
wait:
        jb intr,wait          ;Wait for INTR signal
        ret                   ;Conversion done

read:                     ;Read ADC value
        clr cs                ;Make CS Low
        clr rd                ;Make RD Low
        mov a,adc_port        ;Read the converted value
        mov adc_val,a         ;Store it in local variable
        setb rd               ;Make RD High
        setb cs               ;Make CS High
        ret                   ;Reading done
----------------------------------------------------------------------------------
#include <REG89X51.H>
#define adc_port P2              //ADC Port
#define rd P1_0                  //Read signal P1.0
#define wr P1_1                  //Write signal P1.1
#define cs P1_2                  //Chip Select P1.2
#define intr P1_3                //INTR signal P1.3

void conv();                     //Start of conversion function
void read();                     //Read ADC function

unsigned char adc_val;

void main(){
        while(1){                    //Forever loop
                conv();                  //Start conversion
                read();                  //Read ADC
                P3 = adc_val;            //Send the read value to P3
        }
}

void conv(){
        cs = 0;                      //Make CS low
        wr = 0;                      //Make WR low
        wr = 1;                      //Make WR high
        cs = 1;                      //Make CS high
        while(intr);                 //Wait for INTR to go low
}

void read(){
        cs = 0;                      //Make CS low
        rd = 0;                      //Make RD low
        adc_val = adc_port;          //Read ADC port
        rd = 1;                      //Make RD high
        cs = 1;                      //Make CS high
}

אין תגובות:

הוסף רשומת תגובה