ADC Control IN PIC

Thread Starter

samterminator

Joined Mar 21, 2013
5
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.
 

ErnieM

Joined Apr 24, 2011
8,415
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.)
 

Thread Starter

samterminator

Joined Mar 21, 2013
5
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.

Rich (BB code):
main: ; start program

 readadc wiper, val  ; read 8-bit ADC value into variable val

if val = max then top ;    jump to label top if true
if val > near_max then middle_1  ; 
if val > middle AND val < near_max then middle_2  ;  
if val < middle then bottom  ; 

goto main  ;jump back to the start


top:   ; label
HIGH LED1
LOW LED2
LOW LED3
LOW LED4
goto main ; done jump back to start 

middle_1:   ; label
LOW LED1
HIGH LED2
LOW LED3
LOW LED4
goto main  ; done jump back to start

middle_2:
LOW LED1
LOW LED2
HIGH LED3
LOW LED4
goto main  ; done jump back to start

bottom:  ; label
LOW LED1
LOW LED2
LOW LED3
HIGH LED4

goto main  ; done jump back to start
 
Last edited by a moderator:

tshuck

Joined Oct 18, 2012
3,534
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:

t06afre

Joined May 11, 2009
5,934
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
 

t06afre

Joined May 11, 2009
5,934
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
 

THE_RB

Joined Feb 11, 2008
5,438
This code is a bit nasty;
Rich (BB code):
if val = max then top ;    jump to label top if true
if val > near_max then middle_1  ;
if val > middle AND val < near_max then middle_2  ; 
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;
Rich (BB code):
if val > C then top ; 
if val > B then middle_1  ;
if val > A then middle_2  ; 
goto bottom  ; default
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.
 

Thread Starter

samterminator

Joined Mar 21, 2013
5
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.

Rich (BB code):
void main()
        {
    double count;
        TRISC = 0 ;     // set PORTC as OUTPUT
        TRISA = 1 ;     // set PORTA as OUTPUT

        for( ; ; )         // forever
    {  
      if (count==16)
        count=0;
      else if (PORTA.RA2==0)  // if no input 
        NOP();          // No operation
      else if (PORTA.RA2==1)   // if the is input
        count= count+1;         // increment the variable
        PORTC= count;        // print the number of increments
    }
    }
 
Last edited by a moderator:

t06afre

Joined May 11, 2009
5,934
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.

Rich (BB code):
void main()
        {
    double count;
        TRISC = 0 ;     // set PORTC as OUTPUT
        TRISA = 1 ;     // set PORTA as OUTPUT
 
        for( ; ; )         // forever
    {  
      if (count==16)
        count=0;
      else if (PORTA.RA2==0)  // if no input 
        NOP();          // No operation
      else if (PORTA.RA2==1)   // if the is input
        count= count+1;         // increment the variable
        PORTC= count;        // print the number of increments
    }
    }
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;)
Rich (BB code):
#include <htc.h> //PIC hardware mapping
//config bits that are part-specific for the PIC16F1829
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF);
/* -------------------LATC-----------------
* Bit#: -7---6---5---4---3---2---1---0---
* LED: ---------------|DS4|DS3|DS2|DS1|-
*-----------------------------------------
*/
//Every program needs a `main` function
void main(void) {
TRISCbits.TRISC0 = 0; //using pin as output
LATC = 0; //clear all pins to 0
LATCbits.LATC0 = 1; //turn ON the LED by writing to the latch
while(1) continue; //sit here forever doing nothing, think "while(true), continue in this loop"
}
 

ErnieM

Joined Apr 24, 2011
8,415
I did the following exercise to get more aquaited with the PIC.
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.)
 
Top