我用PIC16F54做了个按键扫描切换数码管显示的程序,数码管采用74HC164驱动,显示程序测试过了,单独的每一页都能正常显示,但一加按键程序就没反应了,朋友们,帮忙看看到底是哪的问题。
/*********PIC16F54控制数码管显示,电机调速**********/
#include <pic.h>
__CONFIG(HS&WDTDIS);
#define DAT RB3
#define CLK RB1
#define S2 RA0
#define S5 RB4
#define S6 RB0
#define S7 RB7
#define S4 RA2
#define S3 RA1
#define OUT RB5
unsigned char time_num;
unsigned char speed_num;
unsigned char display_flag;
unsigned char start;
unsigned char e;
unsigned int x;
unsigned int B;
long A;
// 显示0~9,由于此数码管管脚与74HC164 Q0-Q7管脚不是一一对应的,所以此码与正常的共阳极码表不一致
unsigned char const tab[]={0x04,0x1f,0x48,0x0a,0x13,0x22,0x20,0x0f,0x00,0x02};
//****1MS延时函数********
void Delay(unsigned char us) //5,7,9
{ while(--us); }
void DelayMS(unsigned int iMs)
{
unsigned int i,j;
for(i=0;i<iMs;i++)
for(j=0;j<65;j++)
{
Delay(1);
}
}
void senddata (unsigned char dd)
{
unsigned char c;
CLK = 0;
for(c=0;c<8;c++)
{
CLK = 0;
if(dd&0x80)
DAT = 1;
else
DAT = 0;
CLK =1;
dd = dd<<1;
}
DAT = 0;
}
void page1()
{
senddata(0xfb);
senddata(0xfb);
}
void page2()
{
senddata(tab[time_num/10]);
senddata(tab[time_num%10]);
}
void page3()
{
senddata(tab[0]);
senddata(tab[speed_num]);
}
void Ready_key()
{
if(S2==0)
{
DelayMS(80);
if(S2==0)
{
e++;
if(e%2==1)
{
start=1;
display_flag=2;
// page2();
}
else
{
start=0;
}
while(!S2);
}
}
if(start==1)
{
if(S3==0)
{
DelayMS(80);
if(S3==0)
{
display_flag=2;
time_num++;
if(time_num>=15)
time_num=15;
//page2();
}
}
if(S4==0)
{
DelayMS(80);
if(S4==0)
{
display_flag=2;
if(time_num>1)
time_num--;
if(time_num<=1)
time_num=1;
//page2();
}
}
if(S7==0)
{
DelayMS(80);
if(S7==0)
{
display_flag=3;
speed_num++;
if(speed_num>=9)
speed_num=9;
// page3();
}
}
if(S6==0)
{
DelayMS(80);
if(S6==0)
{
display_flag=3;
if(speed_num>0)
speed_num--;
if(speed_num<=0)
speed_num=0;
// page3();
}
}
}
}
void display()
{
switch(display_flag)
{
case 2: page2(); break;
case 3: page3(); break;
}
}
void main()
{
TRISA=0xff; //初始化RA5-RA0的输入输出方向
TRISB=0xd5; //初始化RB7-RB0的输入输出方向
PORTA=0xff; //初始化RA5-RA0数值
PORTB=0xd5; //初始化RB7-RB0的数值
DelayMS(300);
speed_num=0;
time_num=15;
page1();
while(1)
{
Ready_key();
display();
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
代码要这样贴更能看清楚
- /*********PIC16F54控制数码管显示,电机调速**********/
- #include <pic.h>
- __CONFIG(HS&WDTDIS);
- #define DAT RB3
- #define CLK RB1
- #define S2 RA0
- #define S5 RB4
- #define S6 RB0
- #define S7 RB7
- #define S4 RA2
- #define S3 RA1
- #define OUT RB5
- unsigned char time_num;
- unsigned char speed_num;
- unsigned char display_flag;
- unsigned char start;
- unsigned char e;
- unsigned int x;
- unsigned int B;
- long A;
- // 显示0~9,由于此数码管管脚与74HC164 Q0-Q7管脚不是一一对应的,所以此码与正常的共阳极码表不一致
- unsigned char const tab[]={0x04,0x1f,0x48,0x0a,0x13,0x22,0x20,0x0f,0x00,0x02};
- //****1MS延时函数********
- void Delay(unsigned char us) //5,7,9
- { while(--us); }
- void DelayMS(unsigned int iMs)
- {
- unsigned int i,j;
- for(i=0;i<iMs;i++)
- for(j=0;j<65;j++)
- {
- Delay(1);
- }
- }
- void senddata (unsigned char dd)
- {
- unsigned char c;
- CLK = 0;
- for(c=0;c<8;c++)
- {
- CLK = 0;
- if(dd&0x80)
- DAT = 1;
- else
- DAT = 0;
- CLK =1;
- dd = dd<<1;
- }
- DAT = 0;
- }
- void page1()
- {
- senddata(0xfb);
- senddata(0xfb);
- }
- void page2()
- {
- senddata(tab[time_num/10]);
- senddata(tab[time_num%10]);
- }
- void page3()
- {
- senddata(tab[0]);
- senddata(tab[speed_num]);
- }
- void Ready_key()
- {
- if(S2==0)
- {
- DelayMS(80);
- if(S2==0)
- {
- e++;
- if(e%2==1)
- {
- start=1;
- display_flag=2;
- // page2();
- }
- else
- {
- start=0;
- }
- while(!S2);
- }
- }
- if(start==1)
- {
- if(S3==0)
- {
- DelayMS(80);
- if(S3==0)
- {
- display_flag=2;
- time_num++;
- if(time_num>=15)
- time_num=15;
- //page2();
- }
- }
- if(S4==0)
- {
- DelayMS(80);
- if(S4==0)
- {
- display_flag=2;
- if(time_num>1)
- time_num--;
- if(time_num<=1)
- time_num=1;
- //page2();
- }
- }
- if(S7==0)
- {
- DelayMS(80);
- if(S7==0)
- {
- display_flag=3;
- speed_num++;
- if(speed_num>=9)
- speed_num=9;
- // page3();
- }
- }
- if(S6==0)
- {
- DelayMS(80);
- if(S6==0)
- {
- display_flag=3;
- if(speed_num>0)
- speed_num--;
- if(speed_num<=0)
- speed_num=0;
- // page3();
- }
- }
- }
- }
- void display()
- {
- switch(display_flag)
- {
- case 2: page2(); break;
- case 3: page3(); break;
- }
- }
- void main()
- {
- TRISA=0xff; //初始化RA5-RA0的输入输出方向
- TRISB=0xd5; //初始化RB7-RB0的输入输出方向
- PORTA=0xff; //初始化RA5-RA0数值
- PORTB=0xd5; //初始化RB7-RB0的数值
- DelayMS(300);
- speed_num=0;
- time_num=15;
- page1();
- while(1)
- {
- Ready_key();
- display();
- }
- }
复制代码一周热门 更多>