QC

Đọc Trạng Thái Button PIC dùng Mlap

 Đọc Trạng Thái Button PIC dùng Mlap

Yêu cầu bài toán: 

* Chân C0 Nối với Led 1 : nhấp nháy liên tục delay 100 ms.

* Chân C1 Nối với Led 2: Sáng khi Button nối với B0 Nhấn.

Sơ đồ shematic như hình.


Code chương trình

#include <xc.h>
#define _XTAL_FREQ 20000000 //define crystal frequency to 20MHz

void main(void) 
{
TRISC0=0; //set RC0 pin as a digital output pin
TRISC1=0; //set RC0 pin as a digital output pin
TRISB0=1; //set RC0 pin as a digital output pin
RC0=0;
RC1=0;
while(1)
{
   RC0 = 1;                  // set RC0 pin to logic High &  turn on 
   __delay_ms(100);  //add delay of 1 second 
   RC0 = 0;                  // set RC0 pin to logic low & turn off
  __delay_ms(100);  //add delay of 1 second
  
  if(RB0==1)
  {
    RC1=1;  
  }
  else
  {
      RC1=0;
  }
  

}
return;
}
GIẢI THÍCH CODE:

* Cấu hình Thạch anh 20 MHZ.

#define _XTAL_FREQ 20000000 //define crystal frequency to 20MHz
Bạn có thể cấu hình theo yêu cầu dư án của bạn 8Mhz hay 12 Mhz ...

* CẤU HÌNH OUTPUTS.

TRISC0=0; //set RC0 pin as a digital output pin
TRISC1=0; //set RC0 pin as a digital output pin
TRISC0 =0 ; // Chân C0 dùng làm xuất dữ liệu.
TRISC1 =0 ; // Chân C1 dùng làm xuất dữ liệu.


* CẤU HÌNH INPUTS.
TRISB0=1; //set RC0 pin as a digital output pin
TRISB0 =1; // Chân B0 làm INPUT đọc dữ liệu từ Button.

* LED 1 NỐI RC0, LED 2 NỐI RC1.
CẤU HÌNH 2 LED LÚC ĐẦU TẮT.

RC0=0;
RC1=0;

* CHƯƠNG TRÌNH BLYNK LED RC0 - LED 1.

   RC0 = 1;                  // set RC0 pin to logic High &  turn on 
   __delay_ms(100);  //add delay of 1 second 
   RC0 = 0;                  // set RC0 pin to logic low & turn off
  __delay_ms(100);  //add delay of 1 second
* CHƯƠNG TRÌNH ĐỌC NÚT NHẤN RB0.

  if(RB0==1)
  {
    RC1=1;  // LED 2 BẬT
  }
  else
  {
      RC1=0; // LED 2 TẮT
  }



















































Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...