systeminitial.h
来自「基于飞思卡尔智能车比赛」· C头文件 代码 · 共 1,560 行 · 第 1/3 页
H
1,560 行
//3.7 ,
4.7 ,
5.4 ,
6.4 ,
7.2 ,
8.2 ,
8.8 ,
9.6 ,
10.6 ,
12.6 ,
15.1 ,
18.5 ,
23.5 ,
32.6 ,
-228.5 ,
-211.9 ,
-200 ,
-184.2 ,
-168.3 ,
-152.4 ,
-137.1428571 ,
-120 ,
-102.8571429 ,
-85.71428571 ,
-68.57142857 ,
-51.42857143 ,
-34.28571429 ,
-17.14285714 ,
0 ,
17.14285714 ,
34.28571429 ,
51.42857143 ,
68.57142857 ,
85.71428571 ,
102.8571429 ,
120 ,
137.1428571 ,
152.4 ,
168.3 ,
184.2 ,
200 ,
211.9 ,
228.5 ,
-235.5 ,
-220 ,
-199.4 ,
-176.6 ,
-157.1428571 ,
-141.4285714 ,
-125.7142857 ,
-111.5 ,
-97.6 ,
-85.9 ,
-71.3 ,
-56.8 ,
-41.6 ,
-21.5 ,
0 ,
21.5 ,
41.6 ,
56.8 ,
71.3 ,
85.9 ,
97.6 ,
111.5 ,
125.7142857 ,
141.4285714 ,
157.1428571 ,
176.6 ,
199.4 ,
220 ,
235.5 ,
*/
-254.3 ,
-246.8 ,
-239.3 ,
-216.9 ,
-188.6 ,
-160 ,
-141.3 ,
-124.7 ,
-113 ,
-103 ,
-92.2 ,
-81.4 ,
-63.2 ,
-40.7 ,
0 ,
0 ,
40.7 ,
63.2 ,
81.4 ,
92.2 ,
103 ,
113 ,
124.7 ,
141.3 ,
160 ,
188.6 ,
216.9 ,
239.3 ,
246.8 ,
254.3 ,
};
int ave[21]=
{
21 ,
20 ,
20 ,
20 ,
20 ,
20 ,
18 ,
17 ,
16 ,
14 ,
13 ,
10 ,
9 ,
9 ,
9 ,
8 ,
7 ,
6 ,
5 ,
4 ,
3 ,
/*21 ,
20 ,
20 ,
20 ,
20 ,
20 ,
19 ,
18 ,
17 ,
15 ,
13 ,
10 ,
9 ,
9 ,
9 ,
7 ,
5 ,
6 ,
5 ,
4 ,
3 ,
21 ,
20 ,
20 ,
20 ,
20 ,
20 ,
19 ,
18 ,
17 ,
15 ,
12 ,
9 ,
7 ,
7 ,
7 ,
6 ,
6 ,
6 ,
5 ,
4 ,
3 ,
*/
};
//float index_to_angle[21]={-(10.0f+16.0f),-(7.5f+16.0f),-(5.0f+16.0f),-(2.5f+16.0f),-(13.2f+2.8f),-(9.9f+2.8f),-(6.6f+2.8f),-(3.3f+2.8f),-2.8f,-1.4f,0,1.4f,2.8f,(3.3f+2.8f),(6.6f+2.8f),(9.9f+2.8f),(13.2f+2.8f),(2.5f+16.0f),(5.0f+16.0f),(7.5f+16.0f),(10.0f+16.0f)};
//float index_to_angle[21]={-(10.0f+16.0f),-(7.5f+16.0f),-(7.0f+16.0f),-(4.5f+16.0f),-(7.2f+1.6f),-(3.5f+1.6f),-(2.0f+1.6f),-(0+1.6f),-0.6f,-0.5f,0,0.5f,0.6f,(0+1.6f),(2.0f+1.6f),(3.5f+1.6f),(7.2f+1.6f),(4.5f+16.0f),(7.0f+16.0f),(7.5f+16.0f),(10.0f+16.0f)};
//float index_to_angle[21]={-(10.0f+16.0f),-(7.5f+16.0f),-(5.0f+16.0f),-(2.5f+16.0f),-(8.2f+2.8f),-(6.9f+2.8f),-(4.6f+2.8f),-(2.3f+2.8f),-2.8f,-1.4f,
// 0,1.4f,2.8f,(2.3f+2.8f),(4.6f+2.8f),(6.9f+2.8f),(8.2f+2.8f),(2.5f+16.0f),(5.0f+16.0f),(7.5f+16.0f),(10.0f+16.0f)};
float PID_P=50.1;
float PID_I=160.9;//100;
float PID_D=0.2;
uchar speed_v_init = 50; //应设为直线值 //105:308cm/s //0.341=256*12/15.708/114.7516/5; //41=120*0.341 //68=200*0.341
int speed_v=0;
uint mcnt=0;
uchar c=0;
uchar infrared_value7=0; //声明临时变量,存储红外采集的速度数据
/**********其它***********/
char index_i=0;
int line_i=0,line_ii=0,row_i=0,row_ii=0;
int last_line=0;
int line_i_10=0;
long m=0;
//long j=0;
//int z=0,z1=0;
uint mnz=0;
uchar ad_video_min[3]=255;
uchar last_i=1;
int front_i=0;
int front_flag=0;
int front_sum=0;
int k_angle=0,k_angle_last=0;
float k=0;
uchar direct_flag=0;
int ave_i=0;
int k_counter=0;
uchar begin_line_near=0;
uchar xxx=0;
int counter=0;
int counter_1=0;
int ave_temp;
/***************函数定义****************/
void SYSTEM_Init(void) //系统初始化
{
SYSCLK_Init(); //系统时钟初始化
PORTB_Init(); //端口B初始化
InitPWM(); //PWM初始化
SCI_Init(BUS_CLK,BR); //串口初始化
IRQ_Init(); //外部中断请求
PORTH_Init(); //端口H初始化
ATD0_Init(); //ATD0初始化
ATD1_Init(); //ATD1初始化
flag_ccd_far_enable=1; //远点可见
// PIEH_PIEH0=1; //允许h0口中断
}
void SYSCLK_Init(void) //系统时钟初始化
{
CLKSEL=0x00; //时钟选择寄存器 关闭锁相环
CLKSEL=0;
CLKSEL_PLLSEL = 0;
PLLCTL_PLLON = 0;
SYNR=SYNR_VALUE; //PLL=2*16*(SYNR+1)/(REFDV+1)
REFDV=REFDV_VALUE; //BUS_CLK=24M=16*(SYNR+1)/(REFDV+1);
PLLCTL=192;//0xF1; //PLL控制寄存器
PLLCTL_PLLON = 1;
while(!CRGFLG_LOCK); //等待锁相环时钟达到预期值
//CLKSEL=0x80; //开启锁相环
CLKSEL_PLLSEL = 1;
}
void PORTB_Init(void) //端口B初始化
{
DDRB=0xFF;
PUCR_PUPBE=1;
PORTB=~0x00;
}
void InitPWM() //PWM初始化
{
//bus clock = 16M 注:pwm的文档要看DT128系列的,DP56的和DG128的pwm不一样(PWM period的计算)
//1通道和0通道联用组成16位模式,0通道为高8位,1通道为低8位,pwm输出口为PTP1
DDRA=0x01; //A端口数据方向寄存器第0位为1,表示该位设置为输出
PORTA=0x03; //A端口数据第0位为1,表示该位输出高电平
PWMPOL = 0xFF; //极性选择,开始电平为高
PWMCLK = 0x03; //时钟源选择,选SA
PWMPRCLK = 0x00; //预分频 64,使PWMPER周期寄存器的值大些满足分辨率为0.01ms的要求
PWMSCLA=8;
//PWMSCLB=5;
PWMCAE = 0x00; //左对齐
PWMCTL = 0x70; //16位寄存器
PWMPER01 = 20000;//0x1D4C; //这里pwm_clock就是A,pwm period = pwm clock*(pwmper0&pwmper1) = 64/24M * 7500 =20ms
PWMPER23 = 3200; //控制电机运转
PWMPER45 = 3200;//3200;
//PWMDTY01 = 0x177; //DUTY REGSITERS/7500 = 0x177/7500*20 = 1 ms (右转极限)
PWMDTY01 = 3000;//0x200;//0x232; //DUTY REGSITERS/7500 = 0x232/7500*20 = 1.5ms (转至0度角)
//PWMDTY01 = 0x2EE; //DUTY REGSITERS/7500 = 0x2EE/7500*20 = 2 ms (左转极限)
PWMDTY23 = 0;//0x900;
PWMDTY45 = 0;
PWMCNT01 = 0xAAAA; //CLEAR PWMCNT01
PWMCNT23 = 0xAAAA; //CLEAR PWMCN235
PWMCNT45 = 0xAAAA; //CLEAR PWMCNT45
PWME = 0x3f; //PWM ENABLE
//PWMSDN = 0x13; //关闭允许,激活电平为高电平,强制输出为高电平,允许复位
PWMSDN = 0x00;
}
void PORTH_Init(void) //端口H初始化
{
DDRH_DDRH0=0; //PORTH输入,置一为出
PERH=PERH_PERH0_MASK; //作为输入,使能上拉或下拉
PPSH=PPSH_PPSH0_MASK; //设置为上升沿触发
}
void IRQ_Init(void) //外部中断请求
{
INTCR_IRQE = 1; //开行同步监测 falling level; write once, read anytime
INTCR_IRQEN = 0; //因默认已连接上中断,故先关掉以免干扰
}
void ATD0_Init(void) //ATD0初始化
{
ATD0CTL2=(ATD0CTL2_ADPU_MASK|ATD0CTL2_AFFC_MASK); //ADPU是AD的电源开关
// ADC上电,快速清零, 无等待模式, 禁止外部触发, 禁止中断
ATD0CTL3=(ATD0CTL3_S1C_MASK|ATD0CTL3_S1C_MASK);
// 每个序列5次转换, No FIFO, Freeze模式下继续转换
ATD0CTL4=(ATD0CTL4_SRES8_MASK);
// 8位精度, 2个时钟, ATDClock=[BusClock*0.5]/[PRS+1] ; PRS=1, divider=4
// AD时钟6M
// ATD0CTL5 = (ATD0CTL5_DJM_MASK|ATD0CTL5_SCAN_MASK);//ATD0CTL5要在每次AD开启后初始化
//启动AD转换,连续转换模式,右对齐,
ATD0DIEN=0x00; // 禁止数字输入
}
void ATD1_Init(void) //ATD1初始化
{
ATD1CTL2 = ATD1CTL2_ADPU_MASK|ATD1CTL2_ASCIE_MASK|ATD1CTL2_AFFC_MASK; //ADPU是AD的电源开关 |ATD1CTL2_AFFC_MASK
//ATD1CTL2 = ATD1CTL2_ADPU_MASK|ATD1CTL2_ASCIE_MASK; //ADPU是AD的电源开关
//ADC上电,快速清零,无等待模式,禁止外部触发,禁止中断
//快速清零就是读了就清零
ATD1CTL3 = ATD1CTL3_S1C_MASK|ATD1CTL3_S1C_MASK;
// 每个序列6次转换,No FIFO, Freeze模式下继续转换
//NO FIFO的意思就是当有数据未读而来新的数据时不覆盖此未读的数据,而是等待标志位的清除
ATD1CTL4 = (ATD1CTL4_SRES8_MASK);
//8位精度, 2个时钟, ATDClock=[BusClock*0.5]/[PRS+1]; PRS=1, divider=4
//AD时钟6M
//启动AD转换,连续转换模式,右对齐
ATD1DIEN = 0x00; // 禁止数字输入
}
void MDC_u(uint time) //定时time(us)函数,定时结束后进入MDC_ISR()中断函数
{
MCCTL=(MCCTL_MCZI_MASK|MCCTL_MCEN_MASK); //开定时器,中断使能
MCCNT=time*BUS_CLK; //write to MCCNT会清中断标志
}
void PID(void) //增量式PID正常处理部分
{
long int pwmtemp;
long int pwmtemp1=0;
static int error=0; //现在偏差
static int last_error=0; //上次偏差
static int pre_error=0; //再上一次偏差
error=speed_v-infrared_value7;
//PID_I=abs(error)*5;
pwmtemp=PWMDTY45+PID_P*(error-last_error)+PID_I*(error)+PID_D*(error+pre_error-2*last_error);
if(pwmtemp<0)
{
pwmtemp = 0;
}
else if(pwmtemp>3200)
{
pwmtemp = 3200;
}
if((index_cmos_last>=index_max-9 || index_cmos_last<=index_min+9))
pwmtemp1=(3200-pwmtemp);
//else if((index_cmos_last>21 || index_cmos_last<11))
//pwmtemp1=(3200-pwmtemp)/2;
else
pwmtemp1=0;/**/
PWMDTY45=pwmtemp;
PWMDTY23=pwmtemp1;
pre_error=last_error;
last_error=error;
}
void print_ad_video(void) //输出摄像头整场数据
{
int line,row;
print_string("\r\n\n\r\n\r\n");
for(line=0;line<LINE_num;line++) //可隔行显示
{ if(line%10==0)
{
print_string("\r\nline");
print_uint(line);
print_string(":");
for(row=0;row<ROW_num;row++)
print_uchar(ad_video[line/10][row]);
}
}
/*for(line=0;line<41;line++) //可隔行显示
{
print_string("\r\nline");
print_uint(line);
print_string(":");
for(row=0;row<ROW_num;row++)
print_uchar(ad_video[LINE_num/10+line][row]);
} */
}
/********************中断服务程序*********************/
interrupt void PORTH_ISR(void) //摄像头的场中断,是摄像头每场开始的标志
{
static uchar H_interrupt_num=0;
DisableInterrupts; //???cpu在响应中断时置I为1,即禁止其它中断,所以在此无需手动清I
c++;
#ifdef sci_cmos_select
H_interrupt_num++;
if(H_interrupt_num>=10)
{
H_interrupt_num=0;
once1=1;
PIEH_PIEH0=0;
ATD0CTL2&=~ATD0CTL2_ASCIE_MASK;
INTCR_IRQEN=0;
}
else
#endif
{
PIFH|=PIFH_PIFH0_MASK; //写1清porth0中断标志位
INTCR_IRQEN=1;
line_counter=0;
counter++;
}
EnableInterrupts;
}
//行场消隐期后,关闭行同步检测信号,其开启完全由奇场上升沿控制
void interrupt IRQ_ISR(void) //每场中的行中断,是每行开始的标志
{
DisableInterrupts;
line_counter++;
m++;
if(line_counter>LINE_num+LINE_invalid_num)
{
INTCR_IRQEN=0;
ATD0CTL2&=~ATD0CTL2_ASCIE_MASK; // 关AD中断
for(row_i=0;row_i<ROW_num;row_i++)
{
for(line_i=1;line_i<=9;line_i++)
{
if(ad_video[LINE_num/10+line_i][row_i]<31)
ad_video[0][row_i]=10;
}
}
for(row_i=0;row_i<ROW_num;row_i++)
{
for(line_i=11;line_i<=19;line_i++)
{
if(ad_video[LINE_num/10+line_i][row_i]<31)
ad_video[1][row_i]=10;
}
}
for(row_i=0;row_i<ROW_num;row_i++)
{
for(line_i=21;line_i<=29;line_i++)
{
if(ad_video[LINE_num/10+line_i][row_i]<=31)
ad_video[2][row_i]=10;
}
}
for(row_i=0;row_i<ROW_num;row_i++)
{
for(line_i=31;line_i<=39;line_i++)
{
if(ad_video[LINE_num/10+line_i][row_i]<=31)
ad_video[3][row_i]=10;
}
}
for(row_i=0;row_i<ROW_num;row_i++)
{
for(line_i=41;line_i<=49;line_i++)
{
if(ad_video[LINE_num/10+line_i][row_i]<=31)
ad_video[4][row_i]=10;
}
}
for(row_i=0;row_i<ROW_num;row_i++)
{
for(line_i=51;line_i<=59;line_i++)
{
if(ad_video[LINE_num/10+line_i][row_i]<=31)
ad_video[5][row_i]=10;
}
}
flag_cmos_visible=0;
right_line_cmos=0;
for(line_i=LINE_num-1;line_i>=LINE_num-160-1;line_i--)
{
if(line_i%10==0)
{
line_i_10=line_i/10;
begin_flag=0;
black_num_cmos[line_i_10]=0;
row_i_begin=0;row_i_end=ROW_num-1;
if(index_cmos_last<=index_center)
{
for(row_i=row_i_begin;row_i<row_i_end;row_i++)
{
if(ad_video[line_i_10][row_i]<31)
{
#ifdef sci_cmos_select
if(begin_flag==1)
ad_video[line_i_10][row_i]=0;
#endif
if((ad_video[line_i_10][row_i-1]>31 || row_i==row_i_begin) && begin_flag==0)
{
#ifdef sci_cmos_select
ad_video[line_i_10][row_i]=0;
#endif
index_cmos_begin_temp=row_i;
begin_flag=1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?