我正在使用 FreeScale 9S12C 微控制器并在 Code Warrior 中进行编码。我正在尝试为序列 10011 创建一个 SEQ 检测器。当我进行模拟时,程序卡在函数DelayGate
中,其他一切似乎都正常。似乎 CRGFLG 寄存器中的最后一位从未像预期的那样设置。我相信它应该在每个实时时钟周期结束时设置。我设置RTICTL = 0b01000000
了,所以实时时钟的周期应该是 1.024 毫秒。所以我的预期行为是程序应该停留在DelayGate
大约 1.024 毫秒然后退出,但是程序永远停留在延迟门中并且永远不会退出。由于某种原因,CRGFLG 中的最后一位似乎永远不会被设置,我不知道为什么。感谢任何人都可以为我提供的任何帮助!这是我的代码。我'
// Constants
#define CRYSTAL_CLOCK_FREQ 8000000L //set the clock to 8 MHz?
#define LED_COUNT_MAX 488 //(500 ms)/(1.024 ms) about 488
// Includes
#include <hidef.h> /* common defines & macros */
#include "derivative.h" /* derivative-specific */
// Prototypes
void SysInit(void);
void UpdateLED(void);
int input;
int state;
int nn = 5;
int value;
void Delay(int nn);
int UpdateStatetask(int input, int state);
void DelayGate(void);
int BeepFlag = 0;
int done = 0;
#endif
/*****************************************************
* main - Program main
****************************************************/
void main(void){
COPCTL = 0x00; // Disable the COP timer
RTICTL = 0b01000000; //8 MHz crystal, period of real time clock is 1.024 ms
PTT = 0x00; // initally all logical zero
DDRT = 0b11111100; // PT0 "0" input
//PT1 "1" input
//PT2 SEQ state 1 indication
// PT3 SEQ state 2 indication
// PT4 SEQ state 3 indicaiton
// PT5 SEQ state 4 indication
// PT6 SEQ detection
// PT7 LED Clock
PERT = 0b11111111; // enable pulling on all port T
PPST = 0b11111111; // pull-down to ground all port T
CLKSEL = 0x00;
PLLCTL = 0x00;
CRGINT = 0b10000000;
while (1){
UpdateLED();
DelayGate();
}
}
/**************************************************
* UpdateLED()
* When the LED count runs out, toggle and beep
*************************************************/
void UpdateLED(void){
static int state = 0;
int input;
int LedCount = LED_COUNT_MAX; //488*1.024 ms = 0.4997 s
if (--LedCount == 0){ //decrement LED count
LedCount = LED_COUNT_MAX;
PTT = 0b10000000; //turn on LED clock
}
if (PTT & 0x01 == 1){ //bitwise and, checking for clock LED
if(PTT & 0b00000001){ //"0" input
input = 0;
}
if(PTT & 0b00000010){ //"1" input
input = 1;
}
}
UpdateStatetask(input,state);
}
/**************************************************
* UpdateStatetask()
*************************************************/
int UpdateStatetask(input, state){
switch(state){
case 0:
PTT = 0b00000000; //state 0 no LEDs should light up
if (input == 0){ //SEQ = 10011
state = 0; //if "0" is entered at state zero stay at state zero
}
else if(input == 1){
state = 1; //if "1" is entered at state zero go to state 1
}
break;
case 1:
PTT = 0b00000100; //turn on LED indicating state 1
if (input == 0){ //if "0" is entered at state one go to state two
state = 2;
}
else if(input == 1){ //if "1" is entered at state one stay at state one
state = 1;
}
break;
case 2:
PTT ^= 0b00001100; //state 2 indication turn on 2 LED
if (input == 0){ //if "0" is entered at state two go to state three
state = 3;
}
else if(input == 1){ //if "1" is entered at state two go to state one
state = 1;
}
break;
case 3:
PTT = 0b00011100; //state 3 indication turn on 3 LED
if (input == 0){ //if "0" is entered at state three go to state zero
state = 0;
}
else if(input == 1){ //if "1" is entered at state three go to state four
state = 4;
}
break;
case 4:
PTT = 0b00111100; //state 4 indication turn on 4 LED
if (input == 0){ //if "0" is entered at state four go to state 2
state = 2;
}
else if(input == 1){//if "1" is entered at state four go to state 1
PTT = 0b01111100; //SEQ detection turn on 5 LED
state = 1;
}
break;
default:
state = 0;
break;
}
return state;
}
/**************************************************
* DelayGate
* Wait for timeout, then restart the RTI clock
*************************************************/
void DelayGate(void){
while ( (CRGFLG & 0x80) == 0){
;
}
CRGFLG = 0x80;
}
当我编译我得到的唯一警告是
- 函数调用的结果在这一行被忽略:
UpdateStatetask(input,state)
- 这是这一行的旧式函数调用:
int UpdateStatetask(input, state){
这些警告不应该导致我遇到的问题。谢谢你的帮助!