With such as volatile which well need in order to do

This preview shows page 17 - 20 out of 38 pages.

with, such as volatile, which we’ll need in order to do I/O. 4 The astute student will notice that the binary pattern representing 130 and -126 are identical. It’s only the interpretation of the binary pattern that changes. 5 You didn’t forget the null which terminates all strings, did you?
17
3.4.2 Access to IO Devices In the PIC microcontroller family, IO devices appear as memory locations. The PIC microcontroller, with only a few notable exceptions, has no registers or devices which are wider than 8 bits, so all devices must be accessed as either bytes or the individual bits of which they are made. We can access data in bytes using either the char or unsigned char data type, and the MPLAB C18 compiler provides means to manipulate individual bits through the use of bitfield support and header files which define the registers in terms of their individual, named, bits. For example, we may, at some point, which to clear a flag in a register. For the sake of the example, let us take the INTCON register (Interrupt Control register) and a particular flag, the INT0IF (Timer 0 Interrupt Flag, which indicates that a Timer 0 overflow has occured). We want to clear this flag. INTCON is defined as a variable of type unsigned char in the header file “p18f452.h” as extern volatile near unsigned char INTCON; (The header file also specifies the address of the INTCON so that we can just treat it as a variable. Note that this uses constructs which are not ANSI C compatible.) Without using bitfields, we would clear the INT0IF flag by the following line of C code. INTCON = (INTCON & 0xFD); Note that this is a bit ugly and also a bit problematic for maintenance, containing, as it does, a hard-coded constant. We can reduce the problem indicated by the hard-coded constant by placing the constant in a header file (the problem is still there, but now it’s somebody else’s problem , if we aren’t put in charge of header file maintenance). A more severe problem is that the compiler may have difficulty imple- menting this in an efficient way. To see why this is so, consider how we’d do this in assembly language, using the single line: bcf INTCON,INT0IF (The labels INTCON and INT0IF are defined in the header file “p18f452.inc” which you can include using the line include p18f452.inc near the beginning of your program, if you recall. 18
The compiler cannot read your mind. What it sees is that you want to take the current value of INTCON, and it with a constant, and stuff the result back into INTCON. It might do this with the sequency of instructions movf INTCON,w andlw 0xFD movwf INTCON It is left to the student to determine the performance penalty and the code penalty (number of extra clock cycles and code bytes to perform the operation) incumbent on this approach. How can we get around this? The answer uses bit fields and “anonymous structures within unions” to allow the C programmer to cause the compiler to generate the code desired. The C header file also contains a declaration of a structure as follows: extern volatile near union { struct { unsigned RBIF:1; unsigned INT0F:1; unsigned T0IF:1; unsigned RBIE:1; unsigned INT0E:1;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture