以下小程序在EK-STM32实验板上通过.顺便那位高手 推荐一块最小12位的Led显示芯片,2块zlg7290c成本太高 最好系内建晶震和I2C接口,谢谢!!
#include "stm32f10x_lib.h"
void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void SysTick_Config(void); void Led_RW_ON(void); void Led_RW_OFF(void); void Led_RE_ON(void); void Led_RE_OFF(void); void delay(void);
int main(void) { #ifdef DEBUG debug(); #endif RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); SysTick_Config(); while (1) { Led_RW_ON(); Led_RE_ON(); delay(); Led_RW_OFF(); Led_RE_OFF(); delay(); Led_RW_ON(); Led_RE_ON(); delay(); Led_RW_OFF(); Led_RE_OFF(); delay(); Led_RW_ON(); Led_RE_OFF(); delay(); Led_RW_OFF(); Led_RE_ON(); delay(); Led_RW_ON(); Led_RE_OFF(); delay(); Led_RW_OFF(); Led_RE_ON(); delay(); Led_RW_ON(); Led_RE_OFF(); delay(); Led_RW_OFF(); Led_RE_ON(); delay(); Led_RW_ON(); Led_RE_OFF(); delay(); Led_RW_OFF(); Led_RE_ON(); delay(); Led_RW_ON(); Led_RE_ON(); delay(); Led_RW_OFF(); Led_RE_OFF(); delay(); Led_RW_ON(); Led_RE_ON(); delay(); Led_RW_OFF(); Led_RE_OFF(); delay(); Led_RW_ON(); Led_RE_ON(); delay(); } }
void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2);
/* ADCCLK = PCLK2/6 */ RCC_ADCCLKConfig(RCC_PCLK2_Div6);
/* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 4MHz * 9 = 36 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */ RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }
/* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } }
/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
/* TIM2 clocks enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* CAN Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE); }
/******************************************************************************* * Function Name : GPIO_Configuration * Description : Configures the different GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); }
/******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures the NVIC and Vector Table base address. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif
/* Configure the Priority Group to 2 bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* enabling interrupt */ NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Enable the EXTI3 Interrupt on PD.3 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Enable the EXTI4 Interrupt on PD.4 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Configure the SysTick handler priority */ NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 2, 0); }
void SysTick_Config(void) { /* Configure HCLK clock as SysTick clock source */ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
/* SysTick interrupt each 100 Hz with HCLK equal to 72MHz */ SysTick_SetReload(720000);
/* Enable the SysTick Interrupt */ SysTick_ITConfig(ENABLE);
/* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); }
void delay() { int i; for (i=0; i<0xfffff; i++); }
void Led_RW_ON(void) { GPIO_SetBits(GPIOC, GPIO_Pin_6 | GPIO_Pin_7 ); }
void Led_RW_OFF(void) { GPIO_ResetBits(GPIOC, GPIO_Pin_6 | GPIO_Pin_7 ); } void Led_RE_ON(void) { GPIO_SetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5 ); }
void Led_RE_OFF(void) { GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5 ); } #ifdef DEBUG /******************************************************************************* * Function Name : assert_failed * Description : Reports the name of the source file and the source line number * where the assert error has occurred. * Input : - file: pointer to the source file name * - line: assert error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */ while (1) { } } #endif
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
|