gpio_input_stc8g1k08a.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2022 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /***
  15. * Demo: STC8G1K08A GPIO Input
  16. *
  17. * Pin connection:
  18. *
  19. * ___
  20. * GND <- 1KR <- Key -> P5.4 -| |- P3.3 <-- LED --> 4.7KR -> GND
  21. * 3.3V -> VCC -| |- P3.2 <-- LED --> 4.7KR -> GND
  22. * P5.5 -| |- TX
  23. * GND -> GND -|___|- RX
  24. *
  25. *
  26. * test-board: Minimum System; test-MCU: STC8G1K08A
  27. */
  28. #include "fw_hal.h"
  29. void GPIO_Init(void)
  30. {
  31. // P3.2, P3.3
  32. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_3, GPIO_Mode_Output_PP);
  33. // P5.4, pullup
  34. GPIO_P5_SetMode(GPIO_Pin_4, GPIO_Mode_Input_HIP);
  35. GPIO_SetPullUp(GPIO_Port_5, GPIO_Pin_4, HAL_State_ON);
  36. }
  37. int main(void)
  38. {
  39. GPIO_Init();
  40. /* Turn LED1 on, LED2 off */
  41. P32 = SET;
  42. P33 = RESET;
  43. while(1)
  44. {
  45. /* Check if P5.4 is low (Key pressed) */
  46. if (P54 == RESET)
  47. {
  48. /* Switch */
  49. P3 = P3 & 0xF3 | 0x08;
  50. SYS_Delay(300);
  51. /* Restore */
  52. P3 = P3 & 0xF3 | 0x04;
  53. }
  54. }
  55. }