Current location - Education and Training Encyclopedia - Graduation thesis - How to use 5 1 single chip microcomputer as a calculator?
How to use 5 1 single chip microcomputer as a calculator?
1, hardware simulation diagram

The hardware part is relatively simple. When the keyboard key is pressed, the ports in the row and column are low. Therefore, as long as the scan row and column ports are low, you can determine which key was pressed.

2, the main program flow chart

The main idea of the program is to abstract the keys into characters, and then process the characters. Operands are converted into strings and operators are stored in character form. Then call the compute () function to calculate and return the result. Specific procedures and precautions, as well as the flow chart.

3. PCB drawing drawn by 3.Altium Designer.

4, the program source code

# include & ltreg 5 1 . h & gt; # include & ltintrins.h & gt

# include & ltctype.h & gt

# include & ltstdlib.h & gt

# Define uchar unsigned characters

# Define uint unsigned integer

uchar operand 1[9],operand 2[9];

Uchar operator; ?

Invalid delay (uint);

uchar keyscan();

void disp(void);

Void buf(uint value);

uint compute(uint va 1,uint va2,uchar optor);

Uchar code table [] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,

0x92,0x82,0xf8,0x80,0x90,0x ff }; ?

uchar dbuf[8] = { 10, 10, 10, 10, 10, 10, 10, 10 }; ?

Invalid delay (unit z)

{

uint x,y;

for(x = z; x & gt0; x -)

for(y = 1 10; y & gt0; y-);

}

Uchar key scan ()

{

Uchar Ski; ?

p 1 = 0x Fe;

while((p 1 & amp; 0xf0)! = 0xf0)

{

Delay (3); ?

while((p 1 & amp; 0xf0)! = 0xf0)

{

Switch (P 1)

{

case 0x ee:skey = ' 7 '; Break;

case 0x de:skey = ' 8 '; Break;

case 0x be:skey = ' 9 '; Break;

Case 0x7e: skey ='/'; Break; ?

Default:? skey = ' #

}

while((p 1 & amp; 0xf0)! = 0xf0)?

;

}

}

p 1 = 0x FD;

while((p 1 & amp; 0xf0)! = 0xf0)

{

Delay (3);

while((p 1 & amp; 0xf0)! = 0xf0)

{

Switch (P 1)

{

case 0x ed:skey = ' 4 '; Break;

case 0x DD:skey = ' 5 '; Break;

case 0x BD:skey = ' 6 '; Break;

Case 0x7d:skey =' * ';; Break;

Default:? skey = ' #

}

while((p 1 & amp; 0xf0)! = 0xf0)

;

}

}

p 1 = 0x FB;

while((p 1 & amp; 0xf0)! = 0xf0)

{

Delay (3);

while((p 1 & amp; 0xf0)! = 0xf0)

{

Switch (P 1)

{

case 0xeb:skey = ' 1 '; Break;

case 0x db:skey = ' 2 '; Break;

case 0x bb:skey = ' 3 '; Break;

Case 0x7b: skey ='-'; Break;

Default value: skey =' #

}

while((p 1 & amp; 0xf0)! = 0xf0)

;

}

}

p 1 = 0xf 7;

while((p 1 & amp; 0xf0)! = 0xf0)

{

Delay (3);

while((p 1 & amp; 0xf0)! = 0xf0)

{

Switch (P 1)

{

Case 0xe 7:skey =' $ ';; Break;

case 0xd 7:skey = ' 0 '; Break;

Case 0xb 7:skey =' = ';; Break;

Case 0x77: skey ='+'; Break;

Default:? skey = ' #

}

while((p 1 & amp; 0xf0)! = 0xf0)

;

}

}

Return to skey

}

void main()

