Summary of single chip microcomputer principle and interface technology

Bibliography: single chip microcomputer principle and Interface Technology (compiled by Zhu Xiaohui Laiting)

  1. Application of single chip microcomputer: intelligent instrument; Mechatronics; Real time control; Distributed multi machine system; Application in human life.
  2. The main development trend of single chip microcomputer: CMOS; Low power consumption; Low voltage; Low noise and high reliability.
  3. Features of single chip microcomputer: 8051 single chip microcomputer integrates CPU, ROM, RAM, I/O port, timer / counter and interrupt functions on one chip. The CPU of 8051 is 8 bits; The chip has oscillator and clock circuit, 32 I/O lines, 64KB ROM and RAM for external memory addressing range, 2 16 bit timers / counters, 5 interrupt sources and 2 interrupt priorities; Full duplex serial port; Boolean processor.
  4. ① Oscillation period: refers to the period of the oscillation source. If it is generated internally, it is the oscillation period of quartz crystal.
    ② Clock cycle: 2 times of the full oscillation cycle, clock cycle = oscillation cycle P1 + oscillation cycle P2.
    ③ Machine cycle: a machine cycle includes 6 clock cycles.
    ④ Instruction cycle: the total time taken to complete an instruction cycle.
    The instruction cycle of 8051 includes 1 ~ 4 machine cycles, most of which are single cycle instructions, as well as 2-cycle instructions and 4-cycle instructions.
  5. Characteristics of single chip microcomputer memory: Harvard structure is adopted, program memory and data memory are separated, and each has an independent 64KB addressing space.
  6. Interrupt means that when the CPU is running the program normally, due to internal / external events or events scheduled by the program, the CPU interrupts the running program, turns to the program serving internal / external events or scheduled events, and returns to the program temporarily interrupted after the service is completed.
  7. There are five interrupts in a single chip: two external interrupts, two counter / timer interrupts, and one serial port interrupt.
  8. Interrupt nesting means that when the CPU is processing an interrupt source, that is, executing an interrupt service program, a first interrupt occurs
    Higher interrupt sources apply for interrupts. In order to enable more urgent interrupt sources to receive services in time, it is necessary to temporarily interrupt the currently executing interrupt service program of lower level to process higher-level interrupt sources, and then return to execute the interrupt service program of wave interrupt 3 after execution. However, the interrupt source with low or lower interrupt level cannot interrupt the service with high interrupt level.
  9. Interrupt source refers to any event that causes the interruption of single chip microcomputer. Generally, a single chip microcomputer is allowed to have multiple interrupt sources. External interrupt 0, external interrupt 1, on-chip timer / counter 0, on-chip timer / counter 1, on-chip serial port transmit / receive interrupt.
  10. General form of interrupt service function: return value function name interrupt n(using n). bit cannot refer to the absolute value of the positioning variable. When it is necessary to refer to the absolute value of the positioning variable, it needs to be defined with sbit.
Internal RAM addressfunction
00HZone 0Four groups of general registers R0~R7 can also be used as RAM, and R0 and R1 can be bit addressable
08HZone 1
10HZone 2
1FHZone 3
20HBit addressing area 00H~7FHAll addressable bits, 16 bytes in total, 128 bits
2FH
30HData buffer, stack, unit of workByte addressing only
7FH
80Hspecial function registerByte addressable or bit addressable
FFH
interrupt numberInterrupt sourceEntry address
0External interrupt 0(INT0)0003H
1Timer / counter 0(T0)000BH
2External interrupt 1(INT1)0013H
3Timer / counter 1(T1)001BH
4Serial port interrupt0023H

1. Convert a hexadecimal number into a compressed BCD code

#include<reg51.h>
#include<absacc.h>
main()
{  unsigned char data a[5],b;
	b=DBYTE[0x30];
	a[0]=b/100;
	a[1]=b%100/10;
	a[2]=b%10;
	a[3]=a[1] << 4;
	a[4]=a[3] | a[2];
	DBYTE[0x20]=a[0];
	DBYTE[0x21]=a[4];
	while(1);
}

