米客-显示驱动专家

STM32初探

0
阅读(2853)

上次搞了快STM32板子,纠结的慢慢卡了下,板子不错,但是教程不敢恭维,红X的板子,估计也是超别人的,教程没任何说明,仅仅是些程序和资料。

看了下资料,在结合其他的入门教程,基本的GPIO会了控制。话说好多的程序,学到哪就看哪,弄清楚句子的含义在慢慢消化。

如果从51过来,会消耗比较多的时间,理解这样的控制。

GPIO的控制,由于IO接口已经不是普通的。


STM32的GPIO,如果你要用它,就要做两步工作,第一是配置模式、速度,第二是配置其时钟并使能(貌似说两步有点牵强)。估计现在大家已经糊涂了,好吧,别急,我一点一点给大家解释解释;

我们第一步要知道GPIO的那八种模式是什么,怎么用。GPIO的八种模式如下:

(1)浮空输入:In_Floating

(2)带上拉输入:IPU(In Push_Up)

(3)带下拉输入:IPD(In Push_Down)

(4)模拟输入:AIN(Anolog In)

(5)开漏输出:OUT_PD(OD 代表开漏,既Open Drain,OC代表开集,Open Collect)

(6)推挽输出:OUT_PP(PP代表推挽式,Push_Pull)

(7)复用功能推挽输出:AF_PP(AF代表复用功能,Alternate-Fuction)

(8)复用功能开漏输出:AF_OD


#include "stm32f10x_lib.h"
GPIO_InitTypeDef GPIO_InitStructure;//参看 GPIO.H 结构体名称 变量名
ErrorStatus HSEStartUpStatus;//声明一个枚举变量
u8 count=0;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void); //系统时钟的配置
void NVIC_Configuration(void);
void Delay(vu32 nCount); //延时程序

/*****************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif

/* System Clocks Configuration **********************************************/
RCC_Configuration();

/* NVIC Configuration *******************************************************/
NVIC_Configuration();

/* Configure all unused GPIO port pins in Analog Input mode (floating input
trigger OFF), this will reduce the power consumption and increase the device
immunity against EMI/EMC *************************************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE); //端口时钟使能

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8; //PA0 PA8设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//输入模式
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //PC13设置为输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PD3设置为输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PD3设置为输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOG, &GPIO_InitStructure);



/* Configure IO connected to LED1, LED2, LED3 *********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_Write(GPIOF, 0xfE3f);
Delay(0xFFFFF);
while (1)
{

if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==0) //如果PA0 = 0
{
GPIO_WriteBit(GPIOF,GPIO_Pin_6,Bit_RESET);

Delay(0xAFFFF);
}

if(GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_8)==0) //如果PC13 = 0
{
GPIO_ResetBits(GPIOF, GPIO_Pin_7); //D3亮
Delay(0xAFFFF);
}

if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13)==0) //如果PA8 = 0
{
GPIO_ResetBits(GPIOF, GPIO_Pin_8); //D2亮
Delay(0xAFFFF);
}

GPIO_SetBits(GPIOF, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8);

}
}

/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* 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)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);

/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 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)
{
}
}
}

/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures 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
}

/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}

#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****/



代码表示的是3个按键控制LED的亮

总结下,初了时钟配置等暂时不清楚,I/O控制,很有必要的语句是



GPIO_ResetBits(GPIOF, GPIO_Pin_7); //D3亮
Delay(0xAFFFF);
GPIO_ResetBits(GPIOF, GPIO_Pin_8); //D2亮
Delay(0xAFFFF); */


GPIO_ResetBits(GPIOF, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8);//第二个参数,与功能


IO电平控制
GPIO_SetBits(x,x);

GPIO_ResetBits(x,x);




Baidu
map