Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MikroC - ADC and Compare

Status
Not open for further replies.

wzhn

Newbie level 2
Joined
Jun 4, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,298
Hi,

I'm very (VERY) new to programming. Currently, I am learning the ADC module for the PC16F887 and what I'm attempting to do is as follow:

Receive TWO analog inputs on the input ports, put them through ADC conversion, compare them by a simple logic and then output a HIGH on any of the pre-determined output ports.

Like any beginner, I tried with the ADC library example first and then modify the codes to play with it as I learn. Tried searching for beginner level programming but just couldn't find an appropriate one. Helpful feedbacks are much appreciated.

unsigned int volt_1, volt_2;

void main()
{
ANSEL = 0x06; // Configure AN2 and AN1 pin as analog
ANSELH = 0; // Configure other AN pins as digital I/O
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;

TRISA = 0xFF; // PORTA is input
TRISB = 0x00; // PORTB is output
TRISC = 0x00; // PORTC is output


do
{
volt_1 = ADC_Read(1); // Get 10-bit results of AD conversion
volt_2 = ADC_Read(2);

if (volt_1 > volt_2)
{
PORTC = 0xFF;
delay_ms(500);
}
else
{
PORTB = 0xFF;
delay_ms(500);
}
}

while(1);
}

attached: is my simulation circuit

 

use mikro c help ,it has its own help and examples for adc, i2c etc
 

use mikro c help ,it has its own help and examples for adc, i2c etc

Well, that's what I'm doing. Learning from its examples. But it cant tell me what I'm doing it wrong, can it?:razz:
 

MikroE ADC Example :

Code:
/*
 * Project name:
     ADC_on LEDs (Display the result of ADC on LEDs)
 * Copyright:
     (c) Mikroelektronika, 2011.
 * Revision History:
     20110929:
       - initial release (FJ);
 * Description:
      A simple example of using the ADC library.
      ADC results are displayed on PORTC and PORTD.
 * Test configuration:
     MCU:             PIC18F45K22
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf
     Dev.Board:       EasyPIC7 - ac:ADC
                      http://www.mikroe.com/easypic/
     Oscillator:      HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
     Ext. Modules:    None.
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/mikroc/pic/
 * NOTES:
     - Turn on PORTC and PORTD LEDs on SW3.3 and SW3.4.
     - To simulate analog input on ADC channel 1, use on-board potentiometer P1
       and place jumper J15 to MCU pin corresponding to ADC channel 1 input.
 */

#include <built_in.h>

unsigned int adc_rd;

void main() {
  ANSELA = 0x02;             // Configure RA1 pin as analog
  ANSELC = 0;                // Configure PORTC pins as digital
  ANSELD = 0;                // Configure PORTD pins as digital
  
  TRISA = 0x02;              // Set RA1 pin as input
  TRISC = 0x00;              // Set PORTC as output
  TRISD = 0x00;              // Set PORTD as output

  while (1) {
    adc_rd = ADC_Read(1);    // get ADC value from 1st channel
    LATC = adc_rd;           // display adc_rd[7..0]
    LATD = Hi(adc_rd);       // display adc_rd[9..8]
  }
}



Do you want to compare one ADC value with second ADC value, and if value is higher then turn some LED or something else ?
 

Do you want to compare one ADC value with second ADC value, and if value is higher then turn some LED or something else ?
i am searching for this problem .. i..e. to compare one adc value with other ... any suggestion
 


Code C - [expand]
1
2
3
if(adcresult1 > adcresult2){ //do something }
else if(adcresult1 < adcresult2){ //do some other thing }
else if(adcresult1 == adcresult2){ //do still some other thing }

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top