Tuesday, September 22, 2015

Bài 7: Làm việc với UART1 trong STM8

UART là 1 ngoại vi quan trọng trong vi điều khiển. Nó có rất nhiều ứng dụng trong giao tiếp với các thiết bị khác như máy tính, các modul sử dụng UART khác. v.v.
Trong bài này tôi sẽ hướng dẫn các bạn sử dụng bộ UART1 của STM8S003.
Để sử dụng UART cần có 2 thư viện là stm8s_uart1.c và stm8s_clk.c 
các hàm trong thư viện stm8s_uart1.c
void UART1_Init
(
uint32_t BaudRate, //tốc độ truyền
UART1_WordLength_TypeDef WordLength, // độ dài byte truyền
UART1_StopBits_TypeDef StopBits, //bit stop
UART1_Parity_TypeDef Parity, // parity
UART1_SyncMode_TypeDef SyncMode, //chế độ truyền
UART1_Mode_TypeDef Mode  //kiểu truyền
)




tốc độ truyền UART thường là 9600 ,.v.v
độ dài byte là 
UART1_WORDLENGTH_8D   8 bits Data
UART1_WORDLENGTH_9D   9 bits Data 
thường sử dụng là 8bit
StopBit nên sử dụng UART1_STOPBITS_1
Parity nên sử dụng UART1_PARITY_NO
SyncMode UART1_SYNCMODE_CLOCK_DISABLE chế độ truyền không đồng bộ
Mode có các kiểu sau
UART1_MODE_RX_ENABLE  0x08 chỉ nhận dữ liệu

UART1_MODE_TX_ENABLE  0x04 chỉ truyền dữ liệu

UART1_MODE_TX_DISABLE  0x80 vô hiệu truyền

UART1_MODE_RX_DISABLE  0x40 vô hiệu đường nhận dữ liệu ( chỉ có đường truyền)

UART1_MODE_TXRX_ENABLE  0x0C kích hoạt cả đường Tx và Rx
ví dụ như sau :
  UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);

sau đây là 1 đoạn code ví dụ:
/**
  ******************************************************************************
  * @file    Project/main.c 
  * @author  MCD Application Team
  * @version V2.2.0
  * @date    30-September-2014
  * @brief   Main program body
   ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */ 


/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"

/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
 INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
   char ch;
   ch=UART1_ReceiveData8();
   if(ch=='a')
     GPIO_WriteHigh(GPIOD,GPIO_PIN_3);
   else
   if (ch=='b')
     GPIO_WriteLow(GPIOD,GPIO_PIN_3);
 }
void main(void)
{
  GPIO_Init(GPIOD,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST);
  UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
  UART1_ITConfig(UART1_IT_RXNE_OR , ENABLE);
  UART1_Cmd(ENABLE);
   enableInterrupts();
  /* Infinite loop */
  while (1)
  {
  }
  
}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(u8* file, u32 line)
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Link download project:

Mời các bạn tham gia thảo luận tại :

No comments:

Post a Comment