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

📄 main.c

📁 mc9s12dg128中头文件的相关说明和中断入口设置
💻 C
字号:
#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
//子函数声明
void PORT_Init(void);
void PWM_Init(void) ;
void delay1ms(long a); 
void ECT_Init(void) ;
void Make_Turn(void);
//void delay1us (unsigned int us);
/*********************************************************************************
**********************   子函数定义  *********************************************
*
*
************************************************************************************/
/*********************************************************************************
*函数名:Init_PLL()
*入口函数:无
*出口函数:无
*功能:初使化时钟频率,sys clock=48MHz,BUS CLOCK=24MHz
**********************************************************************************/
void PLL_Init(void)
 {
   REFDV=0x01;//initiate the PLL clock
   SYNR=0x02; //Osc 16M,bus clock 24MHz
   while(!(CRGFLG&0x08))
    {}//wait until steady
   CLKSEL=0x80;//select the PLLCLK
   
   }  
/***********************************************************************************
*函数名:Init_PORT() 
*入口参数:无
*出口参数:无
*功能:端口初始化,包括H,K,A,B,其中A和B口用于LCD显示,H和K口用于红外传感器信号输入
************************************************************************************/   
void PORT_Init(void)
{
 
   DDRK = 0x00;     //八个端口设置为输入
   PUCR_PUPKE = 1;  //设置上拉电阻
   DDRH = 0x00;     //八个端口设置为输入
   RDRH = 0xff;     //降功率启动
   PERH = 0xff;     //设置上拉下拉功能
  // PUCR_PUPAE=1;    //设置A口上拉
  // DDRA      = 0xff;     //A口初始化为输出
  // PORTA = 0x00;
  // PUCR_PUPBE= 1;    //设置B口上拉
  // DDRB      = 0xff ;     //B口初始化为输出
  // PORTB     = 0xff;
  // DDRJ = 0xc0;     //J口初始化,DDRJ7设置为输出,用来驱动LED,DDRJ6设置为输出,用来驱动SPEAK
                    //DDRJ0设置为输入,用来驱动KEY1,DDRJ0用来驱动KEY2;
                    //J口默认情况下即为上拉
 }
/*************************************************************************************
*函数名:PWM_Init()
*入口参数:无
*出口参数:无
*功能:初始化PWM
**************************************************************************************/
 void PWM_Init(void) 
{
    PWMCTL_CON45=1;          //四五两个八位的通道结合成十六位的通道从五通道输出
    PWMCLK=0x0A;             //通道1选择时钟SA,通道3选择时钟SB,通道5选择时钟B
    PWMPRCLK=0x33;           //十六位输出选用1/8时钟频率,八位输出选用1/128时钟频率
    PWMSCLA=0x08;            // Clock A / (2 * PWMSCLA)
    PWMSCLB=0x08;            // Clock B / (2 * PWMSCLB)
    PWMPOL=0xf7;
    PWMPER45=60000;          //周期为20ms
 // PWMDTY45=4500;           // 为了设定占空比占空比为1.5ms?初始1200
    PWMPER1=100;
    PWMDTY1=40;              //占空比40%
 // PWMPER3=0;               //初始化为即为0;
 // PWMDTY3=0;               //占空比0%
    PWME=0x2A;               //电机1,3通道,舵机5通道使能  
    
}

/*************************************************************************************
*函数名: delay1ms()
*入口参数:无
*出口参数:无
*功能:准确延时
***************************************************************************************/
void delay1ms(long ms)
 {
volatile unsigned int i;
while (ms > 0)
{
i = 1846;
/* ------------------------ */
while (i > 0)
{ 
i = i - 1; 
} 
/* ------------------------ */
ms = ms - 1;
}
 }
/**************************************************************************************
*函数名: Make_Turn()
*入口参数:无
*出口参数:无
*功能: 首先判断前后排传感器的状态,然后由模糊 
*Note:整体上属于红外传感器模块                   
***************************************************************************************/ 
void Make_Turn(void) 
{
 unsigned int  head_buffer1 = 0;
 int           Hhead;
// int           Bbody;
 int           angle = 0;   
 unsigned int  sensor_h=0;
 unsigned int  sensor_k=0;
 unsigned int  sensor_kh;
 unsigned int  head_buffer=0;
 unsigned int  body_buffer; 
 
 sensor_h = PTH;
 sensor_k = PORTK&0x3f;
 sensor_kh=   PORTK&0x01;
 head_buffer=0;
 body_buffer = (sensor_k>>1)%32;
 head_buffer =  0 + sensor_h ;
 if(sensor_kh)
 head_buffer+=256;
    //将传感器得到的二进制信号用于小车转向依据
        switch(head_buffer) 
    {
    case 0b111111111: Hhead =10;  break;  //黑线之外
    case 0b111011111: Hhead = 0;  break;  //处于中线上
    case 0b110011111: Hhead = 0;  break;  //处于中线与左1传感器上
    case 0b110111111: Hhead = 0;  break;  //处于左1传感器
    case 0b100111111: Hhead =-8;  break;  //处于左1与左2传感器上
    case 0b101111111: Hhead =-12; break;  //处于左2传感器
    case 0b101111110: Hhead =-16; break;  //处于左2与左3传感器
    case 0b111111110: Hhead =-19; break;  //处于左3传感器
    case 0b011111110: Hhead =-22; break;  //处于左3与左4传感器
    case 0b011111111: Hhead =-25; break;  //处于左4传感器
    case 0b111001111: Hhead = 0;  break;  //处于中线与右1传感器
    case 0b111101111: Hhead = 0;  break;  //处于右1传感器
    case 0b111100111: Hhead = 8;  break;  //处于右1与右2传感器
    case 0b111110111: Hhead =12;  break;  //处于右2传感器
    case 0b111110011: Hhead =16;  break;  //处于右2与右3传感器
    case 0b111111011: Hhead =19;  break;  //处于右3传感器
    case 0b111111001: Hhead =22;  break;  //处于右3与右4传感器 
    case 0b111111101: Hhead =25;  break;  //处于右4传感器
    case 0b110001111: Hhead = 9;  break;  //中线,左一,右一同时检测到黑线,用于十字交叉线的检测
    default:          break;
    }
   if(Hhead == 10)
    {
      PWMDTY1  = PWMDTY1;
      PWMDTY45 = PWMDTY45;
      delay1ms(1);
    } 
    else if(Hhead >=22 || Hhead <=-22)
     {  
      PWMDTY45 = 4500 + 30 * Hhead;
      PWMDTY1  = 55;
      delay1ms(1);
     } 
    else if(Hhead>=12 || Hhead<=-12) 
     {  
      PWMDTY45 = 4500 + 27 * Hhead;
      PWMDTY1  = 40;
      delay1ms(1);
     } 
   else if(Hhead >=8 || Hhead <=-8)
     {  
      PWMDTY45 = 4500 + 25 * Hhead;
      PWMDTY1  = 50;
      delay1ms(1);
     } 
   else 
   {
    PWMDTY45 = 4500;
    PWMDTY1  = 60;
    delay1ms(1);
   } 
}
void main(void)
{
   PLL_Init();
   PORT_Init();
   PWM_Init();
  // ECT_Init();
   EnableInterrupts;
  for(;;) 
  {
   Make_Turn();
  }

}

⌨️ 快捷键说明

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