Using an Interrupt Function in a C Program

The code fragments that must be included in an interrupt system include the following: 1. Code that covers or uncovers interrupts. This can be achieved through setting the 1 and X bits in the CCR, thus executing the command: ORCC #%00010000. 2. Code that defines system vectors that handles interrupts, allowing the CPU to process the requested task. 3. Code that permits precise interrupts. This can be achieved through declaring a function with the type identifier @interrupt; this function may not return a value. And 4, Code that resets any uncompleted interrupt.

The interrupt vector location for the SCI0 in the MC68HC12B32 microcontroller is $FFD6, $FFD7 (Freescale Semiconductor, Inc. 71). However, the interrupt vector is changed to $0B16 in order to elevate to the highest 1 bit for interrupt handling.

In the SCI0 sub-system, the registers must be programmed in such a way that it contains interrupt vectors that enable the routine SCI0 to receive interrupt. Even so, the interrupt vectors can be defined in a separate program file (Kneen). At the C level, an interrupt function is used; a function such as mychar () as illustrated in the example program below is tied to the interrupt vector for the SCI0 receive vector. When the SCI0 receive interrupt occurs, the function mychar () is called to store a character into a buffer. Once an SC10 interrupt is received, it can be cleared by incorporating the SC1SCR1 statement within the @interrupt function. Consequently, with a circular buffer the input or store routine will know if the buffer is full when the read pointer is greater than or equal to the size of the buffer. On the other hand, the output routine will know when the buffer is empty if the read and write buffer pointers are equal.

Program Analysis and Design

The program below shows the use of the interrupt function in a C program, the function mychar () is linked to the interrupt vector for the SCI accept interrupt. When an SCI receive interrupt occurs, mychar () is called to place the data in a circular buffer, defined to be a 512-byte array of characters. Mychar () function signals the receipt of char to the C canonical function acquirech () that receives a character through increasing the buffer write pointer, bottomr. Consequently, the main () function sets up the SCI parameters and start the iteration of putch (acquirech ()) which receives and transmits characters from and/or to the SCI.

The program also incorporates the @interrupt function which is only called from the interrupts, though, not directly from user application code (Cosmic Software 51). This program is compiled, assembled and linked using the cosmic compiler.

C Program

#include /* header */

#define TDRE 0x80 /* transmit ready bit */

/* approve interrupts */

#define myi () _asm (“andcc #$EFn”)

/* variable declaration */

Const int SIZE = 512 /*buffer size in bytes*/

char buffer [SIZE]; /* this is the buffer that stores characters expected from SCI */

char *topc; /* read pointer */

char *bottomr; /* write pointer */

/* Driver function. Sets up the SCI and starts an endless loop of SCI receive transmit */

void main ()

{

topc = bottomr = buffer; /* initialize buffer pointers */

SC1BDL = 26; /* initialize SCI */

SC1CR2 = 0x2c; /* parameters for interrupt */

myi (); /* authorize interrupts */

for (;;) /* loop */

putch (acquirech ()); /* get and put a char */

}

/* this function receives a character. It iterates until a character is received from the input */

char acquirech ()

{

char y; /* character to be returned */

while (topc == bottomr); /* mychar () has not received a new character from the SCI, loop continues */

c = *topc++; /* if not; get the received char pointed at by topc and increase read pointer */

if (topc >= &buffer [SIZE]) /* check for buffer read overflow */

topc = buffer;

return (y);

}

/* Send a char to the SCI*/

void putch (char y)

{

while (! (SC1SR1 & TDRE)) ; /* wait for READY bit */

SC1DRL = y; /* send the character */

}

/* Interrupt handler. */

@interrupt void mychar ()

{

SC1SR1; /* clearing interrupt */

*bottomr++ = SC1DRL; /* increment the buffer write pointer */

if (bottomr >= &buffer [SIZE]) /* check for write buffer overflow */

bottomr = buffer;

}

Works Cited

  1. Cosmic Software. C Cross Compiler User’s Guide for Motorola HC12/HCS12 Evaluation Kit. 4.5. (2004). 17 – 51
  2. Freescale Semiconductor, Inc.”M68HC12 Microcontrollers”. Motorola Semiconductors. 9.6. (2004). 69 – 71.
  3. Kneen, John. Cosmic Compiler. Learning Materials. 2005.

Cite this paper

Select style

Reference

StudyCorgi. (2022, March 29). Using an Interrupt Function in a C Program. https://studycorgi.com/using-an-interrupt-function-in-a-c-program/

Work Cited

"Using an Interrupt Function in a C Program." StudyCorgi, 29 Mar. 2022, studycorgi.com/using-an-interrupt-function-in-a-c-program/.

* Hyperlink the URL after pasting it to your document

References

StudyCorgi. (2022) 'Using an Interrupt Function in a C Program'. 29 March.

1. StudyCorgi. "Using an Interrupt Function in a C Program." March 29, 2022. https://studycorgi.com/using-an-interrupt-function-in-a-c-program/.


Bibliography


StudyCorgi. "Using an Interrupt Function in a C Program." March 29, 2022. https://studycorgi.com/using-an-interrupt-function-in-a-c-program/.

References

StudyCorgi. 2022. "Using an Interrupt Function in a C Program." March 29, 2022. https://studycorgi.com/using-an-interrupt-function-in-a-c-program/.

This paper, “Using an Interrupt Function in a C Program”, was written and voluntary submitted to our free essay database by a straight-A student. Please ensure you properly reference the paper if you're using it to write your assignment.

Before publication, the StudyCorgi editorial team proofread and checked the paper to make sure it meets the highest standards in terms of grammar, punctuation, style, fact accuracy, copyright issues, and inclusive language. Last updated: .

If you are the author of this paper and no longer wish to have it published on StudyCorgi, request the removal. Please use the “Donate your paper” form to submit an essay.