New to mikroE C

Discussion in 'Programmer's Corner' started by R!f@@, Mar 28, 2014.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    New guy here, so please go easy on me :p

    It's my second night on the easyPIC and I think madam is angry.
    But I managed to write what I need on the default PIC & LCD. i.e Stop the scrolling and add delays. And switch the Display from three different sentences as such.
    So far so good.

    My aim was to Display and read ADC.

    I thought why not go for the PIC that I would be using for my project not the big 40pin one.

    After searching I found the obvious one. A 16F88. Enough ports for 16*2 LCD and analog ones for ADC, two total I needed.

    Went through the collection and luckily I had two.

    So now to code for the 16F88 in C. Took me while but managed to change the code and programmed and works like charm.

    Thought I'll check the second F88. plugged in and hit program and mikroProg gave me Vcc voltage error :eek:. Checked the PIC and it was HOT. I managed to put in reverse. Crap ! :eek:

    Took it out and I thought I will put it back again. So tried and to my surprise it's still working. These little bugger's are tough and so was the easyPIC. !!

    Whew ! What a relief. :)

    Now I am trying to read the ADC on AN0 and AN1.

    I think I would need help but I will let you guys know after trying.

    I thought a heads up would be worth it.
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    Hi R!f@@.

    Sometimes the PICs can handle anything, and sometimes they don't...

    Here's a program I used to test on a PIC 16F688, and in C on MPLAB. I know it's a different PIC, but...

    Code ( (Unknown Language)):
    1.  
    2. // ADC test program
    3.  
    4. #include <hth.h>
    5.  
    6. #define _XTAL_FREQ 4000000
    7. #define led RA2
    8.  
    9. unsigned int adc_raw;
    10.  
    11. // Configuration
    12. __CONFIG(FOSC_XT & // Crystal/resonator on RA4 & RA5
    13. WDTE_OFF & // Watchdog timer off
    14. PWRTE_OFF &  // POwer up timer enable off
    15. MCLRE_OFF & // MCLR pin function is digital input, MCLR internally tied to VDD
    16. CP_OFF & // Code protection off
    17. CPD_OFF & // Data code protection off
    18. BOREN_OFF &  // Brown out off
    19. IESO_OFF &  // Internat switchover off
    20. FCMEN_ON); // Fail-Safe clock monitor on
    21.  
    22. // Functions
    23. void adc_init(void)
    24. {
    25. ADCON0bits.ADFM = 1; // Right justified
    26. ADCON0bits.VCFG = 0; // VDD as reference
    27. ADCON0bits.CHS = 0b111; // Analog channel set to AN7
    28. ADCON0bits.ADON = 1; // ADC is enabled
    29. ADCON1bits.ADCS = 0b001; // ADC Conversion Clock Select bit
    30. } // End adc_init
    31.  
    32. void adc_read(void)
    33. {
    34. ADCON0bits.GO = 1;
    35. __delay_us(10);
    36. while(ADCON0bits.nDONE);
    37. } // End adc_read
    38.  
    39. // Main program
    40. void main (void)
    41. {
    42. CMCON0 = 0x07; // Turn off comparators
    43. ANSEL = 0b00001000; // Analog input on RC3/AN7
    44.  
    45. TRISA = 0b00000000;
    46. TRISC = 0b00001000;
    47.  
    48. led = 0;
    49.  
    50. adc_init();
    51.  
    52. while (1)
    53. {
    54. adc_read();
    55. adc_raw = (unsigned int) ADRESH;
    56. adc_raw = adc_raw*256;
    57. adc_raw += (unsigned int) ADRESL;
    58.  
    59. if (adc_raw <= 512)
    60. {
    61. led = 1;
    62. __delay_ms(500);
    63. led = 0;
    64. __delay_ms(500);
    65. }
    66. else
    67. {
    68. led = 1;
    69. __delay_ms(100);
    70. led = 0;
    71. __delay_ms(100);
    72. }
    73. }
    74. }
    75.  
    It's the classic "Turn LED on when ADC value is..."
     
    R!f@@ likes this.
  3. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    Will this work for mikroE.?
     
  4. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    No, I do not think so. I thought it might give you some pointers on how it is in Hi tech C on Mplab. I learn pretty good from reading other code and try to convert it to what i need. :)
     
  5. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    I got to Admit this was easier than I thought, well upto now.

    I managed to control RA pins ( some as ouput ) RA0 & 1 remains as ADC input.
    Now everything is working as I want it.
    The Backlit is properly timed and buzzer too.

    Giving delays and producing sound was never this easy.

    I wrote so much in a few hrs.

    I am lovin it.

    Now comes the ADC reading part.
     
  6. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    OK this is waaaay confusing.

    I am not posting the code yet but I do need some advice.
    PIC is 16F88

    I like to Read the ADC value at 10 bit on RA0 and RA1.
    Actually they are voltage level and current (A) level. But I am now using the pots on the easyPIC Eva.
    I have now XX.XX Volts and XX.XX Amps displayed. At certain location at Row 1 & 2
    I would need up date the XX.XX characters with the ADC value.
    My top will be for now 5V and low is 0V.

    I think I would need to scan RA0, store the value, display the value at the respective LCD location and scan RA1 and do the same.

    I need some pointers as I would like to do it myself.

    What way I should go about.

    Thanks
     
  7. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    Select ADC library.

    Double-click on "ADC_Read" for help.

    Select "conversions" library

    Double-click "WordToStr" for help

    ... same again for LCD library if you have not already succeeded with LCD
     
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    A good practice when working with the ADC inputs is to read the ADC pin (use the MikroC help examples for code) and display the ADC value directly on the LCD.

    That lets you see the ADC read itself is working. Remember on the EasyPIC7 there are lots of things that can be selected to attach to the PIC pins, like the LEDs and pullup resistors, and these can affect the ADC reading.

    So I generally display the raw ADC value on the display. If you set the ADC setup to "left justify" the ADC reading you can just display the HI byte of the ADC (0-255) on the LCD using the ByteToStr() function;

    Code ( (Unknown Language)):
    1.  
    2. unsigned char txt[12]; // used to hold general display texts
    3.  
    4. ByteToStr(ADRESH,txt); // makes byte 0-255 to txt "  0" to "255"
    5. LCD_Write(0,0,txt);  // display it
    6.  
    (sorry I can't remember the exact function name for LCD_write).

    Once you have the ADC values being read and displayed ok, you can move on to the math code to convert the ADC value to volts. And display that.

    Another good trick is to pick up a 20x4 text LCD, it will plug in and work just as good as the 16x2 LCD, but gives you a couple of extra lines to display extra data during testing.
    :)
     
  9. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    OK. To begin I need some bit of explanation on the A/D Conversion Clock select bits.
    The bit 7-6 on ADCON0 of 16F88


    ADCS<1:0>
    if ADCS2=0
    00=Fosc/2
    01=Fosc/8
    10=Fosc/32
    11=FRC.

    if ADCS2=1
    00=Fosc/4
    01=Fosc/16
    10=Fosc/64
    11=FRC.

    I am using 8MHz Xtal.

    What is bit value for bit 7-6 I should use?

    My goal is to use a voltage divider from 0-20V input.
    Should use a reference.?
    I read about internal reference?
    What about external reference ?
    Any advice on this?
     
    Last edited: Mar 29, 2014
  10. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    You are Waaaay over-thinking this...

    You are not writing in assembler, you are using mikroC pro. All the hard work is done for you.

    Use the ADC library function:
    tempres = ADC_Read(n);

    For advanced applications you may need to think about the issues you mention, but to start with, just accept the defaults and ignore these registers.

    ... and yes, keep the analogue input between Vss and Vcc to avoid possible damage to the PIC. Use a voltage divider in the final product, but use the EP7 potentiometers to test with.

    Would you like a very simple starter example to get you going?
     
    R!f@@ likes this.
  11. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    Please show me an example.
     
  12. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    Forum does not seem to like .rar extension, so I added .txt extension

    Download the attachment then rename it to ADCtoLCD.rar

    .then extract
     
    R!f@@ likes this.
  13. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    Getting the hang of it.

    You were right, I was waaaaaaaaaay over thinking.
     
  14. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    Hoooray..! I did it, I did it :D.

    By looking at ur code I wrote it, the while loop below my code with in the main

    Now I can display RA0 (ASCII)value at the Voltage value characters and RA1 value at Current (A) value character location. And it changes according when I vary the two pots.

    The value at RA0 is from 2(3) to 1007(8)
    The value at RA1 is from 0(1) to 1006(7)
    * the bracket value is that the display changes as I think the pot value is not stable.

    Below is the while(1)
     
  15. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    Turn off the LEDs for PORTA if you want the full 1023

    SW3-1 to the left - off
     
  16. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    Damn ! I was not thinking about that.
    Got it. Is there a way to avoid that. I mean to use a separate supply instead of using USB power.

    OK.. Now I need to convert it to Voltage first.
     
  17. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    The problem is not with the power supply, the problem is that the current path through the LED alters the ADC voltages and makes the readings non-linear.

    The solution is not to connect anything to the ADC input pin other than the signal that you want to measure.

    converting 0-1023 into a voltage value for display will be a good test of your C programmings skills.

    ... or if you want to cheat, I would imagine that LIBSTOCK will have an example to follow. Maybe here http://www.libstock.com/projects/view/837/adc-t0-volts ... but I have not tried that program for myself, so cannot be sure it will help.

    Try not to cheat if you can resist it. You will learn more by working it out for yourself.
     
  18. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    I am working my ass off.

    As I wrote inttostrg(voltage,vstrg) from the above example compiler says I reach demo limit. Only that line is giving me that error. I commented out the ammeter part to see if the code is bigger or not but to no vail.
     
  19. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    The compiler has a chart style window that shows you the functions and their sizes.

    Since you are working with demo limits it is good to familiarise yourself with that window and see how large your functions are, and what functions the compiler has linked in by itself.

    Why do you need IntToStr() (signed) when you already use WordToStr() (unsigned)?

    Is there some reason you need to be using both unsigned 16bit vars and signed ones?
     
  20. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    I dunno if there is a reason.
    WordtoStr seems to work for now

    I don't think I need signed ones for this project.
     
Loading...