Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Program Design, Build and Test on Target

EE2003 - Microprocessors
5/12/2013 University of East London Patrick Vieira

EE2003 Module Leader: Dr. Snailum

Table of Contents
Introduction ............................................................................................................................................ 2 Design...................................................................................................................................................... 2 Specification ........................................................................................................................................ 2 State Transition Diagram .................................................................................................................... 3 Flowchart ............................................................................................................................................ 5 Build ........................................................................................................................................................ 7 Program Code ..................................................................................................................................... 7 Conclusion ............................................................................................................................................. 14

Introduction
The microcontroller is the little brother of the very versatile CPU containing microprocessor. The microcontroller is mainly used for low power systems and contains not only the CPU, but the program memory, the random access memory and the input/output ports as well; All of which need to be supplied separately for its big brother. Microcontrollers are used everywhere ranging from the home: Ovens Printers Radios Tvs

To the automobile: Engine Control Radio system Air Bag Automation Window Control

To your personal belongings: Oystercards & ID Cards Wireless Key Locks Intercom opener Mobile Phones

As it can be seen, microcontrollers are an integral part of everyday life and to use them, they first need to be programmed. In this report, a microcontroller will be programmed all the way from design to building it using our beloved program MPLAB and testing on the actual microcontroller PIC 18F87J11.

Design
For a program to be designed, the employer must first send the programmer a document which demonstrates what they want to happen within the program. For this assignment, both a specification and a State Transition Diagram (STD) are provided:

Specification
A house has 2 rooms each of which have a PIR motion sensor. Each room has a security light. There is also a PIR sensor outside the front door, which becomes active when it is dark outside. There is a security light outside the front door. The security lights are switched on, and an audible alarm sounds under certain conditions. If the outside PIR is triggered (i.e. it is dark, and motion is detected), an outside security light is switched on for 20 seconds. Any room sensor activated will trigger the light for (only) that room for

10 seconds. Both room sensors activated within 10 seconds will set off the audible alarm continuously. The only way to stop the audible alarm is to input a correct PIN. If the correct PIN is input, the alarm system is de-activated, and becomes dormant (ready for further burglar detections). If an incorrect PIN is entered, a first warning is given on an LCD panel. If a further incorrect code is entered, the alarm remains on, the outside light flashes, and a message is sent to the police station. Entering the correct code at any time that the audible alarm is on will deactivate the alarm. This specification is very clear as to what the assessor wants in their code and one could be designed solely from this piece of information.

State Transition Diagram


Very much like the written specification, the SDT was very clear. However, this document did not match the specification entirely well. Pieces of information which featured in the written specification were left out in the STD while a whole state transition was added in the STD. This is an unhelpful situation since it is not clear as to what the assessor really wants. Fortunately, this situation was quickly resolved upon discussion with the assessor the STD is the document to follow. The state called AlarmLatched is not mentioned in the written specification nor does the STD mention if the police are called or if the outside light flashes at this state. All other states and events in the STD are congruent with the written specification.

Outside LightsOn MovementS switchLightsOn 20SecElapsed switchLightsOff Dormant 10SecElapsed R1 LightOff 10SecElapsed R1 LightOff R2S R2 LightOn

R1S R1 LightOn

R1Activated

R2Activated

R2S allLightsOn alarmOn

R1S allLightsOn alarmOn

correctCode switchAllOff

AlarmOn

wrongCode giveWarning

tamperSwitchActivated latchAlarm

correctCode switchAllOff

Warning

wrongCode latchAlarm

AlarmLatched

correctCode switchAllOff

Key: inputs: R1S => room1 sensor, simulated on hardware by switch on RB0 active. R2S => room2 sensor, simulated on hardware by switch on RA5 active. MovementS =>movement sensor, simulated on hardware by analogue input on RA3 >= half full scale. CorrectCode simulates correct PIN input, simulated by RB0 active. WrongCode simulates incorrect PIN input, simulated by RA5 active. tamperSwitchActivated, simulated by analogue input on RA3 < half full-scale. outputs: LED D8 indicates Dormant state. D1 indicates R1Activated state. D2 indicates R2Activated state. D3 indicates AlarmOn state. D4 indicates Warning state. D5 indicates AlarmLatched state. D6 indicates OutsideLightsOn state. D7 may be used for any purpose. The LCD should indicate the state in words.

Flowchart
Normally, an employer would supply only a written specification. From then on, all diagrams and code stemming from that specification must be 100% congruent with each other, including state names. Since I was provided with a State Transition Diagram already, by that logic I would have to copy all of the state names up until code implementation. However, I believe that I would find the current state names on the STD slightly difficult to implement all the way to code and therefore I have changed them slightly on the flowchart to fit my preferences. I have made my flowchart using a free CASE tool called Edraw Mind Map 7. The apricot coloured shapes on s flowchart represent the states. In these state boxes are included LED outputs which are all exclusive of each other, which is why the shape used is one for data. Outside LightsOn is represented on the flowchart as outside, R1Activated and R2Activated are represented by room1 and room2 respectively & AlarmLatched is represented by latch. The other two state names are virtually the same as the STD. Even though the readings of the inputs are an actual procedure in the microcontroller, they are also represented as data on the flowchart just to demonstrate that they are in fact inputs. To aid in viewing, the different inputs have been colour coded: pink for input RA3, blue for button RB0 and green for button RA5. The only outputs missing from the flowchart are the LCD outputs. These were left out of the flowchart on purpose so that it could all fit on one page. These will be included and explained in the program code. This leaves the complex delays to be explained. The complex delays are a complicated procedure due to their nested loop configuration. In it, two bit hexadecimal numbers are decremented for a set number of cycles in order to culminate to 10 seconds. For each decrement, each buttons state is read once. The 20 second delay can be called from a sub routine which is set up in the same way as the 10 second delay without checking a buttons state.

Build
Subsequent to completing all six MPLAB introductions, I gained a good grasp on changing the LCD output, jumping from state to state using bit test functions and using the analogue to digital converters storage register. I initially had difficulty finding the LED register and understanding the meaning of set and clear with regards to the button inputs; however what was tough was coming up with an accurate time delay as I found the default ones more complicated than they had to be. Not only that, but there was only a 100ms delay and a 1s delay. I initially attempted to devise a formula for the number of cycles needed based on the supposed frequency of the microcontroller (8MHz according to the datasheet), however it didnt seem to work when implemented into code and tested. Luckily, after testing through trial and error, I devised a nested loop containing three decrementing functions all with temporary values of FFh. I did this with my five second delay sub routine and also with my ten second delay loop featured in room1 and room2. This made it so that I could change the default sub routines in MAIN.asm to make them simpler.

Program Code
;************************************************************ ;* Simulation Burglar Alarm using an LCD panel, 8 LEDs, * ;* two buttons and one analogue input * ;* * ;* Filename: * ;* alarmcode.asm * ;* Dependents: * ;* LCD.asm * ;* 18F87J11.lkr * ;* Apr 2013 PPV * ;* PIC18 Explorer. The following states are included * ;* with this code: * ;* 1. Dormant State (dormant) * ;* * ;* 2. Motion Detected in Room 1 (room1) * ;* * ;* 3. Motion Detected in Room 2 (room2) * ;* * ;* 4. Alarm Activated (alarm) * ;* * ;* 5. Warning: Input Correct Code (warning) * ;* * ;* 6. Alarm Latched: Idiot Detected (latch) * ;* * ;* 7. Motion Detected Outside (outside) * ;* * ;************************************************************ list p=18F87J11 #include p18F87J11.inc CONFIG FOSC = HSPLL CONFIG STVREN = OFF,XINST = OFF,WDTEN = OFF,CP0 = OFF,FCMEN = OFF,IESO = OFF CONFIG MODE = MM #define #define #define #define #define #define rafive_dir rafive rbzero_dir rbzero TRISA,5 PORTA,5 TRISB,0 PORTB,0 LATA,3 TRISA,3

;Push-button RA5 on PCB ;Push-button RB0 on PCB ; EEprom chip select ; EEprom chip select

EEPROM_CS EEPROM_CS_TRIS

EXTERN LCDInit, temp_wr, d_write, i_write, LCDLine_1, LCDLine_2 EXTERN Delay,Check,InitSPI

variables ptr_pos ptr_count temp_1 temp_2 temp_3 ARG1H ARG1L

UDATA_ACS RES 1 RES 1 RES 1 RES 1 RES 1 RES 1 RES 1

