Chat
Danh mục
So sánh 2 ADC  dùng pic 16F877A hiển thị LCD20x4

So sánh 2 ADC dùng pic 16F877A hiển thị LCD20x4

Số lượng:
Thêm vào giỏ
So sánh 2 ADC dùng pic 16F877A hiển thị LCD20x4 đã được thêm vào giỏ hàng


Trong project này, vi điều khiển 16f887 sẽ đọc hai kênh adc, sau đó hiển thị ra lcd, khi nhấn buttom kết nối vào PortB.f0 sẽ hiển thị kết quả so sánh, xem hình sau:



 Code:

/*
This program reads two analog input(potentiometers), stores them and then
display them on LCD, a comparison between analog values can be done by prssing a button.
Programmer: Sameer Semmoor Samroo
Share projects for pic, avr, atmega in C: http://dientudieukhien.net
*/



// LCD module connections
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RC7_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISC6_bit;
sbit LCD_EN_Direction at TRISC7_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections

//variables
unsigned long adc1, adc2;
char adc1_txt[5], adc2_txt[5];
bit oldstate;



//This function configures the settings
void _init()
{

  //LCD settings
  lcd_init();
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Cmd(_LCD_CLEAR);
 
  //ADC Configurations:
  ADCON0.B6 = 1; ADCON0.B7 = 1; //FRC (clock derived from a dedicated internal oscillator = 500 kHz max)
  ANSEL  = 0b00000011; // Configure AN0 & AN1 pin as analog
  ANSELH = 0;  // Configure other AN pins as digital I/O
 
  //A0 & A3 as input
  TRISA.f0=1; TRISA.f1=1;

  // Disable comparators
  C1ON_bit = 0;
  C2ON_bit = 0;

  ADCON0.f0 = 1; //A/D converter module is powered up

}

//This function reads analog input and display them
void read_adc_display()
{
  adc1 = adc_read(0);
  delay_ms(100);
  adc2 = adc_read(1);


  //get ADC1 digits
  adc1_txt[0] = (adc1/1000) % 10 + 48;
  adc1_txt[1] = (adc1/100) % 10 + 48;
  adc1_txt[2] = (adc1/10) % 10 + 48;
  adc1_txt[3] = (adc1%10) + 48;
  adc1_txt[4] =  '\0'; //Null
 
  //get ADC2 digits
  adc2_txt[0] = (adc2/1000) % 10 + 48;
  adc2_txt[1] = (adc2/100) % 10 + 48;
  adc2_txt[2] = (adc2/10) % 10 + 48;
  adc2_txt[3] = (adc2%10) + 48;
  adc2_txt[4] =  '\0'; //Null

  //display resutls
  lcd_out(1,1, "ADC 1 = ");
  lcd_out(1,9, adc1_txt);
 
  lcd_out(2,1, "ADC 2 = ");
  lcd_out(2,9, adc2_txt);
 
}

//Compares ADC1 with ADC2
void ADC_compare()
{
  if(adc1 < adc2)
  {
    lcd_out(4,1, "                   ");
    lcd_out(4,1, "ADC 1 < ADC 2");
  }
  else if(adc1 > adc2)
  {
    lcd_out(4,1, "                   ");
    lcd_out(4,1, "ADC 1 > ADC 2");
  }
  else
  {
     lcd_out(4,1, "                   ");
     lcd_out(4,1, "ADC 1 = ADC 2");
  }
}


void main()
{
   _init();
   while(1) //infinite loop
   {
       read_adc_display();
       delay_ms(500);
     
       //read switch input(compare)
       if (Button(&PORTB, 0, 1, 1)) {  // Detect logical one
       oldstate = 1; // Update flag
       }
       if (oldstate && Button(&PORTB, 0, 1, 0)) {   // Detect one-to-zero transition
        ADC_compare();
       oldstate = 0;// Update flag
      }
    }
}


( File mô phỏng + Code Full)

Theo dientudieukhien.net

Điện Tử | Tin Học - echipkool.com - Chia sẻ kiến thức - Kết nối đam mê điện tử