ARM开发环境th3

    科技2022-07-11  106

    Keil MDK运行一个简单stm32程序 + Proteus完成一个51程序的仿真

    一.简单stm32程序的实现

    程序的编写 ① 程序的功能 由于是初学者,程序实现的功能比较简单,是利用硬件的串口进行循环输出“Hello World !” ② 程序代码 仿照STM32F10x_StdPeriph_Lib_V3.5.0标准库代码风格,以下为主函数main.c的代码 #include "stm32f10x.h" void USART_Configuration(void); void GPIO_Configuration(void); void Delay(__IO uint32_t nCount); void USART1_Puts(char * str); int main(void) { USART_Configuration(); GPIO_Configuration(); USART1_Puts("Hello Wrold!\n"); while (1) { GPIOF->BSRR = 0x000000C0; Delay(0xAFFFF); GPIOF->BRR = 0x000000C0; Delay(0xAFFFF); USART1_Puts("Hello Wrold!\n"); } } void USART_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; //使能串口、串口所用的I/O口以及端口复用时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE); /* A9 USART1_Tx */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX GPIO_Init(GPIOA,&GPIO_InitStructure); /* A10 USART1_Rx */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); /* Enable the USARTx */ USART_Cmd(USART1, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); /* 设置LED对应的引脚 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOF, &GPIO_InitStructure); } void Delay(__IO uint32_t nCount) { for(; nCount != 0; nCount--); } void USART1_Puts(char * str) { while(*str) { USART_SendData(USART1, *str++); /* Loop until the end of transmission */ while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } }

    参考代码源自博客园emouse

    程序的编译 将工程模板test1(我已创建好)中的USER目录下的主函数代码修改为上面的代码,进行编译

    程序的仿真 ①设置debug 点击工具栏中的魔术棒图表,在弹出的窗口中设置

    其中的Dalog DLL,Parameter根据自己的芯片填写 ②点击菜单栏中的Debug进入仿真调试 点击串口URAT1 程序运行后可在URAT1窗口看到输出结果

    二.51单片机之交叉流水灯的设计与仿真

    1.程序的编译

    程序的功能

    本程序实现的是先让上面的四个灯一起亮灭,再让下面的四个灯一起亮灭(设计的为黄灯),然后奇数位置的灯一起亮灭,偶数位置的灯一起亮灭。

    程序的代码

    代码参考百度文库之51单片机程序

    #include<intrins.h> void DelayTime(unsigned int DelayValue) { unsigned int a,b; for(a=0;a<DelayValue;a++) for(b=0;b<1827;b++) } void main() { char b; b=0xfe; while(1) { b=_cro_(b,7); DelayTime(50); p0=b; } }

    2.程序的编译

    可见编译成功,系统会自动生成后缀.hex的文件,绘制完电路图后点击左下方的按钮即可仿真运行。此为Proteus8版本的特性,不需要用keil编程后生成的.hex文件再手动考入到原理图中,自动完成,节约了很多时间。

    3.电路图设计

    4.仿真实现

    电路图报错引脚线重合,自己的重复多次没成功,效果图参考51单片机程序例图

    三. 总结

    proteus问题遇到太多,需要耐心上网查询资料,虽然也不一定能解决。

    Processed: 0.018, SQL: 8