智能车制作
标题:
求教,自己编的一个壁障程序,编译能过,但实现不了功能,求教各位大神
[打印本页]
作者:
youcanyouup
时间:
2014-9-13 10:04
标题:
求教,自己编的一个壁障程序,编译能过,但实现不了功能,求教各位大神
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
/*参数声明*/
#define Sound_Velocity 34000
unsigned long Counter;
unsigned long Distance;
unsigned long CounterDown;
unsigned long CounterUp;
/*函数声明*/
void IC0_Init();
void PIT0_Init();
void delay_20us(unsigned int);
void PLL_Init(void);
void Init_Register(void);
void main(void)
{
/* put your own code here */
void IC0_Init(void);
void PIT0_Init(void);
void PLL_Init(void);
void Init_Register(void);
EnableInterrupts;
for(;;)
{
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
/*计数器中断函数*/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 8 Timer_IC0(void)
{
TFLG1_C0F=1; // 清除中断标志
if(TCTL4==0X01&&TCTL3==0X00)
{
CounterUp=TCNT;
TCTL4=0X00;
TCTL3=0X01;
}
else if(TCTL4==0X00&&TCTL3==0X01)
{
CounterDown=TCNT;
TCTL4=0X01;
TCTL3=0X00;
Counter=CounterDown-CounterUp;
if(Counter>0)//判断计数器是否溢出
{
Distance=Counter*Sound_Velocity/(2*5000000); //计算小车距障碍物的距离
}
else
{
Distance=(Counter+65536)*Sound_Velocity/(2*5000000); //计算小车距障碍物的距离
}
if(Distance<=50) //判断小车距障碍物的距离
{
PORTA_PA2=1;
//PORTK_PK0=0;
}
}
}
#pragma CODE_SEG DEFAULT
/*PIT0中断函数*/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 66 PIT0(void)
{
PORTB_PB0=0;
PORTB_PB0=1;
delay_20us(320);
PORTB_PB0=0;
PITTF_PTF0=1; //清除标志位
}
#pragma CODE_SEG DEFAULT
/*通道0输入捕捉初始化*/
void IC0_Init(void)
{
TSCR2=0X04; // 禁止溢出中断,分频系数为16(80/16MHz)
TIOS_IOS0=0; //通道0为输入捕捉
TCTL3=0X00;
TCTL4=0X01; //设置捕捉方式为上升沿捕捉
TIE_C0I=1; //通道0输入捕捉中断使能
TSCR1=0X80; //使能计数器
}
/*PIT中断初始化*/
void PIT0_Init(void)
{
PITCFLMT_PITE=0; //PIT模块关闭
PITCE_PCE0=1; //PIT通道开启
PITMTLD0=200-1; //8位定时器初值设定(0~255).
PITLD0=40000-1; //低16位计数器=40000(0~65535)
PITMUX_PMUX0=0; //PIT通道0选择时基0
PITINTE_PINTE0=1; //定时器中断通道0中断使能
PITCFLMT_PITE=1; //PIT模块使能
//PIT0中断触发周期= (PITMTLD0+1)x(PITLD0+1)/BUSCLK=100ms
}
/*锁相环初始化*/
void PLL_Init(void)
{
CLKSEL=0x00;
PLLCTL_PLLON=1;
SYNR=0xc0|0x09;
REFDV=0x80|0x01;
POSTDIV=0x00;
_asm(nop);
_asm(nop);
while(!(CRGFLG_LOCK==1));
CLKSEL_PLLSEL=1;
}
/*定义A B T端口的方向寄存器*/
void Init_Register(void)
{
DDRA=0xFF; //PORTA0-7:OUTPUT
DDRB=0XFF; //PORTB0-7:OUTPUT
DDRT=0X00; //PORTT0-7:INPUT
}
/*延时函数*/
void delay_20us(unsigned int num)
{
unsigned int i;
for(i=0;i<=num;i++) //晶振频率16MHz
_asm(nop);
}
欢迎光临 智能车制作 (http://dns.znczz.com/)
Powered by Discuz! X3.2