15 Haziran 2018 Cuma

PIC 16f84 ile 4 Digit 7 Segment ileri geri sayıcı

Pic 16f84, 74HC595, 7404 ve 4 adet ortak anotlu 7 segment display ile yapılmış 0-9999 sayıcı devresidir. Devrede artırma butonu ile ileri sayarken eksiltme butonu ile de sayıları eksiltir reset butonu ise sıfırlamak için kullanılır. 12voltla çalışır 7805 entegresiyle gerilim PIC'in çalışa bileceği  5volt'a düşürülmüştür.

MALΖEME LİSTESİ:
1-7805
2-4MHz kristal
3-2adet 22pf
4-100nf
5-4k7
6-7adet 330hom
7-3adet 10k
8-16F684A
9-2adet 1k

10-3adet buton
11-7404
13-4digit ortak anotlu displey
14-74HC595

DEVRE ŞEMASI:












CCSC KODU:
#include <16F628A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 4000000)
#use fast_io(b)
#use fast_io(a)

#define data_pin PIN_b0
#define clock_pin PIN_b1
#define latch_pin PIN_b2

short s;                                   // Used to know buttons position
unsigned int j, digit ;
unsigned long i = 0;
unsigned int seg(unsigned int num)
{
  switch (num) 
  {
    case 0 : return 0x80;
    case 1 : return 0xF2;
    case 2 : return 0x48;
    case 3 : return 0x60;
    case 4 : return 0x32;
    case 5 : return 0x24;
    case 6 : return 0x04;
    case 7 : return 0xF0;
    case 8 : return 0;
    case 9 : return 0x20;
    }
}
void write_data(unsigned int number)
{
  for(j = 0x80; j > 0; j = j >> 1) 
  {
     if(number & j)
       output_high(data_pin);
     else
       output_low(data_pin);
     output_high(clock_pin);
     output_low(clock_pin);
   }
     output_high(latch_pin);
     output_low(latch_pin);
}
void main(){
  port_b_pullups(TRUE);                  // Enable PORTB pull-ups
  output_b(0);                           // PORTB initial state
  set_tris_b(0x18);                      // Configure RB3 & RB4 pins as inputs
  output_a(0);                           // PORTD initial state
  set_tris_a(0);                         // Configure PORTD pins as inputs
   
  while(TRUE){
    if(input(PIN_b3) && input(PIN_b4))
      s = 1;
    if(s == 1) 
    {
      if(input(PIN_b3) == 0) 
      {
       s = 0;
       i++;
       if(i > 9999)
       //if(i > 99)
         i = 0;     
      }
      if(input(PIN_b4) == 0) 
      {
       s = 0;
       if(i < 1)
         i = 1;
       i--;
      }    
    }
    digit = seg(i % 10);                 // Prepare to display ones
    output_a(0x0F);                      // Turn off all displays
    write_data(digit);
    output_a(0x07);                      // Turn on display for ones
    delay_ms(1);
    digit = seg((i / 10) % 10);          // Prepare to display tens
    output_a(0x0F);                      // Turn off all displays
    write_data(digit);
    output_a(0x0B);                      // Turn on display for tens
    delay_ms(1);
    digit = seg((i / 100) % 10);         // Prepare to display hundreds
    output_a(0x0F);                      // Turn off all displays
    write_data(digit);
    output_a(0x0D);                      // Turn on display for hundreds
    delay_ms(1);
    digit = seg((i / 1000) % 10);        // Prepare to display thousands
    output_a(0x0F);                      // Turn off all displays
    write_data(digit);
    output_a(0x0E);                      // Turn on display for thousands
    delay_ms(1);
  }

}

Hiç yorum yok:

Yorum Gönder