学习目标

  1. 掌握小车的车灯控制
  2. 掌握小车的按键事件
  3. 掌握小车的喇叭控制
  4. 掌握小车的电压检测
  5. 掌握原理图的阅读
  6. 掌握驱动的编写

    学习内容

    LED

    163.png
    通过引脚驱动灯的开关

驱动编写:

  1. #ifndef __LIGHT_H
  2. #define __LIGHT_H
  3. #include "config.h"
  4. #define LIGHT_LEFT_PIN P34
  5. #define LIGHT_RIGHT_PIN P05
  6. #define LIGHT_INIT() {P3M1 &= ~0x10, P3M0 |= 0x10, P0M1 &= ~0x20, P0M0 |= 0x20;}
  7. #define LIGHT_LEFT_ON() (LIGHT_LEFT_PIN = 1)
  8. #define LIGHT_LEFT_OFF() (LIGHT_LEFT_PIN = 0)
  9. #define LIGHT_RIGHT_ON() (LIGHT_RIGHT_PIN = 1)
  10. #define LIGHT_RIGHT_OFF() (LIGHT_RIGHT_PIN = 0)
  11. typedef enum {
  12. LIGHT_ALL = 0, LIGHT_LEFT = 1, LIGHT_RIGHT = 2
  13. } Light;
  14. void Light_init(void);
  15. void Light_on(Light l);
  16. void Light_off(Light l);
  17. #endif
  1. #include "Light.h"
  2. void Light_init(void) {
  3. LIGHT_INIT();
  4. }
  5. void Light_on(Light l) {
  6. if(l == LIGHT_ALL) {
  7. LIGHT_LEFT_ON();
  8. LIGHT_RIGHT_ON();
  9. } else if(l == LIGHT_LEFT) {
  10. LIGHT_LEFT_ON();
  11. } else if(l == LIGHT_RIGHT) {
  12. LIGHT_RIGHT_ON();
  13. }
  14. }
  15. void Light_off(Light l) {
  16. if(l == LIGHT_ALL) {
  17. LIGHT_LEFT_OFF();
  18. LIGHT_RIGHT_OFF();
  19. } else if(l == LIGHT_LEFT) {
  20. LIGHT_LEFT_OFF();
  21. } else if(l == LIGHT_RIGHT) {
  22. LIGHT_RIGHT_OFF();
  23. }
  24. }

按键

164.png
驱动编写:

  1. #ifndef __KEY_H__
  2. #define __KEY_H__
  3. #include "config.h"
  4. #define KEY_PIN P37
  5. #define KEY_PIN_INIT() {P3M1 &= ~0x80, P3M0 &= ~0x80;}
  6. void Key_init();
  7. void Key_scan();
  8. extern void Key_on_keyup();
  9. extern void Key_on_keydown();
  10. #endif
  1. #include "Key.h"
  2. static u8 key_state = 0x01;
  3. void Key_init() {
  4. KEY_PIN_INIT();
  5. }
  6. void Key_scan() {
  7. if(KEY_PIN == 0 && key_state == 1) {
  8. // 当前按下,之前是抬起
  9. key_state = 0;
  10. Key_on_keydown();
  11. } else if(KEY_PIN == 1 && key_state == 0){
  12. key_state = 1;
  13. Key_on_keyup();
  14. }
  15. }

电压检测

166.png
ADC测电压
驱动编写:

  1. #ifndef __BATTERY_H_
  2. #define __BATTERY_H_
  3. #include "config.h"
  4. #include "ADC.h"
  5. #define BATTERY_PIN P01
  6. #define BATTERY_CHN 9
  7. #define BATTERY_PIN_INIT() {P0M1 |= 0x02, P0M0 &= ~0x02;}
  8. void Battery_init();
  9. float Battery_get_voltage();
  10. #endif
  1. #include "Battery.h"
  2. void Battery_init() {
  3. BATTERY_PIN_INIT();
  4. }
  5. float Battery_get_voltage() {
  6. u16 result = Get_ADCResult(BATTERY_CHN);
  7. float v = result * 2.5 / 4096;
  8. return v * 3.0;
  9. }

蜂鸣器

165.png

  • 如果蜂鸣器采用的是无源蜂鸣器,通过指定频率和占空比的PWM进行控制。
  • 如果蜂鸣器采用的是有源蜂鸣器,通电就可以响。

驱动编写:

  1. #ifndef __BUZZER_H__
  2. #define __BUZZER_H__
  3. #include "config.h"
  4. #define BUZZER_PIN P36
  5. #define BUZZER_PIN_INIT() {P3M1 &= ~0x40, P3M0 |= 0x40;}
  6. void Buzzer_init();
  7. void Buzzer_start();
  8. void Buzzer_stop();
  9. #endif
  1. #include "Buzzer.h"
  2. void Buzzer_init() {
  3. BUZZER_PIN_INIT();
  4. }
  5. void Buzzer_start() {
  6. BUZZER_PIN = 1;
  7. }
  8. void Buzzer_stop() {
  9. BUZZER_PIN = 0;
  10. }

练习题

  1. 通过按键来切换蜂蜜器响或者不响
  2. 通过按键来控制灯的开关
  3. 打印电压数据