Đọc giá trị ADC xuất led 7seg with CCS

1:11:00 AM
Chào các bạn trong bài đăng này mình sẽ hướng dẫn các bạn đọc giá trị ADC xuất lên 4 led 7seg sử dụng PIC16F877A.

Ảnh mô phỏng protues.




Đây là code chương trình.

#include <16F877A.h>
#device *16adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

#use delay(clock=4000000)


int8 maled[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int32 giatri,dv,chuc,tram,nghin ;
int32 dem;
void tachso( int32 giatri)
{
  nghin= giatri/1000;
  tram = (giatri/100)%10;
  chuc = (giatri/10)%10; 
  dv = (giatri)%10;
  if(nghin==0)
{
   if(tram==0)
   {
   if(chuc==0)
   {
  output_bit(pin_b0,1);
  output_bit(pin_b1,1);
  output_bit(pin_b2,1);
  output_bit(pin_b3,0);
  output_c(maled[dv]);
  delay_ms(10);
   }
   else
   {
  output_bit(pin_b0,1);
  output_bit(pin_b1,1);
  output_bit(pin_b2,0);
  output_bit(pin_b3,1);
  output_c(maled[chuc]);
  delay_ms(10);
   output_bit(pin_b0,1);
  output_bit(pin_b1,1);
  output_bit(pin_b2,1);
  output_bit(pin_b3,0);
  output_c(maled[dv]);
  delay_ms(10);
   }
   }
   else
   {
  output_bit(pin_b0,1);
  output_bit(pin_b1,0);
  output_bit(pin_b2,1);
  output_bit(pin_b3,1);
  output_c(maled[tram]);
  delay_ms(10);
  output_bit(pin_b0,1);
  output_bit(pin_b1,1);
  output_bit(pin_b2,0);
  output_bit(pin_b3,1);
  output_c(maled[chuc]);
  delay_ms(10);
  output_bit(pin_b0,1);
  output_bit(pin_b1,1);
  output_bit(pin_b2,1);
  output_bit(pin_b3,0);
  output_c(maled[dv]);
  delay_ms(10);
   }
}
else
{
  output_bit(pin_b0,0);
  output_bit(pin_b1,1);
  output_bit(pin_b2,1);
  output_bit(pin_b3,1);
  output_c(maled[nghin]);
  delay_ms(10);
  output_bit(pin_b0,1);
  output_bit(pin_b1,0);
  output_bit(pin_b2,1);
  output_bit(pin_b3,1);
  output_c(maled[tram]);
  delay_ms(10);
  output_bit(pin_b0,1);
  output_bit(pin_b1,1);
  output_bit(pin_b2,0);
  output_bit(pin_b3,1);
  output_c(maled[chuc]);
  delay_ms(10);
  output_bit(pin_b0,1);
  output_bit(pin_b1,1);
  output_bit(pin_b2,1);
  output_bit(pin_b3,0);
  output_c(maled[dv]);
  delay_ms(10);
}
}



  

void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_2);
set_adc_channel(0);
dem =0 ;
set_tris_c(0x00); // xuat data
set_tris_b(0x00);// kich led
output_c(0xff);

while (true)
  {
   giatri =read_adc();
   tachso(giatri);
  }
}

Link download project.
http://adf.ly/11512871/adc-7seg

Share this

Related Posts

Previous
Next Post »