1.選擇晶元
//巨集定義,用於存放stm32暫存器對映
#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
rcc_typedef;
#define rcc ((rcc_typedef *)0x40021000)
//定義typedef型別別名
typedef
struct
gpio_typedef;
//gpioa指向位址gpioa_base,gpioa_base位址存放的資料型別為gpio_typedef
#define gpioa ((gpio_typedef *)gpioa_base)
void
ledinit
(void
)//粗略延時
void
delay_ms
(volatile
unsigned
int t)
intmain
(void
)}
4.除錯
MDK環境下stm32實現printf函式
printf雖然是c語言的標準函式,但是是針對有stdin,stdout裝置的系統的,在嵌入式平台上是沒有標準的stdin,stdout的,需要對映到串列埠後某個輸出物件的,而嵌入式平台各不相同,如果又沒有作業系統的支援,就只能靠自己實現了,因此stm32下的printf不在標準庫里,而在定製的庫里...
STM32的Keil4環境下的printf重定向
在使用stm32的usart傳送資料時,發現傳送資料非常的雞肋,不同資料格式的資料需要處理,這時十分想念當初的printf函式,隨意傳送想要的資料,stm32也能使用printf,但是需要將 stdio.h 函式庫中的printf重定向,因為printf 函式的實現時使用fputc 和putchar...
ubuntu上搭建stm32環境心得
2019 10 16 最近安裝了elementary os 5.0 基於ubuntu18.04 替代了win10,所以需要在該系統上配置stm32開發環境 3 安裝stlinkv2驅動 還要結合上面那個部落格給makefile新增make flash 參考 4 安裝vscode 目前之用來看 5 m...