p1口接阳极数码管 p2.0接一个按键 每按一次键数码管的数加1【中断方式】 直到9 循环进行
我是学机械的,但是老师要求做单片机上机考试,完全没有学过c语言和汇编,在网上查的结果又不是用中断方式,求高手能不能帮助下写段程序,一定要是中断方式才行,万分感激
#include < reg52.h > // 嵌入51单片机头文件
#define uchar unsigned char // 宏定义,用uchar替代无符号字符型
#define uint unsigned int // 宏定义,用uint 替代无符号整数型
sbit Key = P2 ^ 7;
uchar Count = 0;
uchar code DataChar[10] = // 定义数码管的段码 '0'~'9'、'-'、' ' 'P'
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
};
//延时N毫秒
void DelayNms( uint N ) // 利用x、y作简单的自减运算,消耗单片机指令周期,达到延时的目的
{
uint x,y;
for( x=N; x>0; x-- )
for( y=110; y>0; y-- );
}
//数码管显示 // 显示方式为动态扫描,视觉暂留原理
void Display( uchar Num ) // *p指向Main函数中的数组ShowNum的首地址
{
P0 = DataChar[ Num ];// 向P0赋段码值
DelayNms(2); // 短暂的延时
P0 = 0x00; // 让数码管熄灭,否则会造成数字重叠
}
void main( void )
{
while(1)
{
if( Key == 0 )
{
DelayNms(30);
if( Key == 0 )
{
Count += 1;
Count = (Count>9)? 0 : Count;
}
while( !Key );
}
Display( Count );
}

我是学机械的,但是老师要求做单片机上机考试,完全没有学过c语言和汇编,在网上查的结果又不是用中断方式,求高手能不能帮助下写段程序,一定要是中断方式才行,万分感激
#include < reg52.h > // 嵌入51单片机头文件
#define uchar unsigned char // 宏定义,用uchar替代无符号字符型
#define uint unsigned int // 宏定义,用uint 替代无符号整数型
sbit Key = P2 ^ 7;
uchar Count = 0;
uchar code DataChar[10] = // 定义数码管的段码 '0'~'9'、'-'、' ' 'P'
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
};
//延时N毫秒
void DelayNms( uint N ) // 利用x、y作简单的自减运算,消耗单片机指令周期,达到延时的目的
{
uint x,y;
for( x=N; x>0; x-- )
for( y=110; y>0; y-- );
}
//数码管显示 // 显示方式为动态扫描,视觉暂留原理
void Display( uchar Num ) // *p指向Main函数中的数组ShowNum的首地址
{
P0 = DataChar[ Num ];// 向P0赋段码值
DelayNms(2); // 短暂的延时
P0 = 0x00; // 让数码管熄灭,否则会造成数字重叠
}
void main( void )
{
while(1)
{
if( Key == 0 )
{
DelayNms(30);
if( Key == 0 )
{
Count += 1;
Count = (Count>9)? 0 : Count;
}
while( !Key );
}
Display( Count );
}
