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

📄 mc9s12dg128.txt

📁 第一届大学生飞思卡尔智能车程序源码
💻 TXT
📖 第 1 页 / 共 2 页
字号:

小车控制程序清单 


**********************************************************************************/ 

/* 

************************************************************************************ main.c 

* 

*

* 

* 

* 

**********************************************************************************/ 

#include <hidef.h> /* common defines and macros */ 

#include <mc9s12dg128.h> /* derivative information */ 

#pragma LINK_INFO DERIVATIVE "mc9s12dg128b" 

#define HIGHSPEED 11500 /* 速度参量,此处未使用测速模块 */ 

#define LOWSPEED0 12500 /* 0-24000 数值越大,速度越慢 */ 

#define LOWSPEED1 12000 /* used in CarMain() */ 

#define STABMAX 50 

#define StopCar() PORTK |= 0x80 /* stop the motor */ 

#define StartCar() PORTK |= 0x04 /* start the motor */ 

#define BrakeCar() PORTK &= 0xfb /* slow the speed of the SmartCar */ 

unsigned int SYSCLOCK=0; /* update in INT_Timer0() */ 

/* 

*********************************************************************************** 

* FUNCTION PROTOTYPES 

**********************************************************************************/ 

/* write in "SmartCar.c" */ 

void Init_INT_RTI(void); /* initiate Real Time Interrupt */ 

void Init_INT_Timer(void); /* INT_Timer0 initiate */ 

void Init_PWMout(void); /* initiate PWM output */ 

void PWMout(int, int); /* output PWM */ 

/* write in "PID.c" */ 

void Init_PID(void); /* initiate PID parameter */ 

int CalculateP(void); /* calculate parameter P */ 

float CalculatePID(void); /* calculate PID */ 

int SignalProcess(unsigned char); /* Process the signal from the sensors */ 

/* write in "Test.c" */ 

void IOtest(void); /* Test I/O */ 

void PWMtest(void); /* Test PWM output */ 

int SignalTest(void); /* Test the sensors */ 

/* write in local file */ 

void Init(void); /* initiate parameter */ 

void ProtectMoto(void); /* the function protecting the Motor */ 

void CarMain(void); /* SmartCar main function */ 

/* 

*********************************************************************************** 

* 主程序 

* 

* 程序描述: 完成智能小车系统的初始化,通过按键可选择工作模式,有I/O测试,PWM 输出测试 

* 传感器测试,以及小车正常工作模式 

* 

* 硬件连接:PORTB 接传感器 

* PWM 输出口 (1)接舵机 (2)接电机驱动芯片MC33886 

* 

* 说明: 无 

**********************************************************************************/ 

void main(void) 

{ 

Init(); 

DDRB = 0x00; 

switch(PORTB) 

{ 

case 0x80: 

IOtest(); 

break; 

case 0x40: 

PWMtest(); 

break; 

case 0x20: 

SignalTest(); 

break; 

default: 

DDRA = 0x00; 

DDRB = 0xff; 

DDRK = 0xff; 

PORTB = 0xff; 

CarMain(); 

EnableInterrupts; 

for(;;); 

break; 

} 

} 

/* 

*********************************************************************************** 

* 小车寻迹行驶函数 

* 

* 程序描述: 通过传感器采集数据,并对其进行处理,通过PID算法得出小车稳定行驶所需的参数,进而调用PWM输出函数 

* 控制舵机与电机的工作 

* 

* 注意: 这个函数调用了 SignalProcess(unsigned char),BrakeCar(),PWMout(Direction, Velocity) 

* 

* 说明: 无 

**********************************************************************************/ 

void CarMain(void) 

{ 

static int Direction=0, Velocity; 

static unsigned char signal; 

static unsigned int BrakeTime = 0, BrakeControl = 0; 

static unsigned int Stability=0, Stab[STABMAX], PStab=0, StabAver; 

int i; 

signal = PORTA; 

PORTB = ~signal; 

Direction = SignalProcess( signal ); 

/* 稳定性系数的计算 */ 

Stability -= Stab[PStab]; 

Stab[PStab] = (unsigned int)Direction/100; 

Stability += Stab[PStab]; 

PStab++; 

if(PStab >= STABMAX) PStab=0; 

StabAver = 0; 

for(i=0;i<STABMAX;i++) 

{ 

if(Stability > Stab[i]) 

StabAver += Stability - Stab[i]; 

else 

StabAver += Stab[i] - Stability; 

} 

if( BrakeTime != 0) 

{ 

BrakeTime--; 

BrakeCar(); 

} 

else 

{ 

StartCar(); 

if(BrakeControl>0) 

BrakeControl--; 

if(Direction < -4000 || Direction > 4000 ) 

{ 

Velocity = LOWSPEED0; 

if(BrakeControl == 0 && StabAver/STABMAX<22) 

{ 

BrakeTime = 20; 

BrakeControl = 120; 

} 

} 

else 

{ 

if(Direction < -2500 || Direction > 2500 ) 

Velocity = LOWSPEED1; 

else 

Velocity = HIGHSPEED; 

} 

} 

PWMout(Direction, Velocity); 

} 

/* 

*********************************************************************************** 

* 系统初始化函数 

* 

* 程序描述: 初始化了系统时钟,FLASH 和 EEPRO的工作频率,PWM输出口,定时器,以及PID算法中的有关参数 

* 

* 注意: 这个函数调用了 Init_PWMout()nit_INT_Timer()nit_PID()   

* 

* 说明: 无 

**********************************************************************************/ 

