嵌入式学习-ARM
STM32串口控制三盏灯亮灭
MAIN.C
# include "uart4.h"
int main ( )
{ uart4_init ( ) ; gpio_init ( ) ; char str[ 32 ] ; while ( 1 ) { gets ( str) ; if ( my_strcmp ( str, "A" ) == 0 ) { led1 ( ) ; } else if ( my_strcmp ( str, "B" ) == 0 ) { led2 ( ) ; } else if ( my_strcmp ( str, "C" ) == 0 ) { led3 ( ) ; } puts ( str) ; } return 0 ;
}
UART4.C
# include "uart4.h" void uart4_init ( )
{ RCC-> MP_AHB4ENSETR |= ( 0x1 ) ; RCC-> MP_AHB4ENSETR |= ( 0x1 << 6 ) ; RCC-> MP_APB1ENSETR |= ( 0x1 << 16 ) ; GPIOB-> MODER &= ( ~ ( 0X3 << 4 ) ) ; GPIOB-> MODER |= ( 0X1 << 5 ) ; GPIOG-> MODER &= ( ~ ( 0X3 << 22 ) ) ; GPIOG-> MODER |= ( 0X1 << 23 ) ; GPIOB-> AFRL &= ( ~ ( 0XF << 8 ) ) ; GPIOB-> AFRL |= ( 0X8 << 8 ) ; GPIOG-> AFRH &= ( ~ ( 0XF << 12 ) ) ; GPIOG-> AFRH |= ( 0X6 << 12 ) ; USART4-> CR1 &= ( ~ ( 0X1 ) ) ; USART4-> CR1 &= ( ~ ( 0X1 << 12 ) ) ; USART4-> CR1 &= ( ~ ( 0X1 << 28 ) ) ; USART4-> CR1 &= ( ~ ( 0X1 << 10 ) ) ; USART4-> CR2 &= ( ~ ( 0X3 << 12 ) ) ; USART4-> CR1 &= ( ~ ( 0X1 << 15 ) ) ; USART4-> PRESC &= ( ~ ( 0XF ) ) ; USART4-> BRR &= ( ~ ( 0XF ) ) ; USART4-> BRR |= ( 0X22b ) ; USART4-> CR1 |= ( 0X1 << 3 ) ; USART4-> CR1 |= ( 0X1 << 2 ) ; USART4-> CR1 |= ( 0X1 ) ;
} void gpio_init ( )
{ RCC-> MP_AHB4ENSETR |= ( 0X3 << 4 ) ; GPIOE-> MODER &= ( ~ ( 0X3 << 20 ) ) ; GPIOE-> MODER |= ( 0X1 << 20 ) ; GPIOF-> MODER &= ( ~ ( 0X3 << 20 ) ) ; GPIOF-> MODER |= ( 0X1 << 20 ) ; GPIOE-> MODER &= ( ~ ( 0X3 << 16 ) ) ; GPIOE-> MODER |= ( 0X1 << 16 ) ; GPIOE-> OTYPER &= ( ~ ( 0X1 << 10 ) ) ; GPIOF-> OTYPER &= ( ~ ( 0X1 << 10 ) ) ; GPIOE-> OTYPER &= ( ~ ( 0X1 << 8 ) ) ; GPIOE-> OSPEEDR &= ( ~ ( 0X3 << 20 ) ) ; GPIOF-> OSPEEDR &= ( ~ ( 0X3 << 20 ) ) ; GPIOE-> OSPEEDR &= ( ~ ( 0X3 << 16 ) ) ; GPIOE-> PUPDR &= ( ~ ( 0X3 << 20 ) ) ; GPIOF-> PUPDR &= ( ~ ( 0X3 << 20 ) ) ; GPIOE-> PUPDR &= ( ~ ( 0X3 << 16 ) ) ; GPIOE-> ODR &= ( ~ ( 0X1 << 10 ) ) ; GPIOF-> ODR &= ( ~ ( 0X1 << 10 ) ) ; GPIOE-> ODR &= ( ~ ( 0X1 << 8 ) ) ;
}
void putchar ( char a)
{ while ( ! ( USART4-> ISR & ( 0X1 << 7 ) ) ) ; USART4-> TDR = a; while ( ! ( USART4-> ISR & ( 0X1 << 6 ) ) ) ;
}
char getchar ( )
{ while ( ! ( USART4-> ISR & ( 0X1 << 5 ) ) ) ; return USART4-> RDR;
}
void gets ( char * str)
{ while ( 1 ) { * str = getchar ( ) ; putchar ( * str) ; if ( * str == '\r' ) break ; str++ ; } * str = '\0' ; putchar ( '\n' ) ;
}
void puts ( char * s)
{ while ( * s) { putchar ( * s) ; s++ ; } putchar ( '\r' ) ; putchar ( '\n' ) ;
} void led1 ( )
{ if ( GPIOE-> ODR & ( 0X1 << 10 ) ) GPIOE-> ODR &= ( ~ ( 0X1 << 10 ) ) ; else GPIOE-> ODR |= ( 0X1 << 10 ) ;
}
void led2 ( )
{ if ( GPIOF-> ODR & ( 0X1 << 10 ) ) GPIOF-> ODR &= ( ~ ( 0X1 << 10 ) ) ; else GPIOF-> ODR |= ( 0X1 << 10 ) ;
}
void led3 ( )
{ if ( GPIOE-> ODR & ( 0X1 << 8 ) ) GPIOE-> ODR &= ( ~ ( 0X1 << 8 ) ) ; else GPIOE-> ODR |= ( 0X1 << 8 ) ;
} int my_strcmp ( const char * src, const char * dest)
{ while ( * src == * dest) { if ( * dest == '\0' ) break ; src++ ; dest++ ; } return * src - * dest;
}
UART4.H
# ifndef __UART4_H__
# define __UART4_H__ # include "stm32mp1xx_uart.h"
# include "stm32mp1xx_gpio.h"
# include "stm32mp1xx_rcc.H" void uart4_init ( ) ;
void gpio_init ( ) ;
void putchar ( char a) ;
char getchar ( ) ;
void puts ( char * s) ;
void gets ( char * str) ;
void led1 ( ) ;
void led2 ( ) ;
void led3 ( ) ;
int my_strcmp ( const char * src, const char * dest) ; # endif