⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lights.c

📁 eaayarm101自制小车源代码 周立功公司原创
💻 C
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************
//
// lights.c - Functions for dealing with the lights.
//
// Copyright (c) 2005-2007 Luminary Micro, Inc.  All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  Any use in violation
// of the foregoing restrictions may subject the user to criminal sanctions
// under applicable laws, as well as to civil liability for the breach of the
// terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 199 of an01245.
//
//*****************************************************************************

#include "../../hw_comp.h"
#include "../../hw_gpio.h"
#include "../../hw_ints.h"
#include "../../hw_memmap.h"
#include "../../hw_nvic.h"
#include "../../hw_timer.h"
#include "../../hw_types.h"
#include "compiler.h"
#include "lights.h"
#include "system.h"

//*****************************************************************************
//
//! \page lights_intro Introduction
//!
//! The car has four independently controlled LEDs; two for headlights and two
//! for taillights.  Each LED is connected to its own CCP pin which can be
//! operated in PWM mode to provide brightness control.
//!
//! Two methods are provided to control the headlights.  They can be
//! individually turn on or off, or they can both be put into automatic mode.
//! In automatic mode, they respond to changes in the ambient light level; if
//! it gets "too dark" the headlights will turn on and if it gets "too light"
//! the headlights will turn off.
//!
//! The tail lights do not provide an automatic mode; they can simply be turned
//! on and off individually.
//!
//! The code for controlling the lights is contained in <tt>lights.c</tt>,
//! with <tt>lights.h</tt> containing the API definitions for use by the
//! remainder of the application.
//
//*****************************************************************************

//*****************************************************************************
//
//! \defgroup lights_api Definitions
//! @{
//
//*****************************************************************************

//*****************************************************************************
//
//! This define provides the number of processor clocks in a period of the PWM
//! output signal.  It is computed in terms of the processor clock in order to
//! achieve a particular PWM frequency.
//
//*****************************************************************************
#define PWM_CLOCK               (SYSTEM_CLOCK / 5000)

//*****************************************************************************
//
//! The GPIO port that contains the pin to which the photocell is connected.
//
//*****************************************************************************
#define PHOTOCELL_PORT          GPIO_PORTB_BASE

//*****************************************************************************
//
//! The GPIO pin to which the photocell is connected.
//
//*****************************************************************************
#define PHOTOCELL_PIN           (1 << 4)

//*****************************************************************************
//
//! The GPIO port that contains the pin to which the left headlight is
//! connected.
//
//*****************************************************************************
#define HL_LEFT_PORT            GPIO_PORTD_BASE

//*****************************************************************************
//
//! The GPIO pin to which the left headlight is connected.
//
//*****************************************************************************
#define HL_LEFT_PIN             (1 << 4)

//*****************************************************************************
//
//! The GPIO port that contains the pin to which the right headlight is
//! connected.
//
//*****************************************************************************
#define HL_RIGHT_PORT           GPIO_PORTC_BASE

//*****************************************************************************
//
//! The GPIO pin to which the right headlight is connected.
//
//*****************************************************************************
#define HL_RIGHT_PIN            (1 << 5)

//*****************************************************************************
//
//! The GPIO port that contains the pin to which the left tail light is
//! connected.
//
//*****************************************************************************
#define TL_LEFT_PORT            GPIO_PORTD_BASE

//*****************************************************************************
//
//! The GPIO pin to which the left tail light is connected.
//
//*****************************************************************************
#define TL_LEFT_PIN             (1 << 5)

//*****************************************************************************
//
//! The GPIO port that contains the pin to which the right tail light is
//! connected.
//
//*****************************************************************************
#define TL_RIGHT_PORT           GPIO_PORTC_BASE

//*****************************************************************************
//
//! The GPIO pin to which the right tail light is connected.
//
//*****************************************************************************
#define TL_RIGHT_PIN            (1 << 6)

//*****************************************************************************
//
//! This variable contains the current state of the four lights on the car.
//! The fields within this variable are defined by the #FRONT_LEFT,
//! #FRONT_RIGHT, #BACK_LEFT, and #BACK_RIGHT defines.
//!
//! The value of this variable should not be directly modified to change the
//! state of the lights; the corresponding function should be used to change
//! the state of the lights, which will modify this variable as a side effect.
//
//*****************************************************************************
ZERO_INIT unsigned char g_ucLights;

//*****************************************************************************
//
//! This variable contains the current trip point for the headlights when in
//! automatic mode.  It is a value between 0 and 15 and represents the value N
//! in the equation "3.3 * N / 24", which is the voltage applied to the
//! positive terminal of the analog comparator.  The negative terminal is
//! connected to the external photocell; adjusting this value therefore adjusts
//! the amount of light required for the comparator to trip.
//
//*****************************************************************************
ZERO_INIT unsigned char g_ucLightTripPoint;

//*****************************************************************************
//
//! The field in #g_ucLights that indicates if the left headlight is on or off.
//
//*****************************************************************************
#define FRONT_LEFT              0xc0

//*****************************************************************************
//
//! The field in #g_ucLights that indicates if the right headlight is on or
//! off.
//
//*****************************************************************************
#define FRONT_RIGHT             0x30

//*****************************************************************************
//
//! The field in #g_ucLights that indicates if the left tail light is on or
//! off.
//
//*****************************************************************************
#define BACK_LEFT               0x0c

//*****************************************************************************
//
//! The field in #g_ucLights that indicates if the right tail light is on or
//! off.
//
//*****************************************************************************
#define BACK_RIGHT              0x03

//*****************************************************************************
//
//! Handles interrupts from the comparator.
//!
//! This function is called in response to interrupts from the comparator.  It
//! turns the headlights on and off in response to changing light conditions.
//! This is referred to as automatic mode; the headlights are taken out of
//! automatic mode by simply masking the comparator interrupt so that the
//! processor never sees it.
//!
//! \return None.
//
//*****************************************************************************
void
CompIntHandler(void)
{
    //
    // Clear the comparator interrupt.
    //
    HWREG(COMP_BASE + COMP_O_MIS) = COMP_INT_0;

    //
    // See if the comparator output is high.
    //
    if(HWREG(COMP_BASE + COMP_O_ACSTAT0) & COMP_ACSTAT_OVAL)
    {
        //
        // Turn off the GPIO output.
        //
        HWREG(HL_LEFT_PORT + GPIO_O_DATA + (HL_LEFT_PIN << 2)) = 0;
        HWREG(HL_RIGHT_PORT + GPIO_O_DATA + (HL_RIGHT_PIN << 2)) = 0;

        //
        // Indicate that both headlights are on.
        //
        g_ucLights |= FRONT_LEFT | FRONT_RIGHT;
    }
    else
    {
        //
        // Turn on the GPIO output.
        //
        HWREG(HL_LEFT_PORT + GPIO_O_DATA + (HL_LEFT_PIN << 2)) = HL_LEFT_PIN;
        HWREG(HL_RIGHT_PORT + GPIO_O_DATA + (HL_RIGHT_PIN << 2)) =
            HL_RIGHT_PIN;

        //
        // Indicate that both headlights are off.
        //
        g_ucLights &= ~(FRONT_LEFT | FRONT_RIGHT);
    }
}

//*****************************************************************************
//
//! Initializes the headlights and tail lights.
//!
//! This function configures the headlights and tail lights for operation.  All
//! lights default to being turned off, and the trip level of the headlights
//! when in automatic mode is initialized to twelve.
//!
//! \return None.
//
//*****************************************************************************
void
LightsInit(void)
{
    //
    // Enable the comparator internal voltage reference and set it for 1.65V.
    //
    g_ucLightTripPoint = 12;
    HWREG(COMP_BASE + COMP_O_REFCTL) = (COMP_REFCTL_EN | COMP_REFCTL_RNG |
                                        g_ucLightTripPoint);

    //
    // Setup the comparator to interrupt on any transition of the comparator
    // output.
    //
    HWREG(COMP_BASE + COMP_O_ACCTL0) = (COMP_ACCTL_ASRCP_REF |
                                        COMP_ACCTL_ISEN_BOTH);

    //
    // Make the photocell GPIO pin be an analog input.
    //
    HWREG(PHOTOCELL_PORT + GPIO_O_ODR) &= ~(PHOTOCELL_PIN);
    HWREG(PHOTOCELL_PORT + GPIO_O_PUR) &= ~(PHOTOCELL_PIN);
    HWREG(PHOTOCELL_PORT + GPIO_O_PDR) &= ~(PHOTOCELL_PIN);
    HWREG(PHOTOCELL_PORT + GPIO_O_DEN) &= ~(PHOTOCELL_PIN);

    //
    // Enable the comparator interrupt.
    //
    HWREG(NVIC_EN0) = 1 << (INT_COMP0 - 16);

    //
    // Make the head light and tail light GPIO pins be an outputs.
    //
    HWREG(HL_LEFT_PORT + GPIO_O_DIR) |= HL_LEFT_PIN | TL_LEFT_PIN;
    HWREG(HL_RIGHT_PORT + GPIO_O_DIR) |= HL_RIGHT_PIN | TL_RIGHT_PIN;

    //
    // Make the head light and tail light GPIO pins be open drain outputs.
    //
    HWREG(HL_LEFT_PORT + GPIO_O_ODR) |= HL_LEFT_PIN | TL_LEFT_PIN;
    HWREG(HL_RIGHT_PORT + GPIO_O_ODR) |= HL_RIGHT_PIN | TL_RIGHT_PIN;

    //
    // Set the head light and tail light GPIO drive strength to 8mA.
    //
    HWREG(HL_LEFT_PORT + GPIO_O_DR8R) |= HL_LEFT_PIN | TL_LEFT_PIN;
    HWREG(HL_RIGHT_PORT + GPIO_O_DR8R) |= HL_RIGHT_PIN | TL_RIGHT_PIN;

    //
    // Disable the head light and tail light pull-ups.
    //
    HWREG(HL_LEFT_PORT + GPIO_O_PUR) &= ~(HL_LEFT_PIN | TL_LEFT_PIN);
    HWREG(HL_RIGHT_PORT + GPIO_O_PUR) &= ~(HL_RIGHT_PIN | TL_RIGHT_PIN);

    //
    // Turn off the head lights and tail lights.
    //
    HWREG(HL_LEFT_PORT + GPIO_O_DATA + (HL_LEFT_PIN << 2) +
          (TL_LEFT_PIN << 2)) = HL_LEFT_PIN | TL_LEFT_PIN;
    HWREG(HL_RIGHT_PORT + GPIO_O_DATA + (HL_RIGHT_PIN << 2) +
          (TL_RIGHT_PIN << 2)) = HL_RIGHT_PIN | TL_RIGHT_PIN;

    //
    // Configure the timers to produce PWM outputs.
    //
    HWREG(TIMER0_BASE + TIMER_O_CFG) = TIMER_CFG_16_BIT;
    HWREG(TIMER0_BASE + TIMER_O_TAMR) = (TIMER_TNMR_TNAMS |
                                         TIMER_TNMR_TNTMR_PERIOD);
    HWREG(TIMER0_BASE + TIMER_O_TBMR) = (TIMER_TNMR_TNAMS |
                                         TIMER_TNMR_TNTMR_PERIOD);
    HWREG(TIMER1_BASE + TIMER_O_CFG) = TIMER_CFG_16_BIT;
    HWREG(TIMER1_BASE + TIMER_O_TAMR) = (TIMER_TNMR_TNAMS |
                                         TIMER_TNMR_TNTMR_PERIOD);
    HWREG(TIMER1_BASE + TIMER_O_TBMR) = (TIMER_TNMR_TNAMS |
                                         TIMER_TNMR_TNTMR_PERIOD);

    //
    // Set the PWM outputs for a 5kHz period.
    //
    HWREG(TIMER0_BASE + TIMER_O_TAILR) = PWM_CLOCK - 1;
    HWREG(TIMER0_BASE + TIMER_O_TBILR) = PWM_CLOCK - 1;
    HWREG(TIMER1_BASE + TIMER_O_TAILR) = PWM_CLOCK - 1;
    HWREG(TIMER1_BASE + TIMER_O_TBILR) = PWM_CLOCK - 1;

    //
    // Set the initial duty cycle to 0%.
    //
    HWREG(TIMER0_BASE + TIMER_O_TAMATCHR) = 0;
    HWREG(TIMER0_BASE + TIMER_O_TBMATCHR) = 0;

    //
    // Invert the PWM output level so that larger match values result in longer
    // high times on the output PWM signal.
    //
    HWREG(TIMER0_BASE + TIMER_O_CTL) = TIMER_CTL_TAPWML | TIMER_CTL_TBPWML;
    HWREG(TIMER1_BASE + TIMER_O_CTL) = TIMER_CTL_TAPWML | TIMER_CTL_TBPWML;

    //
    // Enable the timers.
    //
    HWREG(TIMER0_BASE + TIMER_O_CTL) |= TIMER_CTL_TAEN | TIMER_CTL_TBEN;
    HWREG(TIMER1_BASE + TIMER_O_CTL) |= TIMER_CTL_TAEN | TIMER_CTL_TBEN;
}

//*****************************************************************************
//
//! Turns the headlights on.
//!
//! This function will turn on both headlights; if either headlight is already
//! on it will remain on.
//!

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -