Stm32 UART - FabLabSeoul/WingProject GitHub Wiki

STM32 UART

์—ฌ๊ธฐ์„œ๋Š” Stm32 ์‹œ๋ฆฌ์–ผํ†ต์‹ ์— ๊ด€ํ•ด์„œ ์„ค๋ช…ํ•œ๋‹ค. ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์— ๊ด€ํ•ด์„œ๋Š” http://www.ni.com/white-paper/2895/ko/ ๊ธ€์„ ์ฐธ์กฐํ•˜์ž.

์‹œ๋ฆฌ์–ผํ†ต์‹ ์€ ๋‘ ์„ ๋งŒ ์—ฐ๊ฒฐํ•˜๋ฉด ์„œ๋กœ ์ •๋ณด๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ๋‘ ์„ ์„ ๊ฐ๊ฐ Rx, Tx ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ •๋ณด๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ์„ ์„ Rx, ์†ก์‹ ํ•˜๋Š” ์„ ์„ Tx๋ผ๊ณ  ํ•œ๋‹ค. (๊ทธ๋ผ์šด๋“œ ์„ ๊นŒ์ง€ ํ•ฉ์น˜๋ฉด, ์ด Rx,Tx,GND ์„ ์ด ํ•„์š”ํ•˜๋‹ค.) ๊ทธ๋ž˜์„œ ์‹œ๋ฆฌ์–ผํ†ต์‹ ์€ ์–‘๋ฐฉํ–ฅ ํ†ต์‹ ์ด๋‹ค. ํ•˜๋“œ์›จ์–ด๊ฐ„์˜ ํ†ต์‹ ์œผ๋กœ ๋งŽ์ด ์“ฐ์ด๊ณ , ๋””๋ฒ„๊น…์—๋„ ๋งŽ์ด ์“ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ์•Œ์•„ ๋‘์–ด์•ผ ํ•œ๋‹ค.

Stm32 Value line discovery๋ณด๋“œ๋Š” USART ๋ชจ๋“ˆ์ด 2๊ฐœ ์žˆ๋‹ค. USART1, USART2๊ฐ€ ๊ทธ๊ฒƒ์ด๋‹ค.

UART์— ๊ด€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Stm32F103์‘์šฉ.pdf ํŒŒ์ผ์„ ์ฐธ๊ณ ํ•˜์ž.

์ด ์˜ˆ์ œ๋Š” Stm32 Value line discovery ๋ณด๋“œ๋ฅผ ๋””๋ฒ„๊น…ํ•˜๊ธฐ ์œ„ํ•œ ์‹œ๋ฆฌ์–ผํฌํŠธ ํ†ต์‹  ์†Œ์Šค๋‹ค. ํŠนํžˆ ์ž์ด๋กœ์„ผ์„œ, ๊ฐ€์†์„ผ์„œ ์ •๋ณด๋ฅผ ํฌํŠธ์—์„œ ์ž…๋ ฅ๋ฐ›์•„ ์‹œ๊ฐํ™”ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ์œ„ํ•ด ์—ฐ๊ตฌํ–ˆ๋‹ค. ์„ผ์„œ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์•„๋‚ ๋กœ๊ทธ ์ž…๋ ฅ ๊ฐ’์„ ๊ทธ๋ž˜ํ”„ํ™” ์‹œํ‚ค๊ธฐ์œ„ํ•ด ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์œผ๋กœ ์•„๋‘์ด๋…ธ ๋ณด๋“œ์— ์ž…๋ ฅํ•˜๊ณ , ์•„๋‘์ด๋…ธ ๋ณด๋“œ๋Š” ๋‹ค์‹œ ์ปดํ“จํ„ฐ์™€ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์„ํ•ด์„œ ์ตœ์ข…์ ์œผ๋กœ Processing์œผ๋กœ ๊ตฌํ˜„๋œ Graphic System์œผ๋กœ ๊ฒฐ๊ณผ๋ฌผ์„ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•œ๋‹ค. ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ Stm32 ๋ณด๋“œ์—์„œ ๋ฐ”๋กœ ์ปดํ“จํ„ฐ์™€ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์„ ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, ์•„์ง ๊ทธ ๋ถ€๋ถ„์€ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

Stm32 ๋ณด๋“œ๋ฅผ USBํฌํŠธ๋กœ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Stm32 Serial debugging ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์ž.

USART, UART, Serial ์— ๊ด€ํ•œ ๋ฌธ์„œ๋Š” http://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter ์—์„œ ํ™•์ธํ•˜์ž. ๊ฐ„๋‹จํžˆ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. USART > UART > Serial

