{
DisableInterrupts//禁止总中断
CLKSEL &= 0x7f //CLKSEL的第7位置0,选择系统时钟源为OSCCLK
PLLCTL &= 0xbf //禁止PLL , PLLCTL.6(pllon)设为0先关闭PLL
SYNR = 0x01 //根据需要的时钟频率设置SYNR和REFDV寄存器
REFDV = 0x00
//PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
PLLCTL |= (1<<6) //打开PLL , PLLCTL.6(pllon)设为1开PLL
while (( CRGFLG&0x08) == 0x00)//通过判断LOCK位,
//确定PLL是否稳定
CLKSEL |= (1<<7) //时钟频率稳定后迟老神,允许锁相环时钟源作为系统时钟源;
//本句 执行后:BusClock=PLLCLK/2 BusClock = 32MHz
}
自己写的,调试过没问题,具体主线你含晌想用多码亏少根据注释自己改
我用了2年多的MC9S12,比较胡誉熟悉,建议早正你看孙同景编的《FREESCALE S12十六位单片机原理及嵌入式开发技术》,直接从后面的IO接口看起,多练习裤睁段例程 很快就会熟练的流水灯的程序何其简单
我这只有舵机,摄像头,和主程序,但是只是框架,要等调试。可以参考哈#ifndef _WATCH_H_ //头文件保护
#define _WATCH_H_
#ifndef ULONG
#define ULONG unsigned long
#define UCHAR unsigned char
#define UINT unsigned int
#endif
#define ROAD_MAX 10
////////////////代设值
#define MP0 //捕捉象素引脚
#define HREF //捕捉行中断引颤春轮脚
//////////////////
static unsigned char Colors[10][48]//象素数组
static unsigned char JudgeMax //最大象素
static unsigned char JudgeMin //最小象素
unsigned char Over //中断函数处理完成
//使用数森御组后要清零
struct ROADS
{
unsigned char LeftRight //1 left, 0 right 2 表示没检测到黑线 4表示起始线
unsigned int road //转向率,0表示直线或则没检测到黑线
}Roads[10] //没检测到黑线一般为前方有大于90度的弯,
//行驶一会后数组会更新,不会因此丢掉跑道
unsigned long RoadLength; //mm单位
void GetRoads() //取象素
void ProcessRoads() //取转向率
void delay(UINT) //延时函数
void ColorToRoads(UINT) //2值化与赋值
#endif //_WATCH_H_
以下为实现文件:
//watch.c
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h>/* derivative information */
#include "watch.h"
#pragma CODE_SEG NON_BANKED
#pragma TRAP_PROC
void GetRoads() //场中断服务函数
{
UINT i
UINT Href= 0
delay(5)
while(HREF)
{
if(Href>= 270)//点读取完毕,退出循环
break
if((270-Href)%21!= 0 || Href<60) //取60-270行中平均间隔的行数,共30行
{
Href++
while(HREF) //等待此行扫描结束
while(!HREF) //等待新行开始扫描
continue
}
delay(...)//根据调式结果具体设置
for(i= 0i<48i++)
{
Colors[((270-Href)%21)][i]= MP0 //读取引脚数据,后来会根据串口还是并口读取做相应修改
if(MP0>judgeMax) //取最大最小值
JudgeMax= MP0
if(MP0<JudgeMin)
JudgeMin= MP0
delay(...) //具体设茄信置
}
Href++
while(HREF)
while(!HREF) //等待行中断结束
}
Over= 1 //中断函数处理完成
}
#pragma CODE_SEG DEFAULT
void ColorToRoads(UINT Href)
{
UINT i
UINT temp //用于起始线判断
UINT temp2
UINT temp3
UCHAR Judge //平均厥值
UINT j= 0
UINT m_nCount= 0
Judge= (JudgeMax+ JydgeMin)/2
Roads[Href].road= 0
Roads[Href].LeftRight= 0
for(i= 0i<48i+= 3)
{
if(Colors[i]<= Judge||
Colors[i+ 1]<= Judge||
Colors[i+ 2]<= Judge)
{
Roads[Href].road&= (0x8000>>(UCHAR)j)//相应位赋值为1
m_nCount++
}
j++
}
if(Roads[Href].road== 0x00)
{
Roads[Href].LeftRight= 0x02 //没有捕捉到黑线设置2
}
if(m_nCount>1) //若黑点数大于1,从左向右数出连续的点中的中间点
{
j= 0
i= 0
while(!(Roads[Href].road&(0x8000>>(UCHAR)i)))
{
i++
}
j= i
while((Roads[Href].road&(0x8000>>(UCHAR)i))
{
i++
if(i>15)
break
}
if(i<15) //起始线判断
{
temp= i
while(!(Roads[Href].road&(0x8000>>(UCHAR)temp)))
{
temp++
temp2= temp
}
if(temp<15)
{
while((Roads[Href].road&(0x8000>>(UCHAR)temp))
{
temp++
temp3= temp
if(temp>15)
break
}
if(temp<15)
{
while(!(Roads[Href].road&(0x8000>>(UCHAR)temp)))
{
temp++
}
if(temp<15)
{
Roads[Href].LeftRight= 0x04
Roads[Href].road&= 0x8000>>(UCHAR)((temp2+ temp3)/2)
return
}
}
}
}
Roads[Href].road&= 0x8000>>(UCHAR)((i+j)/2)
}
}
void ProcessRoads() //路径处理函数,在主函数中调用
{
int i
for (i= 0i<10i++)
{
ColorToRoads(i)
}
}
void delay(UINT m) //延时函数根据调试结果相应做改动
{
UINT i
UINT j
for(i= 0i<mi++)
{
for(j= 0j<200j++)
}
}
再下面就是主函数实现文件:
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h>/* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#include "watch.h" //摄像头
#include "TurnAround.h" //舵机
//#include "..." //直流电机
//#include "..." //测速模块
#ifndef TRUE
#define TRUE 1
#define FLASE 0
#endif
#define ULONG unsigned long
#define UCHAR unsigned char
#define UINT unsigned int
#define LITTLE
#define LARGE
//////////////////////全局变量
UCHAR m_nCount //圈数计算
UCHAR m_nCount2
////////////////////////////////函数定义
void init() //初始化函数 待修改
UCHAR CheckRoad(UCHAR*,UCHAR*) //第1参数返回第几个元素开始
//第2个参数返回第几个元素结束
//0 直线, 0x11有小弯道, 0x21大弯道, 0x31终点
void CarRun(UCHAR, UCHAR, UCHAR)//小车行驶函数
#pragma CODE_SEG NON_BANKED
#pragma TRAP_PROC
void Int_TimerOverFlow(void) //32MHz TCNT 50000--200ms
{
static int m= 0
if(m== 15) //3秒
{
m_nCount2= 0
TSCR1_TEN= 0//关定时器
TFLG2_TOF = 1
}
m++
}
#pragma CODE_SEG DEFAULT
void TimerInit()
{
TIOS= 0x00
TSCR2_PR= 7 //默认情况下是32MHz bus clock,所以分频后主时钟为 0.25MHz
TSCR2_TOI= 1 //OverFlow方式
TCNT= 65535- 50000
TSCR1_TEN= 1 //开定时器
}
////////////////////////////////
void main() //主程序
{
UCHAR ret
UCHAR number1= 0
UCAHR number2= 0
init()
while(1)
{
while(over) //等待扫描完毕
over= 0
IRQCR_IRQEN= 0 //关外部中断IRQ
ProcessRoads() //interface of watch.h
ret= CheckRoad(&number1, &number2) //检测路径
if(ret== 0x31)
break
CarRun(number1, number2, ret)
}
PWME_PWME1= 0//关闭PWM
while(1)
}
void init()
{
IRQCR_IRQE= 1 //下降沿触发IRQ
PE1= 1
EnableInterrupts //开总中断
INTCR_IRQEN= 1
PWMInit()//舵机初始化
Speed(MAX)
}
UCHAR CheckRoad(UCHAR* number1, UCHAR number2)
{
int i
int sub= 0
int psub= 0
int temp
bool flag
UCHAR ret
i= 0
m_nCount2++
while(!Roads[i].road)
{
i++
}
*number1= i
if(Roads[i].road>= Roads[i+ 1].road)
{
flag= TRUE
psub= Roads[0].road- Roads[1].road
}
else
{
flag= FALSE
psub= Roads[1].road- Roads[0].road
}
for(i<9i++)
{
if(Roads[i].LeftRight== 0x04)
{
if(m_nCount2== 0)
{
m_nCount2= 1
m_nCount++
TimerInit()
}
}
if(m_nCount== 3)
{
Speed(0)
ret= 0x31
goto _RET
}
if(Roads[i+ 1].road== 0)
{
break
}
if(flag)
{
sub= Roads[i].road- Roads[i+ 1].road
}
else
{
sub= Roads[i+ 1].road- Roads[i].road
}
sub>>= (UCHAR)i
if(sub>= psub) //取差值
temp= sub- psub
else
temp= psub- sub
if(temp>LARGE) //大弯道
{
ret= 0x21
while(Roads[i].road!= 0)
{
i++
*number2= i
if(i>9)
break
}
goto _RET
}
if(temp>LITTLE) //小弯道
{
ret= 0x11
while(Roads[i].road!= 0)
{
i++
*number2= i
if(i>9)
break
}
goto _RET
}
*number2= i
psub= sub
}
_RET:
return ret
}
void CarRun(UCHAR number1, UCHAR number2, UCHAR ret)
{
int end= Roads[number2].road
Turn(number2- number1, end- 0x100, Roads[number2].LeftRight, ret) //舵机函数
IRQCR_IRQEN= 1//开IRQ
}
下面为舵机头文件:
//TurnAround.h
#ifndef _TURNAROUND_H_ //头文件保护
#define _TURNAROUND_H_
void Turn(unsigned char, unsigned int, unsigned char, unsigned char)//转角函数
void PWMInit() //优先初始化
#endif
再以下为实现文件:(表内容待实验后求得)
//TurnAround.c
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h>/* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#include "TurnAround.h"
#include "..."//直流电机
#ifndef ULONG
#define ULONG unsigned long
#define UCHAR unsigned char
#define UINT unsigned int
#endif
/////////////////////////转角定义
#define RIGHT60 1083
#define RIGHT45 1000
#define RIGHT30 917
#define RIGHT15 833
#define RIGHT5 778
#define MIDDLE 750
#define LEFT5 722
#define LEFT15 667
#define LEFT30 583
#define LEFT45 500
#define LEFT60 417
/////////////////////////速度定义
#define SPEED0 //直线速度
#define SPEED5
#define SPEED15
#define SPEED30
#define SPEED45
#define SPEED60
//PWM查询表 7*10*2
static UINT PWMTable[]=
{
//Left
LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,
LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,
LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,
LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,
LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,
LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,
LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,
//Right
RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,
RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,
RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,
RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,
RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,
RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,
RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60
}
//延时查询表 6*6
static UNIT DelayTable[]=
{
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0
}
void PWMInit()//32HHz
{
PWMCTL_CON01= 1 //0, 1合为16bit
PWMCAE_CAE1= 1 //Center mode
PWMCLK_PLCK1= 0 //Clock A
PWMPRCLK_PCKA= 5 //Clock A= 32MHz/32= 1MHz
PWMPOL_PPOL1= 0 //开始为低电压
PWMPER0,1= 10000 //50Hz PWM输出
PWMDTY0,1= MIDDLE //(10000- 750)* 100%= 92。5%占空比,1.5ms高电压时间
PWME_PWME1= 1 //enable
}
static void delay(int ms)
{
int ii,jj
if (ms<1) ms=1
for(ii=0ii<msii++)
for(jj=0jj<2770jj++) //32MHz--1ms
}
static void SetPWM(UINT m_nPWM) //PWM设置
{
PWMDTY0,1= m_nPWM
}
//number 9种情况,sub 7种情况,LeftRight 2中情况
//共128种情况,采用模糊控制暂时归纳为11种情况
//11种情况将填入表中提供查询,11中情况分别为:
//左右5, 15, 30, 45, 60度
//和正中间0度
static UINT GetPWM(UCHAR number, UINT sub, UCHAR LeftRight)
{
switch(sub) //通过sub算出索引值
{
case 0x8000- 0x0100:
case 0x0100- 0x0001
case 0x0100- 0x0002
sub= 0
break
case 0x4000- 0x0100:
case 0x0100- 0x0004:
sub= 1
break
case 0x2000- 0x0100:
case 0x0100- 0x0008:
sub= 2
break
case 0x1000- 0x0100:
case 0x0100- 0x0010:
sub= 3
break
case 0x0800- 0x0100:
case 0x0100- 0x0020:
sub= 4
break
case 0x0400- 0x0100:
case 0x0100- 0x0040:
sub= 5
break
case 0x0200- 0x0100:
case 0x0100- 0x0100:
case 0x0100- 0x0080:
sub= 6
break
}
return PWMTable[number+ sub* 10+ LeftRight* 70]
}
//通过m_nPWM来改变速度,并返回改变的值
static UINT ChangeSpeed(UINT m_nPWM)
{
UINT m_nSpeed
switch(m_nPWM) //根据m_nPWM 调节速度
{
case LEFT60:
Speed(SPEED60)
m_nSpeed= SPEED60
break
case LEFT45:
Speed(SPEED45)
m_nSpeed= SPEED45
break
case LEFT30:
Speed(SPEED30)
m_nSpeed= SPEED30
break
case LEFT15:
Speed(SPEED15)
m_nSpeed= SPEED15
break
case LEFT5:
Speed(SPEED5)
m_nSpeed= SPEED5
break
case MIDDLE:
Speed(SPEED0)
m_nSpeed= SPEED0
break
case RIGHT60:
Speed(SPEED60)
m_nSpeed= SPEED60
break
case RIGHT45:
Speed(SPEED45)
m_nSpeed= SPEED45
break
case RIGHT30:
Speed(SPEED30)
m_nSpeed= SPEED30
break
case RIGHT15:
Speed(SPEED15)
m_nSpeed= SPEED15
break
case RIGHT5:
Speed(SPEED5)
m_nSpeed= SPEED5
break
}
return m_nSpeed
}
//获得查表时的索引值
UINT GetIndex(UINT m_nSpeed)
{
if(m_nSpeed<= SPEED60)
{
m_nSpeed= 0
}
else if(m_nSpeed<= SPEED45)
{
m_nSpeed= 1
}
else if(m_nSpeed<= SPEED30)
{
m_nSpeed= 2
}
else if(m_nSpeed<= SPEED15)
{
m_nSpeed= 3
}
else if(m_nSpeed<= SPEED5)
{
m_nSpeed= 4
}
else
{
m_nSpeed= 5
}
return m_nSpeed
}
//m_nSpeed2为欲设值
//m_nSpeed为当前速度
UINT GetDelay(UINT m_nSpeed, UINT m_nSpeed2)
{
m_nSpeed= GetIndex(m_nSpeed)
m_nSpeed2= GetIndex(m_nSpeed2)
return DelayTable[m_nSpeed* 6+ m_nSpeed2]
}
void Turn(UCHAR number, UINT sub, UCHAR LeftRight, UCHAR ret)//ret not be used now
{
UINT m_nPWM
UINT m_nSpeed
UINT m_nSpeed2
UINT m_nDelay //延时参数
m_nPWM= GetPWM(number, sub, LeftRight)
m_nSpeed= GetSpeed() //测速模块
m_nSpeed2= ChangeSpeed(m_nPWM)
if(m_nSpeed2>m_nSpeed)
m_nSpeed= m_nSpeed2- m_nSpeed
else
m_nSpeed= m_nSpeed- m_nSpeed2
SetPWM(m_nPWM) //转角
m_nDelay= GetDelay(m_nSpeed, m_nSpeed2)
delay(m_nDelay) //根据速度和角度延时
SetPWM(MIDDLE) //舵机摆正
}
最后说哈,程序只差调试就可以,筐架就是这。我是湖北赛区的,7月就要比赛了,他们车还没做好啊。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)