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

Discussion in 'Embedded Systems and Microcontrollers' started by rocker123uk, Dec 6, 2015.

  1. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    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 (Text):
    1. // Lcd pinout settings
    2. sbit LCD_RS at RB4_bit;
    3. sbit LCD_EN at RB5_bit;
    4. sbit LCD_D7 at RB3_bit;
    5. sbit LCD_D6 at RB2_bit;
    6. sbit LCD_D5 at RB1_bit;
    7. sbit LCD_D4 at RB0_bit;
    8.  
    9. // Pin direction
    10. sbit LCD_RS_Direction at TRISB4_bit;
    11. sbit LCD_EN_Direction at TRISB5_bit;
    12. sbit LCD_D7_Direction at TRISB3_bit;
    13. sbit LCD_D6_Direction at TRISB2_bit;
    14. sbit LCD_D5_Direction at TRISB1_bit;
    15. sbit LCD_D4_Direction at TRISB0_bit;
    16.  
    17. void main() {
    18.      char adc []= "ADC value=";
    19.      char *temp = "0000";
    20.      unsigned int adc_value;
    21.      ADCON0 = 0b10001000; //AN1 analog channel selected
    22.      ADCON1 = 0b01001001;  //PORT configuratoin to AN1
    23.      TRISA = 0b00000010; //RA1 analogue input
    24.      Lcd_Init();
    25.      Lcd_Cmd(_LCD_CLEAR);
    26.      Lcd_Out(1,1,adc);
    27.                  do {
    28.                         adc_value = ADC_Read(1); //read from channel 1
    29.                         temp [0] = adc_value/1000 +48 ; //add 48 to get the ascii value
    30.                         temp [1] = (adc_value/100) %10  +48 ;
    31.                         temp [2] = (adc_value/10) %10 +48;
    32.                         temp [3] = (adc_value/1)%10 +48;
    33.                         Lcd_Out(1,11,temp);
    34.                         Delay_ms(100);
    35.                     } while (1)
    36. }
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,009
    You are already doing it. All you need to do is to add an if statement inside the do loop.

    Code (Text):
    1.  
    2.  
    3. if  (adc_value >= 500 &&  adc_value < 600)
    4. {
    5.      Lcd_Out(1,11,"A");
    6.  
    7. }
    8.  
    9. if  (adc_value >= 600 )
    10. {
    11.      Lcd_Out(1,11,"B");
    12.  
    13. }
    14.  
    15.  
     
  3. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,388
    497
    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.
     
    rocker123uk likes this.
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,886
    1,009
    Yeah you have the hard part done. The rest of it is easy. :)
     
  5. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    Awesome guys, it worked! thank you :)
     
Loading...