言之者无罪,闻之者足以戒。 - “诗序”
上一篇文章说自己已经到了数据处理的阶段了,但是我自己好的好研究了一下当然也询问了别人,之后,我看了看自己第二天写的代码,发现自己数据的读取方面犯了很大的错误。下面我们看一下第二天数据存储方面的程序:
/*矩阵状态读取*/
void Read_TraceMatrix_IO()
{
u8 X=0;
u8 Read_col;
if(Lettic_Flag_row == 1)
{
Lettic_Flag_row = 0;
/*********************************第一行数据的读取**************************************/
TraceMatrix_row1 = 0;
//读取P0口的数据
X=P0;
Read_col=X&0x7f;
//判断是第几列的灯寻到黑线
if(Read_col == 0x01)
{
Row_Col_1[0]=1;
}
if(Read_col == 0x02)
{
Row_Col_1[1]=1;
}
if(Read_col == 0x04)
{
Row_Col_1[2]=1;
}
if(Read_col == 0x08)
{
Row_Col_1[3]=1;
}
if(Read_col == 0x10)
{
Row_Col_1[4]=1;
}
if(Read_col == 0x20)
{
Row_Col_1[5]=1;
}
if(Read_col == 0x40)
{
Row_Col_1[6]=1;
}
TraceMatrix_row1 = 1;
/**********************************第二行数据的读取*********************************/
TraceMatrix_row2 = 0;
X = 0;
//读取P0口的数据
X=P0;
Read_col=X&0x7f;
//判断是第几列的灯寻到黑线
if(Read_col == 0x01)
{
Row_Col_2[0]=1;
}
if(Read_col == 0x02)
{
Row_Col_2[1]=1;
}
if(Read_col == 0x04)
{
Row_Col_2[2]=1;
}
if(Read_col == 0x08)
{
Row_Col_2[3]=1;
}
if(Read_col == 0x10)
{
Row_Col_2[4]=1;
}
if(Read_col == 0x20)
{
Row_Col_2[5]=1;
}
if(Read_col == 0x40)
{
Row_Col_2[6]=1;
}
TraceMatrix_row2 = 1;
/************************************第三行数据的读取**********************************/
TraceMatrix_row3 = 0;
X = 0;
//读取P0口的数据
X=P0;
Read_col=X&0x7f;
//判断是第几列的灯寻到黑线
if(Read_col == 0x01)
{
Row_Col_3[0]=1;
}
if(Read_col == 0x02)
{
Row_Col_3[1]=1;
}
if(Read_col == 0x04)
{
Row_Col_3[2]=1;
}
if(Read_col == 0x08)
{
Row_Col_3[3]=1;
}
if(Read_col == 0x10)
{
Row_Col_3[4]=1;
}
if(Read_col == 0x20)
{
Row_Col_3[5]=1;
}
if(Read_col == 0x40)
{
Row_Col_3[6]=1;
}
TraceMatrix_row3 = 1;
/************************************第四行数据的读取**********************************/
TraceMatrix_row4 = 0;
X = 0;
//读取P0口的数据
X=P0;
Read_col=X&0x7f;
//判断是第几列的灯寻到黑线
if(Read_col == 0x01)
{
Row_Col_4[0]=1;
}
if(Read_col == 0x02)
{
Row_Col_4[1]=1;
}
if(Read_col == 0x04)
{
Row_Col_4[2]=1;
}
if(Read_col == 0x08)
{
Row_Col_4[3]=1;
}
if(Read_col == 0x10)
{
Row_Col_4[4]=1;
}
if(Read_col == 0x20)
{
Row_Col_4[5]=1;
}
if(Read_col == 0x40)
{
Row_Col_4[6]=1;
}
TraceMatrix_row4 = 1;
/************************************第五行数据的读取**********************************/
TraceMatrix_row5 = 0;
X = 0;
//读取P0口的数据
X=P0;
Read_col=X&0x7f;
//判断是第几列的灯寻到黑线
if(Read_col == 0x01)
{
Row_Col_5[0]=1;
}
if(Read_col == 0x02)
{
Row_Col_5[1]=1;
}
if(Read_col == 0x04)
{
Row_Col_5[2]=1;
}
if(Read_col == 0x08)
{
Row_Col_5[3]=1;
}
if(Read_col == 0x10)
{
Row_Col_5[4]=1;
}
if(Read_col == 0x20)
{
Row_Col_5[5]=1;
}
if(Read_col == 0x40)
{
Row_Col_5[6]=1;
}
TraceMatrix_row5 = 1;
}
}
其实错误犯的比较低级,因为矩阵是五乘七的,不可能一排总是只有一个对管扫到黑线,很有可能多个对管同时扫到黑线,所以我对自己数据的读取方面的代码做了更改,之后我在KEIL上面调试了一下,发现可以了。下面我们看一下程序:
/*矩阵状态读取*/
void Read_TraceMatrix_IO()
{
u8 Read_col = 0;
TraceMatrix_row1 = 0; //开启第一行
Read_col = P0;
Row_Col[0] = Read_col;//存储第一行的数据
TraceMatrix_row1 = 1; //关闭第一行
TraceMatrix_row2 = 0;
Read_col = P0;
Row_Col[1] = Read_col;
TraceMatrix_row2 = 1;
TraceMatrix_row3 = 0;
Read_col = P0;
Row_Col[2] = Read_col;
TraceMatrix_row3 = 1;
TraceMatrix_row4 = 0;
Read_col = P0;
Row_Col[3] = Read_col;
TraceMatrix_row4 = 1;
TraceMatrix_row5 = 0;
Read_col = P0;
Row_Col[4] = Read_col;
TraceMatrix_row5 = 1;
}
这样我们只需要一个一维的数组就可以完全搞定数据的读取存储。
其实在数据的读取存储之前,就是用定时器来定时以实现定时检测、存储,但是我觉得自己的定时器好像有些问题,我把最新的定时器代码贴出来,大家看一下有没有什么不对的地方:
/*定时器0初始化函数*/
void Timer0Init()
{
AUXR |= 0X80;//设置定时器T0工作于1T方式
TMOD &=0XF0;//定时器0的工作方式是方式0
TH0=0xFB;//定时0.1ms
TL0=0xAE;
IE = 0X82;//开中断
TR0 = 1;//启动T0
}
/*定时器0的中断函数*/
void Timer0_isr() interrupt 1 using 1
{
Read_time++;
if(Read_time == 5)
{
Read_time = 0;
Read_TraceMatrix_IO();
Data_Flag = 1;
Data_Handling();
}
}
最后才是数据的处理和小车的控制,我个人认为数据的处理还是比较难的一方面,我问了朋友之后,决定采用他的处理方式,数据的处理就放在下一篇文章来说吧!
其实最近的这些天也一直在调,可能我自己比较笨,到现在还是没有调好。我总是觉得自己的定时器或者是PCA配置的有问题,但是我又是按照手册配置的,我自己也不知道是怎么回事,就是小车不听话,我继续努力吧。