Nút bấm với 12f629

12f629 rẻ, nhỏ gọn có 8 chân, 2 chân nguồn 6 chân dữ liệu, bài viết dưới đây nói về mạch sử dụng 1 nút bấm để chuyển trang thái cho các chân ra đông thời hiển thị LED 

Do Pin GP3 chỉ làm input nên dùng để nhận nút bấm điều khiển, còn lại 5 chân dùng để xuất tín hiệu điều khiển:

-       2 chân dùng hiển thị LED GP4, GP5 (2bit -> 4 trạng thái, 0-1-2-3) kết hợp với IC 7447

-       3 chân dùng xuất dữ liệu điều khiển 3 thiết bị GP0, GP1, GP2

Để nạp code, chubgs ta cần nối mạch theo thứ tự Pickit2 pinout như sau:

  1. VPP nối pin 4
  2. VDD nối 1
  3. VSS nối pin 8
  4. DAT nối pin 7
  5. CLK nối pin 6

Code mẫu:

#include <12F629.h>

 

#FUSES NOWDT                    //No Watch Dog Timer

#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT

#FUSES NOCPD                    //No EE protection

#FUSES NOPROTECT                //Code not protected from reading

#FUSES NOMCLR                     //Master Clear pin not in use

#FUSES NOPUT                    //No Power Up Timer

#FUSES NOBROWNOUT               //No brownout reset

#FUSES BANDGAP_HIGH         

#FUSES RESERVED                 //Used to set the reserved FUSE bits

 

#byte TRISA = 0x85

 

#bit RA5 =0x05.3

#bit RA4 =0x05.3

#bit RA3 =0x05.3

#bit RA2 =0x05.2

#bit RA1 =0x05.1 

#bit RA0 =0x05.0

 

#use delay(INTERNAL)

 

#define DATA_B PIN_A4 //Pin 3

#define DATA_A PIN_A5 //Pin 2

#define SW1 PIN_A0

#define SW2 PIN_A1

#define SW3 PIN_A2

 

void display(int choice);

int choice;

 

void main()

{

 

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);

   setup_timer_1(T1_DISABLED);

   setup_comparator(NC_NC);

   setup_vref(FALSE);

  

   trisa = 0b00001000;  

  

   choice=read_eeprom(0x01); //default

   if (choice==0) choice=2;   //for the first in use

  

   while(true)

   {

      //check for button pressed        

      if(RA3==0){       

         if(choice<3) choice++;

         else

            choice=1;

         delay_ms(100);

         delay_ms(100);

         //delay 200 ms is ok

         write_eeprom(0x01,choice);//update the choice in to EEPROM if there is change

      }

     

      display(choice);

   }

  

}

 

void display(int choice)

{

   if(choice==1){

         output_high(DATA_A);

         output_low(DATA_B);

         //--

         output_high(SW1);

         output_low(SW2);

         output_low(SW3);

      }

   if(choice==2){

         output_high(DATA_B);

         output_low(DATA_A);

         //--

         output_high(SW2);

         output_low(SW1);

         output_low(SW3);

      }

   if(choice==3 ){

         output_high(DATA_B);

         output_high(DATA_A);

         //--

         output_high(SW3);

         output_low(SW1);

         output_low(SW2);

      }  

}