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

📄 1.txt

📁 第一届飞思卡尔杯华中科技大学程序1
💻 TXT
字号:
/* 

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

* 工程名称:SmartCar 

* 功能描述:结合飞思卡尔16位单片机MC9S12DG128B完成小车自动寻迹,沿黑线行驶功能 

* IDE环境: Metrowerks CodeWarrior 4.1 

* 组成文件: 

* main.c 

* SmartCar.c/PID.c/LCD1620.c/Test.c 

* 说明: 本版本为智能小车程序早期版本,还有待更进一步完善 

* 日期:2006-5-6 

* (c) Copyright 2006,Zhao Cheng 

* All Rights Reserved 

* 

* 

* By : Zhao Cheng 

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

/* 

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

* 

* (c) Copyright 2006,Zhao Cheng 

* All Rights Reserved 

* 

* By : Zhao Cheng 

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

#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<, SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">]; 

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); 

} 

/* 


⌨️ 快捷键说明

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