智能车制作

标题: 哪位高手给我看看,我用的是G128,为什么D口的小灯不闪烁呢 [打印本页]

作者: Roy罗先森    时间: 2012-9-18 18:29
标题: 哪位高手给我看看,我用的是G128,为什么D口的小灯不闪烁呢
#include <hidef.h>      /* common defines and macros */
#include "derivative.h"      /* derivative-specific definitions */
#include<mc9s12g128.h>
#pragma CODE_SEG __NEAR_SEG NON_BANKED


//////////////////////////////////
//********************将频率升到40M***************************//

void SetBusCLK_nM(byte nM)
{   
    MMCCTL1=0X00;                       
    PKGCR  =0X06;                       
    DIRECT =0x00;
    IVBR   =0xFF;   
    ECLKCTL=0xC0;   
    CPMUPROT   =0x26;   //停止保护时钟配置寄存器
    CPMUCLKS_PSTP=0;    //
    CPMUCLKS_PLLSEL=1;  //应用PLL      
    //CPMUSYNR   =nM-1;   //设置分频因子  
    CPMUSYNR   =0xc0|(nM-1); //设置分频因子
    CPMUREFDIV =0x80|0x00; //pllclock=2*(1+SYNR)= MHz;
    CPMUPOSTDIV=0x00;   // Set the post divider register      
    CPMUPOSTDIV=0x00;   // Set the post divider register  
    CPMUPLL    =0x10;   // Set the PLL frequency modulation  
    while(CPMUFLG_LOCK == 0);          /* Wait until the PLL is within the desired tolerance of the target frequency */
    CPMUPROT=0x00;            /* Enable protection of clock configuration registers */
}


//////////////////////////////////////////////////使用busclock   
void API_Init() {
    CPMUAPICTL_APIFE =0;//在设定值之前需要清零;
    CPMUAPICTL_APICLK = 1;
    CPMUAPIR = 20000;     ///1ms
   
   // CPMUACLKTR=0xFC;// fACLK is trimmed to 10KHz
    //?auto Clock 则要设置此寄存器 为0xfc
     CPMUAPICTL_APIE =1;
      CPMUAPICTL_APIF = 1;
    CPMUAPICTL_APIFE = 1;     
}


////////////////////////////////////////////////////////////

void main(void) {
  /* put your own code here */
  
  SetBusCLK_nM(40);
  DDRD = 0xff;
  PORTD = 0x00;
  API_Init();

        EnableInterrupts;
}
///////////////////////////////////////////////////////////
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 59U void API(void) {
   
   static unsigned  int count =0;
  

  count++;
  if(count >= 300) {    ////100ms
       CPMUAPICTL_APIF = 1;
      count = 0;
      PORTD = ~ PORTD;
     
  }

}
#pragma CODE_SEG DEFAULT //将后续代码置于默认区域内

作者: qinlu123    时间: 2012-9-18 18:40
你把count定义成全局变量试试
作者: Roy罗先森    时间: 2012-9-18 21:05
不行,编译有错误,现象还是一样,D口灯亮但是不闪烁,但是如果是单步调试的话,程序可以进入中断,而且小灯可以亮灭,所以还是不明白,你能帮我看看不?
作者: yukunlinykl    时间: 2012-9-18 21:10
弱猜,是不是闪得太快,眼睛看不出是在闪了?
作者: Roy罗先森    时间: 2012-9-18 21:26
不会,我改过时间试了很多遍
作者: xc229876115    时间: 2012-9-18 21:45
单步执行可行。那就可能是1ms时间太短了。换成1s试试效果???
作者: Roy罗先森    时间: 2012-9-18 23:33
找到原因了,中断需要等待呀,不然程序跑飞了,呵呵,谢谢大家啦
void main(void) {
   /* put your own code here */
   
   SetBusCLK_nM(40);
   DDRD = 0xff;
   PORTD = 0x00;
   API_Init();

        EnableInterrupts;
for(;;)
{}
}




欢迎光临 智能车制作 (http://dns.znczz.com/) Powered by Discuz! X3.2