Title: Example 16 Circular Queue
1Example 16 Circular Queue
2A Circular Queue
Empty
Containing 2 values
3 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
4 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 qbuffQMAX // 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
5void qstore(char c) rear
// inc rear if(rear gt max) rear min
if(rear front) rear-- //
queue is full if(rear lt min) // rewind
rear rear max else qbuffrear
c // store c at rear
6int qempty(void) int flag if(front
rear) flag 1 else flag 0
return (flag)
7 char getq(void) front //
inc front if(front gt max) front 0
return qbufffront // return value at front
8// Example 16 Example of using a queue include
lthidef.hgt / common defines and macros
/ include ltmc9s12dp256.hgt / 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)
9 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