블로그 이미지
안녕1999

카테고리

전체 (3067)
자바스크립트 (20)
안드로이드 (14)
WebGL (4)
변비 (17)
정치,경제 (35)
C언어,ARM (162)
컴퓨터(PC, Note Book, 윈.. (41)
전자회로, PCB (27)
유머,안웃긴,GIF,동영상 (118)
국부론60 (71)
모듈(PCB) (3)
건강 (2)
FreeCAD (25)
PADS (43)
퇴직,퇴사,구직,취업 활동 (3)
C# (86)
엑셀 (8)
워드 (0)
LabView (6)
레고 (30)
FPGA (0)
Total
Today
Yesterday

달력

« » 2025.1
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

공지사항

최근에 올라온 글

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));
}
Posted by 안녕1999
, |

최근에 달린 댓글

글 보관함