2. Convert a compressed BCD code to hexadecimal number

#include<reg51.h>
#include<absacc.h>
main()
{
unsigned char data a[3],b;
	b=DBYTE[0x30];
	a[0]=b>>4;
	a[1]=b&0x0F;
	a[2]=a[0]*10+a[1];
	DBYTE[0x31]=a[2];
	while(1);
}

3. Convert a hexadecimal number to ASCII code

#include<reg51.h>
#include<absacc.h>
main()
{  unsigned char a,b,c;
	a=DBYTE[0x30];
	b=a >> 4;
	if(b>9) b=b-10+'A';
	else b=b+0x30;
	c=a & 0x0F;
	if(c>9) c=c-10+'A';
	else c=c+0x30;
XBYTE[0x30]=b; XBYTE[0x31]=c;
	while(1);
}

4. Convert an ASCII code to a hexadecimal number

#include<reg51.h>
#include<absacc.h>
main()
{
unsigned char a,b;
	a=DBYTE[0x30];
if(a>=0x30 && a<=0x39)
	b=a-0x30;
else if(a>=0x41 && a<=0x46)
	b=a-0x41+10;
else if(a>=0x61 && a<=0x66)
	b=a-0x61+10;
XBYTE[0x31]=b;
while(1);
}

5. Minimum common multiple and maximum common divisor

#include<reg51.h>
#include<absacc.h>
main()
{ unsigned char a,b,c,d,i,j;
a=DBYTE[0x30]; b=DBYTE[0x31];
c=a*b;
for(i=1;i<=c;i++)
{if((c%i==0)&&(i%b==0)&&(i%a==0))
{DBYTE[0x0A]=i;break;}
else DBYTE[0x0A]=c;}
j=DBYTE[0x0A];
d=c/j;
DBYTE[0x0B]=d;
while(1);
}

6. Implement a BCD code addition

#include<reg51.h>
#include<absacc.h>
void main()
{
unsigned char a,b,c;
a=DBYTE[0x08];
b=DBYTE[0x09];
c=(a>>4)*10+(a&0x0F)+(b>>4)*10+(b&0x0F);
if(c>100)
{DBYTE[0x0A]=c-100;DBYTE[0x0B]=0x01;}
else DBYTE[0x0A]=c;
while(1);
}

7. Sum of all even and odd numbers

#include<reg51.h>
#include<absacc.h>
main()
{ unsigned char data a[5],i,sum;
  sum=0;
  for(i=0;i<5;i++)
  {a[i]=DBYTE[0x30+i];
  if(!(a[i]&0x01)) {sum=sum+a[i];}		
}
  DBYTE[0x08]=sum;
  while(1);
}


#include<reg51.h>
#include<absacc.h>
main()
{   unsigned char data a[5],i,sum;
    sum=0;
    for(i=0;i<5;i++)
    {a[i]=DBYTE[0x30+i];
    if(a[i] & 0x01) {sum=sum+a[i];}		
}
     DBYTE[0x08]=sum;
     while(1);
}

8. Accumulation of all even positive numbers and negative numbers

#include<reg51.h>
#include<absacc.h>
main()
{ unsigned char data a[5],i,sum;
  sum=0;
  for(i=0;i<5;i++)                                                           
  {a[i]=DBYTE[0x30+i];
  if(a[i]>=0x80) {sum=sum+a[i];}		
}
  DBYTE[0x08]=sum; 
  while(1);
}


#include<reg51.h>
#include<absacc.h>
main()
{ unsigned char data a[5],i,sum;
   sum=0;
for(i=0;i<5;i++)
   {a[i]=DBYTE[0x30+i];
  if(a[i]<0x80) {sum=sum+a[i];}		
}
DBYTE[0x08]=sum;
while(1);
}

9. Judge whether it is prime

