1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

result is not written to adc register because it has been locked.

Discussion in 'AVR' started by mudassir shah, Jul 19, 2014.

  1. mudassir shah

    mudassir shah New Member

    Joined:
    Jul 19, 2014
    Messages:
    6
    Likes:
    0
    Location:
    pakistan
    Hello;


    I am fresh here. I am working on atmega8. I want to read analog voltage from its ADC0 pin. I write code for it in mikroc pro for avr. But it gives me error that “result is not written to adc register because it has been locked.” It reads only first simple and then stopped. I don’t know how to solve it??? Anybody have any idea about it? Circuit and code are attached.


    Code is:

    Code (text):
    // LCD module connections

    sbit LCD_RS at PORTD0_bit;

    sbit LCD_EN at PORTD1_bit;

    sbit LCD_D4 at PORTD2_bit;

    sbit LCD_D5 at PORTD3_bit;

    sbit LCD_D6 at PORTD4_bit;

    sbit LCD_D7 at PORTD5_bit;


    sbit LCD_RS_Direction at DDD0_bit;

    sbit LCD_EN_Direction at DDD1_bit;

    sbit LCD_D4_Direction at DDD2_bit;

    sbit LCD_D5_Direction at DDD3_bit;

    sbit LCD_D6_Direction at DDD4_bit;

    sbit LCD_D7_Direction at DDD5_bit;

    // End LCD module connections


    unsigned int ADC = 0,adc_result=0;

    unsigned char display[7];



    void main() {
       
        Lcd_Init(); // Initialize LCD
       
        Lcd_Cmd(_LCD_CLEAR); // Clear display
       
        Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
       
       
        Lcd_Out(1,1,"ADC:"); // Write text in first row
       
       
        ADIE_bit = 1;
       
        ADEN_bit = 1;
       
        ADFR_bit = 0;
       
        ADIF_bit = 0;
       
        ADPS2_bit = 1;
       
        ADPS1_bit = 1;
       
        ADPS0_bit = 1;
       
       
       
        ADMUX = 0b01000000;
       
        while(1){
           
            //ADIE_bit = 1;
           
            ADSC_bit = 1;
           
            while(!ADIF_bit);
           
            ADIF_bit = 0;
           
            ADC = (ADCH * 256) + ADCL;
           
            IntToStr(ADC,display);
           
            Lcd_Out(2,1,display);
           
        }
       
    }
    i get error in uploading the circuit.
     
    Last edited by a moderator: Jul 19, 2014
  2. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,514
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    Hello, Mudassir Shah, welcome to ETO!

    I have placed your code inside code tags so that the formatting is preserved. Next time you post code, you can go to the "insert" button, select "code", and paste your code inside the tags. It makes things much neater and easier to read ;)

    As for your question, what is the error you get? It would be very useful to know this information.

    Regards,
    Matt
     
  3. mudassir shah

    mudassir shah New Member

    Joined:
    Jul 19, 2014
    Messages:
    6
    Likes:
    0
    Location:
    pakistan
    i am facing error in uploading image, error is:

    "there was a problem uploading your file. new bitmap.bmp"
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    This assumes you are not allowing enough time to complete the conversion..

    Place a tiny delay after the line...

    ADSC_bit = 1;
     
  6. mudassir shah

    mudassir shah New Member

    Joined:
    Jul 19, 2014
    Messages:
    6
    Likes:
    0
    Location:
    pakistan
    thanks for reply.
    i am not changing ADC channel and and its reference as you can see it in the code.
    according to your instructions, i added 1ms delay but still i am getting the same error.
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    We have one or two really good AVR programmers here at ETO... I'm sure one will shed some light on your problem...

    The datasheet locks the ADC registers at the start of conversion, regardless of a channel change. Read the specification in the MikroC manual... I predominantly use Pic's so some AVR functions are alien to me...
     
  8. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,510
    Likes:
    189
    Location:
    Prague, Czechia (not Chechnya)
    Are you using atmel studio for this, or some other toolchain? Can you debug the device over jtag?
     
  9. mudassir shah

    mudassir shah New Member

    Joined:
    Jul 19, 2014
    Messages:
    6
    Likes:
    0
    Location:
    pakistan
    i am using mikroc pro for avr and proteus.
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    On the MikroC forum the only reference to locked registers is if the port hasn't been set up correctly..

    Check the port direction registers
     
  11. mudassir shah

    mudassir shah New Member

    Joined:
    Jul 19, 2014
    Messages:
    6
    Likes:
    0
    Location:
    pakistan
    i define the port direction according to your instruction. but still i am facing the same problem. new code is
    Code (text):
    // LCD module connections
    sbit LCD_RS at PORTD0_bit;
    sbit LCD_EN at PORTD1_bit;
    sbit LCD_D4 at PORTD2_bit;
    sbit LCD_D5 at PORTD3_bit;
    sbit LCD_D6 at PORTD4_bit;
    sbit LCD_D7 at PORTD5_bit;

    sbit LCD_RS_Direction at DDD0_bit;
    sbit LCD_EN_Direction at DDD1_bit;
    sbit LCD_D4_Direction at DDD2_bit;
    sbit LCD_D5_Direction at DDD3_bit;
    sbit LCD_D6_Direction at DDD4_bit;
    sbit LCD_D7_Direction at DDD5_bit;
    // End LCD module connections

    unsigned int ADC = 0,adc_result=0;
    unsigned char display[7];


    void main() {
      Lcd_Init();                        // Initialize LCD
      Lcd_Cmd(_LCD_CLEAR);               // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

      Lcd_Out(1,1,"ADC:");                 // Write text in first row

           ADIE_bit = 1;
           ADEN_bit = 1;
           ADFR_bit = 0;
           ADIF_bit = 0;
           ADPS2_bit = 1;
           ADPS1_bit = 1;
           ADPS0_bit = 1;

           ADMUX = 0b01000000;
           DDRC.F0 = 0;

           while(1){
                ADSC_bit = 1;
            while(!ADIF_bit);
            ADIF_bit = 0;
            ADC = (ADCH * 256) + ADCL;
                IntToStr(ADC,display);
                Lcd_Out(2,1,display);
           }
    }
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Post your problem on the MikroC forum!!

    Why don't you just use the inbuilt function "readAdc()"
     
  13. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    ONLINE
    ADC result register is 16 bit double buffered register. When you read the ADCL (low byte), the 16bit register update is locked until you read the ADCH (high byte).
    If you read ADCH first and then ADCL, the register will stay locked, preventing any new conversion to be written in the result register.
    (And do not use variable names like "ADC", that may already be reserved).

    Code (C):

    /* Safe way to read ADC result register */
    adc_result = ADCL; /* Read the low byte */
    adc_result += (ADCH<<8); /* Read and add the high byte */
     

    Also, you may have serious problem here:
    ADC = (ADCH * 256) + ADCL;
    The variable "ADC" may already be defined as the adc result register.. and writing to that is not allowed. Change the name of that variable.
     
    Last edited: Jul 23, 2014
    • Agree Agree x 3
    • Thanks Thanks x 1
  14. mudassir shah

    mudassir shah New Member

    Joined:
    Jul 19, 2014
    Messages:
    6
    Likes:
    0
    Location:
    pakistan
    thanks MisterT, now its working.
     
  15. Ajit Jadhav

    Ajit Jadhav New Member

    Joined:
    Oct 27, 2015
    Messages:
    1
    Likes:
    0
    I was also going through same problem :(.. Thanks:)
     

Share This Page