当前位置:首页 > 单片机 > 单片机
[导读]IO输出和输入/*按键连接RB1,点亮LED,PORTA RA0控制输出。按一次点亮,再按一次熄灭,如此循环。RB外接上拉电阻。*/#include #define uchar unsigned char#define uint unsigned int// CONFIG#pragma config FOSC =

IO输出和输入

/*

按键连接RB1,点亮LED,PORTA RA0控制输出。按一次点亮,再按一次熄灭,如此循环。

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 根据机械按键的抖动情况调整,

if(RB1==0)

{

count++;

}

}

}

void main(void)

{

count = 0;

Init_GPIO();

while(1)

{

Keyscan();

if((count%2)==1)

{

PORTA=0x01;

}

else

PORTA = 0x00;

}

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