打印

在使用PIC16F1936学习编程时,写了一个独立铵键的程序遇到的问题

[复制链接]
994|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wd516|  楼主 | 2014-11-3 10:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在使用PIC16F1936学习编程时,写了一个独立铵键的程序,烧录后,按键操作,部分失灵。
按SW2后,LED2亮,再按SW1,SW3,SW4,SW5无反应,按程序来看应该是按一下相应灯会亮才对。
但先按其它按键,最后按SW2,LED灯都会点亮。由于在家自学,无人请教,故发贴寻求帮忙,程序如下:
/***********************************************/
//* Include headfile
/***********************************************/

#include <pic.h>
#include <pic16f1936.h>

__CONFIG(0x0FE4);
__CONFIG(0x1DFF);

#define uint unsigned int
#define uchar unsigned char
#define LED6 RA0
#define LED8 RB5
#define SW2 RA4
#define SW1 RA5
#define SW4 RA6
#define SW3 RA7
#define SW7 RB0
#define LED2 RC1
#define SW5 RC2
#define SW6 RC6
#define LED9 RC7
#define LED7 RB4
#define LED10 RB1
#define LED1 RC0
#define LED3 RA3
#define LED4 RA2
#define LED5 RA1

uint result;

void init(void);
void keyscan();
void Light(uint x);
void delay(uint n);
void main()
{
        result=0x00;
        init();
       
while(1)
        {
                keyscan();
                Light(result);
//                        delay(500);
//                        LED2=!LED2;       
        }
       
}
void init(void)
{
        OSCCON=0b01101010;    //4M 内部晶振
        ANSELA=0;    //RA口为普通I/0口)
        ANSELB=0;
        TRISA=0xf0;                         //RA7:4 定义为输入,RA3:0 定义为输出
        TRISB=0b10000001;                         //
        TRISC=0b00010000;         //通信时,RC5/SDO为输出,RC4/SDI为输入,RC3/SCK为输出
        TRISE=0x04;                       
        PORTA=0xff;
        PORTB=0xff;
        PORTC=0xff;       
}       


/**********************************************
        Function :keyscan
***********************************************/
void keyscan()
{
        if(SW1==0)
        {       
                delay(10);
                if(SW1==0)
                result=0x01;
        }
        if(SW2==0)
        {
                delay(10);
                if(SW2==0)
                result=0x02;       
        }
        if(SW3==0)
        {
                delay(10);
                if(SW3==0)
                result=0x04;
        }
        if(SW4==0)
        {
                delay(10);
                if(SW4==0)
                result=0x08;
        }
        if(SW5==0)
        {
                delay(10);
                if(SW5==0)
                result=0x10;
        }
        if(SW6==0)
        {
                delay(10);
                if(SW6==0)
                result=0x20;
        }
        if(SW7==0)
        {
                delay(10);
                if(SW7==0)
                result=0x40;
        }
       
}

void Light(uint x)
{
        switch (x)
        {
                case 0x00:
                {LED7=0;LED9=0;LED10=0;break;}
                case 0x01:
                {
                        LED4=0;result=0;break;
                }
                case 0x02:
                {
                        LED5=0;result=0;break;       
                }
                case 0x04:
                {
                        LED3=0;result=0;break;
                }
                case 0x08:
                {
                        LED1=0;result=0;break;
                }
                case 0x10:
                {
                        LED2=0;result=0;break;
                }
                case 0x20:
                {
                        LED6=0;result=0;break;
                }
                case 0x40:
                {
                        LED8=0;result=0;break;
                }
        }       
}

void delay(uint n)              
{
        uint i;
        char k;                  //定义整形变量
        for (i=0;i<n;i++)     //延时
        {       
                for(k=246;k>0;k--);
        }       
}
沙发
wangch_sh| | 2014-11-4 11:56 | 只看该作者
没有判断按键释放。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