More PIC Applications:Advantages of C Programming

Advantages of C Programming

The C compiler converts the program into PIC 16-bit machine code. Most of these C statements translate into more than one machine code instruction. This can be confirmed by studying the list file, which is produced by disassembling the machine code.

The pseudocode for the temperature controller above (Program 13.2) can probably be more easily translated into C than assembly language. For example, the conditional control operations defined using IF.THEN statements will translate directly, whereas, in assembler, they have to be implemented by suitable combinations of ‘Bit Test and Skip’ with ‘Goto’ or ‘Call’. The comparison of the ‘average temperature’ with the set values can be done in one statement in C, but in assembler needs a subtract or compare prior to a bit test, which is much more complicated. On the other hand, checking bit inputs is not as easy in C as in assembler, as ANSI C contains no individual bit operations. Bit status in a register has to be checked by using a logical or numerical range check.

There are many references on C programming. To program a microcontroller in C, only the basic set of statements and simple data structures will probably be needed, so if the reader has some knowledge of C already, using it to develop PIC applications should not be too difficult. For further details, see Programming 8-Bit PIC Microcontrollers in C with Interactive Hardware Simulation by this author (Newnes 2008). This uses the CCS C compiler, which has a complete set of ready-made functions that simplify the C code, especially I/O handling and mathematical functions.

1. (a) What interfacing modifications are recommended for the LM35 temperature sensor if the connections are over 1 m long? (4)

(b) For the TEMCON2 system, calculate the output of the LM35 sensor at 25oC, and

the decimal value that would be found in the ADRESL on completion of an A/D conversion of this input, if the result is right justified. (4)

2. State one advantage of (a) the relay output and (b) the FET output as used in the temperature controller. (4)

3. Describe briefly how a multiplexed seven-segment LED display works, and its advantage in terms of I/O requirements. (4)

4. Suggest two reasons why the PIC 16F818 would be preferred over the 16F84A in a temperature control application. (4)

5. Compare PIC assembler and ANSI C programming, outlining the advantages of each. (5)

1. Devise an alternative keypad scanning routine to that in Program 13.1 using the rotate instruction, such that the binary value for the keys 0e9 are stored in a suitable register.

2. Design and implement the fully functional program for the temperature controller based on the pseudocode provided in Program 13.2. The user will enter an upper and lower temperature limit, and set the controller to run mode, where the outputs are operated to maintain the temperature between those limits. The system should tolerate a fault in one sensor which puts the output outside the normal operating range. Develop a full design and performance specification for the controller. Test by simulation.

3. Design a temperature-controlled enclosure with heaters, fan and vent, which will allow a fully functioning temperature control program to be tested. Investigate the design of an interface for the fan sensor, so that the fan speed could be controlled by PWM with feedback. Investigate the set-up required to use the PWM output of the 16F887, and redesign the hardware to connect the fan to a PWM output.

4. Implement the minimal temperature controller proposed above using the 12F675 chip, operating as specified in Table 13.1. Create a schematic, simulate the application (interactively if possible), design a layout, implement and test.

5. Study relevant C programming references and the Microchip manual ‘MPLAB C18 C Compiler, Getting Started’, and modify the program BIN1.C such that the output can be stopped, started and reset by push-button inputs at RA0 and RA1. Why is reading the inputs more difficult in C?

Leave a comment

Your email address will not be published. Required fields are marked *