|
第一次烧写STM32单片机 |
一派护法 十九级 |
DTR电平置低(-3--12V),复位 RTS置高(+3-+12V),选择进入BootLoader ...延时100毫秒 DTR电平变高(+3-+12V)释放复位 RTS维持高 开始连接...5, 接收到:79 1F 在串口COM6连接成功@115200bps,耗时795毫秒 芯片内BootLoader版本号:3.1 芯片PID:00000419 STM32F4xxx_43xx 读出的选项字节: EFAA1055EFAA1055FF3F00C0FF3F00C0 开始全片擦除(全片擦除时间会比较长,请耐心等候!) 全片擦除成功 开始去除写保护 去除写保护成功 DTR电平置低(-3--12V),复位 RTS置高(+3-+12V),选择进入BootLoader ...延时100毫秒 DTR电平变高(+3-+12V)释放复位 RTS维持高 开始连接...5, 接收到:79 1F 在串口COM6连接成功@115200bps,耗时842毫秒 芯片内BootLoader版本号:3.1 芯片PID:00000419 STM32F4xxx_43xx 读出的选项字节: EFAA1055EFAA1055FF3F00C0FF3F00C0 第889毫秒,已准备好 共写入679KB,进度100%,耗时280193毫秒 写入选项字节: FF AA 00 55 FF AA 00 55 FF FF 00 00 FF FF 00 00 成功写入选项字节 写入的选项字节: FFAA0055FFAA0055FFFF0000FFFF0000 从08000000开始运行失败...可能是因为刚写了选项字节!!! www.mcuisp.com(全脱机手持编程器EP968,全球首创)向您报告,命令执行完毕,一切正常
|
一派护法 十九级 |
|
一派护法 十九级 |
读器件信息:
|
一派护法 十九级 |
现在,我正在尝试点亮LED灯。
|
一派护法 十九级 |
【程序】 #include "stm32f4xx.h"
void SystemInit(void) { }
int main(void) { RCC_AHB1ENR |= (1 << 7); GPIOH_MODER &= ~(0x03 << (2 * 10)); GPIOH_MODER |= (1 << 2 * 10); GPIOH_OTYPER &= ~(1 << 1 * 10); GPIOH_OTYPER |= (0 << 1 * 10); GPIOH_OSPEEDR &= ~(0x03 << 2 * 10); GPIOH_OSPEEDR |= (0 << 2 * 10); GPIOH_PUPDR &= ~(0x03 << 2 * 10); GPIOH_PUPDR |= (1 << 2 * 10); GPIOH_BSSR |= (1 << 16 << 10); while (1); return 0; }
|
一派护法 十九级 |
可见,STM32点亮LED要比C51和AVR复杂很多倍。
|
一派护法 十九级 |
【编译错误】 *** Using Compiler 'V5.06 update 1 (build 61)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Build target 'Target 1' compiling led.c... C:\Keil_v5\ARM\PACK\Keil\STM32F4xx_DFP\2.7.0\Drivers\CMSIS\Device\ST\STM32F4xx\Include\stm32f429xx.h(192): error: #5: cannot open source input file "core_cm4.h": No such file or directory #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */ led.c: 0 warnings, 1 error ".\Objects\led.axf" - 1 Error(s), 0 Warning(s). Target not created. Build Time Elapsed: 00:00:14
|
一派护法 十九级 |
*** Using Compiler 'V5.06 update 1 (build 61)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Build target 'Target 1' compiling led.c... led.c(9): error: #20: identifier "RCC_AHB1ENR" is undefined RCC_AHB1ENR |= (1 << 7); led.c(11): error: #20: identifier "GPIOH_MODER" is undefined GPIOH_MODER &= ~(0x03 << (2 * 10)); led.c(14): error: #20: identifier "GPIOH_OTYPER" is undefined GPIOH_OTYPER &= ~(1 << 1 * 10); led.c(17): error: #20: identifier "GPIOH_OSPEEDR" is undefined GPIOH_OSPEEDR &= ~(0x03 << 2 * 10); led.c(20): error: #20: identifier "GPIOH_PUPDR" is undefined GPIOH_PUPDR &= ~(0x03 << 2 * 10); led.c(23): error: #20: identifier "GPIOH_BSSR" is undefined GPIOH_BSSR |= (1 << 16 << 10); led.c(26): warning: #111-D: statement is unreachable return 0; led.c(27): warning: #1-D: last line of file ends without a newline } led.c: 2 warnings, 6 errors ".\Objects\led.axf" - 6 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:01
|
一派护法 十九级 |
#include "stm32f4xx.h"
//#define PERIPH_BASE ((unsigned int)0x40000000) #define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000) #define GPIOH_BASE (AHB1PERIPH_BASE + 0x1C00) #define GPIOH_MODER *(unsigned int*)(GPIOH_BASE+0x00) #define GPIOH_OTYPER *(unsigned int*)(GPIOH_BASE+0x04) #define GPIOH_OSPEEDR *(unsigned int*)(GPIOH_BASE+0x08) #define GPIOH_PUPDR *(unsigned int*)(GPIOH_BASE+0x0C) #define GPIOH_IDR *(unsigned int*)(GPIOH_BASE+0x10) #define GPIOH_ODR *(unsigned int*)(GPIOH_BASE+0x14) #define GPIOH_BSRR *(unsigned int*)(GPIOH_BASE+0x18) #define GPIOH_LCKR *(unsigned int*)(GPIOH_BASE+0x1C) #define GPIOH_AFRL *(unsigned int*)(GPIOH_BASE+0x20) #define GPIOH_AFRH *(unsigned int*)(GPIOH_BASE+0x24) #define RCC_BASE (AHB1PERIPH_BASE + 0x3800) #define RCC_AHB1ENR *(unsigned int*)(RCC_BASE+0x30)
void SystemInit(void) { }
int main(void) { RCC_AHB1ENR |= (1 << 7); GPIOH_MODER &= ~(0x03 << (2 * 10)); GPIOH_MODER |= (1 << 2 * 10); GPIOH_OTYPER &= ~(1 << 1 * 10); GPIOH_OTYPER |= (0 << 1 * 10); GPIOH_OSPEEDR &= ~(0x03 << 2 * 10); GPIOH_OSPEEDR |= (0 << 2 * 10); GPIOH_PUPDR &= ~(0x03 << 2 * 10); GPIOH_PUPDR |= (1 << 2 * 10); GPIOH_BSRR |= (1 << 16 << 10); while (1); }
|
一派护法 十九级 |
*** Using Compiler 'V5.06 update 1 (build 61)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Build target 'Target 1' compiling led.c... linking... Program Size: Code=244 RO-data=444 RW-data=0 ZI-data=1024 FromELF: creating hex file... ".\Objects\led.axf" - 0 Error(s), 0 Warning(s). Build Time Elapsed: 00:00:01
|
一派护法 十九级 |
共需写入0KB!
|
一派护法 十九级 |
用keil的debug可以下载成功: 而且程序运行正常 证明程序没有问题
|
一派护法 十九级 |
【第二个程序:使LED闪烁起来】 void delay() { int i, j; for (i = 0; i < 9999; i++) for (j = 0; j < 99; j++); }
int main(void) { RCC_AHB1ENR |= (1 << 7); GPIOH_MODER &= ~(0x03 << (2 * 10)); GPIOH_MODER |= (1 << 2 * 10); GPIOH_OTYPER &= ~(1 << 1 * 10); GPIOH_OTYPER |= (0 << 1 * 10); GPIOH_OSPEEDR &= ~(0x03 << 2 * 10); GPIOH_OSPEEDR |= (0 << 2 * 10); GPIOH_PUPDR &= ~(0x03 << 2 * 10); GPIOH_PUPDR |= (1 << 2 * 10); while (1) { GPIOH_BSRR |= (1 << 16 << 10); delay(); GPIOH_BSRR |= (1 << 10); delay(); } } 成功!
|
一派护法 十九级 |
以后可以点这个下载。
|
一派护法 十九级 |
循环灯: #include "stm32f4xx.h" #include "led.h"
void SystemInit(void) { }
void delay() { int i, j; for (i = 0; i < 9999; i++) for (j = 0; j < 300; j++); }
int main(void) { RCC_AHB1ENR |= (1 << 7); GPIOH_MODER &= ~(0x03 << 20); GPIOH_MODER |= (0x01 << 20); GPIOH_MODER &= ~(0x03 << 22); GPIOH_MODER |= (0x01 << 22); GPIOH_MODER &= ~(0x03 << 24); GPIOH_MODER |= (0x01 << 24); GPIOH_OTYPER &= ~(1 << 10); GPIOH_OTYPER |= (0 << 10); GPIOH_OTYPER &= ~(1 << 11); GPIOH_OTYPER |= (0 << 11); GPIOH_OTYPER &= ~(1 << 12); GPIOH_OTYPER |= (0 << 12); GPIOH_OSPEEDR &= ~(0x03 << 20); GPIOH_OSPEEDR |= (0 << 20); GPIOH_OSPEEDR &= ~(0x03 << 22); GPIOH_OSPEEDR |= (0 << 22); GPIOH_OSPEEDR &= ~(0x03 << 24); GPIOH_OSPEEDR |= (0 << 24); GPIOH_PUPDR &= ~(0x03 << 20); GPIOH_PUPDR |= (0x01 << 20); GPIOH_PUPDR &= ~(0x03 << 22); GPIOH_PUPDR |= (0x01 << 22); GPIOH_PUPDR &= ~(0x03 << 24); GPIOH_PUPDR |= (0x01 << 24); while (1) { RED_ON; delay(); RED_OFF; GREEN_ON; delay(); GREEN_OFF; BLUE_ON; delay(); BLUE_OFF; // yellow RED_ON; GREEN_ON; delay(); RED_OFF; GREEN_OFF; // purple RED_ON; BLUE_ON; delay(); RED_OFF; BLUE_OFF; // cyan GREEN_ON; BLUE_ON; delay(); GREEN_OFF; BLUE_OFF; // white GREEN_ON; BLUE_ON; RED_ON; delay(); // black GREEN_OFF; BLUE_OFF; RED_OFF; delay(); } }
|
一派护法 十九级 |
【头文件】 //#define PERIPH_BASE ((unsigned int)0x40000000) #define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000) #define GPIOH_BASE (AHB1PERIPH_BASE + 0x1C00) #define GPIOH_MODER *(unsigned int*)(GPIOH_BASE+0x00) #define GPIOH_OTYPER *(unsigned int*)(GPIOH_BASE+0x04) #define GPIOH_OSPEEDR *(unsigned int*)(GPIOH_BASE+0x08) #define GPIOH_PUPDR *(unsigned int*)(GPIOH_BASE+0x0C) #define GPIOH_IDR *(unsigned int*)(GPIOH_BASE+0x10) #define GPIOH_ODR *(unsigned int*)(GPIOH_BASE+0x14) #define GPIOH_BSRR *(unsigned int*)(GPIOH_BASE+0x18) #define GPIOH_LCKR *(unsigned int*)(GPIOH_BASE+0x1C) #define GPIOH_AFRL *(unsigned int*)(GPIOH_BASE+0x20) #define GPIOH_AFRH *(unsigned int*)(GPIOH_BASE+0x24) #define RCC_BASE (AHB1PERIPH_BASE + 0x3800) #define RCC_AHB1ENR *(unsigned int*)(RCC_BASE+0x30)
#define RED_ON GPIOH_BSRR |= (1 << 16 << 10) #define RED_OFF GPIOH_BSRR |= (1 << 10)
#define GREEN_ON GPIOH_BSRR |= (1 << 16 << 11) #define GREEN_OFF GPIOH_BSRR |= (1 << 11)
#define BLUE_ON GPIOH_BSRR |= (1 << 16 << 12) #define BLUE_OFF GPIOH_BSRR |= (1 << 12)
|