中级会员
- 积分
- 223
- 威望
- 181
- 贡献
- 22
- 兑换币
- 0
- 注册时间
- 2009-2-25
- 在线时间
- 10 小时
|
3#
楼主 |
发表于 2009-4-29 19:33:26
|
只看该作者
不行哦!下面是我对程序做相应的修改,但还是采集不到,不知可帮我看看是什么原因?
///////////////////////////////////////////////////////////////////
//PT4输入捕捉测速程序,PT4口输入脉冲
//每次记录脉冲时间可修改宏:TIME_INTER
//说明此程序用于总线时钟在32M情况下,若总线时钟有变化,改分频参数和模数常数以得到精确定时
//author:whut_wj
///////////////////////////////////////////////////////////////////
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
#define TIME_INTER 1000 //1000个ms=1s
unsigned int Get_pulse; //脉冲个数
unsigned int number_count;
void Start_PLL(void) //busCLK=32M
{
REFDV=0x01; // PLLCLK =2*OSCCLK*(SYNR + 1)/(REFDV + 1)
SYNR=0x03; // =2*16M*3/2=64M
asm{
BRCLR CRGFLG,#$08,*
BSET CLKSEL,#$80
}
}
void PWM_Init(void)
{
PWME=0X00; //关闭所有通道使能
PWMCTL=0XE0; //通道2、3;4、5;6、7级联
PWMCAE=0X00; //左对齐
PWMPOL=0XFF; //开始输出时为高电平
PWMPRCLK=0X00; //ClockA=ClockB=bus clock
PWMCLK=0XFF; //所有通道都选择ClockSA或ClockSB
PWMSCLA=32; //ClockSA=ClockA/(2*PWMSCLA)
PWMPER45=10000; //PWM45:20ms,50Hz
PWMDTY45=800; //舵机占空比
PWMSCLB=4; //ClockSB=ClockB/(2*PWMSCLB)
PWMPER67=2000; //PWM67:0.5ms,2KHz
PWMPER23=2000; //PWM23:0.5ms,2KHz
PWMDTY67=1000; //电机占空比
PWMDTY23=0; //电机占空比
PWME=0XFF; //通道23,45,67使能
}
/*initiate SCI*/
void SCI_Init(void)
{
SCI0BD=208; //busclk 32MHz, 9600bps,SCI0BDL=0xcf
SCI0CR1=0;
SCI0CR2=0X2C; //enable Receive Full Interrupt,RX enable,Tx enable
}
void uart_putchar(unsigned char ch)
{
if (ch == '\n')
{
while(!(SCI0SR1&0x80)) ;
SCI0DRL= 0x0d;//output'CR'
return;
}
while(!(SCI0SR1&0x80)) ; //keep waiting when not empty
SCI0DRL=ch;
SCI0SR1_RDRF=1;
}
void Dly_ms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2770;jj++); //32MHz--1ms
}
void MDCInit(void)
{
MCCTL = MCCTL&0Xfb; //模数计数器禁止运行
MCCTL = 0Xe3; //允许中断,模数计数方式
//返回时重新加载所用的常数,分频常数为16
MCCTL = MCCTL|0X04; //模数计数器使能
MCCNT = 2000; //(1/32M)*16*2000= 1ms
MCCTL = MCCTL|0X08 ; //把模数常数寄存器的值加载到模数计数器FLMC;
}
void Init_PT4_Capture()
{
TIOS=0x00; //输入捕捉
TCTL3=0x80; //通道4下降沿触发
TSCR2=0X07; //分频,用来给TCNT作时间参考,这里没有意义
TSCR1=0x80; //Timer Enable
TIE=0X80; //初始化定时器开启
}
void main(void)
{
DisableInterrupts;
Start_PLL();
Init_PT4_Capture();
MDCInit();
PWM_Init();
SCI_Init();
EnableInterrupts;
DDRB=0xff;
PORTB=0XAA;
for(;;)
{
uart_putchar((unsigned char)Get_pulse);
Dly_ms(1000);
}
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 26 MDC_ISR(void)
{
static unsigned int count=0;
count++;
if( count==TIME_INTER) //1秒取一次
{
Get_pulse = number_count;
number_count=0;
count=0;
PORTB^=0Xff;
}
MCFLG = 0x80;
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 12 PT7_Capture(void)
{
TFLG1=0x80;
number_count++;
} |
|