SuperTao 发表于 2012-12-27 11:21:19

AVR引脚输入问题

小弟初学AVR,我把引脚设置成输入状态,然后接地和电源,值都不变。

xiaodao35 发表于 2012-12-27 11:26:17

怎么设置的,贴图贴代码!

SuperTao 发表于 2012-12-27 11:30:42

void jp_init()
{
    PORTB=0xff;
    DDRB=0xff;
}
u8 jp_data(u8 a)
{
    delay_us(3);
    DDRB=0xff;
    delay_us(3);
    PORTB=a;
    delay_us(3);
    DDRB=0x0f;
    delay_us(3);
    a=PORTB;
    return a;
}
void jp()
{
        if (jp_data(0xf0)!=0xf0)
        {
                switch(jp_data(0xfe))
                {
                        case 0xee:
                                key=1;                               
                                break;
                        case 0xde:
                                key=2;                               
                                break;
                        case 0xbe:
                                key=3;                               
                                break;
                }
                switch(jp_data(0xfd))
                {
                        case 0xed:
                                key=4;                       
                                break;
                        case 0xdd:
                                key=5;                               
                                break;
                        case 0xbd:
                                key=6;                               
                                break;
                }
                switch(jp_data(0xfb))
                {
                        case 0xeb:
                                key=7;                               
                                break;
                        case 0xdb:
                                key=8;                               
                                break;
                        case 0xbb:
                                key=9;                               
                                break;
                }
                if (jp_data(0xf7)==0xe7)
                {
                        key=0;
                }
        }

}
这个是矩阵键盘程序

SuperTao 发表于 2012-12-27 11:32:00

#define led0 PORTA.0
#define led1 PORTA.1
#define led2 PORTA.2
#define led3 PORTA.3
#define led4 PORTA.4
#define led5 PORTA.5
#define led6 PORTA.6
#define led7 PORTA.7
    DDRB=0x00;
    PORTA=0xff;
    DDRA=0xff;
      while(1)
    {
       if (!(PORTB&0x01))
       led0=0;
       else
       led0=1;
      
    }
这个是我测试输入引脚的代码

SuperTao 发表于 2012-12-27 11:32:25

xiaodao35 发表于 2012-12-27 11:26 static/image/common/back.gif
怎么设置的,贴图贴代码!

您看看有什么问题吗?

qhshilin 发表于 2012-12-27 11:35:55

#define led7 PORTA.7
错误
AVR端口不能位寻址
使用
#define    SET_BIT(X,Y)         (X|=(1<<Y))       //将某位置1操作
#define    CLR_BIT(X,Y)         (X&=~(1<<Y))   //将某位置0操作DDRB=0x00;
    PORTA=0xff;
    DDRA=0xff;
      while(1)
    {
       if (!(PORTB&0x01))
         CLR__BIT(PORTB,0) ;         
    else
         SET_BIT(PORTB,0);      
    }

xiaodao35 发表于 2012-12-27 11:41:29

SuperTao 发表于 2012-12-27 11:32 static/image/common/back.gif
您看看有什么问题吗?

DDRX=1是输出口!看看数据手册先!

SuperTao 发表于 2012-12-27 11:42:33

qhshilin 发表于 2012-12-27 11:35 static/image/common/back.gif
#define led7 PORTA.7
错误
AVR端口不能位寻址


在CVAVR中,是支持PORTA.x的
不过别的软件不支持,
你的程序非常好,学习了,谢谢。

jimmy_xt 发表于 2012-12-27 12:36:05

{:sad:}输入寄存器是PINX……

SuperTao 发表于 2012-12-27 12:40:45

jimmy_xt 发表于 2012-12-27 12:36 static/image/common/back.gif
输入寄存器是PINX……

{:shy:} 看的不详细,多谢了。

李二牛 发表于 2013-2-5 17:02:06

顶,顺便学习

djnxqc 发表于 2013-5-21 08:38:46

qhshilin 发表于 2012-12-27 11:35 static/image/common/back.gif
#define led7 PORTA.7
错误
AVR端口不能位寻址


值得借鉴{:biggrin:}

djnxqc 发表于 2013-5-21 08:39:15

qhshilin 发表于 2012-12-27 11:35 static/image/common/back.gif
#define led7 PORTA.7
错误
AVR端口不能位寻址


{:biggrin:}
页: [1]
查看完整版本: AVR引脚输入问题