I'm using a FreeScale 9S12C micro controller and am coding in Code Warrior. I'm trying to create a SEQ detector for Sequence 10011. When I do a simulation the program gets stuck in the function "DelayGate", everything else appears ok. It seems that the last bit in the CRGFLG register is never getting set like it's supposed to. I believe it's supposed to get set at the end of every real time clock cycle. I set RTICTL = 0b01000000, so the real time clock should have a period of 1.024 ms. So my expected behavior is that the program should stay in the DelayGate for approximately 1.024 ms and than exit, but the program stays in delay gate for ever and never exits. It appears that the last bit in CRGFLG never gets set for some reason, and I'm not sure why. Thanks for any help anyone can provide me! Here's my code. I'm using a 8 MHz crystal.
// 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;
}
When I compile the only warnings I get is that 1) result of function call is ignored on this line "UpdateStatetask(input,state)" 2) This is the old style of function call here on this line "int UpdateStatetask(input, state){ "
These warnings shouldn't cause the problem I'm having. Thanks for any help!
 
					
				
		
 RadekS
		
			RadekS
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		Hi Dylanhammerman,
RTI is real time interrupt.
I am afraid that it cannot work as you expected. The RTI is enabled/disabled by RTIE bit.
So, you should create RTI interrupt routine and service your delay by some user flags.
The other option is using a different module like TIM timer for such purpose.
I hope it helps you.
Have a great day,
Radek
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
