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

📄 init.c

📁 AirConditioner project
💻 C
字号:
/*  Include  */
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC68HC908GZ60.h> /* include peripheral declarations */
#include "COMMON.h"
#include "INIT.h"

/*  Global Variables */
UINT8 power;
UINT8 ad_data[4];
UINT8 ad_channel[4];
UINT8 adch;
UINT8 process_flag;
struct ad_temperature_convert convert_InCar[41];           //0~40oC
struct ad_temperature_convert convert_OutCar[71];          //-10~60oC

/*****************************************************************************
 * Function:        MCU_Init(void)
 *
 * Description:     Initialization of MCU
 *
 * Returns:         none
 *
 * Notes:           This handler initializes the MCU.
 *
 *****************************************************************************/
void MCU_Init(void)
{  
   // CONFIG 
   CONFIG2        =0x05;         
                                     // MCLK not enabled;Disables the MSCAN08 module;Enables extra divide-by-128 prescaler in timebase module 
                
   CONFIG1        =0x3A;             // COP module enabled
                                     // COP timeout period = 2(18 - 4) COPCLK cycles
                                     // STOP instruction treated as illegal opcode (Reset)
                                     // LVI module power enabled
   // PLL
   PCTL_PLLON       =0;                       //PLL off
   PCTL_BCS         =0;                       //CGMXCLK divided by two drives CGMOUT 
    
   // IRQ
   INTSCR_IMASK     =1;                       //IRQ interrupt requests enabled
   INTSCR_MODE      =0;												//IRQ interrupt requests on falling edges only
   
   // BRK
   //BRKSCR_BRKE      =0;                      //Breaks disabled
   
   // TBM
   TBCR_TBON        =0;                      //Timebase is disabled and the counter initialized to 0s
  
   TBCR_TBR0        =0;
   TBCR_TBR1        =1;
   TBCR_TBR2        =0;                      //Divider262144/fCGMXCLK4M=65.536ms 
   
   TBCR_TBIE        =0;                      //Timebase interrupt is enabled
   TBCR_TBON        =1;                      //Timebase is enabled
  
   // I/O
   DDRA          =0xFF;
   DDRB          =0xFF;
   DDRC          =0xFF;
   DDRD          =0xFF;
   DDRE          =0xFF;
   DDRF          =0xFF;
   DDRG          =0xFF;
 
   DDRB_DDRB7       =0; /*MOTOR_AD input*/    DDRA_DDRA4       =0; /*Keyboard input*/
   DDRB_DDRB6       =0;                       DDRA_DDRA5       =0;  
   DDRB_DDRB5       =0;                       DDRA_DDRA6       =0;  
   DDRB_DDRB3       =0;                       DDRA_DDRA7       =0;  
   
   DDRG_DDRG2       =0; /*SENSOR input*/      DDRG_DDRG0       =0; /*MOTOR_FAULT*/
   DDRG_DDRG1       =0;                       DDRC_DDRC5       =0;
   DDRA_DDRA3       =0;                       DDRC_DDRC6       =0;
   DDRA_DDRA2       =0;                       DDRG_DDRG3       =0;
    
   DDRD_DDRD1       =0; //MISO
  
   PTAPUE_PTAPUE4   =1;                            
   PTAPUE_PTAPUE5   =1;
   PTAPUE_PTAPUE6   =1;
   PTAPUE_PTAPUE7   =1;
 }
 
 /*****************************************************************************
 * Function:        TIM1_Init(void)
 *
 * Description:     Initialization of PWM
 *
 * Returns:         none
 *
 * Notes:           This handler initializes PWM.
 *
 *****************************************************************************/      