void Init(void) 

{ 

REFDV=0x01; /* initiate PLL clock */ 

SYNR =0x02; /* system clock 24M */ 

while (!(CRGFLG & 0x08)){} /* wait untill steady */ 

CLKSEL=0x80; /* 选定所相环时钟 */ 

FCLKDIV=0x49; /* 使FLASH 和 EEPROM */ 

/* 的擦除操作工作频率在200HZ左右 */ 

ECLKDIV=0x49; 

Init_PWMout(); /* 01:50Hz 45:1kHz */ 

Init_INT_Timer(); /* initiate ETC(Enhanced Capture Clock) */ 

Init_PID(); /* initiate PID caculating process */ 

DDRK |= 0x80; /* Start Car -- stop car */ 

PORTK &= 0x7F; 

} 

/* 

*********************************************************************************** 

* SmartCar.c 

* 

* (c) Copyright 2006,Zhao Cheng 

* All Rights Reserved 

* 

* By : Zhao Cheng 

* Data : 2006_5_6 

* Note : Don't change this file if possible. 

**********************************************************************************/ 

#include <hidef.h> 

#include <mc9s12dg128.h> 

extern SYSCLOCK; /* 引用全局变量,系统时钟 */ 

void CarMain(void); 

/* 

*********************************************************************************** 

* PWM初始化函数 

* 

**********************************************************************************/ 

void Init_PWMout(void) 

{ 

PWME = 0x22; /*01:50Hz 45:1kHz */ 

PWMPOL = 0x22; 

PWMCTL = 0x50; 

PWMCLK = 0x02; 

PWMSCLA = 4; 

} 

/* 

*********************************************************************************** 

* PWM输出函数 

* 程序描述:输入参数为方向,速度 

* 方向:-45~45 

* 速度:0~24000 

**********************************************************************************/ 

void PWMout(int Direction, int Velocity) 

{ 

Direction = Direction/3 + 4500; 

if(Direction<3000) Direction=3000; 

if(Direction>6000) Direction=6000; 

PWMPER01 = 60000; /* Center 1500ms*3 */ 

PWMDTY01 = Direction+93; /* 设置舵机角度 */ 

if(Velocity>24000) Velocity=24000; 

PWMPER45 = 24000; /* 1kHz ( <10kHz ) */ 

PWMDTY45 = Velocity; /* 设置电机速度 */ 

} 

/* initiate Real Time Interrupt 1.0 */ 

void Init_INT_RTI(void) 

{ 

RTICTL = 0x74; 

CRGINT |=0x80; 

} 

/* Real Time Interrupt 1.0 */ 

interrupt void INT_RTI(void) 

{ 

CRGFLG |= 0x80; /* clear the interrrupt flag */ 

} 

/* INT_Timer0 initiate 1.0 */ 

void Init_INT_Timer(void) 

{ 

TSCR2 =0x07; /* 128Hz at 16M bus clok */ 

/* 128Hz * 2/3 at 24m bus clock */ 

/* in fact it is a little more than it. */ 

TIOS |=0x01; /* I/O select */ 

TIE |=0x01; /* Interrupt Enable */ 

TSCR1|=0x80; /* TSCR1_TEN=1 //Timer Enable */ 

} 

/* INT_Timer0 1.0 */ 

interrupt void INT_Timer0(void) 

{ 

SYSCLOCK++; 

CarMain(); 

TC0 = TCNT + 1874; /* 1875-1 :100Hz */ 

/* F = Fosc / (TC*128) */ 

TFLG1 |=0x01; /* clear interrupt flag */ 

} 

/* not finished EEPROM */ 

void EEPROM(void) 

{ 

ECLKDIV = 0x4F; 

while(!(ECLKDIV&0x80)) /* wheather */ 

{} 

while(!(ESTAT&0x80)) /* wheather the command buffer is empty */ 

{} 

while(!(EPROT&0x80)) /* wheather the eeprom is enabled to */ 

{} 

ECMD = 0x41; 

ESTAT |= 0x80; 

while(!(ESTAT&0x80)) /* wheather the command buffer is empty */ 

{} 

} 


/* 

*********************************************************************************** 

* PID.c 

* Description: This file includes some basic calculation function of PID 

* (c) Copyright 2006,Zhao Cheng 

* All Rights Reserved 

* 

* By : Zhao Cheng 

* Data : 2006_5_6 

* Note : Don't change this file if possible. 

**********************************************************************************/ 

#include <mc9s12dg128.h> /* derivative information */ 

/* 

*********************************************************************************** 

* 宏定义 

**********************************************************************************/ 

#define STABMAX 50 

#define SENSORNUM 8 

#define SAMPLETIMES 5 

/* 

*********************************************************************************** 

* FUNCTION PROTOTYPES 

**********************************************************************************/ 

int CalculateP(void); 

float CalculatePID(void); 

/********************************** PID控制程序 ********************************/ 

struct CARSTATE 

{ 

int E0; 

int E1; 

int E2; 

int E3; 

float Integral; 

}CarState; 

/* 

*********************************************************************************** 

* 初始化PID参数 

**********************************************************************************/ 

void Init_PID() 

{ 

CarState.E0 = 0; 

⌨️ 快捷键说明

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