DG128的3×3矩阵扫描求帮忙。这几天头都痛了。。。上拉电阻怎么用?3×3扫描程序失败好多次。新生求教。
#include /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include
#define C8LED PORTA
#define P3 PORTB
#pragma MESSAGE DISABLE C4000
byte LED_num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98};byte key=0;
void delayms (int m) {int x,y;for(x=0;x
void display(key) C8LED=LED_num[key];
void matrixkeyscan()
{
byte temp;
P3=0x01;
temp=P3;
temp=temp&0xf0;
if(temp!=0x00)
{
delayms(50);
temp=P3;
temp=temp&0xf0;
if(temp!=0x00)
{
temp=P3;
switch(temp)
{
case 0x11:
key=1;
break;
case 0x21:
key=2;
break;
case 0x41:
key=3;
break;
}
while(temp!=0x00)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0x02;
temp=P3;
temp=temp&0xf0;
if(temp!=0x00)
{
delayms(50);
if(temp!=0x00)
{
temp=P3;
switch(temp)
{
case 0x12:
key=4;
break;
case 0x22:
key=5;
break;
case 0x42:
key=6;
break;
}
while(temp!=0x00)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0x04;
temp=P3;
temp=temp&0xf0;
if(temp!=0x00)
{
delayms(50);
if(temp!=0x00)
{
temp=P3;
switch(temp)
{
case 0x14:
key=7;
break;
case 0x24:
key=8;
break;
case 0x44:
key=9;
break;
}
while(temp!=0x00)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
}
void main(void)
{
DDRA=0xff;
DDRB=0x0f;PUCR=0X02;
while(1)
{
void matrixkeyscan()
;}
}
程序改完了,不过我想实现数码管四位都亮。一个口就不能控制所有的段选型号和位选信号。
PA0—PA7为数码管a b c d e f g dg
PB1—PB6为矩阵键盘 123行456列
位选信号即数码管1234连VCC高电平
功能:四位数码管同时显示对应按键
#include /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include
#define C8LED PORTA
#define P3 PORTB
#define display(key) C8LED=LED_num[key]
#pragma MESSAGE DISABLE C4000
byte LED_num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98};int key=0;
void delayms (int m) {int x,y;for(x=0;x
void matrixkeyscan()
{
byte temp;
P3=0xe0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(50);
temp=temp&0x0f;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0d:
key=1;
break;
case 0x0b:
key=2;
break;
case 0x07:
key=3;
break;
}
}
}
P3=0xd0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(50);
temp=temp&0x0f;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0d:
key=4;
break;
case 0x0b:
key=5;
break;
case 0x07:
key=6;
break;
}
}
}
P3=0xb0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(50);
temp=temp&0x0f;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0d:
key=7;
break;
case 0x0b:
key=8;
break;
case 0x07:
key=9;
break;
}
}
}
display(key);
}
void main(void)
{
DDRA=0xff;
DDRB=0xf0;
while(1)
{
matrixkeyscan();
}
}
我想变化的实现按键控制,怎么办。我想用K口控制,可是没调好。求大家指点。