?? main.c
字號:
/****************************************Copyright (c)**************************************************
** 思 蛻 盟 豆 皮 開 發 小 組
** stmfans 論壇
**
** QQ 群: 65081316 StmFans思蛻盟 1組
** QQ 群: 68584951 StmFans思蛻盟 2組
** http://www.stmfans.com/bbs/
**
** This program was produced by the
** IAR Embedded Workbench 4.0 Kickstart 442
** Copyright 2008-2009 stmfans
** Chip type : STM32F103VB
** Program type : Application
** Clock frequency : 8.000000 MHz
** Memory model :
** External SRAM size :
** Data Stack size :
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: Main.c
**創 建 人: 陳海
**最后修改日期: 2008年10月23日
**描 述: 豆皮開發板教程
**
**--------------歷史版本信息----------------------------------------------------------------------------
** 創建人: 陳海
** 版 本: v0.01
** 日 期: 2008年10月23日
** 描 述: 原始版本
**
**--------------當前版本信息----------------------------------------------------------------------------
** 創建人: 陳海
** 版 本: v0.01
** 日 期: 2008年10月23日
** 描 述: 當前版本
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
static vu32 TimingDelay;
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
/*
#ifdef DEBUG
#undef DEBUG
#endif
*/
#ifdef DEBUG
debug();
#endif
//配置系統時鐘
RCC_Configuration();
//配置 NVIC 和 Vector Table
NVIC_Configuration();
//改變指定管腳的映射 GPIO_Remap_SWJ_Disable SWJ 完全失能(JTAG+SW-DP)
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
//改變指定管腳的映射 GPIO_Remap_SWJ_JTAGDisable JTAG-DP 失能 + SW-DP使能
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
//配置LED使用的GPIO口
LED_GPIO_Configuration();
//配置KEY使用的GPIO口
//KEY_GPIO_Configuration();
//配置buzzer蜂鳴器使用的端口
//BUZZER_GPIO_Configuration();
SysTick_Config();
//打開 LED2 其他的關閉顯示
GPIO_SetBits(GPIOC, GPIO_Pin_10);
GPIO_SetBits(GPIOC, GPIO_Pin_12);
GPIO_ResetBits(GPIOC, GPIO_Pin_11);
//主循環
while (1)
{
// 每隔1S鐘就點亮一次LED
Delay_Ms(500);
GPIO_SetBits(GPIOC, GPIO_Pin_12);
Delay_Ms(500);
GPIO_ResetBits(GPIOC, GPIO_Pin_12);
//循環掃描按鍵 按鍵按下時 對應的LED亮
//LED_For_Key_Shine( KEY_GPIO_Scanning() );
}
}
/*******************************************************************************
* Function Name : SysTick_Config
* Description : Configures SysTick
* Input : None
* Output : None
* Return : None
*******************************************************************************/
//SysTick設置
void SysTick_Config(void)
{
/* Disable SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Disable the SysTick Interrupt */
SysTick_ITConfig(DISABLE);
/* Configure HCLK clock as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
/* SysTick interrupt each 1000 Hz with HCLK equal to 72MHz */
SysTick_SetReload(9000);
/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nTime: specifies the delay time length, in milliseconds.
* Output : None
* Return : None
*******************************************************************************/
void Delay_Ms(u32 nTime)
{
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
TimingDelay = nTime;
while(TimingDelay != 0);
/* Disable SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Clear SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Clear);
}
/*******************************************************************************
* Function Name : TimingDelayMs_Decrement
* Description : Decrements the TimingDelay variable.
* Input : None
* Output : TimingDelay
* Return : None
*******************************************************************************/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
//將外設 RCC寄存器重設為缺省值
RCC_DeInit();
//設置外部高速晶振(HSE)
RCC_HSEConfig(RCC_HSE_ON);
//等待 HSE 起振
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
//預取指緩存使能
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//設置代碼延時值
//FLASH_Latency_2 2 延時周期
FLASH_SetLatency(FLASH_Latency_2);
//設置 AHB 時鐘(HCLK)
//RCC_SYSCLK_Div1 AHB 時鐘 = 系統時鐘
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//設置高速 AHB 時鐘(PCLK2)
//RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
RCC_PCLK2Config(RCC_HCLK_Div1);
//設置低速 AHB 時鐘(PCLK1)
//RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
RCC_PCLK1Config(RCC_HCLK_Div2);
// PLLCLK = 8MHz * 9 = 72 MHz
//設置 PLL 時鐘源及倍頻系數
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//使能或者失能 PLL
RCC_PLLCmd(ENABLE);
//等待指定的 RCC 標志位設置成功 等待PLL初始化成功
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
//設置系統時鐘(SYSCLK) 設置PLL為系統時鐘源
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//等待PLL成功用作于系統時鐘的時鐘源
// 0x00:HSI 作為系統時鐘
// 0x04:HSE作為系統時鐘
// 0x08:PLL作為系統時鐘
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
//使能或者失能 APB2 外設時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LED_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4 | GPIO_Pin_3 | GPIO_Pin_2 ;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void KEY_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
u8 KEY_GPIO_Scanning(void)
{
u8 scan_bit; //單個按鍵掃描變量
u8 scan_sum; //四個按鍵總的情況變量 scan_sum低四位的每一位對應一個按鍵
scan_bit = 0;
scan_sum = 0;
//掃描按鍵
scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
//如果按鍵按下 則延遲 再掃描 然后根據判斷處理
if( 0x01 == scan_bit )
{
delay();
scan_bit = 0;
scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
if( 0x01 == scan_bit )
scan_sum |= GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
scan_bit = 0;
}
scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7);
if( 0x01 == scan_bit )
{
delay();
scan_bit = 0;
scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7);
if( 0x01 == scan_bit )
scan_sum |= GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)<<1;
scan_bit = 0;
}
scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6);
if( 0x01 == scan_bit )
{
delay();
scan_bit = 0;
scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6);
if( 0x01 == scan_bit )
scan_sum |= GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6)<<2;
scan_bit = 0;
}
scan_bit = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15);
if( 0x01 == scan_bit )
{
delay();
scan_bit = 0;
scan_bit = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15);
if( 0x01 == scan_bit )
scan_sum |= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)<<3;
scan_bit = 0;
}
return scan_sum;
}
/*******************************************************************************
* Function Name : BUZZER_GPIO_Configuration
* Description : Configures the BUZZER GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void BUZZER_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//蜂鳴器使用的GPIOB 的 Pin9 端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
//蜂鳴器端口設置為 推挽輸出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LED_For_Key_Shine(u8 scan_sum)
{
//使用一個8位變量 表示當前是否有按鍵按下
u8 key_count = 4 ;
if( scan_sum & 0x01 )
{
GPIO_SetBits(GPIOD, GPIO_Pin_2);
key_count--;
}
else
{
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
key_count++;
}
if( scan_sum & 0x02 )
{
GPIO_SetBits(GPIOD, GPIO_Pin_3);
key_count--;
}
else
{
GPIO_ResetBits(GPIOD, GPIO_Pin_3);
key_count++;
}
if( scan_sum & 0x04 )
{
GPIO_SetBits(GPIOD, GPIO_Pin_4);
key_count--;
}
else
{
GPIO_ResetBits(GPIOD, GPIO_Pin_4);
key_count++;
}
if( scan_sum & 0x08 )
{
GPIO_SetBits(GPIOD, GPIO_Pin_5);
key_count--;
}
else
{
GPIO_ResetBits(GPIOD, GPIO_Pin_5);
key_count++;
}
//如果 key_count為0 則表示當前沒有按鍵被按下 蜂鳴器不響
if( key_count == 0 )
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
else
GPIO_SetBits(GPIOB, GPIO_Pin_9);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures NVIC and Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#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
}
void delay()
{
int i;
for (i=0; i<0xffff; i++)
;
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param 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\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -