智能车制作
标题:
脉冲输入捕捉
[打印本页]
作者:
tungary88
时间:
2009-11-24 15:02
标题:
脉冲输入捕捉
下面的程序是用來捕捉脉冲输入, 但是输出到電腦的數據不是一個恆定的溢出的數, 是什麼地方出了問題呢?
[attach]758[/attach]
#include <hidef.h> /* common defines and macros */
#include <mc9s12xdp512.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xdp512"
#include "Includes.h" //×ÜÍ·Îļþ
/*//-----------------------------------------------------
void setbusclock(void)
{
CLKSEL=0X00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR=1;
REFDV=1; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
_asm(nop); //BUS CLOCK=16M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
//-----------------------------------------------------
*/
////////////////////////////////////////////
//PLL :×ÜÏßʱÖÓ±¶Æµ
void SetPll() //ͨ¹ýÉèÖÃÎå¸ö¼Ä´æÆ÷
{
CLKSEL &= 0x7f;
// CLKSEL=0x00;
PLLCTL &= 0xbf;
SYNR=2; //ʱÖӺϳɼĴæÆ÷ 00-63
REFDV=1; //ʱÖÓ·ÖƵ¼Ä´æÆ÷ 00-15 PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
//=48MHZ BUS CLOCK=0.5*CORE CLOCK=24MHZ
PLLCTL |= (1<<6);
asm NOP;
asm NOP;
asm NOP;
while(0==(CRGFLG&0x08)); //JUDGE THE LOCK BIT, =1 CLOCK STABLE
CLKSEL=0x80; //SELECT PLL clock
}
#define BAUD_9600 0x009c
#define BAUD_19200 0x004e
#define BAUD_38400 0x0027
#define BAUD_43000 0x0023
#define BAUD_56000 0x001b
#define BAUD_57600 0x009c
#define BAUD_115200 0x000D
void Sci0Ini(unsigned int baudrate)
{
SCI0BDH=baudrate>>8; //SCIÄ£¿éʱÖÓ=24M
SCI0BDL=baudrate;//SCI²¨ÌØÂÊ=SCIÄ£¿éʱÖÓ/16/BR
SCI0CR2=0x2c; //#ifdef SCI_REC_INT
}
void PutChar0(unsigned char c) // SENDING FUNCTION pass
{
while((SCI0SR1&0x80) == 0){}
SCI0DRL = c;
}
void Delay_1ms(unsigned int n_ms)
{
unsigned int dd; //scope proof right :1.0025ms
while(n_ms--)
{
dd=3000;
while(dd--) ;
}
}
void TimerInit(void)
{
//½ûÖ¹¶¨Ê±Æ÷Òç³öÖжÏ,·ÖƵÒò×Óp=8
//ÖжÏÒ»´Îʱ¼ä¼ÆË㣺t=n/(fbus/p)=1/38Ãë,ÆäÖÐn=65535,fbus=19.6608MHZ
TIOS = ~(1<<0); //ÉèÖÃͨµÀ0ΪÊäÈ벶׽¹¦ÄÜ
TCTL4 = 0x02; //ÉèÖÃÉÏÉýÑغÍϽµÑØÊäÈ벶׽
TIE = 0x01;
//ÔÊÐíͨµÀ0ÖжÏ
// TSCR2 = 0x80;
// TSCR1 = 0x80; //ÔÊÐíÖ÷¶¨Ê±Æ÷¿ªÊ¼¼ÆÊý
}
int TimeInterCount;
Bool isBeginTimer;
void main(void) {
/* put your own code here */
DisableInterrupts;
//MCUInit();
Sci0Ini(BAUD_9600); //´®¿Ú³õʼ»¯ 9600bps
PutChar0(0xfb);
DDRB=0xFF;
DDRA=0xFF;
DDRD=0xFF;
PORTA=0xFF;
PORTD=0xFF;
PORTB=0xFF; //LED control
PUCR_PUPBE=1;
SetPll();
ICInit();
TimerInit();
TimeInterCount=0;
isBeginTimer=0;
EnableInterrupts;
for(;;) {Delay_1ms(1000);} /* wait forever */
/* please make sure that you never leave this function */
}
//isrTimOver:¶¨Ê±Æ÷Òç³öÖжϴ¦Àí³ÌÐò----------------------------------------*
__interrupt 16 void isrTimOver(void)
{
asm("sei"); //¹Ø×ÜÖжÏ
TimeInterCount++;
PORTD=~PORTD;
// PutChar0(TimeInterCount);
TFLG2 = 0x80; //Çå³ý¶¨Ê±Æ÷Òç³ö±ê־λ
EnableInterrupts; //¿ª×ÜÖжÏ
}
__interrupt 8 void ISR_TimerChan0(void)
{
unsigned char ByteH,ByteL,value;
// unsigned int value;
//unsigned float value1
DISABLE_INTERRUPTS; //‰ÛÜš¸œ½Ë
//LEDDrive();
PORTB=~PORTB;
TSCR2 = 0x00;
TSCR1 = 0x00;
// ByteH=(TimeInterCount&0xff00)>>8;
// ByteL=TimeInterCount&0x00ff;
PutChar0(TimeInterCount);
//PutChar0(ByteH);
//PutChar0(ByteL);
TimeInterCount=0;
TSCR2 = 0x80;
TSCR1 = 0x80;
TFLG1 |= (1<<C0F); //Çå³ýÊäÈ벶׽±ê־λ
ENABLE_INTERRUPTS; //¿ª×ÜÖжÏ
}
void ICInit(void)
{
TSCR1 = 0; //½ûֹʱÖÓ
TIOS = ~(1<<0); //ÉèÖÃͨµÀ0ΪÊäÈ벶׽¹¦ÄÜ
TCTL4 = 0x03; //ÉèÖÃÉÏÉýÑغÍϽµÑØÊäÈ벶׽
TIE = 0x01; //ÔÊÐíͨµÀ0ÖжÏ
TSCR2 = 0x06; //²»ÔÊÐíÒç³öÖжϣ¬·ÖƵÒò×ÓΪ64
TSCR1 |=(1<<TEN); //ʱÖÓ¼ÆÊý
}
复制代码
作者:
zdhly0401
时间:
2009-11-24 17:48
你先测试下各个中断进去了没
作者:
jakerroo
时间:
2009-11-24 22:02
这个太麻烦了,用PE来做,串口只要写波特率,脉冲只要写下降沿上升沿中断就可以
作者:
tungary88
时间:
2009-11-25 11:48
回复
3#
jakerroo
能帮我改一改吗? 十分赶急, 谢谢你
作者:
jakerroo
时间:
2009-11-26 12:07
用PE工作方式配置脉冲捕捉的bean已经发给你了,你配置一些bean,能用简单的为什么要去用寄存器,飞思卡尔很大的优势就是在PE上,做项目很快
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2