void TIM1_Init(void)
{
  T1SC_TOIE     =0;                   //TIM1 overflow interrupts disabled
  T1SC_PS       =0;                   //Internal bus clock ÷ 1
  T1SC_TSTOP    =1;                   //Stop the TIM1 counter
  T1SC_TRST     =1;                   //Reset the TIM1 counter and prescaler
  
  T1MODH        =0x00;
  T1MODL        =0xC8;                //200 times internal clock
  
  T1CH0H        =0x00;
  T1CH0L        =0x00;                //Duty cycle 
  
  T1SC0_MS0A    =1;
  T1SC0_MS0B    =0;
  T1SC0_ELS0A   =0;
  T1SC0_ELS0B   =1;                   //Clear output on compare
  T1SC0_TOV0    =1;                   //toggle-on-overflow
  //T1SC0_CH0IE   =1;                 //Channel 0 CPU interrupt requests enabled
  
  T1SC_TSTOP    =0;                   //clear the TIM1 stop bit

}
/*****************************************************************************
 * Function:        TIM2_Init(void)
 *
 * Description:     Initialization of PWM for Motor
 *
 * Returns:         none
 *
 * Notes:           This handler initializes PWM.
 *
 *****************************************************************************/      

void TIM2_Init(void)
{
  T2SC_TSTOP    = 1;                //Stop the TIM2 counter
  T2SC_TRST     = 1;                //Reset the TIM2 counter and prescaler
  T2SC_TOIE     = 0;                //TIM2 overflow interrupts disabled
  T2SC_PS       = 0;                //Internal bus clock ÷1
  
  
  T2MODH        =0x00;             
  T2MODL        =0xC8;              //200*bus cycle time =5K PWM frequency

  
/*T2CH0*/  
  T2CH0H        =0x00; 
  T2CH0L        =0x64;              //50% duty cycle selected for T2CH0
  
  T2SC0_MS0A    =1;
  T2SC0_MS0B    =0;                 //PWM Mode selected
  T2SC0_TOV0    =1;                 //                 
  T2SC0_ELS0A   =0;
  T2SC0_ELS0B   =1;                 //Clear output on compare                     
  
  T2SC0_CH0IE   =0;                 //Channel 0 CPU interrupt requests disabled
/*T2CH1*/
  T2CH1H        =0x00; 
  T2CH1L        =0x64;              //50% duty cycle selected for T2CH1
  
  T2SC1_MS1A    =1;
                                    //PWM Mode selected
  T2SC1_TOV1    =1;                 //                 
  T2SC1_ELS1A   =0;
  T2SC1_ELS1B   =1;                 //Clear output on compare
  
  T2SC1_CH1IE   =0;                 //Channel 1 CPU interrupt requests disabled
/*T2CH2*/  
  T2CH2H        =0x00; 
  T2CH2L        =0x64;              //50% duty cycle selected for T2CH2
  
  T2SC2_MS2A    =1;
                                    //PWM Mode selected
  T2SC2_TOV2    =1;                 //                 
  T2SC2_ELS2A   =0;
  T2SC2_ELS2B   =1;                 //Clear output on compare
  
  T2SC2_CH2IE   =0;                 //Channel 2 CPU interrupt requests disabled
/*T2CH3*/  
  T2CH3H        =0x00; 
  T2CH3L        =0x64;              //50% duty cycle selected for T2CH3
  
  T2SC3_MS3A    =1;
                                    //PWM Mode selected
  T2SC3_TOV3    =1;                 //                 
  T2SC3_ELS3A   =0;
  T2SC3_ELS3B   =1;                 //Clear output on compare
  
  T2SC3_CH3IE   =0;                 //Channel 3 CPU interrupt requests disabled
  
  T2SC_TSTOP    =0;                 //Clear the TIM2 stop bit
  
}

/*****************************************************************************
 * Function:        KBI_Init(void)
 *
 * Description:     none
 *
 * Returns:         none
 *
 * Notes:           This handler initializes the KBI module.
 *
 *****************************************************************************/