{

Uint value 1, value 2, value;

uchar ckey,cut 1 = 0,cut 2 = 0;

Uchar operator;

uchar i,bool = 0;

Initialization:

buf(0);

disp();

Value = 0;

cut 1 = cut 2 = 0;

bool = 0;

for(I = 0; I<9; i++)

{

operand 1[I]= ' \ 0 ';

operand 2[I]= ' \ 0 ';

}

while( 1)

{

ckey = key scan(); ?

If (ckey! = '#')

{?

if(isdigit(ckey))?

{

Switch (Boolean type)

{

Case 0:

operand 1[cut 1]= ckey;

operand 1[cut 1+ 1]= ' \ 0 ';

value 1 = atoi(operand 1); ?

cut 1++;

buf(value 1);

disp();

Break;

Case 1:

operand 2[cut 2]= ckey;

operand 2[cut 2+ 1]= ' \ 0 ';

value 2 = atoi(operand 2);

cut 2++;

buf(value 2);

disp();

Break;

Default: break

}

}

else if(ckey = = '+' | | ckey = = '-' | | ckey = = ' * ' | | ckey = = '/')

{

bool = 1;

operator = ckey

buf(0);

dbuf[7]= 10;

disp();

}

else if(ckey == '= ')

{

Value = compute(value 1, value2, operator);

Buf (value);

disp();

And (1)?

{

ckey = key scan();

if(ckey == '$ ')?

Go to init

other

{

Buf (value);

disp();

}

}

}

else if(ckey == '$ ')?

{ goto init}

}

disp();

}

}

Uint calculation (uint va 1, uint va2, uchar optor)

{

Uint value;

Switch (optor)

{

case '+':value = va 1+va2; Break;

case '-':value = va 1-va2; Break;

case ' * ':value = va 1 * va2; Break;

case '/':value = va 1/va2; Break;

Default:? Break;

}

Return value;

}

Void buf (unit value)

{

Ucal I;

if(val == 0)

{

dbuf[7]= 0;

I = 6;

}

other

for(I = 7; val & gt0; I-)

{

dbuf[I]= val % 10;

val/= 10;

}

for(; I>0; I-)

dbuf[I]= 10;

}

Invalid display (invalid)

{

Utcharbsel

bsel = 0x 0 1;

for(n = 0; n & lt8; n++)

{?

P2 = bsel;

P0 = table[dbuf[n]];

bsel=_crol_(bsel, 1);

Delay (3);

P0 = 0xff

}

}

Extended data:

PROTEUS is a senior assistant in classroom teaching of single chip microcomputer.

PROTEUS can not only visualize the functions of many single-chip computer instances, but also visualize the running process of many single-chip computer instances. The former can get the effect of physical demonstration experiment to a considerable extent, while the latter is difficult to achieve.

Its components, connecting wires and so on are highly corresponding to the traditional single chip microcomputer experimental hardware. To some extent, this has replaced the traditional functions of MCU experiment teaching, such as component selection, circuit connection, circuit detection, circuit modification, software debugging, running results and so on.

Curriculum design and graduation design are important practical links for students' employment. PROTEUS provides a large number of component libraries that the laboratory can't match, provides flexibility in modifying circuit design, provides virtual instruments that the laboratory can't match in quantity and quality, and provides a platform for cultivating students' practical spirit and creative spirit.

With the development of science and technology, "computer simulation technology" has become an important means for many design departments to carry out preliminary design. It has the characteristics of flexible design and unified results and processes. It can greatly shorten the design time, reduce the cost and reduce the engineering manufacturing risk. I believe PROTEUS can also be more and more widely used in the development and application of single chip microcomputer.

Using Proteus software to simulate the design of single chip microcomputer system is a comprehensive application of virtual simulation technology and computer multimedia technology, which is conducive to cultivating students' circuit design ability and operation ability of simulation software.

In the course design of single chip microcomputer and the national electronic design competition for college students, we use Proteus? Developing environment to train students, without hardware investment, students generally report that learning single-chip microcomputer is easier to accept and improve than simply learning book knowledge.

Practice has proved that the design efficiency of single chip microcomputer system can be greatly improved after the successful system simulation development with Proteus. Therefore, Proteus has high popularization and utilization value.

Baidu encyclopedia -protues