How to program a ADC for a PIC6f690 with c++

This is a discussion on How to program a ADC for a PIC6f690 with c++ within the C++ Programming forums, part of the General Programming Boards category; Hello Gents, I need to program a ADC for a PIC6f690 A variable resistor (0...5V) is my analog input and ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    How to program a ADC for a PIC6f690 with c++

    Hello Gents,

    I need to program a ADC for a PIC6f690
    A variable resistor (0...5V) is my analog input and the ANSEL bits gives me the output result (0...255). I want to display the 4 leds. leds 1 is on when the output is lower the 32, led 2 is on when the output is between the 32 and 63, led 3 is on between the 64 and the 127 and led 4 is on higher then 127.

    Can somebody help me with writting the C++ code.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    How about.
    Code:
    void main ( ) {
      while ( 1 ) {
        int resistorValue = getResistorValue();  // A function YOU need to write
        if ( resistorValue < 32 ) {
          lightLed(1);  // A function YOU need to write
        } else if ( resistorValue < 64 ) {
          lightLed(2);
        } // and so on
      }
    }
    Yes we can help with the generalities of programming, but for device specific bit bashing like reading a specific port register, you need to show some initiative by reading the device programmers manual, and posting some actual code.


    Perhaps ask on forums dedicated to the device in question, if you're stuck on a device specific issue -> Microchip
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Hello Salem,

    First thank you for the quick reply.
    I know how to install the port registers.
    Here is my code what i have make:

    Code:
    main()
    { ANSEL = 0b10000001;
    ANSELH = 0;
    TRISA=0x01; // PORTA as Anlog input */
    TRISC=0x00; // PORTC as output */
    while(1) /* loop */
    {
    ADCON0=0b10000001; 
    ADCON1= 0b01010000; 
    
    ADIE = 0; /* Masking the interrupt */
    ADIF = 0; /* Resetting the ADC interupt bit */ 
    GO_DONE=1; /* Staring the ADC process */ 
    while(GO_DONE); /* Wait for conversion complete */
    
    ADC_VALUE = (ADRESL); 
    
    if (ADC_VALUE < 32) PORTC=displayportc[1]
    else if (ADC_VALUE > 32 & <64) PORTC=displayportc[2]
    
    if (ADC_VALUE > 64 & < 128) PORTC=displayportc[3]
    else if (ADC_VALUE <255 & > 128) PORTC=displayportc[4]
    
    }
    }
    Is this okay?

    Thanks allready for the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Replies: 5
    Last Post: 08-17-2007, 12:43 AM
  3. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 12:53 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21