ADC Control IN PIC

Discussion in 'Embedded Systems and Microcontrollers' started by samterminator, Apr 28, 2014.

  1. samterminator

    Thread Starter New Member

    Mar 21, 2013
    5
    0
    I want to write a program in C that takes the input of the potentiometer in the pickit. The program should rotate the 4 LED in the pickit board left or right depending on the direction that the potentiometer has been rotated. if the arrow of the potentiometer points to the right the LED's should start to rotate to the right. Also the speed of rotation of the LED's should depend on how far from the center the arrow on the potentiometer points. Moreover, I would like to use the pushbutton on the board as a pause switch. So when the pushbutton is pressed the LED's should stop at their current position even when the potentiometer is rotating. Any help would be appreciated thanks.
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Ok which compiler do you use, as a starter. Have you any experience in C or using C on a micro?
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    What assembler or compiler will you use? (XC8 is a great choice.)

    What development board do you have? (PICkit 2 has two different boards.)

    Have you seen the Microchip lessons for their boards? (The older board can be quite hard to find.)
     
  4. samterminator

    Thread Starter New Member

    Mar 21, 2013
    5
    0
    I am using the pickit 3 and PIC16f1829. I have little experience with microcontrollers. The biggest problem I have now is reading the values from the potentiometer in other words converting analog to digital. this way i could set ranges of when which LED goes on or off according to which way i turn my potentiometer. I know that i have to set a reference voltage of 5v which is 255 in digital then go from there but thats about it i am stuck for the moment on this part. I have the following pseudo code written for now , the top part is where i am stuck.

    Code ( (Unknown Language)):
    1.  
    2. main: ; start program
    3.  
    4.  readadc wiper, val  ; read 8-bit ADC value into variable val
    5.  
    6. if val = max then top ;    jump to label top if true
    7. if val > near_max then middle_1  ;
    8. if val > middle AND val < near_max then middle_2  ;  
    9. if val < middle then bottom  ;
    10.  
    11. goto main  ;jump back to the start
    12.  
    13.  
    14. top:   ; label
    15. HIGH LED1
    16. LOW LED2
    17. LOW LED3
    18. LOW LED4
    19. goto main ; done jump back to start
    20.  
    21. middle_1:   ; label
    22. LOW LED1
    23. HIGH LED2
    24. LOW LED3
    25. LOW LED4
    26. goto main  ; done jump back to start
    27.  
    28. middle_2:
    29. LOW LED1
    30. LOW LED2
    31. HIGH LED3
    32. LOW LED4
    33. goto main  ; done jump back to start
    34.  
    35. bottom:  ; label
    36. LOW LED1
    37. LOW LED2
    38. LOW LED3
    39. HIGH LED4
    40.  
    41. goto main  ; done jump back to start
    42.  
     
    Last edited by a moderator: Apr 29, 2014
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You need to break your code up into parts you can understand before you will get anywhere.

    Try figuring out how to use the ADC, then determine the relationship between voltage and potentiometer shaft position. From there, you can determine how to illuminate the LEDs to correspond properly.

    You will need to open the datasheet. There is no other way. Look at the section on the ADC, starting at page 151, and read how to use it (paying particular attention to section 16.2.6 on page 157), then read up on the pertinent registers(ADCON0(pg.158), ADCON1(pg.159), ANSELx, etc). If after you've done this you need help, feel free to ask.

    Note I will be the third to ask, what compiler are you using?

    By the way, 5V is not 255 in digital. 5V is 255 when using a 5V reference voltage and an 8 bit representation.
     
    Last edited: Apr 29, 2014
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Have you written a working "hallo world" program like lightning a LED and then getting it to blink. This exercise my look simple but it is quite useful in order to learn some compiler basics
    Oh by the way your ADC in your PIC is 10 bit not 8 bit
     
    samterminator likes this.
  7. samterminator

    Thread Starter New Member

    Mar 21, 2013
    5
    0
    thanks for the help , i am using xc8 as my compiler.
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    A common mistake made by a beginner is to not set the configuration bits properly. So thats why I am asking. Have you made a very simple program that works on your PIC? Instead of going around in circles just post your code. What ever it may be it may give us an idea why struggle
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    That makes it sound like you have the PICkit 3 Low Pin Count Demo Board. That link has some resources, including some excellent lesson plans to teach you the basics of these things.

    The bad news is they are written in assembler, but you should at least read thru them. The same concepts apply to C programs.
     
    samterminator likes this.
  10. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    This code is a bit nasty;
    Code ( (Unknown Language)):
    1.  
    2. if val = max then top ;    jump to label top if true
    3. if val > near_max then middle_1  ;
    4. if val > middle AND val < near_max then middle_2  ;
    5. if val < middle then bottom  ;
    You are testing using 4 different comparison types; =, >, > AND <, <.
    Ouch.

    A better system is to use only > comparison, and a final default;
    Code ( (Unknown Language)):
    1.  
    2. if val > C then top ;
    3. if val > B then middle_1  ;
    4. if val > A then middle_2  ;
    5. goto bottom  ; default
    6.  
    This is much easier to work with and debug. :) Then you just need to assign the 3 numeric values to the constants A, B and C.
     
    samterminator likes this.
  11. samterminator

    Thread Starter New Member

    Mar 21, 2013
    5
    0
    Thank you guys for your input. I did the following exercise to get more aquaited with the PIC. I would like to know if this is correct then i will go back and do the ADC one. Well the following code will count how many time a user presses the press button then output the value using the leds in the board. note that port RA2 is the push button, and port c is the 4 LEDs i have. let me know if this is correct thanks.

    Code ( (Unknown Language)):
    1.  
    2. void main()
    3.         {
    4.     double count;
    5.         TRISC = 0 ;     // set PORTC as OUTPUT
    6.         TRISA = 1 ;     // set PORTA as OUTPUT
    7.  
    8.         for( ; ; )         // forever
    9.     {  
    10.       if (count==16)
    11.         count=0;
    12.       else if (PORTA.RA2==0)  // if no input
    13.         NOP();          // No operation
    14.       else if (PORTA.RA2==1)   // if the is input
    15.         count= count+1;         // increment the variable
    16.         PORTC= count;        // print the number of increments
    17.     }
    18.     }
    19.  
     
    Last edited by a moderator: Apr 30, 2014
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    First have you downloaded this http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit3_Starter_Kit.zip

    In the folder ....src/pic16/c you will find complete Source code in C for many basic tasks. They have included projects for both MPLAB and MPLABX. Just select open project in the file menu. As tip ALWAYS work with projects then using MPLAB(X).
    Also then you post your code. Include ALL the code from the first include # to the end like this. Never just the main function as important information will be left out. Programming is attention to details;)
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h> //PIC hardware mapping
    3. //config bits that are part-specific for the PIC16F1829
    4. __CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
    5. __CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF);
    6. /* -------------------LATC-----------------
    7. * Bit#: -7---6---5---4---3---2---1---0---
    8. * LED: ---------------|DS4|DS3|DS2|DS1|-
    9. *-----------------------------------------
    10. */
    11. //Every program needs a `main` function
    12. void main(void) {
    13. TRISCbits.TRISC0 = 0; //using pin as output
    14. LATC = 0; //clear all pins to 0
    15. LATCbits.LATC0 = 1; //turn ON the LED by writing to the latch
    16. while(1) continue; //sit here forever doing nothing, think "while(true), continue in this loop"
    17. }
    18.  
     
    ErnieM likes this.
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Awesome, looks like you found tutorials for that board in C!

    That makes samterminator's job easier.
     
  14. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The great thing here is you have to tools to check this yourself. The PICkit (2 or 3) can be used to debug code running inside your PIC.

    With the debugger you can run the program one line at a time, see what happens or what doesn't happen. You can have a watch window to observe the value of any register or variable.

    (I would check it some but I don't have any time this morn on my way out to work.)
     
Loading...