https://static.assets-stash.eet-china.com/album/old-resources/2008/12/12/8ee2aa15-a51b-441d-96f5-a15089fc5edf.rar
这个是STM32 HELLO WORLD! 程序,下载并解压后,里面有<<说明.PDF>>
文件,请看看!觉得经典的程序。完全不用STM32库!而且很简洁,简单。
非常适合STM32初学者学习.
http://bbs.embcom.net/forum-15-1.html
http://bbs.21ic.com/club/bbs/bbsView.asp?boardid=11
NETJOB @2008-12-12
下面是 初始化内设函数代码:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STM32的 外设初始化函数库
// netjob @2008-7-6
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "stm32f103-s.h"
#include "lowlevelinit.h"
//=========================================================================
//
//=========================================================================
void delay()
{
int i,j;
j=0;
for (i=0; i<0xfffff; i++) j++; // 1,048,575
}
//=========================================================================
// LD1=LED4=PC7(PWM) LD2=LED4=PC6(PWM) LD3=LED2=PC5 LD4=LED1=PC4
//=========================================================================
void Led_RW_ON(char index)
{
switch(index)
{
case 0:
STM32_Gpioc_Regs->bsrr.bit.BR7 =1;// 1:清除对应的ODRy位为0 关LD1
STM32_Gpioc_Regs->bsrr.bit.BS7 =1;// 1:设置对应的ODRy位为1 开LD1
break;
case 1:
STM32_Gpioc_Regs->bsrr.bit.BR6 =1;// 1:清除对应的ODRy位为0 关LD2
STM32_Gpioc_Regs->bsrr.bit.BS6 =1;// 1:设置对应的ODRy位为1 开LD2
break;
case 2:
STM32_Gpioc_Regs->bsrr.bit.BR5 =1;// 1:清除对应的ODRy位为0 关LD3
STM32_Gpioc_Regs->bsrr.bit.BS5 =1;// 1:设置对应的ODRy位为1 开LD3
break;
case 3:
STM32_Gpioc_Regs->bsrr.bit.BR4 =1;// 1:清除对应的ODRy位为0 关LD4
STM32_Gpioc_Regs->bsrr.bit.BS4 =1;// 1:设置对应的ODRy位为1 开LD4
break;
}
}//end sub
//=========================================================================
//
//=========================================================================
void Led_RW_OFF(char index)
{
switch(index)
{
case 0:
STM32_Gpioc_Regs->bsrr.bit.BS7 =1;// 1:设置对应的ODRy位为1 开LD1
STM32_Gpioc_Regs->bsrr.bit.BR7 =1;// 1:清除对应的ODRy位为0 关LD1
break;
case 1:
STM32_Gpioc_Regs->bsrr.bit.BS6 =1;// 1:设置对应的ODRy位为1 开LD2
STM32_Gpioc_Regs->bsrr.bit.BR6 =1;// 1:清除对应的ODRy位为0 关LD2
break;
case 2:
STM32_Gpioc_Regs->bsrr.bit.BS5 =1;// 1:设置对应的ODRy位为1 开LD3
STM32_Gpioc_Regs->bsrr.bit.BR5 =1;// 1:清除对应的ODRy位为0 关LD3
break;
case 3:
STM32_Gpioc_Regs->bsrr.bit.BS4 =1;// 1:设置对应的ODRy位为1 开LD4
STM32_Gpioc_Regs->bsrr.bit.BR4 =1;// 1:清除对应的ODRy位为0 关LD4
break;
}
}//end sub
//=========================================================================
// RCC_Configuration(void)
//=========================================================================
void STM32_RCC_Configuration(void)
{
/* Disable APB2 Peripheral Reset */
STM32_Rcc_Regs->apb2rstr.all=0x00000000;
/* Disable APB1 Peripheral Reset */
STM32_Rcc_Regs->apb1rstr.all=0x00000000;
/* FLITF and SRAM Clock ON */
STM32_Rcc_Regs->ahbenr.all= 0x00000014;
/* Disable APB2 Peripheral Clock */
STM32_Rcc_Regs->apb2enr.all=0x00000000;
/* Disable APB2 Peripheral Clock */
STM32_Rcc_Regs->apb1enr.all=0x00000000;
STM32_Rcc_Regs->cr.bit.HSION=1; // HSI振荡器开启
/* Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], ADCPRE[1:0] and MCO[2:0] bits*/
STM32_Rcc_Regs->cfgr.all&= 0xF8FF0000;
/* Reset HSEON, CSSON and PLLON bits */
STM32_Rcc_Regs->cr.all&= 0xFEF6FFFF;
/* Reset HSEBYP bit */
STM32_Rcc_Regs->cr.all&= 0xFFFBFFFF;
/* Reset PLLSRC, PLLXTPRE, PLLMUL[3:0] and USBPRE bits */
STM32_Rcc_Regs->cfgr.all&= 0xFF80FFFF;
/* Disable all interrupts */
STM32_Rcc_Regs->cr.all= 0x00000000;
STM32_Rcc_Regs->cr.bit.HSEON=0; // HSE振荡器开启关闭
STM32_Rcc_Regs->cr.bit.HSEBYP=0; // 外部高速时钟没有旁路
STM32_Rcc_Regs->cr.bit.HSERDY=0; // 外部高速时钟就绪标志清零
STM32_Rcc_Regs->cr.bit.HSEON=1; // 外部高速时钟使能 1:HSE振荡器开启
while( !(STM32_Rcc_Regs->cr.bit.HSERDY ) );//由硬件置1来指示外部时钟已经稳定。
/* HCLK = SYSCLK=72MHZ :HLCK 提供给CPU, 内存和DMA 最大72MHZ */
STM32_Rcc_Regs->cfgr.bit.HPRE=RCC_AHB_SYSCLK_DIV1_B; //AHB预分频
/* PCLK2 = HCLK/1=SYSCLK=72 PCLK2 提供给 APB2外设,最大72MHZ */
STM32_Rcc_Regs->cfgr.bit.PPRE2=RCC_APB2_HCLK_DIV1_B; //高速APB预分频(APB2)
/* PCLK1 = HCLK/2=SYSCLK/2=36 PCLK1提供给APB1外设,最大36MHZ */
STM32_Rcc_Regs->cfgr.bit.PPRE1=RCC_APB1_HCLK_DIV2_B; //低速APB预分频(APB1)必须保证APB1时钟频率不超过36MHz
/* ADCCLK = PCLK2/6 ADC转换速率:72M/6 = 12MHZ */
STM32_Rcc_Regs->cfgr.bit.ADCPRE=RCC_ADCPRE_PCLK2_DIV6_B;
/* Flash 2 wait state */
STM32_Flash_Regs->ACR.all&=((u32)0x00000038); //清零某些位
STM32_Flash_Regs->ACR.bit.LATENCY=2;
STM32_Flash_Regs->ACR.bit.PRFTBE=1; //预取缓冲区使能
/* PLLCLK = 8MHz * 9 = 72MHZ ) */
STM32_Rcc_Regs->cfgr.bit.PLLXTPRE=0; //HSE分频器作为PLL输入 0:HSE不分频
STM32_Rcc_Regs->cfgr.bit.PLLSRC=1; //HSE时钟作为PLL输入时钟
STM32_Rcc_Regs->cfgr.bit.PLLMUL=RCC_PLL_9_B; //PLL倍频系数9
/* Enable PLL */
STM32_Rcc_Regs->cr.bit.PLLON=1; // PLL使能
while( !(STM32_Rcc_Regs->cr.bit.PLLRDY ) ); // PLL时钟就绪标志 PLL锁定后由硬件置1
/* Select PLL as system clock source */
STM32_Rcc_Regs->cfgr.bit.SW=0;
STM32_Rcc_Regs->cfgr.bit.SW=2; //RCC_SW_SYSCLK_PLL
/* Wait till PLL is used as system clock source */
while(STM32_Rcc_Regs->cfgr.bit.SWS!=2); //RCC_SWS_SYSCLK_PLL
/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
STM32_Rcc_Regs->apb2enr.all |=(RCC_ADC1EN|RCC_AFIOEN|RCC_IOPAEN|RCC_IOPBEN|RCC_IOPCEN|RCC_IOPDEN|RCC_IOPEEN);
/* TIM3 AND tim4 clocks enable */
STM32_Rcc_Regs->apb1enr.all |=(RCC_TIM3EN| RCC_CANEN|RCC_TIM4EN);
} //end sub
//=========================================================================
// GPIO_Configuration(void)
//=========================================================================
void STM32_GPIO_Configuration(void)
{
/* Configure PC.04 -- PC.11 as Output push-pull */
// LED 灯
STM32_Gpioc_Regs->crl.bit.CNF4=Output_push_pull; // PC.04 推挽输出
STM32_Gpioc_Regs->crl.bit.MODE4=Output_Mode_50mhz; // PC.04 输出模式,最大速度50MHz
STM32_Gpioc_Regs->crl.bit.CNF5=Output_push_pull; // PC.05 推挽输出
STM32_Gpioc_Regs->crl.bit.MODE5=Output_Mode_50mhz; // PC.05 输出模式,最大速度50MHz
STM32_Gpioc_Regs->crl.bit.CNF6=Output_push_pull;
STM32_Gpioc_Regs->crl.bit.MODE6=Output_Mode_50mhz;
STM32_Gpioc_Regs->crl.bit.CNF7=Output_push_pull;
STM32_Gpioc_Regs->crl.bit.MODE7=Output_Mode_50mhz;
}//ENDSUB
//=========================================================================
// Button_Config(void)
// 开发板上的4按键输入初始化
//=========================================================================
void STM32_Button_Config(void)
{
/* Enable GPIOD clock */
STM32_Rcc_Regs->apb2enr.bit.IOPDEN=1;//IOPDEN:IO口D时钟使能
/* Configure PD.03, PD.04 as output push-pull */
// 注意了 这里是推挽输出 而实在是浮空输入!
STM32_Gpiod_Regs->crl.bit.CNF3=Input_floating; // PD.03 浮空输入
STM32_Gpiod_Regs->crl.bit.MODE3=Input_Mode; // PD.03 输入模式
STM32_Gpiod_Regs->crl.bit.CNF4=Input_floating; // PD.04 浮空输入
STM32_Gpiod_Regs->crl.bit.MODE4=Input_Mode; // PD.04 输入模式
}
//=========================================================================
// NVIC_Configuration(void)
//=========================================================================
void STM32_NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
STM32_Scb_Regs->ExceptionTableOffset.all=0x20000000;
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
STM32_Scb_Regs->ExceptionTableOffset.all=0x08000000;
#endif
/* Configure the Priority Group to 2 bits */
STM32_Scb_Regs->AIRC.all= ((u32)0x05FA0000)|((u32)0x500);
/* Configure the SysTick handler priority */
STM32_Scb_Regs->SystemPriority[11].all=2;
}//end sub
//=========================================================================
// STM32_Interrupt_Set(void)
// 开中断 定时器2中断
//=========================================================================
void STM32_Interrupt_Set(void)
{
/* enabling interrupt 设置TIM2中断优先级,和开中断*/
STM32_Nvic_Regs->Priority[28].all=0x80;
STM32_Nvic_Regs->Enable[0].bit.INT28=1; // 28 35 可设置 TIM2 TIM2全局中断 0x0000_00B0
}//END SUB
//=========================================================================
// TIM_Configuration(void)
// 设置TIMER-2定时器 为计数溢出中断,10毫秒中断一次
// TIM2、3、4的时钟源是 APB1 即是 PCLK1 ( APB1 对应 PCLK1 )
// PCLK1 = APB1 = HCLK/2 = SYSCLK/2 = 36MHZ (36,000,000 HZ)
// 但是倍频器会自动倍2, 又倍频回72MHZ.
//=========================================================================
void STM32_TIM2_Configuration(void)
{
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure ;
// TIM_DeInit( TIM4);//复位TIM4定时器
STM32_Rcc_Regs->apb1rstr.all |= RCC_TIM2RST;
STM32_Rcc_Regs->apb1rstr.all &= ~RCC_TIM2RST;
STM32_Rcc_Regs->apb1enr.all |=RCC_TIM2EN;
/* TIM2 configuration */
//TIM_TimeBaseStructure.TIM_Period = 1000;
//TIM_TimeBaseStructure.TIM_Prescaler = 72;
//TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
STM32_Tim2_Regs->arr.all=10000; // 定时10毫秒 1MHZ/S 除 10,000 HZ
STM32_Tim2_Regs->psc.all=72; // 72分频
STM32_Tim2_Regs->cr1.bit.CKD=0; // 时钟分频因子
STM32_Tim2_Regs->cr1.bit.DIR=0; // 0:计数器向上计数
/* Clear TIM2 update pending flag[清除TIM2溢出中断标志] */
//TIM_ClearFlag(TIM2, TIM_FLAG_Update);
STM32_Tim2_Regs->sr.bit.UIF=0; //更新中断标记 由软件清0 ,例如当上溢或下溢时,软件对CNT重新初始化
/* Enable TIM2 Update interrupt [TIM2溢出中断允许]*/
//TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
STM32_Tim2_Regs->dier.bit.UIE=1;// 允许更新中断
/* TIM2 enable counter [允许tim2计数]*/
//TIM_Cmd(TIM2, ENABLE);
STM32_Tim2_Regs->cr1.bit.CEN=1;// 开启计数器
STM32_Tim2_Regs->egr.bit.UG=1; //触发一次事件中断。
STM32_Tim2_Regs->sr.bit.UIF=0;
} //end sub
用户20789 2008-12-13 16:25
用户1175252 2007-1-12 10:28