adc pic16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by ramya77, Apr 22, 2015.

pls anyone explain this...i couldnt fix it

  1. embedded

    0 vote(s)
    0.0%
  2. pic16f877a

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. ramya77

    Thread Starter New Member

    Apr 22, 2015
    7
    0
    here is the adc pgm interfacing with lcd..i m having doubt on this c code

    #include<pic.h>

    #define _XTAL_FREQ 20000000

    void conversion(unsigned int rx_con);

    void command(unsigned int rx_cmd);

    void string(unsigned char *rs_string);

    void data(unsigned int rx_data);

    void main()

    {

    unsigned int a,b,e,f,c,d;

    TRISA1=1;

    TRISE=0x00;

    TRISB=0x00;

    ADCON0=0x0d;

    ADCON1=0x82;

    command(0x01);

    command(0x06);

    command(0x0c);

    command(0x38);

    command(0x80);

    while(1)

    {

    {

    ADGO=1;

    while(ADGO==1);

    //pls explain thse steps alone sir..i couldn’t fix it..what about 256 & 49

    a=ADRESH;

    b=ADRESL;

    a=a*256;

    c=a+b;

    c=c*.49;


    conversion(c);

    command(0x80);

    data('T');

    }

    }

    }

    void command(unsigned int rx_cmd)

    {

    PORTE=0x04;

    PORTB=rx_cmd;

    __delay_ms(10);

    RE2=0;

    }

    void data(unsigned int rx_data)

    {

    PORTE=0x05;

    PORTB=rx_data;

    __delay_ms(10);

    RE2=0;

    }

    void conversion(unsigned int rx_con)

    {

    data(((rx_con/1000)+0x30));

    data((((rx_con%1000)/100)+0x30));

    data(((((rx_con%1000)%100)/10)+0x30));

    data(((((rx_con%1000)%100)%10)+0x30));

    }
     
  2. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    The ADC code only

    Code (C):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. #define _XTAL_FREQ 20000000
    4.  
    5.  
    6. #define DAD PORTB
    7.  
    8.  
    9. main(){
    10.     TRISB=0X00;
    11. TRISA = 0xff ;
    12. ADCON1=0b00000000;
    13. ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
    14.     ADIF=0;
    15.     ADIE=1;
    16.     PEIE=1;
    17. while(1){
    18.  
    19.     //GIE=1;
    20.  
    21.     __delay_us(10);
    22. GO_DONE=1;
    23.     __delay_us(10);
    24.  
    25. PORTB=ADRESH;
    26.     }
    27. }
     
  3. ramya77

    Thread Starter New Member

    Apr 22, 2015
    7
    0
     
  4. ramya77

    Thread Starter New Member

    Apr 22, 2015
    7
    0
    i m asking about the code which i shown in green colour sir


    a=ADRESH;

    b=ADRESL;

    a=a*256;

    c=a+b;

    c=c*.49;
     
  5. Alberto

    Active Member

    Nov 7, 2008
    169
    36
    Variable a contains the high byte
    Variable b contains the low byte
    Variable c contains the word ( 16 bits value)
    0.49 is the convertion coefficient from ADC Count to some real value.

    Cheers

    Alberto
     
  6. ramya77

    Thread Starter New Member

    Apr 22, 2015
    7
    0
    thanks alberto..but wat is a=a*256 & y we doing that...actually "a " is right justified (2 bit) & "b" is 8bit..it is configured in ADCON1=0x82(means 10000010)..
    my doubt is y we multiplying 256 with "a"??????????????whether we making 2 bit to 8bit!!!!!!!!!!!!!!!!!!!!
    sorry if i m confusing u
     
  7. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    Which micro is this see the total bits of ADC!
    the a,b are the unsigned int of 16bits.
    the resultant value of ADC is stored in A,b variable for further processing!
    the binary is converted to other resultant value may be some temperature etc etc

    a=ADRESH;

    b=ADRESL;
     
  8. ramya77

    Thread Starter New Member

    Apr 22, 2015
    7
    0
    thank u sir,, sorry sir i m fresher only i m just started to learn things..actually my doubt was not abot conversion...my doubt is how the high & low value is stored...and "a " have 2 bit ..while "b" have "8"bit...(we mentioned right justification in ADCON1)..am i correct or not
     
  9. ramya77

    Thread Starter New Member

    Apr 22, 2015
    7
    0
    anyone pls explain i^2c pgm in 8051 (hitech c)...pllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss.........
     
  10. ramya77

    Thread Starter New Member

    Apr 22, 2015
    7
    0
    I WANT ANY TO HELP ME..I HAVE DOUBT ON THIS RED COLOURED AREA...PLS EXPLAIN ANYONE...I M STRUGGLING FOR THREE DAYS
    //Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
    #include<reg51.h>
    #include<intrins.h> //For using [_nop_()]
    sbit sda=P1^0;
    sbit scl=P1^1;
    sbit led=P0^3;
    bit ack;
    sbit led1=P0^1;
    sfr lcd_data_pin=0xA0;//p2 port
    sfr output=0x80;//p0 port
    sbit rs=P3^0;
    sbit rw=P3^1;
    sbit en=P3^6;
    unsigned char reead,write,write2,i,j;
    unsigned int temp;
    void delay(unsigned int count)
    {
    int i,j;
    for(i=0;i<count;i++)
    for(j=0;j<1275;j++);
    }
    void lcd_command(unsigned char comm)
    {
    lcd_data_pin=comm;
    en=1;
    rs=0;
    rw=0;
    delay(1);
    en=0;
    }
    void lcd_data(unsigned char disp)
    {
    lcd_data_pin=disp;
    en=1;
    rs=1;
    rw=0;
    delay(1);
    en=0;
    }
    lcd_dataa(unsigned char *disp)
    {
    int x;
    for(x=0;disp[x]!=0;x++)
    {
    lcd_data(disp[x]);
    }
    }
    void lcd_ini()
    {
    lcd_command(0x38); // for using 8-bit 2 row LCD
    delay(5);
    lcd_command(0x0F); // for display on, cursor blinking
    delay(5);
    lcd_command(0x80);
    delay(5);
    }
    void aknowledge() //acknowledge condition
    {
    scl=1;
    _nop_();
    _nop_();
    scl=0;

    }
    void start() //start condition
    {
    sda=1;
    scl=1;
    _nop_(); //No operation
    _nop_();
    sda=0;
    scl=0;
    }
    void stop() //stop condition
    {
    sda=0;
    scl=1;
    _nop_();
    _nop_();
    sda=1;
    scl=0;
    }
    void send_byte(unsigned char value) //send byte serially
    {
    unsigned int i;
    unsigned char send;
    send=value;
    for(i=0;i<8;i++)
    {
    sda=send/128; //extracting MSB
    send=send<<1; //shiftng left
    scl=1;
    _nop_();
    _nop_();
    scl=0;

    }
    ack=sda; //reading acknowledge
    sda=0;

    }
    unsigned char read_byte() //reading from EEPROM serially
    {
    unsigned int i;
    sda=1;
    reead=0;
    for(i=0;i<8;i++)
    {
    reead=reead<<1;
    scl=1;
    _nop_();
    _nop_();
    if(sda==1)
    reead++;
    scl=0;
    }
    sda=0;
    return reead; //Returns 8 bit data here
    }

    void save() //save in EEPROM
    {
    start();
    send_byte(0xA0); //device address
    aknowledge();
    send_byte(0x00); //word address
    aknowledge();
    send_byte(5); //send data
    aknowledge();
    send_byte(65);
    aknowledge();
    stop();
    if(ack==0)
    {
    led1=1;
    delay(100);
    led1=0;
    delay(100);
    lcd_command(0x86);
    lcd_data('5');
    lcd_command(0x87);
    lcd_data('A');
    }
    else
    led1=1;
    aknowledge();
    }
    void Read()
    {
    start();
    send_byte(0xA0);
    aknowledge();
    send_byte(0x00);
    aknowledge();
    start();
    send_byte(0xA1); //device address
    aknowledge();
    i=read_byte();
    aknowledge();
    j=read_byte();
    aknowledge();
    stop();
    if(i==5)
    {
    led=0;
    delay(100);
    led=1;
    delay(100);
    write=i+48;
    lcd_command(0xC6);
    lcd_data(write);
    }
    else
    led=1;
    if(j==65)
    {
    lcd_command(0xC7);
    lcd_data(j);
    }
    aknowledge();
    }

    void main()
    {
    lcd_ini();
    lcd_dataa("Sent :");
    lcd_command(0xC0);
    lcd_dataa("Read :");
    temp=0;
    while(1)
    {
    save();
    Read();
    }
     
Loading...