把自己大三写的代码翻出来了,贴出来,看看有多少人做过同样的东西。

bfdeh 2013-03-09 09:35:44
/** ###################################################################
** Filename : smartcar.C
** Project : smartcar
** Processor : MC9S12DG128BCPV
** Version : Driver 01.11
** Compiler : Metrowerks HC12 C Compiler
** Date/Time : 2008-4-7, 22:16
** Abstract :
** Main module.
** Here is to be placed user's code.
** Settings :
** Contents :
** No public methods
**
** (c) Copyright UNIS, spol. s r.o. 1997-2005
** UNIS, spol. s r.o.
** Jundrovska 33
** 624 00 Brno
** Czech Republic
** http : www.processorexpert.com
** mail : info@processorexpert.com
** ###################################################################*/
/* MODULE smartcar */

/* Including used modules for compiling procedure */
#include "Cpu.h"
#include "Events.h"
#include "rudder.h"
#include "speed_timer.h"
#include "speed_cnt.h"
#include "engine.h"
/* Include shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
/*
可能需要临时调整的参数如下:
1、二值化时的阈值,暂定为一行中 最大值*0.3+最小值*0.7-25
2、弯道速度egde_cnt及PWM
*/
#define line_width 55 //单行点数
byte picture_line0[line_width]; //单行数组
byte picture_line1[line_width]; //单行数组
byte picture_line2[line_width]; //单行数组
byte picture_line3[line_width]; //单行数组
byte picture_line4[line_width]; //单行数组
int location[10]={0,0,0,0,0,0,0,0,0,0}; //记录连续多次黑线位置
int edge_cnt=0; //脉冲边沿个数,测速用
byte line=50;
/**********************************************************
函数名:delay(int cnt),delay1(int cnt)
功能描述:延时函数
**********************************************************/
void delay1(unsigned int cnt) {
unsigned int i;
for(i=0;i<cnt;i++) {;}
}
/**********************************************************
函数名:AD_init();
功能描述:初始化AD
**********************************************************/
void AD_init(void)
{
ATD0CTL2=0b11000000; //开启转换,快速清除,等待模式继续转换,禁止外部中断,禁止转换结束中断
ATD0CTL3=0b00001000; //转换序列长度为1
ATD0CTL4=0b10000001; //八位精度,4M时钟
ATD0CTL5=0b10100000; //单通道,连续转换,0通道,右对齐
ATD0DIEN=0b00000000;
}
/**********************************************************
函数名:get_fix_line(byte my_line);
功能描述:指定采集第my_line行开始的五行
**********************************************************/
void get_fix_line(byte my_line){
int j=0,i;
while(PORTA_BIT6);
while(!PORTA_BIT6);//等待场信号
/**************************第一行**************************/
for(i=0;i<my_line;i++){
while(PORTA_BIT7);
while(!PORTA_BIT7);//等待行信号
}
delay1(20);
for(j=line_width-1;j>=0;j--){
while(!ATD0STAT1_CCF0);
picture_line0[j]=ATD0DR0L;
}
/***************************第二行**************************/
for(i=0;i<5;i++){
while(PORTA_BIT7);
while(!PORTA_BIT7);//等待行信号
}
delay1(20);
for(j=line_width-1;j>=0;j--){
while(!ATD0STAT1_CCF0);
picture_line1[j]=ATD0DR0L;
}
/***************************第三行**************************/
for(i=0;i<5;i++){
while(PORTA_BIT7);
while(!PORTA_BIT7);//等待行信号
}
delay1(20);
for(j=line_width-1;j>=0;j--){
while(!ATD0STAT1_CCF0);
picture_line2[j]=ATD0DR0L;
}
/***************************第四行**************************/
for(i=0;i<5;i++){
while(PORTA_BIT7);
while(!PORTA_BIT7);//等待行信号
}
delay1(20);
for(j=line_width-1;j>=0;j--){
while(!ATD0STAT1_CCF0);
picture_line3[j]=ATD0DR0L;
}
/***************************第五行**************************/
for(i=0;i<5;i++){
while(PORTA_BIT7);
while(!PORTA_BIT7);//等待行信号
}
delay1(20);
for(j=line_width-1;j>=0;j--){
while(!ATD0STAT1_CCF0);
picture_line4[j]=ATD0DR0L;
}
}
/**********************************************************
函数名:bin_line(byte picture_line[line_width]);
功能描述:二值化一行,以一行中 最大值*0.3+最小值*0.7-25 为阀值
**********************************************************/
void bin_line(byte picture_line[line_width]){
int i,valve,max,min;

max=min=picture_line[0];
for(i=1;i<line_width;i++){ //获取最大值与最小值
if(picture_line[i]<min)
min=picture_line[i];
else if(picture_line[i]>max)
max=picture_line[i];
}
valve=min+(max-min)*3/10-15; //计算阀值
for(i=0;i<line_width;i++){ //二值化
if(picture_line[i]<=valve)
picture_line[i]=0;
else
picture_line[i]=0xff;
}

}
/***********************************************************
函数名:int subscript(byte picture_line[line_width]);
功能描述:返回黑线位置下标,没有检测到黑线时返回0
**********************************************************/
int subscript(byte picture_line[line_width]) {
int i,z_cnt=0;
for(i=0;i<line_width;i++)
if(picture_line[i]==0)
{
z_cnt+=1;
if(z_cnt==3)
return i;
}
else
z_cnt=0;
return 0;
}
/***********************************************************
函数名:void all_subscript();
功能描述:返回黑线位置下标,没有检测到黑线时返回0
**********************************************************/
void all_subscript() {
int i,j,temp;
for(i=0;i<3;i++)
for(j=i+1;j<5;j++)
if(location[i]<location[j]){
temp=location[i];
location[i]=location[j];
location[j]=temp;
}
}
/**********************************************************
函数名SCI0_Init(void);
功能描述:串口初始化,波特率:9600
**********************************************************/
void SCI0_Init(void)
{
setReg8(SCI0CR1, 0);/* SCI0CR1: LOOPS=0,SCISWAI=0,RSRC=0,M=0,WAKE=0,ILT=0,PE=0,PT=0 */
setReg8(SCI0SR2, 0);/* SCI0SR2: ??=0,??=0,??=0,??=0,??=0,BRK13=0,TXDIR=0,RAF=0 */
(void) SCI0SR1;/* Reset interrupt request flags */
SCI0CR2 = 0;/*SCI0CR2: SCTIE=0,TCIE=0,RIE=0,ILIE=0,TE=0,RE=0,RWU=0,SBK=0 Disable error interrupts */
SCI0BD = 156;/* Set prescaler bits */
SCI0CR2 |= (SCI0CR2_TE_MASK | SCI0CR2_RE_MASK); /* Enable transmitter, Enable receiver */
}
/***********************************************************
函数名ASCI0_Send(char text);
功能描述:串口发送,text为ASCII码或字符
**********************************************************/
void ASCI0_Send(char text) {
while(SCI0SR1_TC!=1);
while(SCI0SR1_TDRE!=1);
SCI0DRL=text;
}
/**********************************************************
函数名:send_line();
功能描述:发送一行
**********************************************************/
void send_line(){
int j;
for(j=0;j<line_width;j++)
ASCI0_Send(picture_line0[j]);
}
/**********************************************************
函数名:void main();
功能描述:主函数
**********************************************************/
void main(void)
{
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
PE_low_level_init();
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */


SCI0_Init();
DDRA=0X21;
PUCR=0B00010011; //初始化A口上拉输入,最低位为输出
DDRB=0XFF; //B口输出
PORTB=0Xf0;
//PWME_PWME3 =0; //禁止电机PWM
while(PORTA_BIT1){ //等待输入
PORTA_BIT0=1;
}
PORTB=0Xff;
AD_init(); //初始化并启动AD
speed_cnt_Enable();
speed_timer_Enable();
for(;;){
get_fix_line(line);
bin_line(picture_line0);
bin_line(picture_line1);
bin_line(picture_line2);
bin_line(picture_line3);
bin_line(picture_line4);
location[0]=subscript(picture_line0);
location[1]=subscript(picture_line1);
location[2]=subscript(picture_line2);
location[3]=subscript(picture_line3);
location[4]=subscript(picture_line4);
all_subscript();
if(location[2]!=0)
PWMDTY01=location[2]*19+4000;
if(location[2]<=20||location[2]>=36) //弯道
{
if(edge_cnt>15)//速度较大,刹车
{
PORTB=0X7f; //弯道刹车,另一个灯
PORTA_BIT0=1;
}
else
PORTA_BIT0=0;
PWMDTY23=12000;
line=25;
}
else
{
PWMDTY23=11000;
PORTB=0xff;
line=25;
if(edge_cnt>25){
PORTB=0X00; //直道高速,刹车全亮
PORTA_BIT0=1;
}
else
PORTA_BIT0=0;
}
}
/******************PWM_end**********************/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;){}
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END smartcar */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 2.96 [03.76]
** for the Freescale HCS12 series of microcontrollers.
**
** ###################################################################
*/
...全文
287 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
bfdeh 2013-03-18
  • 打赏
  • 举报