void KBI_Init(void)
{
  INTKBSCR_IMASKK    =1;              //Mask keyboard interrupts
                                          
  INTKBIER_KBIE4=1;                   //PTA4~7 as keyboard interrupt pins
  INTKBIER_KBIE5=1;
  INTKBIER_KBIE6=1;
  INTKBIER_KBIE7=1;
  
  INTKBIPR_KBIP4=0;
  INTKBIPR_KBIP5=0;
  INTKBIPR_KBIP6=0;
  INTKBIPR_KBIP7=0;                   //Keyboard polarity is falling edge and/or low level
  
  INTKBSCR_MODEK     =0;              //Keyboard interrupt requests on edges only
  
  INTKBSCR_ACKK      =1;              //Clear any false interrupts
  INTKBSCR_IMASKK    =0;              //Clear the IMASKK bit
}

/******************************************************************************
 * Function:        AD_Init(void)
 *
 * Description:     AD initialization 
 *                  
 *
 * Returns:         none
 *
 * Notes:           
 *
 ******************************************************************************/

void AD_Init(void)
{ 
  ADSCR_AIEN         =0;                     //ADC interrupt disabled
  ADSCR_ADCO         =0;                     //One ADC conversion
  
  ADCLK_ADIV2        =0;
  ADCLK_ADIV1        =0;
  ADCLK_ADIV0        =0;                     //ADC input clock ÷ 1
  
  ADCLK_MODE1        =0;
  ADCLK_MODE0        =0;                     //8-bit truncation mode
  
  ADCLK_ADICLK       =1;                     //Internal bus clock selected 1M
  
} 

/*****************************************************************************
 * Function:        Variables_Init(void)
 *
 * Description:     Initialization of PWM
 *
 * Returns:         none
 *
 * Notes:           This handler initializes PWM.
 *
 *****************************************************************************/
 void Variables_Init(void) {
   UINT8 i,j;
 
 	 MOTOR_MIX=1;
   MOTOR_CYCLE=0;
   MOTOR_MODE=0;
   MOTOR_CENTER=0;
  
   MOTOR_MIX_EN=0;
   MOTOR_CYCLE_EN=0;
   MOTOR_MODE_EN=0;
   MOTOR_CENTER_EN=0;
  
   COMPRESSOR=OFF;
   CS16512=ON;
   power =ON;
   
   ad_data[0]=0;
   ad_data[1]=0;
   ad_data[2]=0;
   ad_data[3]=0;
  
   ad_channel[0]=TEMP_InCar;
   ad_channel[1]=TEMP_OutCar;
   ad_channel[2]=TEMP_Water;
   ad_channel[3]=SunLight;
  
   adch=0;
   process_flag=0;  
  
 ///// 
  for(i=0;i<41;i++){			                  //(TEMPERATURE+1)/(6 AD_DATA)   0~40oC    
    convert_InCar[i].ad=i*6;
    convert_InCar[i].temperature=i;
  }
 ///// 
  for(j=0;j<16;j++){											  //(TEMPERATURE+1)/(3 AD_DATA)
    convert_OutCar[j].ad=j*3;
    convert_OutCar[j].temperature=j-10;
  }
  for(j=17;j<61;j++){											  //(TEMPERATURE+1)/(4 AD_DATA)
    convert_OutCar[j].ad=45+(j-15)*4;
    convert_OutCar[j].temperature=j-10;
  }
  for(j=61;j<71;j++){										    //(TEMPERATURE+1)/(3 AD_DATA)
    convert_OutCar[j].ad=225+(j-60)*3;
    convert_OutCar[j].temperature=j-10;
  }
 
 }
/*****************************************************************************
 * Function:        GZ60_Init(void)
 *
 * Description:     none
 *
 * Returns:         none
 *
 * Notes:           This handler initializes the KBI module.
 *
 *****************************************************************************/
void GZ60_Init(void)
{
  MCU_Init();
  TIM1_Init();
  TIM2_Init();
  KBI_Init();
  AD_Init();
  Variables_Init();
}

⌨️ 快捷键说明

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