本文软件使用的是proteus及MDK5,具体安装过程可在搜索proteus和MDK5的安装教程
本文软件使用的是proteus及MDK5,具体安装过程可在搜索proteus和MDK5的安装教程,本文主要讲的是STM32与51的对比。 可参考以下安装教程链接: MDK5 Proteus
以下是本篇文章主要内容,下面案例可供参考
具体区别可参考:链接
代码如下(示例): 主函数:
#include "stm32f10x.h" #include "led.h" int main(void) { LED_Init();//初始化LED while(1){ GPIO_ResetBits(GPIOC,GPIO_Pin_0);//让PC_0口输出为低电平,从而使灯0亮 Delay(100);//延时100ms GPIO_SetBits(GPIOC,GPIO_Pin_0);//灯0灭 GPIO_ResetBits(GPIOC,GPIO_Pin_1);//灯1亮 Delay(100); GPIO_SetBits(GPIOC,GPIO_Pin_1);//灯1灭 GPIO_ResetBits(GPIOC,GPIO_Pin_2);//灯2亮 Delay(100); GPIO_SetBits(GPIOC,GPIO_Pin_2);//灯2灭 GPIO_ResetBits(GPIOC,GPIO_Pin_3);//灯3亮 Delay(100); GPIO_SetBits(GPIOC,GPIO_Pin_3);//灯3灭 GPIO_ResetBits(GPIOC,GPIO_Pin_4);//灯4亮 Delay(100); GPIO_SetBits(GPIOC,GPIO_Pin_4);//灯4灭 GPIO_ResetBits(GPIOC,GPIO_Pin_5);//灯5亮 Delay(100); GPIO_SetBits(GPIOC,GPIO_Pin_5);//灯5灭 GPIO_ResetBits(GPIOC,GPIO_Pin_6);//灯6亮 Delay(100); GPIO_SetBits(GPIOC,GPIO_Pin_6);//灯6灭 GPIO_ResetBits(GPIOC,GPIO_Pin_7);//灯7亮 Delay(100); GPIO_SetBits(GPIOC,GPIO_Pin_7);//灯7灭 GPIO_ResetBits(GPIOC,GPIO_Pin_0);//灯1亮 Delay(100);}}delay:
void Delay(u16 t) { u32 i; while(t--) for(i=1;i<1000;i++); }stm32f10x.h部分代码:
#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE) #define GPIOB ((GPIO_TypeDef *) GPIOB_BASE) #define GPIOC ((GPIO_TypeDef *) GPIOC_BASE) #define GPIOD ((GPIO_TypeDef *) GPIOD_BASE) #define GPIOE ((GPIO_TypeDef *) GPIOE_BASE) #define GPIOF ((GPIO_TypeDef *) GPIOF_BASE) #define GPIOG ((GPIO_TypeDef *) GPIOG_BASE)仿真电路图:
仿真效果图
注意事项: proteus仿真的时候要将VCCA和VSSA接入电网,及design中的configuration power 要将VSSA加入右边。
仿真图:
C51可以直接通过对寄存器的输出赋值,而STM32赋值的时候需要位操作,这样能够提高单片机运行的效率,能够是单片机反应更迅速,而且,STM32封装得也比51完善,能够实现更多的功能。