智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 1775|回复: 4
打印 上一主题 下一主题

摄像头程序

[复制链接]

1

主题

1

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
202
威望
146
贡献
42
兑换币
2
注册时间
2012-3-29
在线时间
7 小时
毕业学校
河南大学
跳转到指定楼层
1#
发表于 2012-3-29 18:30:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Main.c
#include <hidef.h> /* common defines and macros */
#include <mc9s12db128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12db128b"
#include "define.h"
#include "init.h"
// variable used in video process
volatile unsigned char image_data[ROW_MAX][LINE_MAX] ; // data array of picture
unsigned char black_x[ROW_MAX] ; // 0ne-dimensional array
unsigned char row ; // x-position of the array
unsigned char line ; // y-position of the array
unsigned int row_count ; // row counter
unsigned char line_sample ; // used to counter in AD
unsigned char row_image ;
unsigned char line_temp ; // temperary variable used in data transfer
unsigned char sample_data[LINE_MAX] ; // used to save one-dimension array got in
interruption
// variables below are used in speed measure
Unsigned char pulse[5] ; // used to save data in PA process
Unsigned char counter; // temporary counter in Speed detect
Unsigned char cur_speed; // current speed
short stand;
short data;
unsigned char curve ; // valve used to decide straight or turn
short Bounds(short data);
short FuzzyLogic(short stand);
/*----------------------------------------------------------------------------*\
receive_sci
\*----------------------------------------------------------------------------*/
unsigned char receive_sci(void) // receive data through sci
{ unsigned char sci_data;
while(SCI0SR1_RDRF!=1);
sci_data=SCI0DRL;
return sci_data;
}
/*----------------------------------------------------------------------------*\
transmit_sci
\*----------------------------------------------------------------------------*/
void transmit_sci(unsigned char transmit_data) // send data through sci
{
while(SCI0SR1_TC!=1);
while(SCI0SR1_TDRE!=1);
SCI0DRL=transmit_data;
}
/*****************************************************************************
***/
/*----------------------------------------------------------------------------*\
abs_sub
\*----------------------------------------------------------------------------*/
unsigned char abs_sub(unsigned char num1, unsigned char num2)
{ unsigned char difference;
if(num1>=num2){
difference=num1-num2;
}else{
difference=num2-num1;
}
return difference;
}
void pwm_set(unsigned int dutycycle)
{
PWMDTY1=dutycycle&0x00FF;
PWMDTY0=dutycycle>>8;
}
void get_black_wire(void) // used to extract black wire
{ unsigned char i;
for(row=0;row<ROW_MAX;row++){
for(line=LINE_MIN;line<LINE_MAX-3;line++){
if(image_data[row][line]>image_data[row][line+3]+VALVE){
for(i=3;i<10;i++){
if(image_data[row][line+i]+VALVE<image_data[row][line+i+3]){
black_x[row]=line+i/2+2;
i=10;
}
}
line=LINE_MAX;
} else{
//black_x[row]=(black_x[row]/45)*78;
}
}
}
}
/*----------------------------------------------------------------------------*\
speed_control
\*----------------------------------------------------------------------------*/
void speed_control(void)
{
unsigned int sum,average;
sum=0;
for(row=0;row<FIRST_FIVE;row++){
sum=sum+black_x[row];
}
average=sum/FIRST_FIVE;
curve=0;
for(row=0;row<FIRST_FIVE;row++)
{
curve=curve+abs_sub(black_x[row],average);
if(curve>CURVE_MAX){
curve_flag=0;
speed=low_speed;}
else{
curve_flag=1;
speed=hign_speed;
}
}
}
/*----------------------------------------------------------------------------*\
steer_control
\*----------------------------------------------------------------------------*/
void steer_control(void)
{ unsigned int dutycycle;
unsigned char video_center;
unsigned int coefficient;
int E,U; //current
static int e=0;
video_center=(LINE_MIN+LINE_MAX)/2;
stand=abs_sub(black_x[1]+ black_x[9],2*black_x[5]);
E=video_center-black_x[8];
coefficient=30+1*FuzzyLogic(stand);
U=coefficient*E;
dutycycle=Bounds(center+U);
pwm_set(dutycycle);
}
// make sure it is within bounds
short Bounds(short data){
if(data>right_limit){
data = right_limit;
}
if(data<left_limit){
data = left_limit;
}
return data;
}
Void speed_get(void)
{
Unsigned char temp;
Counter++;
Temp=PACN1;
cur_speed=temp-pulse[counter-1];
pulse[counter-1]=temp;
if(counter==5)
{
counter=0;
}
}
Void set_speed(unsigned char desired_speed)
{
If(desired_speed<cur_speed)
{
PWMDTY2=low_speed;
}
Else
{
PWMDTY2=high_speed;
}
}
/*****************************************************************************
*\
Main
\*****************************************************************************
*/
void main(void) {
// main functiion
init_PORT() ;
// port initialization
init_PLL() ;
// setting of the PLL
init_ECT();
init_PWM() ;
// PWM INITIALIZATION
init_SPEED() ;
init_SCI() ;
for(;;) {
if(field_signal==0){ // even->odd
while(field_signal==0);
row_count=0;
row_image=0;
EnableInterrupts;
while(row_count<ROW_END){
get_black_wire();
speed_control();
steer_control();
}
DisableInterrupts;

38

主题

1651

帖子

0

精华

功勋会员

Rank: 10Rank: 10Rank: 10

积分
8770
威望
4444
贡献
2638
兑换币
1818
注册时间
2010-12-29
在线时间
844 小时
2#
发表于 2012-3-29 18:48:07 | 只看该作者
这样粘贴出来的代码,没有人会看的!
回复 支持 反对

使用道具 举报

13

主题

225

帖子

0

精华

杰出人士

Rank: 12Rank: 12Rank: 12

积分
12487

优秀会员奖章活跃会员奖章

威望
10220
贡献
2111
兑换币
24
注册时间
2012-3-6
在线时间
78 小时
3#
发表于 2012-3-31 17:52:25 | 只看该作者
跟上楼一样

回复 支持 反对

使用道具 举报

3

主题

36

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
464
威望
318
贡献
106
兑换币
0
注册时间
2011-9-18
在线时间
20 小时
4#
发表于 2012-3-31 18:44:14 | 只看该作者
怎么没有注释呢?
回复 支持 反对

使用道具 举报

7

主题

73

帖子

0

精华

高级会员

Rank: 4

积分
627
威望
355
贡献
168
兑换币
130
注册时间
2012-10-21
在线时间
52 小时
5#
发表于 2012-11-4 21:18:13 | 只看该作者
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-9-21 08:09 , Processed in 0.152673 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表