Download AN155: Stepper Motor Reference Design
Transcript
AN155
//
//
//
//
//
//
//
//
//
//
//
//
//
readc()
The readc() function is the lowest level function which provides
direct access to the read buffer. It reads one character from the
read buffer.
Note that readc will wait if the read buffer is empty. This is usually
desired if the program is waiting for user input.
readc is a driver function and is closely integrated with the ISR.
UART interrupts are temporarily disabled while the buffer is updated,
This prevents the ISR from modifying the buffer and counter values
during processing. That would be bad.
char readc(void)
{
char theChar;
// the character to return
signed char i;
// signed value to build location
while (readCount==0);
// wait here if buffer empty [blocking]
ES0 = 0;
// disable UART interrupts
i = readIndex - readCount;
// back up by readcount
if (i < 0)
// fix value if out of range
i+=READ_BUFFER_SIZE;
theChar = readBuffer[i];
// get character from read buffer
readCount--;
// one less character in the buffer
ES0 = 1;
// enable UARt interrupt
return theChar;
// return the character
}
//----------------------------------------------------------------------------//
// writec()
//
// The writec() function is the lowest level function which allows access
// to the write buffer. It writes one character to the write buffer.
//
// Note that writec will wait if the write buffer is full. This is usually
// desired to prevent the write buffer from overflowing.
//
// writec is a driver function and is closely integrated with the ISR.
// UART interrupts are temporarily disabled while the buffer is updated to
// prevent the ISR from modifying the buffer and counter values during
// processing. That would be bad.
void writec(char theChar)
{
// wait here if full [blocking]
while (writeCount >= WRITE_BUFFER_SIZE);
ES0 = 0;
// disable UART interrupts
writeBuffer[writeIndex] = theChar; // put character in buffer at writeIndex
writeIndex++;
// increment index
if (writeIndex >= WRITE_BUFFER_SIZE)// fix if out of range
writeIndex = 0;
writeCount++;
// one more character in buffer
if(TX_Idle)
// if transmitter idle flag set
{
TI0 = 1;
// force TX interrupt
TX_Idle = 0;
// idle no more
}
ES0 = 1;
// enable UART interrupts
Rev. 1.1
33