#include<reg51.h>
#include<absacc.h>
main()
{
unsigned char a,b,c;
a=DBYTE[0x08];
c=0xFF;
for(b=2;b<a;b++)
{  if(a%b==0)
{c=0;break;}
}
DBYTE[0x09]=c;
while(1);
}

10. Sort from small to large

#include<reg51.h>
#include<absacc.h>
void main()
{  unsigned char data a[10],t;
unsigned char i,j;
	for(i=0;i<10;i++)
{a[i]=DBYTE[i+0x30];}
for(i=0;i<9;i++)
 {for(j=0;j<9-i;j++)
  {if(a[j]>a[j+1]) 
    {t=a[j];a[j]=a[j+1];a[j+1]=t;} }}
for(i=0;i<10;i++)
{DBYTE[i+0x30]=a[i];}
while(1);
}

11. Cumulative sum of uncertain times

#include<reg51.h>
#include<absacc.h>
#include<math.h>
void main()
{  unsigned int sum,i,n,x;
n=DBYTE[0x30];
x=DBYTE[0x31];
sum=0;
for(i=1;i<=n;i++)
{sum=sum+pow(i,x);}
DBYTE[0x35]=sum;
while(1);
}


#include<reg51.h>
#include<absacc.h>
void main()
{  unsigned int sum,i,n;
   n=DBYTE[0x30];
    sum=0;
    for(i=1;i<=n;i++)
   {sum=sum+i*i;}
    DBYTE[0x35]=sum;
     while(1);
}


#include<reg51.h>
#include<absacc.h>
main()
{ unsigned char a,b,i,j,sum;
   a=DBYTE[0x30];
   sum=0;
    for(i=1;i<=a;i++)
    {b=1;
    for(j=1;j<=i;j++)
    {b=j*b;}
     sum=sum+b;}
      DBYTE[0x31]=sum;
while(1);
}

12. Even numbers are split into the sum of two prime numbers

#include<reg51.h>
#include<absacc.h>
#include<math.h>
void main()
{  unsigned char a,b,c,d;
a=DBYTE[0x30];
for(b=3;b<=a/2;b+=2)
{ for(c=2;c<=sqrt(b);c++)
if(b%c==0) break;
if(c>sqrt(b)) d=a-b;
else break;
for(c=2;c<=sqrt(d);c++)
if(d%c==0) break;
if(c>sqrt(d))
{DBYTE[0x31]=b;
DBYTE[0x32]=d;} }
while(1);
}

13. Split into the sum of squares of four numbers

#include<reg51.h>
#include<absacc.h>
void main()
{  unsigned char a,b,c,d,x;
x=DBYTE[0x30];
for(a=1;a<=x/2;a++)
for(b=0;b<=a;b++)
for(c=0;c<=b;c++)
for(d=0;d<=c;d++)
if(x==a*a+b*b+c*c+d*d) 
{ DBYTE[0x20]=a;
DBYTE[0x21]=b;
DBYTE[0x22]=c;
DBYTE[0x23]=d; }
while(1);
}

14. Uppercase letters are converted to lowercase letters, while numbers remain unchanged

#include<reg51.h>
#include<absacc.h>
main()
{  unsigned char x;
x=DBYTE[0x30];
if(x>=0x30&&x<=0x39)  {DBYTE[0x08]=x;}
else	{x=x+0x20;}
DBYTE[0x08]=x;
while(1);
}

15. Output 50 rectangular pulses at P1 port

#include<reg51.h> 
#include<absacc.h>
sbit u=P1^4; //Define the u bit as P1 four
void delay30ms(void)
{  unsigned char m,n;
for(m=0;m<100;m++)
for(n=0;n<100;n++)
;  }
void main(void)
{  unsigned char i;
u=1; //Initialize output high level
for(i=0;i<50;i++) //Output 50 rectangular pulses
{  u=1;
delay30ms();
u=0;
delay30ms();  }
while(1) ; //Infinite loop to prevent the program from "flying"
}

Keywords: Single-Chip Microcomputer

Added by Nuv on Sat, 15 Jan 2022 10:56:46 +0200