90 likes | 350 Views
Example 16 Circular Queue. Lecture L7.2. A Circular Queue. Empty. Containing 2 values. Listing 16.2 Function prototypes (queue.h) // queue.h A character queue void initq(void); // initialize the queue void qstore(char); // store character in queue
E N D
Example 16 Circular Queue Lecture L7.2
A Circular Queue Empty Containing 2 values
Listing 16.2 Function prototypes (queue.h) // queue.h A character queue void initq(void); // initialize the queue void qstore(char); // store character in queue int qempty(void); // return 0 if queue is not empty char getq(void); // read character from queue
Listing 16.1 A character queue (queue.c) // queue.c A character queue #include "queue.h" // prototype definitions #define QMAX 16 // size of queue static char qbuff[QMAX]; // the queue static int front; static int rear; // queue pointers static int min = 0; // start of queue static int max = QMAX-1; // end of queue void initq(void){ min = 0; front = 0; rear = 0; max = QMAX-1; }
void qstore(char c){ rear++; // inc rear if(rear > max) rear = min; if(rear == front){ rear--; // queue is full if(rear < min) // rewind rear rear = max; }else qbuff[rear] = c; // store c at rear }
int qempty(void){ int flag; if(front == rear) flag = 1; else flag = 0; return (flag); }
char getq(void){ front++; // inc front if(front > max) front = 0; return qbuff[front]; // return value at front }
// Example 16: Example of using a queue #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "queue.h" #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { char* blanks; char c, a; blanks = " "; PLL_init(); // set system clock frequency to 24 MHz lcd_init(); // enable lcd initq(); // initialize the queue keypad_enable(); // enable keypad set_lcd_addr(0x00); // display on 1st line while(1) { c = getkey(); // read keypad a = hex2asc(c); // convert to ascii qstore(a); // and store in queue data8(a); // display on LCD wait_keyup(); // wait to release key switch(c){
case 0xE: // if enter (*) key set_lcd_addr(0x40); // display on 2nd line while(qempty() != 0){ // empty the queue data8(getq()); // and display on lcd } set_lcd_addr(0x00); // clear 1st line type_lcd(blanks); wait_keyup(); // wait to release key set_lcd_addr(0x00); // display on 1st line break; case 0xF: // if clear (#) key clear_lcd(); // clear lcd display wait_keyup(); // wait to release key break; default: break; } } }