;address for AtoD result high ;add for AtoD result low

STARTUP CODE 0 NOP goto start NOP NOP NOP PROG1 CODE stan_table data data data data data data data data data data data data data data data data start bsf call OSCTUNE,PLLEN LCDInit ;Enable PLL ;Initialize the LCD " Outside " "Microchip PICDEM" " PIC18 Explorer " " Burglar Alarm: " " Patrick Vieira " " Dormant State: " "Nothing Detected" "Motion detected:" " in Room 1 " " in Room 2 " "Alarm Triggered:" " Input PIN Code " " WARNING: Input " " Correct Code!! " " Alarm Latched: " " Idiot Detected " ;0 ;16 ;32 ;48 ;64 ;80 ;96 ;112 ;128 ;144 ;160 ;176 ;192 ;208 ;224 ;240 ;table for standard code

bsf setf setf bcf bcf movlw movwf movlw movwf movlw movwf bsf bsf clrf clrf

WDTCON,ADSHR ANCON0 ANCON1 ANCON1,0 WDTCON,ADSHR B'10100100' TXSTA .255 SPBRG B'10010000' RCSTA TRISB,0 TRISA,5 TRISD LATD

;Shared SFR ;Make RA0 Analog input

;initialize USART ;8-bit, Async, High Speed ;9.6kbaud @ 40MHz

;make switch RB0 an Input ;make switch RA5 an Input ;make LEDs an Output

;*********************** BEGINNING CODE ************************** ;Introduction movlw .16 ;send "Microchip PICDEM" to LCD movwf ptr_pos call stan_char_1 movlw movwf call call call call .32 ptr_pos stan_char_2 delay_1s delay_1s delay_1s ;send " PIC18 Explorer " to LCD ;delay for display ;delay for display ;delay for display ;send " Burglar Alarm: " to LCD

movlw .48

movwf ptr_pos call stan_char_1 movlw .64 movwf ptr_pos call stan_char_2 ;************************************* ;Code to set up the A to D ;************************************* bsf WDTCON,ADSHR setf ANCON0 setf ANCON1 bcf ANCON1,0 bcf WDTCON,ADSHR clrf bsf ; ADCON0 ADCON0,0 ;send " Patrick Vieira " to LCD

;Shared SFR ;Make RA0 Analog input

;turn on A/D ;Fos/32 left justified (Active) ;Fos/32 right justified (Disabled)

movlw b'00111010' movlw b'10111010' movwf ADCON1 call delay_5s

;------------------ Dormant State ---------------------------dormant call call call btfss bra btfss bra delay_100ms delay_100ms delay_100ms rbzero $-2 rafive $-2

;Wait for Button RB0 to be released ;Wait for Button RA5 to be released

movlw 0x80 movwf LATD movlw .80 movwf ptr_pos call stan_char_1 movlw .96 movwf ptr_pos call stan_char_2 ;wait for key press waitloop btfss goto btfss goto bsf btfsc bra btfsc goto goto rbzero room1 rafive room2 ADCON0,GO ADCON0,GO $-2 ADRESH,7 outside waitloop

;LEDs will display 80h in binary: 10000000 ;Display " Dormant State "

;Display "Nothing Detected"

;Checks if RB0 has been pressed ;If it has, go to Room 1 ;Checks if RA5 has been pressed ;If it has, go to Room 2 ;Start Analogue to 10 bit Digital conversion ;Ensures conversion has completed ;Checks value of most significant bit (MSB) ;If value of MSB is 1, uC skips to "outside" state ;If value of MSB is 0, uC loops back to loop

;------------------ Motion Detected in Room 1 ---------------------------room1 call delay_100ms call delay_100ms call delay_100ms btfss rbzero ;Wait for Button RB0 to be released bra $-2 movlw 0x01 movwf LATD movlw .112 movwf ptr_pos call stan_char_1 ;LEDs will display 01h in binary: 00000001 ;Display "Motion detected "

movlw .128 movwf ptr_pos call stan_char_2 ;wait for key press movlw movwf movlw movwf movlw movwf looproom1 btfss goto decfsz goto movlw movwf decfsz goto movlw movwf decfsz goto goto 0xFF temp_1 0xFF temp_2 0xFF temp_3 rafive alarm temp_1,F looproom1 0xFF temp_1 temp_2,F looproom1 0xFF temp_2 temp_3,F looproom1 dormant

;Display "

in Room 1

"

;Checks if RA5 has been pressed ;Go to alarm

;Go to dormant after 10 seconds

;------------------ Motion Detected in Room 2 ---------------------------room2 call delay_100ms call delay_100ms call delay_100ms btfss rafive ;Wait for Button RA5 to be released bra $-2 movlw 0x02 movwf LATD movlw .112 movwf ptr_pos call stan_char_1 movlw .144 movwf ptr_pos call stan_char_2 movlw movwf movlw movwf movlw movwf looproom2 btfss goto decfsz goto movlw movwf decfsz goto movlw movwf decfsz goto goto 0xFF temp_1 0xFF temp_2 0xFF temp_3 rbzero alarm temp_1,F looproom2 0xFF temp_1 temp_2,F looproom2 0xFF temp_2 temp_3,F looproom2 dormant ;LEDs will display 02h in binary: 00000010 ;Display "Motion detected "

;Display "

in Room 2

"

;Go to alarm if RB0 is pressed

;Go to dormant after 10 seconds

;------------------ Alarm Activated -------------------------------alarm call delay_100ms call delay_100ms call delay_100ms btfss rafive ;Wait for Button RA5 to be released bra $-2

btfss rbzero bra $-2 movlw 0x04 movwf LATD movlw .160 movwf ptr_pos call stan_char_1 movlw .176 movwf ptr_pos call stan_char_2 loopalarm btfss goto btfss goto bsf btfsc bra btfss goto bra rbzero dormant rafive warning ADCON0,GO ADCON0,GO $-2 ADRESH,7 latch loopalarm

;Wait for Button RB0 to be released

;LEDs will display 04h in binary: 00000100 ;Display "Alarm Triggered:"

;Display " Input PIN Code "

;Go to dormant if RB0 is pressed ;Go to warning if RA5 is pressed

;Loop back to detect required input (forever)

;----------------- Warning: Input Correct Code -----------------warning call delay_100ms call delay_100ms call delay_100ms btfss rafive ;Wait for Button RA5 to be released bra $-2 movlw 0x08 movwf LATD movlw .192 movwf ptr_pos call stan_char_1 movlw .208 movwf ptr_pos call stan_char_2 loopwarning btfss goto btfss goto bra rbzero dormant rafive latch loopwarning ;LEDs will display 08h in binary: 00001000 ;Display " WARNING: Input "

;Display " Correct Code!! "

;Go to dormant if RB0 is pressed ;Go to latch if RA5 is pressed

;---------------- Alarm Latched: Police Alerted -------------------latch call delay_100ms call delay_100ms call delay_100ms btfss rafive ;Wait for Button RA5 to be released bra $-2 movlw 0x10 movwf LATD movlw .224 movwf ptr_pos call stan_char_1 movlw .240 movwf ptr_pos call stan_char_2 looplatch btfss rbzero goto dormant bra looplatch ;LEDs will display 10h in binary: 00010000 ;Display " Alarm Latched: "

;Display " Idiot Detected "

;Go to dormant is RB0 is pressed ;Loop back (forever)

;------------------ Movement Detected Outside --------------------outside call delay_100ms call delay_100ms call delay_100ms movlw 0x20 movwf LATD 00100000 movlw .112 movwf ptr_pos call stan_char_1 movlw .0 movwf ptr_pos call stan_char_2 call call call call goto delay_5s delay_5s delay_5s delay_5s dormant ;Go to dormant after 20 seconds ;LEDs will display 20h in binary: ;send "Motion detected " to LCD

;send "

Outside

" to LCD

;************************** SUB-ROUTINES ************************** ;****************************************************************** ;****************************************************************** ;----Standard code, Place characters on line-1-------------------------stan_char_1 call LCDLine_1 ;move cursor to line 1 movlw .16 ;1-full line of LCD movwf ptr_count movlw UPPER stan_table movwf TBLPTRU movlw HIGH stan_table movwf TBLPTRH movlw LOW stan_table movwf TBLPTRL movf ptr_pos,W addwf TBLPTRL,F clrf WREG addwfc TBLPTRH,F addwfc TBLPTRU,F stan_next_char_1 tblrd *+ movff TABLAT,temp_wr call d_write decfsz ptr_count,F bra stan_next_char_1 movlw movwf btfss goto movlw movwf btfss goto return ;----Standard code, Place characters on line-2-------------------------stan_char_2 call LCDLine_2 ;move cursor to line 2 movlw .16 ;1-full line of LCD movwf ptr_count movlw UPPER stan_table movwf TBLPTRU movlw HIGH stan_table movwf TBLPTRH movlw LOW stan_table movwf TBLPTRL "\n" TXREG TXSTA,TRMT $-2 "\r" TXREG TXSTA,TRMT $-2

;send character to LCD ;move pointer to next char ;move data into TXREG ;next line ;wait for data TX ;move data into TXREG ;carriage return ;wait for data TX

movf addwf clrf addwfc addwfc

ptr_pos,W TBLPTRL,F WREG TBLPTRH,F TBLPTRU,F

stan_next_char_2 tblrd *+ movff TABLAT,temp_wr call d_write decfsz ptr_count,F bra stan_next_char_2 movlw movwf btfss goto movlw movwf btfss goto "\n" TXREG TXSTA,TRMT $-2 "\r" TXREG TXSTA,TRMT $-2

;send character to LCD ;move pointer to next char ;move data into TXREG ;next line ;wait for data TX ;move data into TXREG ;carriage return ;wait for data TX

return ;---------------------------------------------------------------------;------------------ 100ms Delay -------------------------------delay_100ms movlw 0x64 movwf temp_1 movlw 0x62 movwf temp_2 movlw 0x09 movwf temp_3 dloop100 decfsz bra movlw movwf decfsz bra movlw movwf decfsz bra return temp_1,F dloop100 0x64 temp_1 temp_2,F dloop100 0x62 temp_2 temp_3,F dloop100

;---------------- 1s Delay ----------------------------------delay_1s movlw 0xFF movwf temp_1 movlw 0xFF movwf temp_2 movlw 0x33 movwf temp_3 dloop1s decfsz bra movlw movwf decfsz bra movlw movwf decfsz bra return temp_1,F dloop1s 0xFF temp_1 temp_2,F dloop1s 0xFF temp_2 temp_3,F dloop1s

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; delay_5s movlw movwf movlw movwf

0xFF temp_1 0xFF temp_2

movlw 0xFF movwf temp_3 dloop5s decfsz bra movlw movwf decfsz bra movlw movwf decfsz bra return end temp_1,F dloop5s 0xFF temp_1 temp_2,F dloop5s 0xFF temp_2 temp_3,F dloop5s

A lot of redundant information has been taken out such as the IF statement regarding another PIC microcontroller, PIC 18F8722. Having this program being able to be cross compatible can be a beneficial thing; however for this purpose it is unneeded.

Conclusion
This project was a huge success. The program built successfully and fit all the criteria when uploaded onto the microcontrollers ROM. The delay loops were accurate to within one second of the target time, the microcontroller skipped to the correct state when the given event took place and the correct outputs featured in the desired stage of the implementation. If I had the chance to repeat this project, I would want to implement delays with perfect timing both with delays through using a proven formula and by using timers and interrupts. Another thing which would be interesting to implement would be making the seventh LED (which was not used in this project) flash to demonstrate the alarm sounding aloud. Timers and interrupts would be a great substitute for the 10 second delay at rooms one and two. While the timer ticks away, the microprocessor can constantly keep checking the buttons to see if any have become Clear. This makes the system more efficient in checking for bits whenever a time limit is required to be implemented. The flashing seventh LED implementation would be tougher to accomplish. Every time the program would change state, the LED would stop flashing for a moment. Since there is the string which makes the user let go of the button in order to progress further in the program, the only way which I can see it working is if there were two more buttons on the microcontroller to simulate the correct and incorrect PIN inputs. This project has challenged my programming skills very far and I cannot wait for another so I can develop my skills and be pushed further.

Aspect of Work

Max

1st Mark

2nd Mark

Marker's Comments

Flowchart

20

Program code (documentation) (including labels, layout, comments etc)

40

Program execution (states, inputs/outputs)

30

Demonstration Completed By Student

Report

10

Total

100

Students Comments: Fun piece of coursework, especially the coding section.

Lecturers Overall Comments:

nd

Markers Comments:

Date:

You might also like