SMT32 - UART1 설정
C언어,ARM / 2020. 5. 9. 22:18
UART설정하려면,
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
2가지를 꼭 해주어야하는데,
USART1이 AHB1,APB2,... 어디 붙어있는지 알기위해서는, 데이터시트를 뒤지던가, 코드를 뒤지던가 해야한다.
이럴때, 헤더파일만 찾아보면, 쉽게 알 수 있다.
stm32f2xx_rcc.h
/** @defgroup RCC_APB2_Peripherals
* @{
*/
#define RCC_APB2Periph_TIM1 ((uint32_t)0x00000001)
#define RCC_APB2Periph_TIM8 ((uint32_t)0x00000002)
#define RCC_APB2Periph_USART1 ((uint32_t)0x00000010)
#define RCC_APB2Periph_USART6 ((uint32_t)0x00000020)
#define RCC_APB2Periph_ADC ((uint32_t)0x00000100)
#define RCC_APB2Periph_ADC1 ((uint32_t)0x00000100)
#define RCC_APB2Periph_ADC2 ((uint32_t)0x00000200)
#define RCC_APB2Periph_ADC3 ((uint32_t)0x00000400)
#define RCC_APB2Periph_SDIO ((uint32_t)0x00000800)
#define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000)
#define RCC_APB2Periph_SYSCFG ((uint32_t)0x00004000)
#define RCC_APB2Periph_TIM9 ((uint32_t)0x00010000)
#define RCC_APB2Periph_TIM10 ((uint32_t)0x00020000)
#define RCC_APB2Periph_TIM11 ((uint32_t)0x00040000)
#define IS_RCC_APB2_PERIPH(PERIPH) ((((PERIPH) & 0xFFF8A0CC) == 0x00) && ((PERIPH) != 0x00))
#define IS_RCC_APB2_RESET_PERIPH(PERIPH) ((((PERIPH) & 0xFFF8A6CC) == 0x00) && ((PERIPH) != 0x00))
//TX=PA9,RX=PA10
#define UART1_GPIOx GPIOA
//#define UART1_BaudRate 115200
//#define UART1_BaudRate 57600
//#define UART1_BaudRate 38400
#define UART1_BaudRate 9600
void UART1_Init()
{
USART_InitTypeDef ui;GPIO_InitTypeDef g;NVIC_InitTypeDef n;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(UART1_GPIOx,GPIO_PinSource9,GPIO_AF_USART1);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(UART1_GPIOx,GPIO_PinSource10,GPIO_AF_USART1);
/* Configure USART Tx as alternate function */
g.GPIO_OType=GPIO_OType_PP;
g.GPIO_PuPd=GPIO_PuPd_UP;
g.GPIO_Mode=GPIO_Mode_AF;
g.GPIO_Pin=GPIO_Pin_9;
g.GPIO_Speed=GPIO_Speed_2MHz;//GPIO_Speed_50MHz;
GPIO_Init(UART1_GPIOx,&g);
/* Configure USART Rx as alternate function */
g.GPIO_Mode=GPIO_Mode_AF;
g.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(UART1_GPIOx,&g);
/* Enable USART Interrupt */
n.NVIC_IRQChannel=USART1_IRQn;
n.NVIC_IRQChannelPreemptionPriority=15;//0~15 lower priority value indicates a higher priority
n.NVIC_IRQChannelSubPriority=15;
n.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&n);
/* USART configuration */
ui.USART_BaudRate=UART1_BaudRate;
ui.USART_WordLength=USART_WordLength_8b;
ui.USART_StopBits=USART_StopBits_1;
ui.USART_Parity=USART_Parity_No;
ui.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
ui.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1,&ui);
/* Enable Receive interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//Receive Data register not empty interrupt
//USART_ITConfig(UART1_GPIOx, USART_IT_TXE, ENABLE);//Transmit Data Register empty interrupt
//USART_ITConfig(UART1_GPIOx, USART_IT_TC, ENABLE);//Transmission complete interrupt
/* Enable USART */
USART_Cmd(USART1,ENABLE);
}
void UART1_write(void *pdata,int len)
{
byte *p=(byte*)pdata;
while(len)
{
UART1_WRITE_BYTE(*p);
p++;
len--;
while(UART1_IS_TX_END()==0)
{
CPU_sleep();
}
}
}
void UART1_putch(char a)
{
UART1_write(&a,1);
}
void UART1_puts(char *s)
{
UART1_write(s,strlen(s));
}
'C언어,ARM' 카테고리의 다른 글
ARM cortex 업체별 코드 호환성 (0) | 2020.05.09 |
---|---|
STM32 - ST-LINK download (0) | 2020.05.09 |
error C2733: wmemchr (0) | 2020.05.01 |
디지털 오실로스코프 프로그램 (0) | 2020.04.18 |
fatal error LNK1120: 1개의 확인할 수 없는 외부 참조입니다. - GetRuntimeClass (1) | 2020.04.18 |