Thursday, August 6, 2015

Bài 5 : Ngắt ngoài với STM8 (EXTI peripheral)

1-Giới thiệu.
1 ưu điểm rất mạnh của STM8 so với các dòng vi điều khiển 8 bit cổ điển như 89x52, AVR ,hay Pic là tất cả các chân của nó đều có thể là chân ngắt.
Điều đó là 1 sự tuyệt vời khi làm các ứng dụng với nút bấm điều khiển hoặc bàn phím, Nó giúp chúng ta không cần phải quét phím liên tục.
2- Cách làm việc
Để làm việc với ngắt ngoài chúng ta cần 2 thư viện đó là stm8_gpio.c và stm8s_exti.c
Các bạn cần add chúng vào để làm việc.
GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_IN_PU_IT); // chân PD2 là chân vào, trở kéo lên và có ngắt ngoài
Để hiểu câu lệnh này các bạn xem lại bài 1
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD, EXTI_SENSITIVITY_FALL_ONLY);// chỉ cho phép ngắt sườn xuống ở cổng Pd
 enableInterrupts();// kích hoạt ngắt
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 3)
{

}
do 1 Port có thể có nhiều ngắt xảy ra nên bạn cần kiểm tra từng chân với lệnh GPIO_ReadInputPin()
nếu =0 thì thực hiện
Sau đây là 1 chương ví dụ : đèn led lắp tại PD3 và nút nhấn lắp tại PD2 , Khi ấn nút thì đổi trạng thái của đèn
/**
  ******************************************************************************
  * @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(EXTI_PORTD_IRQHandler, 6)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
  GPIO_WriteReverse(GPIOD,GPIO_PIN_3);
}
void main(void)
{
  GPIO_Init(GPIOD,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_SLOW );
  GPIO_Init(GPIOD,GPIO_PIN_2,GPIO_MODE_IN_PU_IT );
  EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD, EXTI_SENSITIVITY_FALL_ONLY);

  GPIO_WriteHigh(GPIOD,GPIO_PIN_3);
  
 enableInterrupts();// kích ho?t ng?t
  /* 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****/


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

1 comment: