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

📄 main.c

📁 基于Atmel ATMega88+ATA682的应用实例 主要功能包括: 1) PWM输出控制 2) H-Bridge,4 POWER FET 驱动 3) 模拟量比较
💻 C
字号:
/*
**
****************************************************************************
**
**
**             Copyright (c) 2007 - Atmel Corporation
**             Proprietaty Information
**
** Project    	: ATMEGA88 + ATA6824 High Temperature H-bridge System
** Module     	: main.c
** Description	: High temperature DC motor control
**                To be used with "High temperature H-bridge System board"
**
** Version :     Date:         Author:      Comment:
**    1.0        12.02.2007    F.G.          Creation
**
** LICENSE -
**
** ATMEL - 2007
** All software programs are provided 'as is' without warranty of any kind:
** Atmel does not state the suitability of the provided materials for any
** purpose. Atmel hereby disclaim all warranties and conditions with regard
** to the provided software, including all implied warranties, fitness for
** a particular purpose, title and non-infringement.In no event will Atmel
** be liable for any indirect or consequential damages or any damages
** whatsoever resulting from the usage of the software program.
****************************************************************************
**
*/


/*_____ F U S E S  C O N F I G U R A T I O N ______________________________*/
/* - 8Mhz internal RC Oscillator in being used
 *
 * Caution about MCU start-up time in Fuse configuration :
 * - Care should be taken in Fuse configuration to set up a MCU start-up time
 *   of 0ms or 4.1ms. (By default, it is set to 65ms)
 * - ATA6824 waits a watchdog trigger before typically td = 68ms with 10% tolerance
 * - When no trigger happens during this period of time, MCU is reseted.
 * - Default MCU startup time is set to 65ms, which might be too long regarding
 *   td (68ms ~10%) before a reset occur. MCU won't be able to generate the
 *   WD trig if  td < 65ms. As a result, MCU won't start before being reseted!
 ***************************************************************************/



/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"
//#include "ADC_task.h"     // ADC used to acquire Motor current, desired speed and Vbat
#include "Timer0_PWM.h"   // Timer0 used to generate PWM and Software time base
#include "ATA6824_defs.h" // ATA6824 specific routines (WD, Diagnostic)
#include "Hall_sensors.h" // Manages hall sensors pin change interrupts (PCINT0)
//#include "an_compare.h"   // Detects over current condition


/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N S ______________________________________________*/
//! @defgroup Motor_control Motor status definitions
//! Used in main() to manage DC motor
//! @{
typedef enum
{
  MOTOR_STATUS_STOPPED,   //!< Motor stopped status
  MOTOR_STATUS_CW,        //!< Motor running CW
  MOTOR_STATUS_CCW        //!< Motor running CCW
} Motor_ctrl_t;           //!< Type used to manage motor
//! @}

/*_____ P R O T O T Y P E S - D E C L A R A T I O N ________________________*/
void display_defaults(void);  // Fault output on LEDs


/*_____ G L O B A L S ______________________________________________________*/

/**
 * @fn main manages ADC acquisitions, time base, watchdog refresh, motor
 * operation, fault display and clear.
 **/
void main(void)
{
 // Motor_ctrl_t motor_status = MOTOR_STATUS_STOPPED;

  CLKPR   = 0x80;
  CLKPR   = 0x00;                 // No system clock prescaler
  CONFIG_IO_PORTS();              // Configure I/O ports

  //ADC_Init();                     // initialize ADC
  Timer0_start();                 // Starts Timer 0 for a 31250Hz PWM and
                                  // time management.
  Hall_sensors_ISR_init();        // Configures Pin change interrupts (PCINT0)
  Diag_inputs_ISR_init();         // Configures DG1/2/3 pin change interrupts (PCINT1)
  //AN_compare_init();              // Configures over-current detection through
                                  // analog comparator

  SET_WD_TRIG();                  // 1st watchdog refresh

  __enable_interrupt();           // enable interrupts

  while(1)                        // End-less loop
  {
  //  ADC_task();                   // Manage ADC acquisitions
    manage_time_base();           // Manage time base
    refresh_ATA6824_watchdog();   // Refresh ATA6824 Watchdog

    /* Motor management */
/*    switch(motor_status)
    {
      //--------------------------------------------------------------------
      case MOTOR_STATUS_STOPPED:                        // Motor is stopped
        if (GET_SWITCH_CW())
        {
          SET_MOTOR_DIR();                              // Set motor direction
          TIMER0_SET_OC0B_PWM((U8)((adc_get_speed()>>2)));  // Update PWM ratio
          motor_status = MOTOR_STATUS_CW;
        }
        else if (GET_SWITCH_CCW())
        {
          RESET_MOTOR_DIR();                            // Reset motor direction
          TIMER0_SET_OC0B_PWM((U8)((adc_get_speed()>>2)));  // Update PWM ratio
          motor_status = MOTOR_STATUS_CCW;
        }
        else
          TIMER0_SET_OC0B_PWM(0x00);                    // Set PWM ratio to zero
        break;

      //--------------------------------------------------------------------
      case MOTOR_STATUS_CW:                             // Motor runs Clockwise
      case MOTOR_STATUS_CCW:                            // Motor runs counter clockwise

        if (!GET_SWITCH_CW() && !GET_SWITCH_CCW())      // Stop command from switch
        {
          TIMER0_SET_OC0B_PWM(0x00);                    // Set PWM ratio to zero
          motor_status = MOTOR_STATUS_STOPPED;          // Update motor status
        }
        else
          TIMER0_SET_OC0B_PWM((U8)((adc_get_speed()>>2)));  // Update PWM ratio
        break;

      //--------------------------------------------------------------------
      default:
        motor_status = MOTOR_STATUS_STOPPED;
        TIMER0_SET_OC0B_PWM(0x00);                    // Set PWM ratio to zero
        break;
    }
*/
    // Default display on LEDs
    display_defaults();

    // Clear faults when Release push-button is pushed and faults no more remain
    if (GET_RELEASE_BP())
      clear_faults();       // Clear not remaining faults (latched by software)
  };
}



/*! @brief display_defaults signals to the user that diagnostics have detected
 *  a fault. It displays it on corresponding LED.
 *  Two faults are software latched. When those faults have been latched, the
 *  corresponding LEDs is/are set ON. Those are cleared when faults are unlatched
 *  The DG3 over-temperature doesn't need to be latched. As long as it's present
 *  the corresponding LED is toogled, then cleared if it's not present.
 */
void display_defaults(void)
{
  // Display short-cicuit detected (latched DG1 signal)
  if (ATA6824_diag_mgt.dg1 != NO_FAILURE)    SET_DG1_LED();
  else                                       RESET_DG1_LED();

  // Display pump charge failure, undervoltage or overvoltage detected (latched DG2 signal)
  if (ATA6824_diag_mgt.dg2 != NO_FAILURE)    SET_DG2_LED();
  else                                       RESET_DG2_LED();

  // Toggle DG3_LED in over-temperature conditions
  if (time_count_1s.ovf == true)
  {
    if (ATA6824_diag_mgt.dg3 != NO_FAILURE)
      TOGGLE_DG3_LED();
    else
      RESET_DG3_LED();
    time_count_1s.ovf = false;  // Clear 1s overflow flag
  }
}

⌨️ 快捷键说明

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