A Case Study Part 2 (PIC Microcontroller)

PI output

Subroutine SPI_WRITE is similar to that described in Program 12.1 but transmits two serialized data streams simultaneously. The datum in DATA_OUT_L is sent via RA3 whilst that in DATA_OUT_H is sent out via RA0. A common clock is used.

Before considering the coding for the three processes, we will briefly look at the initialization code common to the entire software system. The function of this startup code is: Default duration setting

To place a default value for the time-out in location 0 of the Data EEPROM. The address of this cell is in the special/test configuration area at 2100b and the de directive is used to specify the load time data.

The value of 10 as shown means that a freshly programmed PIC will default to a 10 minute count down. This value can subsequently be altered using the Set-time process described below. Vectors

To initialize the Reset vector at 000b to point to MAIN and Interrupt vector at 004b to point to ISR. Port setting

To make Port A[4:0] and PortB[7:5] outputs and all other lines inputs. Timer 0 setting

To set up the Prescale ratio to 1:64 and Timer 0 clock source to internal. The Timer 0 interrupt is also enabled.

Process select

To check the state of the DIAG and SETT switches to choose either the Diagnostic or Set-time processes. If neither switch is closed the normal Main process is entered.

The Set switch is named SETT in the code, as set is a Microchip-compatible assembler directive.


4set is the same as the equ directive except that the assigned value may be subsequently altered by other set directives.

Program 16.3 The initialization code.

Program 16.3 The initialization code.

If the DIAG switch is closed when the PIC comes out of reset then the code transfers to the subroutine DIAGNOSTIC.

The Diagnostic process aims to exercise the various peripheral devices interfaced to the process in order to verify in a reproducible manner the status of the interconnection and the devices themselves.

Switches

Five switches are input via PortB. By checking each switch in turn and if closed lighting one of the LEDs or sounding the buzzer both switches and the listed output devices are tested. The DIAG switch is of course verified by moving the system into this process and the Reset switch is tested by initiating the startup process.

If there were more switches than output devices then either combinations of the latter could be activated or else one or more segments in the numerical display pushed into service.

Program 16.4 The Diagnostic process.

Program 16.4 The Diagnostic process.

LEDs and buzzer

The static output devices are tested in conjunction with the switch test listed above. Of course the failure of a LED to light or buzzer to sound may be due to either the input or output device circuit. Determining which is easily accomplished by using a voltmeter or logic probe. Also all LEDs are illuminated during the Set-time process.

Display

Each of the display devices is exercised by lighting one segment moving on once each second in an endless loop. This is implemented by generating a walking unary patterntmpA137_thumb[2]

sent out to the output subroutine SPI_WRITE once each time the file register NEW_SEC is non zero. NEW_SEC is incremented in the Timer0 interrupt-handling routine each time the Seconds count is incremented and cleared in the Diagnostic procedure code. This acts as a ratchet giving only one new display each second.

The Set-time process is entered when the SETT switch is closed whenever the processor comes out of reset. Its function is to allow the operator to change the contents of the EEPROM Data module location 00h to any value up to 99. This location holds the initial count-down value used by the Main process to determine the length of the procedure.

The strategy behind the coding shown in Program 16.5 is to initialize the Second count to 99 and let it decrement at a 1-second rate as determined by the foreground ISR.

Program 16.5 The Set-time process.

Program 16.5 The Set-time process.

The value of SECOND is sent to the Display subroutine each time the ISR sets the flag file register NEW_SEC to a non zero value, that is once per second. DISPLAY clears NEW_SEC so the net effect to update the display only once each second. Each second the SETT switch is checked and when open the state of the Seconds count is transferred to the EEPROM Data module at UPDATE using the EE_PUT subroutine of Program 15.2.

The Complete background system flow chart is shown in Fig. 16.3. This shows in outline the decision flow taken after a reset and in detail the Main process. Although this looks rather complex, it may be broken down into five phases with corresponding coding shown in Program 16.6.

The Main process.

Fig. 16.3 The Main process.

Preamble

On reset if neither SETT or DIAG switches are closed the Main procedure code is entered at MAIN_PROC. This reads the initial value of the countdown period from EEPROM location 00h and initializes the count chain. The green lamp is illuminated and other lamps and buzzer are turned off.

Countdown

The Countdown phase continually displays the Minute count – updated behind the scenes by the ISR. The green lamp remains illuminated as long as this display does not drop below 03 This phase is complete whenever the count drops below 3 minutes or else the STOP switch is closed. In the latter case all displays are blanked and the PIC is put into its Sleep state.

In all situations except where the STOP command is issued the Minute count is displayed at 1-minute intervals. The routine at REPEAT checks the Second count and if zero the loop is repeated – that is once per minute. The simpler alternative of continually refreshing the 7-segment readouts gives an inferior display as the data being serially shifted at frequent rate may partially illuminate segments which are nominally off. In addition, repeating the loop each minute eases the task of sounding the buzzer once only when the Minute count drops to two and one.

Two minutes to go

When the display istmpA141_thumb[2]the amber lamp is illuminated This is timed using the NEW_SEC variable. Again the loop can be prematurely exited if the STOP switch has been closed.

One minute to go

When the display istmpA142_thumb[2]the loop diverts to illuminate the red lamp. The buzzer is sounded for two seconds; implemented in code as two 1-second buzzes.

Timed out

When the Minute count reaches zero, not only istmpA143_thumb[2]displayed but also the buzzer sounds continually. This cacophony can only be silenced by pressing the Stop switch – or by resetting and starting again. As in previous situations when the Stop switch is closed, all displays are blanked out and the PIC is placed in its Sleep state.

Once the source code has been assembled and where possible simulated it can then be burnt into the PIC’s Program store. In the first instance only the diagnostic software and associated tasks need be programmed in order to check the target hardware. The precise details will depend somewhat on the PIC programmer being used and its associated software.

Program 16.6 The Main process.

Program 16.6 The Main process.

Program 16.6 The Main process

Program 16.6 The Main process

The screen shot shown in Fig. 16.4 shows the situation where the Microchip Picstart Plus development programmer is used in conjunction with the MPLAB IDE. Communication with the host computer is via a RS-232 serial port and contact is made from the Picstart Plus menu. The right-hand window allows the operator to set up the Configuration Bits (fuses), shown in the left-hand window. Once this is set up, the operator can Blank out, Read from, Program or Verify the contents of the EPROM or EEPROM Program store is the same as that produced by the last assembly process. This process can only be carried out where the Code Protect has not been turned on. Once this is the case, it is irreversible and neither Program or Verify tasks can be carried out.

The middle window shows the status of the Program or Verify process. As shown here, it is announcing that it has completed the task up to 03FFb and is reporting success. The complete process takes less than a minute for the PIC16F84 and the 252 program words that this case study software generates.

Programming the PIC from MPLAB.

Fig. 16.4 Programming the PIC from MPLAB.

With F series PICs, the programming process maybe repeated without any preparation of the Program store up to 100 times without any deterioration of the Flash EEPROM. C series PICS,6 such as the PIC16C74, have EPROM Program stores. Where the target has a quartz window it must be erased using a suitable UV-based EPROM eraser for approximately 20 minutes before programming, unless code is being added to previously unprogrammed memory. Although quartz windowed devices are necessary for development purposes, they are relatively expensive. Thus cheaper windowless version are used for production purposes, and are called One-Time Programmable (OTP) since they cannot subsequently be erased. Part numbers which are windowed are usually identified by JW postfix; for example, PIC16C74B-20/JW is a 20 MHz ceramic windowed PIC16C74B part and and the PIC16C74B-4/P is a 4 MHz OTP version in a 40-pin plastic DIL package. Ensure that you obtain the correct device!

The hardware and software circuits have been presented here as a simple illustrative case study to integrate many of the techniques described in the body of the text. If you decide to build your own version,files, C coding, PCB, comparison with a Motorola 68000 MPU version and other ideas for experimentation, which you are welcome to contribute are given on the associated Web site detailed in the Introduction. Good luck!

5 With 772 instructions left unused, the PIC18F83 with a Program store of 512 words could be used as the target process with a small reduction in cost.

6The exception being the obsolete PIC16C83/4 which also has an EEPROM Program store.

The Microchip PICSTART Plus programmer.

Fig. 16.5 The Microchip PICSTART Plus programmer.

Next post:

Previous post: