PIC16 Programming C Beginning Start
● Simple program and test circuit● Variables, looping, and decisions
● SIREN program
Simple Programming C in Pic.
Microcontroller programs contain three main features:
● Sequences of instructions
● Conditional repetition of sequences
● Selection of alternative sequences
The following basic programs show how these processes are implemented in CCS C. The program in Listing 2.1 is a minimal program that simply sets the bits of an 8-bit port in the 16F877 to any required combination.
Pic C Programming Creation.
The development process was introduced in Part 1, and further details are provided in
Appendices A, B, and C. Briefly, the program project is created as follows:
1. Assuming that MPLAB and CCS C compiler are installed, create a folder for the project files, and an MPLAB project called OUTNUM. Copy the MCU header file 16F877.h from the CCS header file folder to the project folder.
2. Write the program (OUTNUM.C) in the source code edit window of MPLAB, referring to the compiler manual for the correct syntax, and save it in the project folder. Assign the source code and header file in the project window.
3. Build the project (compile and link all files) to create OUTNUM.COF. Correct any syntax and linker errors.
4. Run the program in MPSIM simulation mode. Use the source code debugging window to trace the program execution and the watch window to track the CPU variables. Correct any logical errors.
5. Optionally, the program can be tested in Proteus VSM, which once installed, can be selected from the debugger menu.
Pic C Programming Testing
The program could be tested by downloading to a suitable hardware target system, but it is preferable to debug it first in simulation mode, either in MPLAB or, preferably, in Proteus VSM. In the VSM schematic capture and cosimulation module ISIS, the target PIC is selected from the component library and placed on the schematic. The application file OUTNUM.COF previously created by the compiler is attached to it ( Figure 2.1 ) and the schematic saved in the project folder. When the simulation is run, the state of the
outputs is indicated by red and blue indicators. Although not absolutely necessary for program testing in simulation mode, a set of LEDs with their load resistors are attached to Port D, since these are required in the actual hardware to display the outputs ( Figure 2.2 ). No other circuit components or connections are required at this stage, since the simulation runs correctly without a clock circuit. In the real hardware, the clock circuit must be added and !MCLR input tied to V dd ( 5 V). Here, the clock frequency is set in the MCU properties dialog when the program is attached. To take advantage of the full debugging facilities of MPLAB, Proteus VSM can be run from within MPLAB by installing it in the debug tool menu. For this, a plug-in needs to be downloaded from www.labcenter.co.uk. When selected, the simulator runs in a VSM viewer window ( Figure 2.3 ).
Programming C In Pic Analysis
The main program contains just one statement, output_D(255) . This means output the number 255 10 as a binary code to Port D of the chip, setting all pins high (obviously, any number between 0 and 255 results in a corresponding output bit combination). All statements are terminated with a semicolon. This statement is a function call, which means the compiler gets the machine code for this operation from the standard set of built-in functions supplied with the compiler. This particular function is one of a set of library functions of the form output_x(n) , where x is the port number (A–E), and n is the output value (0–255). The general form of the C function is function_name(). Any information needed by the function, the function parameter(s), is inserted into the parentheses. The main program starts with the key words void main() and is enclosed between curly brackets, or braces, as they are officially known. All program blocks are enclosedThe preprocessor directive # include " 16F877A.h " instructs the compiler to include this processor-specific file at the top of the program. It contains labels for the registers in the selected MCU, so that the compiler knows where to store MCU control variables. Comments can be enclosed between slash/star (/*...*/) control characters or can follow a double slash (//), in which case the comment is terminated with a line return. The program header should contain as much information as possible to assist the user and
facilitate future modifications. Ideally, line comments should describe the effect of the
statement in the target system.
The meaning of the C program is independent of the layout on the page. Only the sequence of characters is significant to the compiler. However, in practice, the program source code should be arranged to make it as easy to understand as possible. Spaces or tabs can be used to indent each block (program level), and the open and close braces should be lined up in the same column so that the brace pairs can be matched up when checking the program. This makes subsequent source code debugging and modification
easier. The benefits of good layout become more obvious later, when more complex
programs are developed. By tradition, C source code is written mainly in lower case, with upper case used for certain key words.
No comments:
Post a Comment