STM32学习2(GPIO EXTI SYSTICK 通信基本概念)
GPIO的使用:
打开GPIO时钟根据手册寄存器映射看在哪根总线上,设一个GPIO_InitTypeDef结构体变量,根据实际给改结构体里面各项赋值,GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct),就初始化设置完了。
具体应用读写直接调用
/* GPIO Read and Write functions **********************************************/
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
void GPIO_ToggleBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
EXTI外设上引脚电平变化中断:
先设置GPIO,记得pupd中设为浮空GPIO_PuPd_NOPULL;
EXTI_InitTypeDef EXTI_KEYSCAN_Struct;
/*enable syscfg clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
/*select pa0*/
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);
/*Specifies the EXTI lines to be enabled or disabled.select EXTI0*/
EXTI_KEYSCAN_Struct.EXTI_Line = EXTI_Line0;
/*Specifies the new state of the selected EXTI lines.*/
EXTI_KEYSCAN_Struct.EXTI_LineCmd = ENABLE;
/*Specifies the mode for the EXTI lines.set mode to intterrupt*/
EXTI_KEYSCAN_Struct.EXTI_Mode = EXTI_Mode_Interrupt;
/*Specifies the trigger signal active edge for the EXTI lines.*/
EXTI_KEYSCAN_Struct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_KEYSCAN_Struct);
/*这部分设置中断优先级,这里我就随便写了一个,优先级组是0,抢占优先级是0,次优先级是1*/
NVIC_InitTypeDef NVIC_InitStruct;
/*Configures the priority grouping: pre-emption priority and subpriority.*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/*!< Specifies the IRQ channel to be enabled or disabled.This parameter can be an enumerator of @ref IRQn_Type enumeration (For the complete STM32 Devices IRQ Channels
list, please refer to stm32f4xx.h file) */
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;//STM32F4XX Interrupt Number Definition
/*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled or disabled. This parameter can be set either to ENABLE or DISABLE */
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
/*!< Specifies the pre-emption priority for the IRQ channel specified in NVIC_IRQChannel. This parameter can be a value between 0 and 15 as described in the table @ref MISC_NVIC_Priority_Table
A lower priority value indicates a higher priority */
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
/*!< Specifies the subpriority level for the IRQ channel specified in NVIC_IRQChannel. This parameter can be a value between 0 and 15 as described in the table @ref MISC_NVIC_Priority_Table
A lower priority value indicates a higher priority */
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
然后就是中断函数了
void EXTI0_IRQHandler(void)//这名字不能变,不然就进到启动文件中中断弱定义的部分中去了。
{
if((EXTI_GetITStatus(EXTI_Line0))!=RESET)//看下是不是真的这个中断了
{
//该干嘛干嘛
}
EXTI_ClearITPendingBit(EXTI_Line0);//出中断之前清下标志位
}
SysTick:
这是在内核中的,cortex-m4通用;
只要调用这个函数配置 __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
void init_Systick(uint32_t time)
{
/*SystemCoreClock = 180mhz 1/time s interrupt*/
if(SysTick_Config(SystemCoreClock/time)) while(1);
}
void delay_ms(uint32_t time0)
{
DELAYTIME = time0;
while(DELAYTIME != 0);
}
中断有预先写好,往里面填就行了
void SysTick_Handler(void)
{
if(DELAYTIME!=0)
{
DELAYTIME--;
}
}
通信:
串并行:串行速度慢但是距离远抗干扰强成本低,并行速度快。
全双工,半双工,单工:全双工可同时收发,半双工只能分时收发,单工只能往一个固定方向传输。
同步、异步:同步需时钟信号,一个时钟信号传输一位数据。异步就没时钟信号了,数据信号线平时置高,收发数据时,先有数据起始位拉低信号线,然后数据,校验位,停止位。所以同步速度快但对时钟信号要求高。
比特率:每秒传输的二进制位数 bit/s
波特率:每秒传输的码元个数(在只有高低电平的时候波特率等同比特率,但有时需两个二进制位才表示一个码元)
UART串口:
先设置串口用到的GPIO及对应RCC。打开要用的串口RCC。初始化usartx配置USART_InitStructure里的每个元素,设置中断组和抢占优先级,次优先级,设置使能串口某中断,使能串口。写中断函数,如USART1的中断服务函数名USART1_IRQHandler