Proteus simulation STM32 LED lamp experiment

1. Use keil 5 software to create source files

1. Create a new project file

note Here to choose STM32 chip
Insert picture description here
Insert picture description here

2. Enter the following code and add it to the project file

#include "reg52.h"
sbit hr=P1^0;
sbit hy=P1^1;
sbit hg=P1^2;
sbit sr=P1^3;
sbit sy=P1^4;
sbit sg=P1^5;
sbit h1=P2^0;
sbit h2=P2^1;
sbit s1=P2^2;
sbit s2=P2^3;
int count=0;
int time=0;
int num[10]={
    
    0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
void delay(int t)
{
    
    
	int x,y;
	for(x=0;x<t;x++)
	{
    
    
		for(y=0;y<100;y++);
	}
}

void main()
{
    
    
	
	P1=0xff;
	hg=0;
	sr=0;
	P2=0x00;
	TMOD=0x01;
	TL0=0x3C;
	TH0=0xB0;
	ET0=1;
	EA=1;
	
	TR0=1;
	while(1)
	{
    
    
		 if(time<30)//ºáÂÌ Êúºì
		{
    
    
			h1=1;P0=num[((30-time)/10)];delay(10);h1=0;
			h2=1;P0=num[((30-time)%10)];delay(10);h2=0;
			s1=1;P0=num[((33-time)/10)];delay(10);s1=0;
			s2=1;P0=num[((33-time)%10)];delay(10);s2=0;
		}
		else if(time<33)//ºá»Æ Êúºì
		{
    
    
			h1=1;P0=num[((33-time)/10)];delay(10);h1=0;
			h2=1;P0=num[((33-time)%10)];delay(10);h2=0;
			s1=1;P0=num[((33-time)/10)];delay(10);s1=0;
			s2=1;P0=num[((33-time)%10)];delay(10);s2=0;
		}
		else if(time<63)//ºáºì ÊúÂÌ
		{
    
    
			h1=1;P0=num[((66-time)/10)];delay(10);h1=0;
			h2=1;P0=num[((66-time)%10)];delay(10);h2=0;
			s1=1;P0=num[((63-time)/10)];delay(10);s1=0;
			s2=1;P0=num[((63-time)%10)];delay(10);s2=0;
		}
		else if(time<66)//ºáºì Êú»Æ
		{
    
    
			h1=1;P0=num[((66-time)/10)];delay(10);h1=0;
			h2=1;P0=num[((66-time)%10)];delay(10);h2=0;
			s1=1;P0=num[((66-time)/10)];delay(10);s1=0;
			s2=1;P0=num[((66-time)%10)];delay(10);s2=0;
			
		}
	}
}

void timer() interrupt 1
{
    
    
	TH0=0x3C;
	TL0=0xB0;
	count=count+1;
	if(count==20)
	{
    
    
		count=0;
		time=time+1;
		
		//led
		if(time==30)
		{
    
    
			P1=0xff;hy=0;sr=0;
		}
		else if(time==33)
		{
    
    
			P1=0xff;hr=0;sg=0;
		}
		else if(time==63)
		{
    
    
			P1=0xff;hr=0;sy=0;
		}
		else if(time==66)
		{
    
    
			time=0;
			P1=0xff;hg=0;sr=0;
		}
	}
	
}

Insert picture description here

3. Finally compile to generate a .hex file

Insert picture description here
So far, all software preparations have been completed

2. Use Protues 8.9 for hardware simulation

1. Establish a schematic design

Bold style

2. Select the required device

Insert picture description here

3. Design the circuit diagram

note The selected chip model is STM32F103R6
Insert picture description here

4. Click on the chip

Select the .hex file just generated, put it into the chip and
Insert picture description here
run it to see the result:
Insert picture description here
Because the time interval set by the code is too short, the LED turns on faster, but it succeeded in the end.

Guess you like

Origin blog.csdn.net/weixin_47357131/article/details/109575306