John Pinares
Newbie level 4
- Joined
- Aug 31, 2014
- Messages
- 6
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 55
Hey, everyone. So I was given this code with that is supposed to add a sentence to a queue every time a switch (A0) is switched and output them character by character. Right now I cant seem to add the sentence more than 3 times to the queue. Can you guys see anything that I may have missed? So far the only change I've made was on line 193, the transition from OutChar to OutWait used to be !TxReady, I changed it to one. All it really did was let me add the string to the queue 3 times instead of one. Here is the code:
Thanks for any help in advance.
Code:
/*
This code was automatically generated using the Riverside-Irvine State machine Builder tool
Version 2.7 --- 10/31/2014 20:21:14 PST
*/
#include "rims.h"
/*This code will be shared between state machines.*/
static char * pstr;
#define NULL 0
#define MAX 150
const char EOS='\0';
typedef struct que
{
int arr[MAX];
int rear,front;
}que;
que q;
int isfull(que *p)
{
if (((p->front==0)&&(p->rear==MAX-1))||(p->front==p->rear+1))
return 1;
else
return 0;
}
int isempty(que *p)
{
if(p->front ==p->rear)
return 1; //true
else
return 0; //false
}
void insertq(que *p,int v)
{
int t;
t = (p->rear+1)%MAX;
if(t == p->front)
printf("\nQueue Overflow\n");
else
{
p->rear=t;
p->arr[p->rear]=v;
}
}
int removeq(que *p)
{
if(isempty(p))
{
printf("\nQueue Underflow");
exit(0);
}
else
{
p->front=(p->front + 1)%MAX;
return(p->arr[p->front]);
}
}
unsigned char TimerFlag = 0;
void TimerISR() {
TimerFlag = 1;
}
enum SM1_States { SM1_Wait, SM1_OutStr, SM1_init } SM1_State;
TickFct_ButtonMgr() {
/*VARIABLES MUST BE DECLARED STATIC*/
/*e.g., static int x = 0;*/
/*Define User Variables and Functions For this State Machine Here.*/
static char * mystr="Now is the time for all good men to come to the aid of their country\n";
switch(SM1_State) { // Transitions
case -1:
SM1_State = SM1_init;
break;
case SM1_Wait:
if (A0) {
SM1_State = SM1_OutStr;
pstr=mystr;
}
break;
case SM1_OutStr:
if (!A0) {
SM1_State = SM1_Wait;
}
break;
case SM1_init:
if (1) {
SM1_State = SM1_Wait;
}
break;
default:
SM1_State = SM1_init;
} // Transitions
switch(SM1_State) { // State actions
case SM1_Wait:
pstr=NULL;
B0=0;
break;
case SM1_OutStr:
B0=1;
break;
case SM1_init:
q.rear=q.front=0;
UARTOn();
// activate UART
printf("Str len:%d\n",strlen(mystr));
break;
default: // ADD default behaviour below
break;
} // State actions
}
enum SM2_States { SM2_WaitPstr, SM2_QMgr } SM2_State;
TickFct_QueMgr() {
/* LOAD THE QUEUE */
/*VARIABLES MUST BE DECLARED STATIC*/
/*e.g., static int x = 0;*/
/*Define user variables for this state machine here.*/
switch(SM2_State) { // Transitions
case -1:
SM2_State = SM2_WaitPstr;
break;
case SM2_WaitPstr:
if (pstr!=NULL) {
SM2_State = SM2_QMgr;
}
break;
case SM2_QMgr:
if (1) {
SM2_State = SM2_WaitPstr;
}
break;
default:
SM2_State = SM2_WaitPstr;
} // Transitions
switch(SM2_State) { // State actions
case SM2_WaitPstr:
B1=0;
break;
case SM2_QMgr:
// LOAD STRING ONTO QUEUE
B1=1;
//printf("%s",pstr);
// or
while(*pstr!=EOS){
insertq(&q,*pstr++);
}
pstr=NULL;
break;
default: // ADD default behaviour below
break;
} // State actions
}
enum SM3_States { SM3_QWait, SM3_OutWait, SM3_OutChar } SM3_State;
TickFct_UARTMgr() {
/*VARIABLES MUST BE DECLARED STATIC*/
/*e.g., static int x = 0;*/
/*Define user variables for this state machine here.*/
char tmp;
switch(SM3_State) { // Transitions
case -1:
SM3_State = SM3_QWait;
break;
case SM3_QWait:
if (!isempty(&q)) {
SM3_State = SM3_OutWait;
}
break;
case SM3_OutWait:
if (isempty(&q)) {
SM3_State = SM3_QWait;
}
else if (TxReady) {
SM3_State = SM3_OutChar;
}
break;
case SM3_OutChar:
if (1) { //**LINE 193, ONLY CHANGE MADE YET***
SM3_State = SM3_OutWait;
}
break;
default:
SM3_State = SM3_QWait;
} // Transitions
switch(SM3_State) { // State actions
case SM3_QWait:
B2=0;
break;
case SM3_OutWait:
B2=0;
break;
case SM3_OutChar:
B2=1;
T=removeq(&q);
break;
default: // ADD default behaviour below
break;
} // State actions
}
int main() {
B = 0; //Init outputs
TimerSet(50);
TimerOn();
SM1_State = -1;
SM2_State = -1;
SM3_State = -1;
while(1) {
TickFct_ButtonMgr();
TickFct_QueMgr();
TickFct_UARTMgr();
while (!TimerFlag);
TimerFlag = 0;
}
}
Thanks for any help in advance.