PIC Lab, PIC16, Experiment #2: Button connection

uC: PIC16F628a

Compiler: Hi-TECH PICC

Goal: To connect button and learn how to enable IO

Button connection

One from simplest things you can try with microcontroller – to connect button and make uC to do what you want.

Just a simple code there:

#include <htc.h>
#define _XTAL_FREQ 4000000 //Clock 4MHz
unsigned char sRB1=0;  //led is off initially
void main() {
 TRISB = 0b11111101;  // B2 output
 RB1 = 0;             // make В1 0
 for (;;) {
 if (RB0 == 0) {
 __delay_ms(30);  //debounce
     if (RB0 == 0) { sRB1 = !sRB1;}
 RB1 = sRB1;

Debounce is realized with delay macro, easy not optimal, but easy…

1 thought on “PIC Lab, PIC16, Experiment #2: Button connection

  1. Pingback: PIC мк. Эксперимент №2. Подключение кнопки. | PIC микроконтроллеры

Leave a Reply

Your email address will not be published.