PIC16F877A 的IO口輸入和輸出 單按鍵輸入,單LED點(diǎn)亮
掃描二維碼
隨時(shí)隨地手機(jī)看文章
IO輸出和輸入
/*
按鍵連接RB1,點(diǎn)亮LED,PORTA RA0控制輸出。按一次點(diǎn)亮,再按一次熄滅,如此循環(huán)。
RB外接上拉電阻。
*/
#include
#define uchar unsigned char
#define uint unsigned int
// CONFIG
#pragma config FOSC = HS //
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
uchar count;
void delay(uint x)
{
uint y,z;
for(y=x;y>0;y--)
for(z=25;z>0;z--);
}
void Init_GPIO()
{
TRISA = 0X00; // RA0 output direction
PORTA=0X00;
TRISB = 0x02; // RB1 input
PORTB = 0x00;
}
void Keyscan()
{
//temp = PORTB&0x02;
if(RB1==0)
{
delay(200); // delay time 根據(jù)機(jī)械按鍵的抖動(dòng)情況調(diào)整,
if(RB1==0)
{
count++;
}
}
}
void main(void)
{
count = 0;
Init_GPIO();
while(1)
{
Keyscan();
if((count%2)==1)
{
PORTA=0x01;
}
else
PORTA = 0x00;
}
}