์ด ์˜ˆ์ œ๋Š” STM32\stm32vldiscovery_package\Project\Examples\Serial ํด๋”์— ์žˆ๋‹ค. STM32 Value line discovery ์šฉ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์˜ˆ์ œ๋‹ค.

์‹œ๋ฆฌ์–ผ ํ†ต์‹  Rx,Tx ์„ค์ •

PA10-Rx, PA9-Tx ๋กœ ํฌํŠธ๋ฅผ ์„ค์ •ํ•œ๋‹ค. ํฌํŠธ๋ฅผ Alternate Function ๋ชจ๋“œ๋กœ ์„ค์ •ํ•œ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. I2C์„ค์ •๋„ ์ด์™€ ๋น„์Šทํ•˜๋‹ค.

GPIO_InitTypeDef GPIO_InitStructure;

// RCC Configuration
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	

// Port ์„ค์ •
// PA9  - Tx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// PA10  - Rx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART ์„ค์ •

ADC, DAC ๋ฌธ์„œ๋ฅผ ์ˆ™์ง€ํ–ˆ๋‹ค๋ฉด, USART ๋˜ํ•œ ํฌ๊ฒŒ ๋‹ค๋ฅผ๊ฒŒ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ํด๋Ÿญ์„ ์„ค์ •ํ•˜๊ณ , USART์— ๊ด€ํ•œ ์˜ต์…˜์„ ์„ ํƒํ•˜๊ณ , Enable์„ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค. ์ „์†ก์€ ํฌ๊ธฐ๋Š” 1 ๋ฐ”์ดํŠธ ๋‹จ์œ„์ด๊ณ , 1 ์ดˆ์— 9600 ๋น„ํŠธ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์ „์†ก๋Ÿ‰์„ ๋„˜์–ด๊ฐ€๋ฉด ๋น„ํŠธ๊ฐ€ ๋ฐ€๋ ค์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ „์ฒด ์ •๋ณด๊ฐ€ ๊นจ์ง€๊ฒŒ ๋œ๋‹ค. ๋ณด๋“œ ๋ ˆ์ดํŠธ๋ฅผ ์ฃผ์˜ํ•ด์„œ ์„ค์ •ํ•˜์ž. ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ 19200๊นŒ์ง€ ์ž˜ ์ž‘๋™ํ•œ๋‹ค. USART์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋งค๋‰ด์–ผ์„ ์ฐธ์กฐํ•˜์ž.

USART_InitTypeDef USART_InitStructure;

// RCC Configuration
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );

// UART Port ์„ค์ •
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);

์‹œ๋ฆฌ์–ผ ํ†ต์‹ 

ํ•œ ๋ฐ”์ดํŠธ์”ฉ ์ •๋ณด๋ฅผ ์†ก์‹ ํ•œ๋‹ค. USART_SendData2() ํ•จ์ˆ˜๋Š” ๊ธฐ์กด stm32f10x_usart.h ํŒŒ์ผ์— ์„ ์–ธ๋œ USART_SendData() ํ•จ์ˆ˜์™€ ๊ธฐ๋Šฅ์ ์œผ๋กœ ๊ฑฐ์˜ ๊ฐ™์ง€๋งŒ, ํ•จ์ˆ˜ ์ธ์ž๊ฐ€ unsigned short ์—์„œ short ๋ฐ”๊ผˆ๋‹ค๋Š” ์ ์—์„œ ๋‹ค๋ฅด๋‹ค. unsigned short๊ฐ€ ํ•จ์ˆ˜์ธ์ž๊ฐ€ ๋˜๋ฉด, ์Œ์ˆ˜๋กœ ๋œ ์ •๋ณด๋ฅผ ์†ก์‹ ํ•  ๋•Œ, ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

void print_byte(short  c)
{
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
    USART_SendData2(USART1, c);
}

void loop(void)
{
    print_byte( '1' );
    osa_mdelay(100);
	
    print_byte( '0' );		
    osa_mdelay(100);	
}

์ตœ์ข… ์˜ˆ์ œ ์ฝ”๋“œ

์ด ์˜ˆ์ œ๋Š” STM32 ์—์„œ '1' ๊ณผ '0' ์ •๋ณด๋ฅผ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์„ ํ†ตํ•ด ๋ณด๋‚ธ๋‹ค. RxTx ํฌํŠธ๋Š” ๊ฐ๊ฐ PA10, PA9 ๋ฒˆ ํฌํŠธ๋‹ค. ์ด ํฌํŠธ๋ฅผ ์•„๋‘์ด๋…ธ ๋ณด๋“œ์˜ RxTx ํฌํŠธ์— ์—ฐ๊ฒฐํ•œ๋‹ค. ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด, STM32 Rx -> Arduino Tx, STM32 Tx -> Arduino Rx ์™€ ์—ฐ๊ฒฐํ•œ๋‹ค. ์•„๋‘์ด๋…ธ ๋ณด๋“œ๋Š” ์‹œ๋ฆฌ์–ผํฌํŠธ๋ฅผ ์ฝ์–ด '1' ์ผ ๋•Œ์™€ '0'์ผ ๋•Œ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์„ ํ•˜๊ฒŒ ํ•œ๋‹ค. ์•„๋‘์ด๋…ธ์™€ PC๊ฐ€ ์—ฐ๊ฒฐ๋œ ์ƒํƒœ๋ผ๋ฉด, Processing์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ๊ทธ๋ž˜ํ”ฝ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

๋‚ ๊ฐœ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์ž์ด๋กœ์„ผ์„œ ์ •๋ณด, x,y,z ๊ฐ€์†์„ผ์„œ ์ •๋ณด x,y,z ๋ฅผ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์œผ๋กœ ์•„๋‘์ด๋…ธ์— ์ „๋‹ฌํ•˜๊ณ , ์•„๋‘์ด๋…ธ๋Š” ๋‹ค์‹œ PC๋กœ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ฒŒ ํ•œ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ Processing ์—์„œ๋Š” ๊ทธ ์ •๋ณด๋ฅผ ํ™”๋ฉด์— ๊ทธ๋ž˜ํ”ฝ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ฒŒ ํ•œ๋‹ค.

STM32 ๋ณด๋“œ์—์„œ Processing๊ณผ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ์•„๋‘์ด๋…ธ๋ฅผ ํ†ตํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์•„์ง ๊ทธ ๋ฐฉ๋ฒ•์„ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค.

์ด ์†Œ์Šค๋Š” ์‹ค์ œ ์˜ˆ์ œ์†Œ์Šค์™€ ์กฐ๊ธˆ ์ฐจ์ด๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๋‹ค.

#include "stm32f10x.h"

void __attribute__((weak)) osa_mdelay(unsigned int msec);
void print_byte(unsigned short  c);

 void setup(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    // RCC Configuration
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	

    // Port ์„ค์ •
    // PA9  - Tx
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // PA10  - Rx
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // UART Port ์„ค์ •
    USART_InitStructure.USART_BaudRate    = 9600;
    USART_InitStructure.USART_WordLength   = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits    = USART_StopBits_1;
    USART_InitStructure.USART_Parity    = USART_Parity_No ;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode    = USART_Mode_Rx | USART_Mode_Tx;

    USART_Init(USART1, &USART_InitStructure);
    USART_Cmd(USART1, ENABLE);
}

void loop(void)
{
    print_byte( '1' );
    osa_mdelay(1000);
	
    print_byte( '0' );		
    osa_mdelay(1000);	
}

int main(void)
{
    setup();

    while(1)
    {
        loop();
    }
}

void __attribute__((weak)) osa_mdelay(unsigned int msec)
{
	uint32_t temp;
	SysTick->LOAD=(uint32_t)msec*(HSE_VALUE/1000);
	SysTick->VAL =0x00;		// clear Count flag
	SysTick->CTRL=0x01;
	do
	{
		temp=SysTick->CTRL;
	}
	while((temp&0x01)&&!(temp&(1<<16)));	// wait Count flag set
	SysTick->CTRL=0x00;
	SysTick->VAL =0X00;
}

void __attribute__((weak)) delay_us(unsigned int usec)
{
	uint32_t temp;
	SysTick->LOAD=(uint32_t)usec*(HSE_VALUE/1000000);
	SysTick->VAL =0x00;		// clear Count flag
	SysTick->CTRL=0x01;
	do
	{
		temp=SysTick->CTRL;
	}
	while((temp&0x01)&&!(temp&(1<<16)));	// wait Count flag set
	SysTick->CTRL=0x00;
	SysTick->VAL =0X00;
}

void print_byte(unsigned short  c)
{
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
	USART_SendData(USART1, c);
}

================================

๋ ˆํผ๋Ÿฐ์Šค

http://lifeseed.tistory.com/78

โš ๏ธ **GitHub.com Fallback** โš ๏ธ