首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Cortex入门stm32流水灯实验

Cortex入门stm32流水灯实验

test.c 包括3个头文件
#include "stm32f10x_lib.h" STM32的一些库函数
#include "sys.h" 系统时钟初始化及一些延时函数
#include "led.h" 流水灯相关程序
=========================================================================
先看一下 stm32f10x_lib.h
里面又包括一个头文件 #include "stm32f10x_map.h"
接下来是类似
#ifdef _ADC
#include "stm32f10x_adc.h"
#endif
#ifdef _BKP
#include "stm32f10x_bkp.h"
#endif
#ifdef _CAN
#include "stm32f10x_can.h"
#endif
........
#ifdef _GPIO 如果定义了_GPIO 则包含GPIO相关的头文件头 定义这些宏是在stm32f10x_conf.h中定义的
#include "stm32f10x_gpio.h"
#endif
.......
再看这个头文件 stm32f10x_map.h
又包括几个头文件
#include "stm32f10x_conf.h" 类似 #define _GPIOA 宏定义
#include "stm32f10x_type.h" 类似 typedef signed long s32; 的一些数据类型
#include "cortexm3_macro.h"
stm32f10x_map.h定义了
typedef struct
{
vu32 CRL;
vu32 CRH;
vu32 IDR;
vu32 ODR;
vu32 BSRR;
vu32 BRR;
vu32 LCKR;
} GPIO_TypeDef;
类似的结构体
......
#define PERIPH_BASE ((u32)0x40000000)
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
然后来了重要的部分
#ifdef _GPIOC
#define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)
#endif
把从GPIOC_BASE 开始的内存强制类型转换成 GPIO_TypeDef 这种类型的结构体
在led.h 中这样调用 GPIOC->ODR=(GPIOC->ODR&~LED0)|(x ? LED0:0)
0x40011000 +12的地址赋值,即完成对相应寄存器的赋值
===================================================================
再看一下 sys.h 简单的寄存器赋值,对照手册看就很容易理解
//us延时函数
void delay_us(unsigned int us)
{
u8 n;
while(us--)for(n=0;n}
//ms延时函数
void delay_ms(unsigned int ms)
{
while(ms--)delay_us(1000);
}
//把所有时钟寄存器复位
void RCC_DeInit(void)
{
RCC->APB2RSTR = 0x00000000;//外设复位
RCC->APB1RSTR = 0x00000000;
RCC->AHBENR = 0x00000014; //flash时钟,闪存时钟使能.DMA时钟关闭
RCC->APB2ENR = 0x00000000; //外设时钟关闭.
RCC->APB1ENR = 0x00000000;
RCC->CR |= 0x00000001; //使能内部高速时钟HSION
RCC->CFGR &= 0xF8FF0000; //复位SW[1:0],HPRE[3:0],PPRE1[2:0],PPRE2[2:0],ADCPRE[1:0],MCO[2:0]
RCC->CR &= 0xFEF6FFFF; //复位HSEON,CSSON,PLLON
RCC->CR &= 0xFFFBFFFF; //复位HSEBYP
RCC->CFGR &= 0xFF80FFFF; //复位PLLSRC, PLLXTPRE, PLLMUL[3:0] and USBPRE
RCC->CIR = 0x00000000; //关闭所有中断
}
//外部8M,则得到72M的系统时钟
void Stm32_Clock_Init(void)
{
unsigned char temp=0;
u8 timeout=0;
RCC_DeInit();
RCC->CR|=0x00010000; //外部高速时钟使能HSEON
timeout=0;
while(!(RCC->CR>>17)&&timeout<200)timeout++;//等待外部时钟就绪
//0-24M 等待0;24-48M 等待1;48-72M等待2;(非常重要!)
FLASH->ACR|=0x32;//FLASH 2个延时周期
RCC->CFGR|=0X001D2400;//APB1/2=DIV2;AHB=DIV1LL=9*CLK;HSE作为PLL时钟源
RCC->CR|=0x01000000; //PLLON
timeout=0;
while(!(RCC->CR>>25)&&timeout<200)timeout++;//等待PLL锁定
RCC->CFGR|=0x00000002;//PLL作为系统时钟
while(temp!=0x02&&timeout<200) //等待PLL作为系统时钟设置成功
{
temp=RCC->CFGR>>2;
timeout++;
temp&=0x03;
}
}
==============================================================================
led.h
#define LED0 (1<<10)// PC10
#define LED1 (1<<11)// PC11
#define LED2 (1<<12)// PC12
#define LED3 (1<<2) // PD2
#define LED0_SET(x) GPIOC->ODR=(GPIOC->ODR&~LED0)|(x ? LED0:0)
#define LED1_SET(x) GPIOC->ODR=(GPIOC->ODR&~LED1)|(x ? LED1:0)
#define LED2_SET(x) GPIOC->ODR=(GPIOC->ODR&~LED2)|(x ? LED2:0)
#define LED3_SET(x) GPIOD->ODR=(GPIOD->ODR&~LED3)|(x ? LED3:0)
//LED IO初始化
void led_init(void)
{
RCC->APB2ENR|=1<<4; //使能PORTC时钟
GPIOC->CRH&=0XFFF000FF;
GPIOC->CRH|=0X00033300;//PC.10/11/12推挽输出
GPIOC->ODR|=0X1C00; //PC.10/11/12 输出高
RCC->APB2ENR|=1<<5; //使能PORTD时钟
GPIOD->CRL&=0XFFFFF0FF;
GPIOD->CRL|=0X300; //PD.2推挽输出
GPIOD->ODR|=1<<2; //PD.2输出高
}
返回列表