Article directory
1. Install keil software
1. Prepare the installation package
First, you need to download and install the mdk5 software and the stm32 package. Here are the packages needed to configure the MDK.
Link: https://pan.baidu.com/s/1f0nHjn7sSG8SK3waV_G1Pg
Extraction code: 94xf
After decompressing the compressed package, we can start to install MDK.
(The resource comes from https://blog.csdn.net/ssj925319/article/details/108919862)
2. Install Keil
After decompressing the compressed package, double-click mdk_510.exe
and follow the installation operation NEXT
There is no special requirement for the name and email here
Click to complete the installation
After clicking OK, the mouse will turn into a circle, because various packs are being installed online, but the installation will fail, don’t worry , close the window in the upper right corner, and start to manually install the pack package below.
3. Install stm32 pack
In the file just decompressed, double-click ARM.CMSIS.3.20.4
and click NEXT to
complete the installation
. Also find Keil.STM32F1xx_DFP.1.0.4 in the decompressed file just now
to complete the installation
. Other common settings about keil can also be found at the top link with details
2. Complete a simple stm assembly language programming
1. New construction projects
Click Project on the upper toolbar to name the file and select the stored file address.
Select the corresponding core. Here we select STM32F103RB
and check the corresponding options. Click OK to complete the project creation.
2. Create a new main.c file
Click an icon like a white paper in the upper right corner
and enter the following code
#define PERIPH_BASE ((unsigned int)0x40000000)//AHB
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)
//GPIOA_BASE=0x40000000+0x10000+0x0800=0x40010800,该地址为GPIOA的基地址
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
//GPIOB_BASE=0x40000000+0x10000+0x0C00=0x40010C00,该地址为GPIOB的基地址
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
//GPIOC_BASE=0x40000000+0x10000+0x1000=0x40011000,该地址为GPIOC的基地址
#define GPIOD_BASE (APB2PERIPH_BASE + 0x1400)
//GPIOD_BASE=0x40000000+0x10000+0x1400=0x40011400,该地址为GPIOD的基地址
#define GPIOE_BASE (APB2PERIPH_BASE + 0x1800)
//GPIOE_BASE=0x40000000+0x10000+0x0800=0x40011800,该地址为GPIOE的基地址
#define GPIOF_BASE (APB2PERIPH_BASE + 0x1C00)
//GPIOF_BASE=0x40000000+0x10000+0x0800=0x40011C00,该地址为GPIOF的基地址
#define GPIOG_BASE (APB2PERIPH_BASE + 0x2000)
//GPIOG_BASE=0x40000000+0x10000+0x0800=0x40012000,该地址为GPIOG的基地址
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define LED0 MEM_ADDR(BITBAND(GPIOA_ODR_Addr,8))
//#define LED0 *((volatile unsigned long *)(0x422101a0)) //PA8
//定义typedef类型别名
typedef struct
{
volatile unsigned int CR;
volatile unsigned int CFGR;
volatile unsigned int CIR;
volatile unsigned int APB2RSTR;
volatile unsigned int APB1RSTR;
volatile unsigned int AHBENR;
volatile unsigned int APB2ENR;
volatile unsigned int APB1ENR;
volatile unsigned int BDCR;
volatile unsigned int CSR;
} RCC_TypeDef;
#define RCC ((RCC_TypeDef *)0x40021000)
//定义typedef类型别名
typedef struct
{
volatile unsigned int CRL;
volatile unsigned int CRH;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
volatile unsigned int BRR;
volatile unsigned int LCKR;
} GPIO_TypeDef;
//GPIOA指向地址GPIOA_BASE,GPIOA_BASE地址存放的数据类型为GPIO_TypeDef
#define GPIOA ((GPIO_TypeDef *)GPIOA_BASE)
void LEDInit( void )
{
RCC->APB2ENR|=1<<2; //GPIOA 时钟开启
GPIOA->CRH&=0XFFFFFFF0;
GPIOA->CRH|=0X00000003;
}
//粗略延时
void Delay_ms( volatile unsigned int t)
{
unsigned int i,n;
for (n=0;n<t;n++)
for (i=0;i<800;i++);
}
int main(void)
{
LEDInit();
while (1)
{
LED0=0;//LED熄灭
Delay_ms(500);//延时时间
LED0=1;//LED亮
Delay_ms(500);//延时时间
}
}
After the input is complete, click Save in the upper left corner, and a window will pop up to name the file. Enter main.c (note that the file format.c must be entered here, otherwise the file will not be considered as a C language file, and an error will occur)
Click After saving, you can find that the font color has changed.
Right-click Source Group 1, and then click Add Existing Files to Group... (add the main.c file under the project)
to add the main.c file just now to the project file, and then you can find There is an additional main.c file under the Source Group 1 file
3. Compile
Click the icon in the upper left corner to compile the program, and
a box will appear at the moment
, indicating that there are 0 warnings and 0 errors, and the compilation is successful.
4. Stm32 simulation debugging
Before debugging, you need to make corresponding settings.
Find an icon like a magic wand on the top and click
to pop up a window, click Debug, check Use Simulator, select ULINK2/ME Cortex Debugger, and click Settings.
Then there is another pop-up window, make sure that the Port is JTAG, Reset can be set to Autodetect or SYSRESEETREQ, then click OK to return to the previous window, and then click OK.
After that is debugging, click the icon like a magnifying glass on the top,
which means single-step debugging.
The upper left is the tools that can be used during debugging.
So far, the compilation and debugging are completed.
5. Supplement
1. In addition, in the icon of the magic wand mentioned earlier, click Output and check Create HEX File. After some compilation and debugging, a .hex file will be generated in the storage folder. The hex file is very useful
2.
After the compilation is completed, the size of each part can be viewed in the lower box.
In the absence of hardware devices, the compilation and simulation debugging of the program are simply completed.
6. Hex file analysis and interpretation
You can find the newly generated hex file in the file.
Analyze the content of the first line: 020000040800F2, which can be regarded as: 0x02, 0x00, 0x00, 0x04, 0x08, 0x00, 0xF2. The first four bytes and the last byte have special meanings. In the middle is data. Interpretation as follows:
0x02: Indicates that there are two data in this line of data.
Two 0x00: Indicates the start address bit of the data in this row.
0x04: extended linear address record: used to identify the extended linear address record (other data representation:
'00'Data Rrecord: used to record data, most of the records in the HEX file are data records
'01' file end record: used to mark the end of the file, put it at the end of the file, and mark the end of the HEX file
'02'Extended segment address record: the record used to identify the extended segment address
'03' start segment address record: start segment address record
'05' start linear address record: start linear address record
)
0x08, 0x00: two bytes of data in this line, the data type is 04, that is, this line records an extended address (0x08 0x00 is address information, and the usage is to use this address (0x0800<<16) as the base address .0xF2
: checksum, checksum = 0x100 - accumulated sum
3. Reference materials
https://blog.csdn.net/weixin_39752827/article/details/81477686
https://blog.csdn.net/qq_43279579/article/details/111717607
https://blog.csdn.net/ssj925319/article/details/108919862