3.软件
beep、exti、gate、LCD1602、led、tasks驱动文件添加 GPIO常用函数 中断配置流程 main.c程序
# include "sys.h"
# include "delay.h"
# include "led.h"
# include "tasks.h"
# include "gate.h"
# include "beep.h"
# include "exti.h"
# include "lcd1602.h"
int main ( void )
{
HAL_Init ( ) ;
stm32_clock_init ( RCC_PLL_MUL9) ;
led_init ( ) ;
gate_init ( ) ;
beep_init ( ) ;
exti_init ( ) ;
lcd1602_init ( ) ;
lcd1602_show_line ( 1 , 2 , "HELLO" ) ;
while ( 1 )
{
}
}
# include "exti.h"
# include "sys.h"
# include "delay.h"
# include "led.h"
uint8_t ia_flag = FALSE;
void exti_init ( void )
{
GPIO_InitTypeDef gpio_initstruct;
__HAL_RCC_GPIOB_CLK_ENABLE ( ) ;
gpio_initstruct. Pin = GPIO_PIN_4;
gpio_initstruct. Mode = GPIO_MODE_IT_FALLING;
gpio_initstruct. Pull = GPIO_PULLUP;
HAL_GPIO_Init ( GPIOB, & gpio_initstruct) ;
HAL_NVIC_SetPriority ( EXTI4_IRQn, 2 , 0 ) ;
HAL_NVIC_EnableIRQ ( EXTI4_IRQn) ;
}
void EXTI4_IRQHandler ( void )
{
HAL_GPIO_EXTI_IRQHandler ( GPIO_PIN_4) ;
}
void HAL_GPIO_EXTI_Callback ( uint16_t GPIO_Pin)
{
delay_ms ( 20 ) ;
if ( GPIO_Pin == GPIO_PIN_4)
{
if ( HAL_GPIO_ReadPin ( GPIOB, GPIO_PIN_4) == GPIO_PIN_RESET)
ia_flag = TRUE;
}
}
uint8_t ia_flag_get ( void )
{
uint8_t temp = ia_flag;
ia_flag = FALSE;
return temp;
}
void ia_flag_set ( uint8_t value)
{
ia_flag = value;
}
# ifndef __EXTI_H__
# define __EXTI_H__
# include "stdint.h"
# define TRUE 1
# define FALSE 0
void exti_init ( void ) ;
uint8_t ia_flag_get ( void ) ;
void ia_flag_set ( uint8_t value) ;
# endif
# include "gate.h"
# include "sys.h"
void gate_init ( void )
{
GPIO_InitTypeDef gpio_initstruct;
__HAL_RCC_GPIOB_CLK_ENABLE ( ) ;
gpio_initstruct. Pin = GPIO_PIN_6;
gpio_initstruct. Mode = GPIO_MODE_OUTPUT_PP;
gpio_initstruct. Pull = GPIO_PULLUP;
gpio_initstruct. Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init ( GPIOB, & gpio_initstruct) ;
gate_off ( ) ;
}
void gate_on ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_6, GPIO_PIN_RESET) ;
}
void gate_off ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_6, GPIO_PIN_SET) ;
}
uint8_t gate_status_get ( void )
{
return ( uint8_t ) HAL_GPIO_ReadPin ( GPIOB, GPIO_PIN_6) ;
}
# ifndef __GATE_H__
# define __GATE_H__
# include "stdint.h"
# define GATE_STATUS_ON 0
# define GATE_STATUS_OFF 1
void gate_init ( void ) ;
void gate_on ( void ) ;
void gate_off ( void ) ;
uint8_t gate_status_get ( void ) ;
# endif
# include "beep.h"
# include "sys.h"
void beep_init ( void )
{
GPIO_InitTypeDef gpio_initstruct;
__HAL_RCC_GPIOB_CLK_ENABLE ( ) ;
gpio_initstruct. Pin = GPIO_PIN_5;
gpio_initstruct. Mode = GPIO_MODE_OUTPUT_PP;
gpio_initstruct. Pull = GPIO_PULLUP;
gpio_initstruct. Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init ( GPIOB, & gpio_initstruct) ;
beep_off ( ) ;
}
void beep_on ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_5, GPIO_PIN_RESET) ;
}
void beep_off ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_5, GPIO_PIN_SET) ;
}
# ifndef __BEEP_H__
# define __BEEP_H__
void beep_init ( void ) ;
void beep_on ( void ) ;
void beep_off ( void ) ;
# endif
# include "led.h"
# include "sys.h"
void led_init ( void )
{
GPIO_InitTypeDef gpio_initstruct;
__HAL_RCC_GPIOB_CLK_ENABLE ( ) ;
gpio_initstruct. Pin = GPIO_PIN_8 | GPIO_PIN_9;
gpio_initstruct. Mode = GPIO_MODE_OUTPUT_PP;
gpio_initstruct. Pull = GPIO_PULLUP;
gpio_initstruct. Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init ( GPIOB, & gpio_initstruct) ;
led1_off ( ) ;
led2_off ( ) ;
}
void led1_on ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_8, GPIO_PIN_RESET) ;
}
void led1_off ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_8, GPIO_PIN_SET) ;
}
void led1_toggle ( void )
{
HAL_GPIO_TogglePin ( GPIOB, GPIO_PIN_8) ;
}
void led2_on ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_RESET) ;
}
void led2_off ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_SET) ;
}
void led2_toggle ( void )
{
HAL_GPIO_TogglePin ( GPIOB, GPIO_PIN_9) ;
}
# ifndef __LED_H__
# define __LED_H__
void led_init ( void ) ;
void led1_on ( void ) ;
void led1_off ( void ) ;
void led1_toggle ( void ) ;
void led2_on ( void ) ;
void led2_off ( void ) ;
void led2_toggle ( void ) ;
# endif
# include "tasks.h"
# include "led.h"
uint32_t task1_cnt = 0 ;
uint32_t task2_cnt = 0 ;
uint8_t task1_flag = 0 ;
uint8_t task2_flag = 0 ;
void systick_isr ( void )
{
if ( task1_cnt < 1000 )
task1_cnt++ ;
else
{
task1_flag = 1 ;
task1_cnt = 0 ;
}
if ( task2_cnt < 500 )
task2_cnt++ ;
else
{
task2_flag = 1 ;
task2_cnt = 0 ;
}
}
void task1 ( void )
{
if ( task1_flag == 0 )
return ;
task1_flag = 0 ;
led1_toggle ( ) ;
}
void task2 ( void )
{
if ( task2_flag == 0 )
return ;
task2_flag = 0 ;
led2_toggle ( ) ;
}
# ifndef __TASKS_H__
# define __TASKS_H__
# include "sys.h"
void systick_isr ( void ) ;
void task1 ( void ) ;
void task2 ( void ) ;
# endif
# include "lcd1602.h"
# include "string.h"
# include <stdarg.h>
# include "delay.h"
# define RS_GPIO_Port GPIOB
# define RS_GPIO_PIN GPIO_PIN_1
# define RS_HIGH HAL_GPIO_WritePin ( RS_GPIO_Port, RS_GPIO_PIN, GPIO_PIN_SET)
# define RS_LOW HAL_GPIO_WritePin ( RS_GPIO_Port, RS_GPIO_PIN, GPIO_PIN_RESET)
# define RW_GPIO_Port GPIOB
# define RW_GPIO_PIN GPIO_PIN_2
# define RW_HIGH HAL_GPIO_WritePin ( RW_GPIO_Port, RW_GPIO_PIN, GPIO_PIN_SET)
# define RW_LOW HAL_GPIO_WritePin ( RW_GPIO_Port, RW_GPIO_PIN, GPIO_PIN_RESET)
# define EN_GPIO_Port GPIOB
# define EN_GPIO_PIN GPIO_PIN_10
# define EN_HIGH HAL_GPIO_WritePin ( EN_GPIO_Port, EN_GPIO_PIN, GPIO_PIN_SET)
# define EN_LOW HAL_GPIO_WritePin ( EN_GPIO_Port, EN_GPIO_PIN, GPIO_PIN_RESET)
void lcd1602_gpio_init ( void )
{
GPIO_InitTypeDef GPIO_InitStruct = { 0 } ;
__HAL_RCC_GPIOA_CLK_ENABLE ( ) ;
__HAL_RCC_GPIOB_CLK_ENABLE ( ) ;
GPIO_InitStruct. Pin = GPIO_PIN_0| GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3
| GPIO_PIN_4| GPIO_PIN_5| GPIO_PIN_6| GPIO_PIN_7;
GPIO_InitStruct. Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct. Pull = GPIO_NOPULL;
GPIO_InitStruct. Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init ( GPIOA, & GPIO_InitStruct) ;
GPIO_InitStruct. Pin = GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_10;
GPIO_InitStruct. Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct. Pull = GPIO_NOPULL;
GPIO_InitStruct. Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init ( GPIOB, & GPIO_InitStruct) ;
}
void lcd1602_start ( void )
{
delay_ms ( 15 ) ;
lcd1602_write_cmd ( 0x38 ) ;
delay_ms ( 5 ) ;
lcd1602_write_cmd ( 0x38 ) ;
lcd1602_write_cmd ( 0x08 ) ;
lcd1602_write_cmd ( 0x01 ) ;
lcd1602_write_cmd ( 0x06 ) ;
lcd1602_write_cmd ( 0x0c ) ;
}
void lcd1602_init ( void )
{
lcd1602_gpio_init ( ) ;
lcd1602_start ( ) ;
}
void lcd1602_write_cmd ( char cmd)
{
RS_LOW;
RW_LOW;
EN_LOW;
GPIOA-> ODR = cmd;
delay_ms ( 5 ) ;
EN_HIGH;
delay_ms ( 5 ) ;
EN_LOW;
}
void lcd1602_write_data ( char dataShow)
{
RS_HIGH;
RW_LOW;
EN_LOW;
GPIOA-> ODR = dataShow;
delay_ms ( 5 ) ;
EN_HIGH;
delay_ms ( 5 ) ;
EN_LOW;
}
void lcd1602_right_move ( void )
{
for ( int i= 0 ; i < 16 ; i++ ) {
delay_ms ( 1000 ) ;
lcd1602_write_cmd ( 0x18 ) ;
}
}
void lcd1602_show_line ( char row, char col, char * string)
{
switch ( row) {
case 1 :
lcd1602_write_cmd ( 0x80 + col) ;
while ( * string) {
lcd1602_write_data ( * string) ;
string++ ;
}
break ;
case 2 :
lcd1602_write_cmd ( 0x80 + 0x40 + col) ;
while ( * string) {
lcd1602_write_data ( * string) ;
string++ ;
}
break ;
}
}
# ifndef __LCD1602_H__
# define __LCD1602_H__
# include <stdint.h>
void lcd1602_init ( void ) ;
void lcd1602_write_cmd ( char cmd) ;
void lcd1602_write_data ( char dataShow) ;
void lcd1602_show_line ( char row, char col, char * string) ;
void lcd1602_right_move ( void ) ;
# endif