Saturday, July 18, 2015

Bài 3: GPIO trong STM8S003 với thư viện STM8S/A Standard Peripherals Firmware Library

trước khi xem bài này các bạn cần biết cách thêm thư viên  STM8 Standard Peripherals tại link sau đây
http://codientu.org/threads/16110/ 
trong link này có hướng dẫn các bạn thêm thư viện vào project
Các hàm xử lý GPIO của stm8s nằm trong file stm8s_gpio.h
void GPIO_DeInit (GPIOx)
Đưa các thanh ghi của GPIO về cấu hình cơ bản
void GPIO_ExternalPullUpConfig(GPIOx,GPIO_PIN_z, NewState)
cấu hình trở treo(Pull-up) trên các trên
GPIOx là GPIOA,GPIOB,....
GPIO_PIN_x =0->7
NewState : DISABLE   : không treo
                   ENABLE    : có treo
void GPIO_Init(GPIOx,GPIO_PIN_z,GPIO_Mode)
GPIO_Mode : 
GPIO_MODE_IN_FL_NO_IT = (uint8_t)0x00, /*!< Input floating, no external interrupt */ thả nổi
GPIO_MODE_IN_PU_NO_IT = (uint8_t)0x40, /*!< Input pull-up, no external interrupt */ trở treo
GPIO_MODE_IN_FL_IT = (uint8_t)0x20, /*!< Input floating, external interrupt */ có ngắt
GPIO_MODE_IN_PU_IT = (uint8_t)0x60, /*!< Input pull-up, external interrupt */ có ngắt
GPIO_MODE_OUT_OD_LOW_FAST = (uint8_t)0xA0, /*!< Output open-drain, low level, 10MHz */ thả nổi 
GPIO_MODE_OUT_PP_LOW_FAST = (uint8_t)0xE0, /*!< Output push-pull, low level, 10MHz */
GPIO_MODE_OUT_OD_LOW_SLOW = (uint8_t)0x80, /*!< Output open-drain, low level, 2MHz */
GPIO_MODE_OUT_PP_LOW_SLOW = (uint8_t)0xC0, /*!< Output push-pull, low level, 2MHz */
GPIO_MODE_OUT_OD_HIZ_FAST = (uint8_t)0xB0, /*!< Output open-drain, high-impedance level,10MHz */
GPIO_MODE_OUT_PP_HIGH_FAST = (uint8_t)0xF0, /*!< Output push-pull, high level, 10MHz */
GPIO_MODE_OUT_OD_HIZ_SLOW = (uint8_t)0x90, /*!< Output open-drain, high-impedance level, 2MHz */
GPIO_MODE_OUT_PP_HIGH_SLOW = (uint8_t)0xD0 /*!< Output push-pull, high level, 2MHz */
uint8_t GPIO_ReadInputData(GPIOx)
-Đọc giá trị trên port GPIOx
BitStatus GPIO_ReadInputPin(GPIOx,GPIO_Pin)
-Đọc giá trị trên chân Pin của PORTx
uint8_t GPIO_ReadOutputData  ( GPIO_TypeDef *  GPIOx )   
-đọc giá trị xuất ra trên Portx
 (x = A đến I). 
void  GPIO_Write (GPIO_TypeDef *GPIOx, uint8_t PortVal) 
-Xuất giá trị PortVal ra Portx
void  GPIO_WriteHigh (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins) 
Xuất High(=1) ra chân Pin
void  GPIO_WriteLow (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins) 
Xuất Low(=0) ra chân Pin
void  GPIO_WriteReverse (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins) 
Đảo trạng thái chân Pin
Nếu các bạn không muốn dùng thư viện của ST thì các bạn có thể thao tác trực tiếp với thanh ghi giống như sau :
GPIOD->DDR|=(uint8_t)(GPIO_PIN_3|GPIO_PIN_4);
GPIOD->CR1|=(uint8_t)(GPIO_PIN_3|GPIO_PIN_4);
GPIOD-.CR2&=~(uint8_t)(GPIO_PIN_3|GPIO_PIN_4);
Ở trên là toàn bộ lý thuyết về  GPIO của STM8
Sau đây tôi sẽ đưa ra 1 ví dụ nhỏ để các bạn tham khảo:

Ấn nút thì đèn sáng nhả nút thì đèn tối

#include "stm8s.h"
#include "stm8s_gpio.h"
//void Delay(uint16_t nCount)
//{
  ///* Decrement nCount value */
  //while (nCount != 0)
  //{
    //nCount--;
 // }
//}
void main()
{
GPIO_Init(GPIOD,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_SLOW);
GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_IN_PU_NO_IT);
  while(1)
    {
   
      if( !GPIO_ReadInputPin(GPIOD,GPIO_PIN_4))
        GPIO_WriteHigh(GPIOD,GPIO_PIN_3);
      else
    GPIO_WriteLow(GPIOD,GPIO_PIN_3);
    }
 } 
Chúc các bạn thành công


5 comments:

  1. muon cho led sang dan roi mo dan thi lam sao ban

    ReplyDelete
    Replies
    1. thì bạn sử dụng timer tạo xung PWM để điều khiển sáng tối.
      Mà lần sau bạn viết có dấu nhé

      Delete
    2. pwm cứng của stm8 có 3 cái thôi à. mình muốn xuất ra tất cả 16 ngõ ra lận bạn. có mấy bác kêu mình viết pwm mềm, mà mình ko hiểu pwm là sao, cach viết thế nào.

      Delete
  2. Bác cho em xin library proteus stm8 với ạ,em cảm ơn bác nhiều!
    Gmail: bloodkiss.tnut@gmail.com

    ReplyDelete
  3. I'm looking for the Stm8 proteus library. I want to simulate. As you share. respects

    Erkan Pehlivan
    pehlivan.erkan@gmail.com

    ReplyDelete