1) 지식 창고는 본인이 작성한 콘텐츠(팁/노하우/리소스/강좌 등)을 무료 혹은 가상화폐인 납포인트를 통해 공유하는 공간입니다.
2) 본인이 작성한 콘텐츠에 대해서만 지식 창고에 등록할 수 있으며, 저작권에 위배되는 콘텐츠는 사전경고 없이 삭제될 수 있습니다.
3) 콘텐츠 구매 및 첨부파일 다운로드는 회원그룹 '연구원' 이상 가능하오니, 경험치를 쌓아 진급한 후에 이용 부탁드립니다.
4) 무료 콘텐츠의 본문은 구매절차 없이 즉시 이용할 수 있으며, 판매 납포인트가 있는 콘텐츠는 구매 후 이용할 수 있습니다.
5) 콘텐츠 판매에 따른 납포인트 수익은 지정한 비율(50%)에 따라 판매자에게 지급하며, 납포인트 수익을 통해 진급을 빨리할 수 있습니다.
6) 구매 후 평가를 하면 구매 납포인트의 20%를 돌려 드립니다.
판매자 | 아크마 | 판매 납포인트 | 무료 | 평점 | 0점 / 총 0명 참여 |
---|
The STM32 USART_Irq example program shows how to configure and use the USART1 of STMicroelectronics STM32F103xx microcontroller in interrupt driven mode.
The configuration of USART1 is 9600 Baud, 8 data bits, 1 stop bit, no parity and no flow control. Retargetting is used to read a character over the serial input and to print out a string to the serial output. A buffer handling is implemented for transmit / receive data.
The configuration of the clocks and USART is done using the Configuration Wizard in file STM32_Init.c.
*---------------------------------------------------------------------------- * Name: Usart.c * Purpose: USART usage for STM32 * Version: V1.00 *---------------------------------------------------------------------------- * This file is part of the uVision/ARM development tools. * This software may only be used under the terms of a valid, current, * end user licence from KEIL for a compatible version of KEIL software * development tools. Nothing else gives you the right to use this software. * * Copyright (c) 2005-2007 Keil Software. All rights reserved. *----------------------------------------------------------------------------*/ #include <stm32f10x_lib.h> // STM32F10x Library Definitions #include <stdio.h> #include "STM32_Init.h" // STM32 Initialization /*---------------------------------------------------------------------------- Notes: The length of the receive and transmit buffers must be a power of 2. Each buffer has a next_in and a next_out index. If next_in = next_out, the buffer is empty. (next_in - next_out) % buffer_size = the number of characters in the buffer. *----------------------------------------------------------------------------*/ #define TBUF_SIZE 256 /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/ #define RBUF_SIZE 256 /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/ /*---------------------------------------------------------------------------- *----------------------------------------------------------------------------*/ #if TBUF_SIZE < 2 #error TBUF_SIZE is too small. It must be larger than 1. #elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0) #error TBUF_SIZE must be a power of 2. #endif #if RBUF_SIZE < 2 #error RBUF_SIZE is too small. It must be larger than 1. #elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0) #error RBUF_SIZE must be a power of 2. #endif /*---------------------------------------------------------------------------- *----------------------------------------------------------------------------*/ struct buf_st { unsigned int in; // Next In Index unsigned int out; // Next Out Index char buf [RBUF_SIZE]; // Buffer }; static struct buf_st rbuf = { 0, 0, }; #define SIO_RBUFLEN ((unsigned short)(rbuf.in - rbuf.out)) static struct buf_st tbuf = { 0, 0, }; #define SIO_TBUFLEN ((unsigned short)(tbuf.in - tbuf.out)) static unsigned int tx_restart = 1; // NZ if TX restart is required /*---------------------------------------------------------------------------- USART1_IRQHandler Handles USART1 global interrupt request. *----------------------------------------------------------------------------*/ void USART1_IRQHandler (void) { volatile unsigned int IIR; struct buf_st *p; IIR = USART1->SR; if (IIR & USART_FLAG_RXNE) { // read interrupt USART1->SR &= ~USART_FLAG_RXNE; // clear interrupt p = &rbuf; if (((p->in - p->out) & ~(RBUF_SIZE-1)) == 0) { p->buf [p->in & (RBUF_SIZE-1)] = (USART1->DR & 0x1FF); p->in++; } } if (IIR & USART_FLAG_TXE) { USART1->SR &= ~USART_FLAG_TXE; // clear interrupt p = &tbuf; if (p->in != p->out) { USART1->DR = (p->buf [p->out & (TBUF_SIZE-1)] & 0x1FF); p->out++; tx_restart = 0; } else { tx_restart = 1; USART1->CR1 &= ~USART_FLAG_TXE; // disable TX interrupt if nothing to send } } } /*------------------------------------------------------------------------------ buffer_Init initialize the buffers *------------------------------------------------------------------------------*/ void buffer_Init (void) { tbuf.in = 0; // Clear com buffer indexes tbuf.out = 0; tx_restart = 1; rbuf.in = 0; rbuf.out = 0; } /*------------------------------------------------------------------------------ SenChar transmit a character *------------------------------------------------------------------------------*/ int SendChar (int c) { struct buf_st *p = &tbuf; // If the buffer is full, return an error value if (SIO_TBUFLEN >= TBUF_SIZE) return (-1); p->buf [p->in & (TBUF_SIZE - 1)] = c; // Add data to the transmit buffer. p->in++; if (tx_restart) { // If transmit interrupt is disabled, enable it tx_restart = 0; USART1->CR1 |= USART_FLAG_TXE; // enable TX interrupt } return (0); } /*------------------------------------------------------------------------------ GetKey receive a character *------------------------------------------------------------------------------*/ int GetKey (void) { struct buf_st *p = &rbuf; if (SIO_RBUFLEN == 0) return (-1); return (p->buf [(p->out++) & (RBUF_SIZE - 1)]); } /*---------------------------------------------------------------------------- MAIN function *----------------------------------------------------------------------------*/ int main (void) { buffer_Init(); // init RX / TX buffers stm32_Init (); // STM32 setup printf ("Interrupt driven Serial I/O Example\r\n\r\n"); while (1) { // Loop forever unsigned char c; printf ("Press a key. "); c = getchar (); printf ("\r\n"); printf ("You pressed '%c'.\r\n\r\n", c); } // end while } // end main
좋은 자료 감사합니다.