回复
引用 11 楼 TianXuanXingHe_ 的回复:
大二的时候,软工的课程设计,写了个虚拟光驱。
星星眼 2013-03-18
  • 打赏
  • 举报
回复
大二的时候,软工的课程设计,写了个虚拟光驱。
wendian1989 2013-03-17
  • 打赏
  • 举报
回复
wendian1989 2013-03-17
  • 打赏
  • 举报
回复
hutooo 2013-03-17
  • 打赏
  • 举报
回复
膜拜学长
程序员一灯 2013-03-17
  • 打赏
  • 举报
回复
bfdeh 2013-03-10
  • 打赏
  • 举报
回复
引用 2 楼 zfen12x 的回复:
比我现在写的都好。
引用 4 楼 LaoMuTou 的回复:
写的还不错 ..
bfdeh 2013-03-10
  • 打赏
  • 举报
回复
引用 3 楼 Y_mumu 的回复:
大二,还没写过这个,写的都是图书,学生信息管理系统什么的
我大二的时候也写你这些东西,主要是考试系统。
LaoMuTou 2013-03-10
  • 打赏
  • 举报
回复
写的还不错 ..
Y_mumu 2013-03-10
  • 打赏
  • 举报
回复
大二,还没写过这个,写的都是图书,学生信息管理系统什么的
zfen12x 2013-03-09
  • 打赏
  • 举报
回复
比我现在写的都好。
gogogo 2013-03-09
  • 打赏
  • 举报
回复

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