Board logo

标题: 请教strongchen斑竹:模数递减计数器MDC问题!!!谢谢 [打印本页]

作者: lanhaigo    时间: 2007-6-29 19:45     标题: 请教strongchen斑竹:模数递减计数器MDC问题!!!谢谢

我作了个,MDC程序,用它做计时器,延时一秒.但是无论怎样改变预分频MCPR1,MCPR0,两次中断的时间间隔不变(即,频率不便);另外改变MCCNT的植,两次中断的时间间隔也不变!
希望斑竹指点,谢谢!!


#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"

long int counter,temp1;
byte flag;
byte value_lamp,cycle2;


#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 26 void Timer_ISR(void){

counter++;
if(counter%25000==0) { //dalay 1s
flag = 1;
counter = 0;
temp1 = MCCNT;
MCCTL_MCZI = 0; // MDC 屏蔽中断
//MCCNT = 0xC350;
// PORTB = 0xe7;

}
/* clear RTIF bit */
//TFLG2_TOF = 1;
}
#pragma CODE_SEG DEFAULT


void main(void) {
/* put your own code here */

temp1 =0;
counter = 0;
flag = 0;
value_lamp = 0xaa;


DDRB=0xff; /* PTB as output */
PORTB=0x7e; /* LEDs off */

MCCTL_MCEN = 1; // MDC允许工作
MCCTL_MODMC = 1; // MDC连续工作方式 ,
MCCTL_MCPR0 = 1;
MCCTL_MCPR1 = 1; // [MCPR1 MCPR0] set Prescaler Rate -->
// [00]:1 [01]:4 [10]:8 [11]:16

//MCCNT = 0xc350;//计数50000 //不起作用1

EnableInterrupts;
for(;;) {

MCCTL_MCZI = 1; // MDC 允许中断
if(flag==1){
value_lamp = ~value_lamp ;
PORTB = value_lamp;
flag = 0;
}
} /* wait forever */
/* please make sure that you never leave this function */
}
作者: strongchen    时间: 2007-7-2 12:06

你在中断服务程序里没有清中断标志,实际上你的延时完全是靠“if(counter%25000==0)”完成的,当然无法通过MCPR1和MCPR0改变。把程序照如下修改试试:

long int counter,temp1;
byte flag;
byte value_lamp,cycle2;


#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 26 void Timer_ISR(void){

counter++;
/*
if(counter%25000==0) { //dalay 1s
flag = 1;
counter = 0;
temp1 = MCCNT;
//MCCTL_MCZI = 0; // MDC ÆÁ±ÎÖжÏ
//MCCNT = 0xC350;
// PORTB = 0xe7;
}
*/

/* clear RTIF bit */
//TFLG2_TOF = 1;

flag = 1;
MCFLG_MCZF = 1;
}
#pragma CODE_SEG DEFAULT


void main(void) {
/* put your own code here */

temp1 =0;
counter = 0;
flag = 0;
value_lamp = 0xaa;


DDRB=0xff; /* PTB as output */
PORTB=0x7e; /* LEDs off */

//MCCTL_MCEN = 1; // MDCÔÊÐí¹¤×÷
MCCTL_MODMC = 1; // MDCÁ¬Ðø¹¤×÷·½Ê½ ,
MCCTL_MCPR0 = 0;
MCCTL_MCPR1 = 0; // [MCPR1 MCPR0] set Prescaler Rate -->
// [00]:1 [01]:4 [10]:8 [11]:16
MCCTL_MCZI = 1; // MDC ÔÊÐíÖжÏ
MCCTL_MCEN = 1; // MDCÔÊÐí¹¤×÷

//MCCNT = 0xc350;//¼ÆÊý50000 //²»Æð×÷ÓÃ1

EnableInterrupts;
for(;;) {

//MCCTL_MCZI = 1; // MDC ÔÊÐíÖжÏ
if(flag==1){
value_lamp = ~value_lamp ;
PORTB = value_lamp;
flag = 0;
}
} /* wait forever */
/* please make sure that you never leave this function */
}





欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0