do something when adc reaches a certain value from a variable resistor (MikroC programming)

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
Hey guys, I have created a code where adc conversion takes place from a variable resistor and displays it on screen (0-1023)...thankfully it is successful, however, i want my program to display something on the screen when a certain value is reached for example, if the adc value reaches 500 on the LCD i want it to display the letter A on the screen and if it reaches 600 it displays B? Do i need an interrupt or a timer or can it be easily done?

Any help or guidance would be highly Appreciated. :)

Thank you

the MCU is PIC16F877A

THE CODE:

Code:
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;

void main() {
     char adc []= "ADC value=";
     char *temp = "0000";
     unsigned int adc_value;
     ADCON0 = 0b10001000; //AN1 analog channel selected
     ADCON1 = 0b01001001;  //PORT configuratoin to AN1
     TRISA = 0b00000010; //RA1 analogue input
     Lcd_Init();
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Out(1,1,adc);
                 do {
                        adc_value = ADC_Read(1); //read from channel 1
                        temp [0] = adc_value/1000 +48 ; //add 48 to get the ascii value
                        temp [1] = (adc_value/100) %10  +48 ;
                        temp [2] = (adc_value/10) %10 +48;
                        temp [3] = (adc_value/1)%10 +48;
                        Lcd_Out(1,11,temp);
                        Delay_ms(100);
                    } while (1)
}
 

spinnaker

Joined Oct 29, 2009
7,837
You are already doing it. All you need to do is to add an if statement inside the do loop.

Code:
if  (adc_value >= 500 &&  adc_value < 600)
{
     Lcd_Out(1,11,"A");

}

if  (adc_value >= 600 )
{
     Lcd_Out(1,11,"B");

}
 

shteii01

Joined Feb 19, 2010
4,647
You are already doing it. All you need to do is to add an if statement inside the do loop.

Code:
if  (adc_value >= 500 &&  adc_value < 600)
{
     Lcd_Out(1,11,"A");

}

if  (adc_value >= 600 )
{
     Lcd_Out(1,11,"B");

}
What Spin wrote.
You are reading ADC and storing the reading in adc_value. Now that you have it stored, you can do whatever you want with it. Spin's two if statements simply run the compassing according to the conditions you listed. If condition is met, print the letter on LCD, then move to the next line of code. If condition is not met, nothing is printed on LCD and next line of code is executed.
 
Top