1、掌握单片机最小系统三要素电路设计规则。
1)
2)
3)
2、掌握函数间相互调用的方法和规则。
函数的形式参数和实际参数
实参:在调用一个有参数的函数时,函数名后边括号中的参数。
形参:在被调用的函数进行定义时,括号内的参数叫做形式参数。
1、函数定义中指定的形参,在未发生调用时不占用内存,函数调用时分配内存单元,调用结束释放内存。
2,或者复杂的表达式,但是要求必须有确定的值,在调用时传递给形参。c=add(1,a+b);
3、参必须制定数据类型,和变量定义一样。
4参和实参的数据类型应该相同或者赋值兼容。
5、主调函数在调用函数之前,应对被调函数做原型声明。
6、实参向形参的数据传递是单向传递,不能有形参再回传给实参。
3、会独立按键和矩阵按键的电路设计方法和软件编程思路。
1)
2)
3)
4)按键消抖方法,遍历八个字节,当八个字节在连续时间字节相同,则判定按钮按下
5)按钮按下与否的判断,就是跟按钮前一刻的值判断,当前值与前一刻的值不同,则按钮按下。
4一按实现一个数码管数字从F~0递减的变化程序。
#include <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
sbit KeyIn1 = P2^4;
sbit KeyIn2 = P2^5;
sbit KeyIn3 = P2^6;
sbit KeyIn4 = P2^7;
sbit KeyOut1 = P2^3;
sbit KeyOut2 = P2^2;
sbit KeyOut3 = P2^1;
sbit KeyOut4 = P2^0;
unsigned char code LedChar[] = { //ÊýÂë¹ÜÏÔʾ×Ö·ûת»»±í
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
bit i = 1,Key = 1;
unsigned int cnt = 0;
void main()
{
EA = 1;
KeyOut4 = 0;
ENLED = 0;
addr3 = 1;
TMOD = 0x01;
TH0 = 0xF8;
TL0 =0xcb;
addr1 = 0;
addr2 = 0;
addr0 = 0;
ET0 = 1;
TR0 = 1;
P0 = LedChar[cnt];
while(1)
{
if(Key != i)
{
if(i == 0)
{
P0 = LedChar[cnt];
cnt++;
if(cnt >= 15)
{
cnt = 0;
}
}
i = Key;
}
}
}
void InterruptTimer0() interrupt 1
{
static unsigned int arr = 0xff;
TH0 = 0xF8;
TL0 =0xcb;
arr = (arr << 1) | KeyIn2;
if(arr == 0xff)
{
Key = 1;
}
else if(arr == 0x00)
{
Key = 0;
}
else {}
}
5、用矩阵按键做一个简易减法计算器。
#include <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
sbit KeyIn1 = P2^4;
sbit KeyIn2 = P2^5;
sbit KeyIn3 = P2^6;
sbit KeyIn4 = P2^7;
sbit KeyOut1 = P2^3;
sbit KeyOut2 = P2^2;
sbit KeyOut3 = P2^1;
sbit KeyOut4 = P2^0;
unsigned char code LedChar[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char code KeyCodeMap[4][4] = {
{ 0x31, 0x32, 0x33, 0x26 },
{ 0x34, 0x35, 0x36, 0x25 },
{ 0x37, 0x38, 0x39, 0x28 },
{ 0x30, 0x1B, 0x0D, 0x27 }
};
unsigned char KeySta[4][4] = {
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
void KeyDriver();
void main()
{
EA = 1;
ENLED = 0;
addr3 = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
LedBuff[0] = LedChar[0];
while (1)
{
KeyDriver();
}
}
void ShowNumber(long num)
{
signed char i;
unsigned char sign;
unsigned char buf[6];
if (num < 0)
{
sign = 1;
num = -num;
}
else
{
sign = 0;
}
for (i=0; i<6; i++)
{
buf[i] = num % 10;
num = num / 10;
}
for (i=5; i>=1; i--)
{
if (buf[i] == 0)
LedBuff[i] = 0xFF;
else
break;
}
if (sign != 0)
{
if (i < 5)
{
LedBuff[i+1] = 0xBF;
}
}
for ( ; i>=0; i--)
{
LedBuff[i] = LedChar[buf[i]];
}
}
void KeyAction(unsigned char keycode)
{
static char oprt = 0;
static long result = 0;
static long addend = 0;
if ((keycode>=0x30) && (keycode<=0x39))
{
addend = (addend*10)+(keycode-0x30);
ShowNumber(addend);
}
else if (keycode == 0x26)
{
oprt = 0;
result = addend;
addend = 0;
ShowNumber(addend);
}
else if (keycode == 0x28)
{
oprt = 1;
result = addend;
addend = 0;
ShowNumber(addend);
}
else if (keycode == 0x0D)
{
if (oprt == 0)
{
result += addend;
}
else
{
result -= addend;
}
addend = 0;
ShowNumber(result);
}
else if (keycode == 0x1B)
{
addend = 0;
result = 0;
ShowNumber(addend);
}
}
void KeyDriver()
{
unsigned char i, j;
static unsigned char backup[4][4] = {
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
if (backup[i][j] != KeySta[i][j])
{
if (backup[i][j] != 0)
{
KeyAction(KeyCodeMap[i][j]);
}
backup[i][j] = KeySta[i][j];
}
}
}
}
void KeyScan()
{
static unsigned char add = 0;
unsigned char j ;
static unsigned char keybuf[4][4] = {
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
keybuf[add][0] = (keybuf[add][0] << 1) | KeyIn1;
keybuf[add][1] = (keybuf[add][1] << 1) | KeyIn2;
keybuf[add][2] = (keybuf[add][2] << 1) | KeyIn3;
keybuf[add][3] = (keybuf[add][3] << 1) | KeyIn4;
for(j = 0;j <4;j++)
{
if( (keybuf[add][j] & 0x0F) == 0x00 )
{
KeySta[add][j] = 0;
}
else if((keybuf[add][j] & 0x0F) == 0x0f)
{
KeySta[add][j] = 1;
}
}
add++;
add = add & 0x03;
switch(add)
{
case 0:KeyOut4 = 1;KeyOut1 = 0;break;
case 1:KeyOut1 = 1;KeyOut2 = 0;break;
case 2:KeyOut2 = 1;KeyOut3 = 0;break;
case 3:KeyOut3 = 1;KeyOut4 = 0;break;
default:break;
}
}
void LedScan()
{
static unsigned char i = 0;
P0 = 0xff;
switch(i)
{
case 0:addr2 = 0;addr1 = 0;addr0 = 0;i++;P0 = LedBuff[0];break;
case 1:addr2 = 0;addr1 = 0;addr0 = 1;i++;P0 = LedBuff[1];break;
case 2:addr2 = 0;addr1 = 1;addr0 = 0;i++;P0 = LedBuff[2];break;
case 3:addr2 = 0;addr1 = 1;addr0 = 1;i++;P0 = LedBuff[3];break;
case 4:addr2 = 1;addr1 = 0;addr0 = 0;i++;P0 = LedBuff[4];break;
case 5:addr2 = 1;addr1 = 0;addr0 = 1;i=0;P0 = LedBuff[5];break;
default:break;
}
}
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;
LedScan();
KeyScan();
}