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

code error

Discussion in 'AVR' started by Adam2014, Aug 18, 2014.

  1. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    hi
    i upload a frequency meter code & schematic
    but there is an error in measurement ...
    for example if i apply an square wave with 5000Hz ,,, LCD display it about 40khz !!!
    what is the error ?
     

    Attached Files:

    • fff.txt
      File size:
      2.8 KB
      Views:
      234
    • fff.PNG
      fff.PNG
      File size:
      47.8 KB
      Views:
      216
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,142
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Code (c):
    freq = dur + i*65536; /* calculate the frequency as in previous equation */
    dur and i are declared as int's..... overflow!!!

    Cast to longs or declare them as longs...
     
  3. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    thanks
    I'll try again
     
  4. dave

    Dave New Member

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


     
  5. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0

    when i rebuild the the code and restart compile
    i see this message "Linker error: the program has no 'main' function"
    and proteus can't work with new edit !!!
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,142
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Post your corrections...

    Changing an int to a long can't stop compilation...
     
  7. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    Hi
    thanks Ian Rogers
    i solve the problem :)
    i have a questions !
    Q1) i need to build a bottom to make a force calibration when i press it .
    how i can do it in my code ?
    the main idea i want to do is "re execute the calibration subroutine" when there is an interrupt from calibration bottom


    Q2) how i can monitor the pin state depending on the external switch state ..
    i mean how i can know the pin is High/Low using atmega16 & c code
     
    Last edited: Aug 19, 2014
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,142
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    I normally use a button to give a low to the micro pin ( pulled up internally ) then poll the pin for changes..
    If you write a small function that can be called from your super loop, that will analyze a range of pins to report a button press. Then you can act on this report...

    ie something like this...
    Code (c):

    int keyhit()
       {
       if(!PIN1) return 1;
       if(!PIN2) return 2;
       return 0;
       }
     
    You will need a debounce.... I use a 200mS delay to debounce..
    Code (c):

    int keyhit()
       {
       int key=0;
       if(!PIN1) key += 1;
       if(!PIN2) key += 2;
       if(key) delayMs(200);
       return key
       }
     
     
    • Like Like x 1
  9. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    thanks :)
    i define a bottom ca=PORTD.0; // do a forced calibration
    how i can make a forced calibration at any time if (ca=High) ,,, even if the Micro controller do another task
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,142
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    When you say "bottom" I was assuming "Button"

    If you poll a button each cycle of a super loop its usually fast enough not to miss a press.. However... If you need to you can implement an interrupt to take care of the button press..... If you have capture compare CCP module.. you can detect a single button press and have automatic noise rejection as you can program the module to ensure the key is pressed for a minimum time before registering it..
     
  11. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    Hi
    I use code vision avr to program my micro controller atmega16a
    when i run the program it still work with the internal oscillation 1MHZ even if i set timer with external clock 16Mhz
    " AVR Core Clock frequency: 16.000000 MHz "
    so that the value showed is multiple by 16
    what the solution to have the real value ?
    i try CKSEL=0xf; but with that instruction there is a syntax error
     
  12. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,486
    Likes:
    185
    Location:
    Prague, Czechia (not Chechnya)
    If I remeber correctly the clock settings for the ATmega series are done in fuses, not in code. You need to set this in your programming tool.
     
  13. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    HI
    what the problems with my code to display capacitor value ?
    because it display the value on all 16 digits LCD Screen ???

    //-----------disply Capacitor function --------------------//
    float dis_capacitor(unsigned long int c)

    {

    float cf;
    lcd_clear();
    lcd_putsf("Capacitor=");
    lcd_gotoxy(0,1);
    if(c>=0.000001)
    {
    cf=(float)c*1000000;
    ftoa(cf,3, buffer);
    lcd_puts(buffer);
    lcd_putsf("uF");
    }
    else if (c>=0.000000001)
    {
    cxxf=(float)c*1000000000;
    ftoa(cf,3, buffer);
    lcd_puts(buffer);
    lcd_putsf("nF");
    }
    else
    {
    ltoa(cf, buffer);
    lcd_puts(buffer);
    lcd_putsf("pF");
    }
    delay_ms(3000);
    return 0;
    lcd_clear();
    lcd_gotoxy(0,0);
    }
     
  14. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,486
    Likes:
    185
    Location:
    Prague, Czechia (not Chechnya)
    If c is an int, then it will be allways true, unless c is zero. Not sure about the rest.
     
  15. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    HI
    when i run the program it take long time to execute
    the attached image show the MCU set ,,, what is the problem?
     

    Attached Files:

    • crys.PNG
      crys.PNG
      File size:
      45.3 KB
      Views:
      161
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,142
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    When you "run" your simulation... go into debug mode and see what's causing the slow down....

    Have you managed a clean compile??? Otherwise the hex file is never updated...

    Step through the program and keep an eye on the "warnings" in the status bar...
     
  17. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    Hi
    when i run LC meter the LCD display an initial value with out any DUT (device under test)
    How i can edit my code to subtract it from the reading thus LCD display an initial value 0.000 ?
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,142
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    You need to "Map" your input to your LCD output.... If you google you will find examples of mapping.

    Mapping...
    Display_Value = (((Read_Value - Read_Value_zero_figure) * Displayed_Value_span_figure ) / Read_Value_Span_figure) - Display_Value_Zero_figure....
     
  19. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    thanks Ian Rogers
    I'm working now with ADC ...
    i want to convert voltage from analog signal to digital and then store it in parameter Vo
    where the MCU store its digital value that equal Vo
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,142
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    WHY ARE YOU SHOUTING!!!

    I'm not sure what you mean... If you create a variable to hold the result... You need to fetch the value stored in the result register... I can't remember it's name on the AVR chips... The "Map" function should resemble my example above..
     
  21. Adam2014

    Adam2014 Member

    Joined:
    Jan 25, 2014
    Messages:
    109
    Likes:
    0
    SHOUTING !!!
    in my circuit design x=f(V0) ,,, so i want to measure voltage using ADC and then calculate x
     
    • Dislike Dislike x 1

Share This Page