Home

PIC Alarm Clock

image

Contents

1. Alarm Stack Alarm Modification Stack Daily Alarm 0x109 Daily Alarm Mod 0 180 Snooze Alarm 0x104 Snooze Alarm Mod 0184 Special Alarm 1 198 Special Alarm Mod 1 188 Special Alarm 30 17 Special Alarm Mod 30 Fe 0x180 0x200 Alarm Menu Pressing a from the default mode will take you to the alarm menu which gives the user access to the special alarms by adding one or reviewing the list and default nap and snooze lengths The alarm menu states on pages 26 27 is a series of passive states that display or prompt user input that takes place in the following active state These are really best seen in their state diagrams and explanations also see the Users Manual for an external view of how they work 5 19 08 Page 20 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Initialization After the boot menu occurs we are tasked with initializing registers and interrupts as well as resetting different systems Each system has a reset function to call to initialize its respective registers Register Initialization We must set the current state MASTER_STATE page to the default zero This also turns off the alarm ringing and makes us not snoozing see Super States on page 25 The delay state counter DelayCount page needs to be turned off which means negative The delay state register DelayState page itself is set to the default state for safety The switc
2. PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation State Diagram Page 2 W2sec e eee 7 4 ee 7 P antes Key i f Set time 4 PR Push and release i delay amp add HD Hold down i empty alarm 1 W wait for a certain time to A ES pass weee Qe o O rotary dialed i If none 1 1 A ae E i Y 28 EME 1 t time button escape 4 Settim beet d daily alarm button Lo a 1 1 y delay 1 a alarm others buttons i h i S set button 1 VA corresponding up down i button i N A i i Show Set N i i Current Alarm i EA i hh mm N iLOR A Note for each of these states if PRt occors go to state 00 HD s W 8 sec 19 Ifonce _ e ie es ie Etat E Show Set Next day to ring dddd 4 OB e Addornot F that day of the ey S Vi i 1B Prompt day of the week DDDD If certain days Go to next day of the week Day of week of the week If daily E ty Change which alarm out current one State 28 5 19 08 Page 27 of 64 PIC HEU QUIN Developer s Guide John Tooker PIC18F4550 Implementation State3F Opening Message Comes from Reset Goes to Default state State00 Display Opening message Description Idles until delay count expires State30 View Current Time hh mm escaping out of state where we don t want a pressed butt
3. PIC18 Alarm Clock MT asm 5 19 08 Page 50 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Add_MT Reset_MT Return Value Description Initializes registers gives initial time should not be on the first day Md 0000 Display_ day of the week Return Value Description Writes the day of the week to Display_Buf Special Notes day of the week Sun Mon Tue Wed Thu Fri or Sat Variables Used Source File ENEE 440 PIC18 Alarm Clock MT asm MT_MD_Change_q Arguments FSRO Description Uses the arrow keys to update certain registers treating them like hours dates minutes seconds Based on FSRO we can alter many different registers MT mm ss MT hh mm Ma dddd Daily Alarm hhmm Snooze defaults hh mm nap defaults hh mm Current alarm hh mm Current alarm dddd Special Notes A little complicated but it works Variables Used Source File ENEE 440 PIC18 Alarm Clock MT asm 5 19 08 Page 51 of 64 PIC CHEV QU Developer s Guide John Tooker PIC18F4550 Implementation MD_Change_q Description Similar to the previous function but only changes the day of the week the current day of the week Special Notes Variables Used Source File ENEE 440 PIC18 Alarm Clock MT asm MTMD_dq_IncDec_Date_High Arguments WREG compact BCD form Description Register pointed to by FSRO treated like the higher byte of a date FSRO WREG FSRO Special Notes WREG 1 0x01 10
4. A Part of what else needs O ae Pos i Description a dll display e States that are only active when called immediately change based on their rules How we get from stat e x to state y Figure 4 5 19 08 Page 25 of 64 5 19 08 State Diagram Page 1 PR Push and release HD Hold down W wait for a certain time to pass O rotary dialed t time button escape d daily alarm button a alarm others buttons s set button VA corresponding up down PIC HEU OLOMI Developer s Guide John Tooker PIC18F4550 Implementation hh mm button 8 AND OR 3 mr Automacially a SY go back Toggle Daily t Alarm off er E fF a geet Enter Nap Mode _ hhmm Automacially left go back handled d by snooze code lt PR VA 7 W 2 sec 9 6 W 2 sec next page 9 next page Show Current PR s amp MS 7 PR VA Time hh mm W 0 20 sec PRa amp IMS 7 HD d PR t 0E Review Add Set Snooze or Nap PR VA PR VA CA W 2 sec 0C Set Default PR t State 00 Figure 5 Set Current PRVA rn 11 Set Current Date dddd Set Current Day of Week DDD IHDt PRVA PR VA Show Daily Alarm hh mm 13 Set Daily Alarm hh mm HD a PRVA DA W 2 sec 1C Set Default Nap Length hh mm PR VA O PRt State 00 Page 26 of 64
5. Comes from State09 Goes to Yes or No State1A rEPt Repeat REVIEW EDIT LED is green SET ALARM LED green Description Idle while displaying message Display 5 19 08 Page 34 of 64 PIC HEU AQUI Developers Guide John Tooker PIC18F4550 Implementation State1A Yes or No repeat Comes from Repeat State0A Goes to Display Set State3B or Prompt Every Day State39 Displa Y n Yes or No pay REVIEW EDIT LED is green SET ALARM LED green If user pushed VA for yes continue to state 39 then 3A Description If user pushed VA for no add 4 to stack pointers FSR1 FSR2 effectively adding this alarm to the stack Register Notes Edits FSR1 and FSR2 within rules specified State3A Yes or No Every Day Comes from Every Day State39 Goes to Display Set State3B or Display Can t State2A Displa Y n Yes or No pay REVIEW EDIT LED is green SET ALARM LED green If user pushed VA for no continue to state 2A since we cannot have an alarm that both repeats AND does not ring every day originally we would continue to state OB Description where we would start the process of prompting which days of the week we want this alarm to ring If user pushed VA for yes add 4 to stack pointers FSR1 FSR2 effectively adding this alarm to the stack Register Notes Edits FSR1 and FSR2 within rules specified State2A Display Can t Comes from State3A
6. MASTER_STATE s value and add in the number of instructions that are between the push and the beginning of the actual code After this is done we just need to update the pushed PC value to the current PC value RETURN instruction 5 19 08 Page 23 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation States Figure 4 Figure 5 and Figure 6 contain state diagrams that completely outline the special workings of this processor All of the code pertaining to the states are in ENEE 440 PICO8 Alarm Clock Main asm Explanation States are stored in MASTER_STATE page The higher two bits are set if the alarm is currently ringing or if we are in snooze mode respectfully When either is high we execute super state code code that will happen despite which actual state we are in When snoozing we do not use any of the normal states When the alarm is ringing along with executing special code we also execute part of the code in state00 page so that we may see the current time but cannot leave this state The lower 6 bits are the normal state numbers seen in the top center of the state bubbles The super states are in Figure 4 as well as how to read the state diagram figures State List Format State Comes from Summary of which states can get to this one Goes to Summery of which states can be arrived at from this one What is on the LED s If not stated the colon is off If Display no
7. which 7Seg to write to jscii hex offset in JSCII table offset translated and stored at correct area in display buffer SEGMENTS_TO_DISPLAY area which 7Seg to write to jscii actual bits to be displayed Result Display_Buf area jscii Arguments Result Arguments Sub_P_set_L flag_reg register where interrupt flag is flag_bit bit in register which is the flag s_p sub priority 0 7 Start_Srvs_L s_p is 1 if service is needed flag_reg flag_bit is cleared turn off intrpt Sub_P_ex_L where_to Label Call to start service s_p sub priority 0 7 Write in order of priority highest first If currently executing will continue to execute interrupt if we need to start start Clears corresponding Start_Srvs_L bit when we start as well setting the In_Service_L bit Page 56 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Appendix E JSCII Table This is a set of characters mapped to program memory and are indexed by their hex value similar to how ASCII works The first 16 entries line up to the numbers O through F so translation of hex and BCD numbers has been efficienized Character Offset Character Offset Character Offset JSCII_O 0x00 JSCII_F OxOF JSCII_M 0x26 JSCII_1 0x01 JSCII_SP 0x10 JSCII_N 0x27 JSCII_2 0x02 JSCII_UN 0x11 JSCII_O 0x28 JSCII_3 0x03 JSCII_UP 0x12 JSCII_P 0x29 JSCII_4 0x04 JSCII_HY 0x13 JSCII_Q 0x2A JSCII_5 0x05 JSCII_DP 0x14 JSCII_R 0x2B JSCI
8. 08 PIC CHEV OLORI Developer s Guide John Tooker PIC18F4550 Implementation Timer 1 8 20 48 timer 2 9 Timer 2 20 Timer 3 20 Page 64 of 64
9. 0x10 1 0x99 10 0x90 Variables Used FSROL Source File ENEE 440 PIC18 Alarm Clock MT asm MTMD_dq_IncDec_Date_Low Arguments WREG compact BCD form Description Register pointed to by FSRO treated like the lower byte of a date FSRO WREG FSRO Special Notes See special notes in MTMD_dq_IncDec_Date_High Variables Used FSROL Source File ENEE 440 PIC18 Alarm Clock MT asm MTMD_dq_IncDec_Hour Arguments WREG compact BCD form Description Register pointed to by FSRO treated like in hour format FSRO WREG FSRO Special Notes See special notes in MTMD_dq_IncDec_Date_High Variables Used FSROL Source File ENEE 440 PIC18 Alarm Clock MT asm 5 19 08 Page 52 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation MTMD_dq_IncDec_MnSc Arguments WREG compact BCD form Description Register pointed to by FSRO treated like a minute or a second both behave the same FSRO WREG FSRO Special Notes See special notes in MTMD_dq_IncDec_Date_High Variables Used FSROL Source File ENEE 440 PIC18 Alarm Clock MT asm RE_intrpt Return Value Description Compares the current state of the rotary encoder with the previous recorded state Adjusts RE_POS higher if rotated clockwise lower if counterclockwise See page 17 Special Notes Should call on interrupt faster then a person can spin the dial Variables Used RE_state RE_POS Source File ENEE 440 PIC18 Alarm Clock RE asm RE_reset
10. Return Value QuerySwitchesll Arguments Return Value WREG nibble no switch pressed nibble not zero means that switch is pressed Description Checks the SPP port to see which switches are pressed First checks to see if any are pressed if so we allow the rightmost four switches to register responses this response is unique for each combination of those four switches being pressed using this we can see which switches are pressed Continue to next four switches We stop when two switches are identified even if there are more pressed The rightmost ones are identified first Special Notes Uses FSRO identifies up to two switches being pressed at once Variables Used SwTmpCmp Source File ENEE 440 PIC18 Alarm Clock SW asm 5 19 08 Page 53 of 64 PIC HEU QUA Developer s Guide John Tooker PIC18F4550 Implementation UpdateSwitches Return Value Description Calls QuerySwitchesll updates Sw_State_16 Sw_PState_16 Sw_State_7C Sw_PState_7C with current previous switch states Special Notes Should call from interrupt slow then the period of bouncing Variables Used ENEE 440 PIC18 Alarm Clock SW asm Source File ENEE 440 PIC18 Alarm Clock SW asm resetSwictches sss Return Value Description Looking back this is not needed since we don t use US_pot or Sw_State_HD PR anymore Special Notes Those variables were part of a failed attempt to monitor the switches that would have taken most of the switch manage
11. Variables Used Source File ENEE 440 PIC18 Alarm Clock Main asm SetHDCount Arguments HOLDDOWNLIMIT Description Checks to see that HOLDDOWNLIMIT is in the bounds of HDCount HDCount MIN HOLDDOWNLIMIT 0x7F Special Notes Variables Used HDCount Source File ENEE 440 PIC18 Alarm Clock Main asm AlarmRinging_q Arguments gt O Return Value WREG 1 if not ringing 0 if ringing Description In the interval MT sec 00 MT hun lt 10 check for alarms alarm date Md alarm time MT hh mm if so return positive Special Notes Keeps FSRO the way it found it Variables Used MASTER_DATE MASTER_TIME FSRO Source File ENEE 440 PIC18 Alarm Clock Alarms asm silenceAlarm Description Look at alarm pointed to by WREG s modification code and apply it to the alarm alter or not the alarm s date in the stack 5 19 08 Page 47 of 64 PIC HEU QU Developer s Guide John Tooker PIC18F4550 Implementation AddAlarm Arguments Description Creates an empty alarm on the stack Does not alter the stack pointers though this way we do not have to save the alarm until it is set Modification data set up so that the code is OXFF we do not know if we repeat or not Special Notes Assumes caller function has checked to see that the stack is NOT full Variables Used Alarm_SP Alarm_mod_SP MASTER_DATE MASTER_TIME Source File ENEE 440 PIC18 Alarm Clock Alarms asm AddAlarmSpace Arguments OO Description Chan
12. aspects of the program separate from the overall picture After the general description of the alarm clock functions subroutines will be grouped together and described based on how each system acts on as whole Please be familiar with and refer to Microchip s document No 39632D PIC18F2455 2550 4455 4550 Data Sheet available from their website http www microchip com It will be assumed you the developer will find the basic programming techniques listed in that manual What will is not covered in that manual and will be talked about briefly is the P24 hardware with which PIC18 interfaces Notations and Convention See Appendix A Acronyms and Abbreviations Number formats Numbers will be listed as Ox for hexadecimal interpretation and without Ox for decimal interpretation They will be stored Little Endian style the lowest byte at the lowest address Registers hold 8 bits of data instructions take up 2 Bytes in program memory and the program counter can only point to every other address in program memory 1 Word 2 bytes Programming Consistencies Assembler directives will be in CAPITAL LETTERS Tabs Please view with you text editor set for tabs being displayed as 4 spaces Function labels will exist without being tabbed over labels within functions will be preceded by two spaces Instructions will be preceded by one tab instruction arguments three tabs from beginning of the line Comments When explaini
13. message Display State08 Edit Current Alarm s Date Comes from Date display State18 by waiting or holding down s Goes to Which frequency state State09 by holding down s Current alarm s date REVIEW EDIT LED is green SET ALARM LED green Change the current alarm s date by pressing and releasing AV This is when the next time the alarm will Display Description 5 19 08 Page 33 of 64 PIC CHE QU Developer s Guide John Tooker PIC18F4550 Implementation ring can not ring for several days even if alarm is set to ring every day once it rings it will ring every day Register Notes Edits FSR1 and FSR2 within rules specified Look s at the current alarm s modification data in the modification stack based on that value go to 0x00 once state19 0x01 everyday state39 0x02 certain days of the week unimplemented OxFF not set yet state0A Register Notes Edits FSR1 and FSR2 within rules specified Description State19 Display Once Comes from State09 Goes to State28 Start cycle over again oncE once REVIEW EDIT LED is green Description Idle while displaying message Display State39 Display Prompt Every Day Comes from State09 Goes to State28 Start cycle over again EdaY Every Day Every Day REVIEW EDIT LED is green SET ALARM LED green Description Idle while displaying message Display State0A Prompt Repeat
14. registers the first 0x59 slots of ram In the code there are some UDATA labels that are old especially in the switch file Besides the temporary ones the in use ones are in tables below MASTER_STATE State Holds the current state A 1 when alarm is ringing S N 1 when in snooze nap mode SET_MS macro Main Loop most states ENEE 440 PIC18 Alarm Clock Main asm MASTER_TIME hh mm ss cc Keeps track of current time in hours minutes seconds and 1 100 of a second Set_Snooze Alarm_Ringing Tick_MT others ENEE 440 PIC18 Alarm Clock MT asm MASTER_DATE 4 digit date in compact BCD form Keeps the current date Set_Snooze Alarm_Ringing Tick_MT others ENEE 440 PIC18 Alarm Clock MT asm MASTER_DAY Pee SE RW TMS Holds the day of the week only one bit set at once rotate left to get next day of the week if negative rotate again States 21 and 31 ENEE 440 PIC18 Alarm Clock MT asm In_Service_L Bits tell which sub priority low interrupts are in service highest sub priority in bit 7 1 in service 5 19 08 ENEE 440 PIC18 Alarm Clock Low Int asm Start_Srvs_L OO Bos O y O hin w Bits tell which low sub priority interrupt needs to be started Highest sub priority is bit 7 1 needs service 0 does not need service ENEE 440 PIC18 Alarm Clock Low Int asm DelayCount IS Holds the number of 0 01 seconds left until we need to change to the state in DelaySta
15. seems to help but other states brighter Have to change daily alarm s date when you change master date 5 19 08 Page 60 of 64 PIC HEU MORI Developer s Guide John Tooker PIC18F4550 Implementation 5 2 2008 Having trouble showing the difference between time of alarm and master time difference is how long is left of snooze alarm Adjusting for BCD and O through 59 possible is too hard so subtract both if result gives no borrow STATUS 0 then ok else minutes left is 60 Alarm s Minutes Time s Minutes 5 4 2008 Starting to add the code to add modify user defined alarms Yesterday and today changed how you review set alarms it is all done in the same functions just start and exit differently Don t let modify stack pointers until done editing alarm then you don t have to worry about it ringing while setting it Also allows you to cancel that alarm not written till you see set 5 5 2008 Trying to get programming done tonight won t happen seem to add alarms that ring everyday or once won t get certain days of the week Still need to have LED s reflect what you are doing 5 6 2008 Able to successfully add and delete alarms Made the decision to not implement setting or seeing an alarm to the day of the week can t have it ring every Tuesday but have left in the states to do so just not filled up taken care so we don t accidentally get to one Fixed RE error of turning down past 00 00 to FF FF there was a D
16. time see page 17 for more detail The actual structure yields checking one register to see if it is negative to tell if the counter is active or not Responding to Pressing of a Switch This is fairly well documented in the Switches section on page 12 But without too much code we can respond to a switch being pressed pressed and released or held down We have a hold down limit that allows us to easily change how long it takes to consider a switch held down from zero to 1 27 seconds though this needed to be changed to 0 to 32767 for the quicker demo file since 1 27 seconds passed hundreds of times quicker thus requiring an extra register 5 19 08 Page 39 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Appendices Appendix A Acronyms and Abbreviations Acronym Meaning Translation Abbreviation 7Seg Seven Segment Display BCD Binary Coded Decimal JSCII John s Standard Code for Information Interchange LED Light Emitting Diode Rotary Encoder RE SW Switch hh SSS E two nibbles of hours in BCD format m two nibbles of minutes in BCD format ss two nibbles of seconds in BCD format cc two nibbles of hundredths of seconds in BCD format m AAA O MT MASTER_TIME Md MASTER_DATE MD MASTER_DAY 5 19 08 Page 40 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Appendix B Global and Important Variables used 45 out of the 96 allowed UDATA
17. 0 PIC18 Alarm Clock Alarms asm Holds alarm constants and functions to set alter view alarms and the alarm stacks page 19 ENEE 440 PIC18 Alarm Clock Buzz asm Not used would make sure sounds happens when called from interrupt but not completed yet ENEE 440 PIC18 Alarm Clock High Int asm Directs High interrupt page 9 ENEE 440 PIC18 Alarm Clock LED asm Contains constants and functions to refresh display JSCII constant values as well as output hardware constancies page 12 ENEE 440 PIC18 Alarm Clock Low Int asm Manages low priority interrupts with sub priorities page 9 ENEE 440 PIC18 Alarm Clock Main asm Main asm contains the main function interrupt declaration calling of resets main loop managing of states alarms and snoozing status see page 22 Also contains code for each state see page 24 ENEE 440 PIC18 Alarm Clock MT asm Controls master time called from by high priority interrupt see page 11 Also updates other time sensitive counters see pages 15 17 ENEE 440 PIC18 Alarm Clock RE asm Keeps track of rotary encoder on interrupt see page 17 ENEE 440 PIC18 Alarm Clock SW asm Contains switch constants and functions updated on interrupt see page 13 Interrupts High Interrupt The high priority interrupt is responsible for keeping track of time It fires when Timer 1 s counter overflows This interrupt fires every hundredth of a second see page 11 for how this is accompli
18. 11 DISP_NONE b 11111111 rightmost 7seg rightcenter 7seg leftcenter 7seg leftmost 7seg colon and apostraphy red LED s green LED s both red and green all on all 7segs on all off To set the high bit of FSRO in LED_Write argument to translate the WREG argument DISP_Trans b 100000000 Alarm Stack Constants Alarm_Base_addr Alarm_Base_size Alarm_Mod_Base_addr Alarm_Mod_Base_size 0x100 0x80 0x180 0x80 Timing in 1 100 of a second HOLDDOWNLIMIT STD_Times_Out STD_GoBack_Wait STD_Disp_Wait 0x30 480 ms 0x0320 8 seconds 0x0190 4 seconds 0x00C8 2 seconds PIC CHE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Appendix G Developer s Notes These are note collected and typed out in addition to paper notes which will not be included They are an insight into my successes and frustrations They are not complete and should be taken at face value 4 4 2008 Displayed John traffic light on board using display algorithm 4 5 2008 Slowed down display refresh rate to 1Hz as well as spaced our ghosting prevention step from refresh step to assure that ghosting was not a problem 4 6 2008 When writing values to display using LED_Write rightmost seven segment value shown on leftmost seven segment display as well when calling left to right Fixed by writing left to write but leftmost last Not a problem with definitions Not an issue with Refresh_LED subroutine Possibly issue wit
19. 7Seg 7Seg LED LED Contains direct translation of what should be displayed on the LED s Refresh_LED LED_Write ENEE 440 PIC18 Alarm Clock Main asm 5 19 08 LED cnt Used to both drive the Anodes and also pick which byte of Display_Buf to refresh to the screen there are 8 pairs What is shown above is the contents at one point in time next time the function is called that will be rotated Refresh_LED ENEE 440 PIC18 Alarm Clock LED asm RE_POS IS Holds the position of the rotary encoder rotating clockwise corresponds to increasing this number This number will be incremented by 96 for every full revolution RE_intrpt snooze_manage ENEE 440 PIC18 Alarm Clock RE asm RE_ state Previous state of the rotary encoder Compare to current state to see if a change increase or decrease RE_POS 01 gt 00 clockwise 01 gt 11 counterclockwise 11 gt 01 clockwise 11 gt 10 counterclockwise just half of the changes possible RE_intrpt ENEE 440 PIC18 Alarm Clock RE asm Page 43 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation SwTmpCmp IS A EA A ees Used for a variety of switch tasks from holding Sw_ State _16 AA Pin Pin Pin P n P n Pin Updated on an interrupt to keep the bits consistent with which switches are currently being pressed Bit 1 is switch 1 bit 6 is switch 6 Accurate to two switches being pressed at once 1 C
20. D s The eighth one would have one bit for the sound output depending on the jumper The display needs to be refreshed fairly often and is done so in the main loop page 22 via a function called Refresh_LED page Refresh_LED simply takes what is in each buffer location and prints it to the actual display through the SPP port page 12 but we only update one part of the display each time we get our call We take care of ghosting by turning off the cathodes Issues There is an issue of ghosting that believe comes from a portion of my code that have used to test a function that have not removed and have not found Writing to the buffer Using a function called LED_Write page we can write to the display buffer with some organization The two arguments are WREG and FSRO WREG holds the value to be displayed and FSRO holds both where to display that value and how to interpret the value in WREG There are two ways to translate the WREG value 1 Pre coded for the display using constants we can submit as an argument the values that directly translate to a character on the display 2 Translate the value in WREG as a JSCIl page 57 most useful for displaying a number the first 16 values in the JSCII table are the numbers O through A respectfully order The high byte of FSRO controls this decision The lower byte of FSRO controls which part of the display our value goes lts bit values correspond to each of the seven areas Seven Se
21. Date Comes from Viewing current time State00 by pressing t setting current date viewing current day of week default Goes to state States 11 21 00 by holding down t pressing t or waiting 8 seconds respectfully Display Current Date CURRENT TIME LED is red Description Displays the current date State11 Set Current Date Comes from Viewing current date State01 by holding down t Goes to Viewing the current date State00 by releasing t Displa Current date pay CURRENT TIME LED is red SET TIME LED is red When holding down f pressing VA will change the Description rent date State21 View Current Day of the Week Comes from Viewing current date State01 by pressing Y setting current day of the week viewing current time in Goes to minutes and seconds default state States 02 31 00 by holding down t pressing t or waiting 8 seconds respct Display Current day of the week CURRENT TIME LED is red Description Displays the current day of the week State31 Set Current Day of the Week Comes from Viewing current day of the week by holding down t Goes to Viewing the current day of the week by releasing t Current day of the week CURRENT TIME LED is red SET TIME LED is red When holding down f pressing VA will change the current day of the week Display Description 5 19 08 Page 29 of 64 PIC HEU OLORI Developer s G
22. ECFSZ instruction that should have been a DECF Almost done need to If daily alarm rings set ALARM ON bit to red if special alarms set that LED to green Need to make the modifications work correctly need to set unset daily alarm 5 7 2008 Got Alarm On light to properly light To set disable the daily alarm disable set date to 00 00 enable set date to Md check to see if set if not add 1 5 19 08 Page 61 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation D10K have NOT done much any testing of the alarm clock when the date roles over from 9999 to 0000 know have stated that the initial time should not be in the first day mostly because that is where place the daily alarm when it is not ringing The error could range from the daily alarm going off no matter what on day 0000 to the user being confused for various other reasons 5 19 08 Page 62 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Index A Accuracy 9 active 19 29 38 57 Alarm alarm ringing 20 21 alarm clock 4 9 16 60 B boot 6 20 C colon 10 11 23 56 compact BCD 9 18 40 41 49 50 51 counter 4 7 8 10 14 15 16 17 20 22 38 48 53 57 58 D delay state 7 17 20 23 44 Display 6 7 8 10 11 16 17 19 20 21 23 27 28 29 30 31 32 33 34 35 36 39 41 46 47 48 49 54 55 57 58 1 0 anode 11 37 58
23. Goes to Prompt Repeat State0A Displa CAnt can t play REVIEW EDIT LED is green SET ALARM LED green Idle while displaying message we then ask about Description repeating again until the user repeats every day or not at all or exits before this alarm is saved State3B Display Set Comes from No Yes State 1A 3A respectfully OB if implemented Goes to Start display of alarms beginning State28 Display SEt set REVIEW EDIT LED is green Description Idle while displaying message 5 19 08 Page 35 of 64 PIC CHE QU Developer s Guide John Tooker PIC18F4550 Implementation State0D Prompt Delete Any of the special alarm viewing setting states by holding Comes from dawn id Yes or No State1D REVIEW EDIT LED is green SET ALARM LED green Display DEL Delete Description Idle while displaying message Goes to State1D Yes or No Delete Comes from Prompt Delete Goes to Beginning of viewing setting special alarms or back to the state which existed previous to arriving at StateOD Yn yesorno REVIEW EDIT LED is green SET ALARM LED green If user says yes then delete the current alarm by moving everything else in the stack down one alarm space and decrementing the alarm points by 4 then display erased If user says no go back to previous state held in temporary portion of alarm s modification entry the 2 low
24. I_6 0x06 JSCII_EQ 0x15 JSCII_S 0x2C JSCII_7 0x07 JSCII_CO 0x16 JSCII_T 0x2D JSCII_8 0x08 JSCII_AP 0x16 JSCII_U 0x2E JSCII_9 0x09 JSCII_G 0x20 JSCII_V Ox2F JSCH_A Ox0A JSCII_H 0x21 JSCII_W 0x30 JSCII_B Ox0B JSCII_I 0x22 JSCII_X 0x31 JSCII_C Ox0C JSCII_J 0x23 JSCII_Y 0x32 JSCII_D 0x0D JSCII_K 0x24 JSCII_Z 0x33 JSCII_E Ox0E JSCII_L 0x25 How they appear on the 7Seg display W and M should repeat themselves 5 19 08 Page 57 of 64 Appendix F Other Constants SPP Address Definitions CATHODES 0x00 ANODES 0x01 SWITCHES 0x02 RE_ENC 0x02 Rotary Encoder Constants RE_Bit_A 7 RE_Bit_B 6 RE_Mask b 11000000 LED Segment Definitions Cathodes SS_A b 11011111 SS_B b 11110111 SS_C b 10111111 SS_D b 11111101 SS_E b 11111011 SS_F b 01111111 SS_G b 11101111 SS_P b 11111110 b 00000000 b 11111111 SS_ON SS_OFF b 10111111 b 11010111 SS_apo SS_col LED_11 LED_12 LED_21 LED_22 LED_31 LED_32 b 01111111 b 10111111 b 11011111 b 11111011 b 11111110 b 11111101 LED Segment Bit Definitions Cathodes LED_11_bp LED_12_bp LED_21_bp LED_22 bp LED_31_bp ONODN LED bit position LED bit position LED bit position LED bit position LED bit position LED Area Definitions Anodes DISP_RM DISP_RC DISP_LC DISP_LM DISP_COL DISP_RED DISP_GRN DISP_YLW DISP_ALL b 11110111 b 11101111 b 10111111 b 01111111 b 11011111 b 11111011 b 11111110 b 11111010 b 00000000 DISP_7SEG b 001001
25. LIY HHI Developer s Guide PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation Information in this document is provided in connection with John products No license express or implied by estoppel or otherwise to any intellectual property rights is granted by this document Except as provided in John s Terms and Conditions of Sale for such products John assumes no liability whatsoever and John disclaims any express or implied warranty relating to sale and or use of John products including liability or warranties relating to fitness for a particular purpose merchantability or infringement of any patent copyright or other intellectual property right John s products are not intended for use in medical life saving or life sustaining applications John may make changes to specifications and product descriptions at any time without notice Designers must not rely on the absence or characteristics of any features or instructions marked reserved or undefined John reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them John s PIC Architecture processors e g PIC18F4550 processor may contain design defects or errors known as errata which may cause the product to deviate from published specifications Such errata are not covered by John s warranty Current characterized errata are available in User s Manual C
26. PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Identifying how switches have been pressed in states functions outside of the interrupt The procedure for identifying if a switch has been pressed and released and or held down 1 Determine if the switch is currently being held down test a bit in Sw_State_16 for switches 1 6 or in Sw_State_7C for switches 7 C a Ifitis being pressed i There is a counter HDCount that keeps track of how many hundredths of a second have passed This should be set once a switch has been pressed we do so here if needed ii See if that counter has reached zero if so then that switch has been held down for the proper time to classify as being Held Down 1 If HDCount has reached zero a Disable counter b Execute code that should happen after the switch has been held down 2 Else exit b Else it is not being pressed we need to see if had been pressed via Sw_PState_16 7C to see if it had been held down previously every time Sw_State_xx gets updated the old value gets stored in Sw_PSate_xx i If it had not previously been pressed then another switch may have been previously pressed exit ii If HDCount is not set then no switch has been pressed exit iii Else we had been pressing our switch which means HDCount has started and we need to clear it We also do whatever needs to be done when this switch has been Pressed and Released There is an examp
27. age by waiting or holding down s If there are 30 special alarms display full State37 Else display time then display alarm s time State17 Display Figure out if there are special alarms in the stack if so Description continue to display the time of the current special alarm Only here for one cycle Register Notes Edits FSR1 and FSR2 within rules specified Goes to 5 19 08 Page 32 of 64 PIC HEU QU Developer s Guide John Tooker PIC18F4550 Implementation State37 Display Full State17 Display Time Comes from Set time delay with without adding an empty alarm Goes to Showing setting current alarm s time State07 tiM time REVIEW EDIT LED is green Description Idle while displaying message Display State07 Edit Current Alarm s Time Comes from Time display State17 by waiting or holding down s Goes to Displaying date State18 by holding down s Current alarm s time hours and minutes flashes at 1 Hz REVIEW EDIT LED is green SET ALARM LED green Change the current alarm s time by pressing and releasing AV Register Notes Edits FSR1 and FSR2 within rules specified Display Description State 18 Display Date Comes from Viewing Setting Current Alarm s time State07 Goes to Viewing Setting Current Alarm s date State08 dAtE date REVIEW EDIT LED is green Description Idle while displaying
28. alled it looks at the rotary encoder info on the SPP port compares it to the previous state RE_state page and then updates RE_POS page accordingly Clockwise rotations corresponds to RE_POS being incremented and conversely Changing States As have said before states control the unique happenings within our alarm clock That being said there are two ways to change states the first is to let a time run out the second is to manually change states based on user input Some states employ both of these transitions Changing based on the timer One of the functions of the main loop is to see if it is time to change to a new state this is done in the function Time_Change_State page Time_Change_State looks at DelayCount page to see if it is time to change to a new state when it is zero If itis zero we set our next state to the value kept in DelayState page and we disable the counter make it negative we also change the DelayState value to zero the default state as an error precaution Setting up DelayCount and DelayState should be done in the preceding state That is if we want to display dAtE for 2 seconds the preceding state needs to set DelayCount to 0xC8 200 and DelayState to current state before the transition All that the current state needs to do is update the display The current state is unable to set these delay values without much trickery without trickery we would consistently set th
29. button 4 15 17 18 20 21 27 57 58 cathode 11 37 Pulse Width Modulator 9 Switches 6 11 12 38 interrupt 6 7 8 9 10 12 14 16 20 21 40 42 43 48 51 52 53 54 57 58 J JSCII 8 11 39 48 54 55 L latency 9 10 Little Endian 4 5 19 08 M Main Loop 6 7 8 10 11 16 21 38 40 43 57 58 Microchip 4 37 P P24 4 PIC 2 6 37 PIC18 4 8 20 38 40 41 42 43 44 45 46 47 48 49 50 51 52 prescale 10 20 53 R register 7 17 20 21 37 38 40 51 53 54 57 58 Register BSR 10 Display_Buf 57 FSRO 11 45 48 49 50 51 56 58 FSR1 31 32 33 34 35 44 46 FSR2 31 32 33 34 35 44 46 MASTER_DATE 9 10 40 45 46 49 MASTER_STATE 4 20 21 22 23 40 43 44 53 MASTER_TIME 8 9 10 40 43 45 46 49 RE_POS 16 41 51 58 SPP 11 16 37 51 56 58 SPPDATA 13 37 54 SPPEPS 37 54 STATUS 10 59 Sw_State_16 12 14 42 44 52 Sw_State_7C 12 14 15 42 44 52 WREG 10 11 44 45 47 48 49 50 51 54 56 Registers 4 S Seven Segment 11 57 7seg 30 37 56 57 58 State 4 5 6 7 8 10 12 15 16 17 19 20 21 23 27 29 30 31 32 34 35 36 38 40 41 44 45 48 51 current state 7 10 16 20 21 40 41 51 default state 16 17 20 21 27 28 29 30 38 44 53 passive states 16 19 Super States 20 Page 63 of 64 7 Timer Timer 0 20 5 19
30. ce we clear its bit in Start_Srvs and set its bit in In_Service_L see page which tells us it is running Latency The latency to respond to an interrupt grows with each sub priority we implement The highest one will start in 4 n 6 cycles where n is the number of sub priority interrupts This assumes each interrupt fires at once Note that in by now we have already taken care of the interrupt flags the only thing we have not done is backup any variables we need to save The lowest sub priority will start in 3 n 5 n 8 n cycles This assumes that no other interrupts have occurred still being worked on The time it takes to resume a higher sub priority interrupt when a lower sub priority interrupt fires is about 4 n 3 cycles Tasks associated with sub priorities The switches are updated at 22 9 Hz via timer O lower sub priority 1 The rotary encoder position is updated at 6510 Hz via timer 2 higher sub priority 3 The buzzing sound updated at 3520 Hz but the interrupt is not actually set up so it will never ring actually just adds latency right now but kept in since we will want our alarm to make noise may not be needed with Pulse Width Modulator highest implemented sub priority 6 Modes The word Modes will refer to systems of states and interrupts acting together to serve an overall independent idea Keeping time Accuracy Off by 12 minutes and 32 seconds per year Storing time Aside for what is d
31. date of the alarm and the current time We must also respond to the rotary encoder Turning clockwise means adding more time to the alarm and conversely After adjusting the time we need to see if the alarm time agrees with the current time if so the user has dialed to zero minutes left This allows us to exit the snooze mode without the alarm ringing since we will not be interrupted to check to see if the alarm is ringing it is not possible for it to accidentally ring unless the time actually expires with 1 minute left as the user is about to dial down then we don t actually have an error the user might be annoyed though in fact the user will probably enter snooze mode right away There is not a great way to check for this since we cannot predict what the user will do 5 19 08 Page 22 of 64 PIC CHE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Switched States Based on Delay Timer As talked about on page 17 if DelayCount page both high and low bytes are zero then we need to switch states to the one recorded in DelayState page as well as turning off the DelayCounter Going to a State s Code SeekState page contains code to take the value in MASTER_STATE and use it as an offset to the current program counter to jump to a GOTO command used a jump tabled indexed by the MASTER_STATE value Calculating the offset PC involves pushing the current PC onto the stack so we can edit it We then can shift
32. duration Source File ENEE 440 PIC18 Alarm Clock Alarms asm Refresh_LED Return Value Description Copies values in Display_Buf to display one area at time also checks to see if the daily alarm will ring looking at ALARM_STATE not figuring it out here Special Notes Variables Used ALARM_STATE LED_cnt Display_Buf Source File ENEE 440 PIC18 Alarm Clock LED asm 5 19 08 Page 49 of 64 PIC HEU QU Developer s Guide John Tooker PIC18F4550 Implementation Reset_LED Arguments OO Description Sets up default values for the LED displays as well as the welcome screen nn BODA 7SEG t i i i i i i tstsS LED_Write Arguments WREG FSRO Description Writes values to the display buffer Value is in WREG if FSROH is 1 then translate this value using BCD_to_7SEG else just write directly FSROL indicates which parts of the display buffer should be written to Special Notes Variables Used Source File ENEE 440 PIC18 Alarm Clock LED asm Tick_MT Arguments Description Called from interrupt should be called every 1 100 of a second works with Timer 1 s counter to accomplish this Increments MT Md and MD by 1 100 of a second also decrements DelayCount and HDCount if they are not turned off gt 0 Takes care of flashing the at 1 or 5 Hz depending on the state Also flashed the LED if the alarm is currently ringing Special Notes MT Md MD DelayCount HDCount Variables Used Source File ENEE 440
33. e delay counter and it would never reach zero The current states can also leave based on user input but in doing so they should clear the delay registers otherwise when the time runs out we will leave the current next state and go to the one specified by DelayState Downside we cannot have two passive states occur after each other The way around this is to place a third state between the two passive ones This third state will set up the delay registers for second passive state and then set the current state to that second state Set up delay Set up delay for state 1 for state 2 goto 1 goto 2 Figure 3 Changing based on user input We have seen how to interpret the switch state registers page 12 over time we uses that code in each state For the most part this code is copied in each state since it needs to be modified so much perhaps a series of macros would be more appropriate but the overall effect is the same 5 19 08 Page 17 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation There are three ways for a state to respond to a switch being pressed the state can respond in two separate ways to the second to responses below 1 Do nothing do not even check its bit values 2 Wait for it to be held down held down counter expires 3 See that it was pressed and released released before held down counter expirers A couple of the states make use of the fact that a switch was held down to ge
34. ed that it was in fact previously held down The states can then read Sw_State_16 7C and see if those switches are being pressed if a function reads a switch it should clear that corresponding flag this gives the interrupt control of the refresh rate not whatever frequency the function in question is being called at QuerySwitchesll While testing for the switches found that in each set of 4 every combination of pressing produced a unique response see the Table 1 without masking the response Switches 1 4 correspond to 5 8 and 9 C on the other blocks respectively 5 19 08 Page 13 of 64 PIC HEU QUIN Developers Guide John Tooker PIC18F4550 Implementation Table 1 Switch es Pressed Value Read Hex None 4 2 3 4 1 amp 2 1 amp 3 1 amp 4 283 284 384 18283 18284 18384 28384 All O N P 2 0Q 09Q OI O O 2 IO NODO m mT This means that each switch corresponds to a bit in the result what is read from SPPDATA A 1 is read when the switch is not pressed a when switch 000 CET 432 0 is read gt 1 is pressed My clock design will not have an action attributed to having two switches pressed in the same block at the same time so will look only at two switches being pressed in different blocks except for rightmost block there two can be pressed 5 19 08 Page 14 of 64
35. enu to enter alarm menu system make Description menu choices by pressing the arrow underneath whichever 7seg matches State06 Display Review Comes from Alarm menu State0E Goes to Set time delay State28 Display ReVA review alarm Description Idle while displaying message 5 19 08 Page 31 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation State28 Prepare to View Special Alarms Review Alarm message alarm set confirmation or viewing alarms frequency states 06 3B 19 39 by Comes from waiting or holding down s Or by any o the view set special alarm states by holding down a If there are no special alarms display none State27 Else display time then display alarm s time State17 Display Figure out if there are special alarms in the stack if so Description continue to display the time of the current special alarm Only here for one cycle Register Notes Edits FSR1 and FSR2 within rules specified Goes to State27 Display None Comes from State28 Goes to Default state State00 Display nonE Description Idle while displaying message State16 Display Add Alarm Comes from Alarm menu State0E Goes to Set time delay and add empty alarm State38 Display AddA add alarm Description Idle while displaying message State38 Prepare to Add a Special Alarm Comes from Review Alarm mess
36. esesesesesesesecesesesesesesecececececececss 9 ENEE 440 PIC 18 Alarm Clock BUZZ amp SI1 wiccccccccccccccccsecesesesesesecececesesesesesesesesesesesesesesesesesesesesesesesesesesesesecesesesecesecececeseees 9 ENEE 440 PIC 18 Alarm Clock High Int asm c cccccccccscsesencecsneeseneecsneneseneeccanessaaeesenaneseaeeseaeesesaneseneassaaeeseaeessaaneneneesenees 9 ENEE 440 PIC18 Alarm Clock LED aSM icccccccccccccccccceccsesesesesecececececesesesesesesesesesesesesesesesesesesesesesesesesesesecesesesecesecececececes 9 ENEE 440 PIC18 Alarm Clock LOW IN ASIN wicccccccccccccccccccesesesesesececececesecesesesesesesesesesesesesesesesesesesesesesesesesesesesesececesesesecss 9 ENEE 440 PIC18 Alarm Clock Main AS uicccccccccccccccccccesesesesesececesesesesesesesesesesesesesesesesesesesesesesesesesesesesesesesesesecesecesesesenes 9 ENEE 440 PIC18 Alarm Clock MT aSM oooonnccicnnnccninnccanananacacinnn nana 9 ENEE 440 PIC18 Alarm Clock RE aSM icccccccccccccccccccsesesesesesececesecececesesesesesesesesesesesesesesesesesesesesesesesesesesecesesecesececececececes 9 ENEE 440 PIC18 Alarm Clock SW asD ocicccciccccccinccanananacanana narra 9 INTERRUPTS ata radaecoperendids 9 AA gthesgeeansbviayscaviveds N ESEE S 9 Low merap WIA OISDALCNON iross onna nea A OE O A OE 9 MODES ina E A A ai aaa asaan 10 K6 DING AAA E O o eae Adan A 10 DIS DIAV a ae 12 SWINE S a a a O iodo 12 Rotary ENCOlii ias 17 CHANGING Salas aii eiii edi 17 Al MS daa acacia 19 Alarm Men
37. est byte Register Notes Edits FSR1 and FSR2 within rules specified Display Description State2D Display Erased Comes from Yes or No State1D Goes to Beginning of reviewing alarms State28 ErAS Erased REVIEW EDIT LED is green SET ALARM LED green Description Idle while displaying message Display 5 19 08 Page 36 of 64 PIC HEU QU Developers Guide John Tooker PIC18F4550 Implementation State26 Display Snooze Set State0C Set Default Snooze Length Comes from Display snooze set by waiting Goes to Default state by pressing t or s Display Default snooze length hours and minutes Use VA to adjust default snooze length automatically Description Saved State36 Display Nap Set Goes to Set default nap length State1C Display MAPS Nap Set default Description Idle while displaying message State1C State0C Set Default Nap Timer Length Goes to Default state by pressing t or s Display Default nap timer length hours and minutes Description Use VA to adjust default nap length automatically saved 5 19 08 Page 37 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Hardware Input and Output The input and output of this device used the SPP ports of the PIC On the other side of the PIC there are eight external registers that hold the output values Writing an address writing to SPPEPS will pick
38. ference this will make it harder to find it try searching the name 5 19 08 Page 6 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation Overview of the System General Description First we must note that the first 0x1000 lines of code on the PIC are reserved for the boot program thus everything for us starts at address 0x1000 and up On reset the main function sets up data and interrupts for other functions to run After this we enter the main loop IdentidemVicis for responsibilities of the main loop see page 8 The program flow as a whole is directed by which state is currently being occupied list of states on page 24 Tasks in the main loop and interrupts function as ways to update data for each state with a few exceptions Figure 1 is the front panel of the display it dictates which buttons do what functions and which LED s have certain significances HA CUIIGIT TUIG kA NHICRONE Dn O no RUARMBON COG TWIG Q Q MIG PWY ACAR M O Q WUXI OGY Figure 2 Switches are arranged as such 1 3 5 7 9 B 1 8 are arrows 9 C are mode buttons 2 4 6 8 A C LED s are labeled according to their matrix value column row starting in the upper left corner 5 19 08 Page 7 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Program Flow Reset e Initiate registers and interrupts page 9 Main Loop e Refresh LED s page 12 e See
39. for prescale to get the number of instructions executed in 0 01 seconds 60 0000 instructions So if we could set our counter to 60 000 we would overflow every 0 01 seconds This can be done by manually setting our counter to 15A0 since 60 000 Ox FF15A0 This would be wonderful if it could be done at the time of overflow but it cannot So we must account for latency by setting our counter to 60 000 latency This latency was found experimentally via the simulator but there are 6 instructions that need to happen before our counter gets reset so set counter to 0x15A6 Error in the time explained If high interrupt latency was constant it is close we would have not time lost error in our system But that is not the case The latency is sometimes more that 6 instructions which introduces error Experimentally see README txt we have a relative error of 0 000023826 0 0023826 or loosing 12 minutes and 32 seconds per year or 14 4 seconds per week Other time keeping tasks We take care of some other tasks in the time keeping interrupt decrementing the state delay counter page 17 decrementing the switch hold down counter page 15 flashing the colon at 1 Hz 5 Hz or off based on the current state and if the alarm in ringing flash the LED s red yellow green off at 1 Hz for display information see page 12 Interrupt Justification Normally one would not expect an interrupt to commit this much time from the normal program but
40. g also modifies the ringing alarm so that it will ring or not ring the next day If an alarm is ringing we set the current state to the default state because we want to see the current time We also want to respond to the user so they can turn off the alarm or enter snooze We use the lower byte of DelayState to store rotary encoder position when the alarm rings If that value changes we enter the snooze mode We also look for the a or s button to turn off the alarm or enter snooze mode respectfully Turning off the alarm is as simple as deasserting the alarm ringing bit of MASTER_STATE and going to state30 page So we do not register a as being pressed in the default state Entering snooze mode involves calculating the snooze alarm time and date by adding the current time and date with the default snooze time and storing this information in the alarm stack we also need to assert the snoozing bit in MASTER_STATE Managing in Snooze Mode If the snooze bit of MASTER_STATE page is set we do not want to execute any of the states code So we check that bit and skip the snooze code if we are not snoozing Otherwise we execute Manage_Snooze page Entering nap mode also has this effect but we have set the snooze alarm calculating in the default nap time in place of the default snooze time see state04 page Manage_Snooze displays the time left of the snoozing session This is calculated by subtracting the time
41. ges the stack pointers to add an alarm onto the stack in the way have used it add the alarm first stack does not recognized it yet and then update the stack pointer when print SET on the display Special Notes Assumes caller function has checked to see that the stack is NOT full Variables Used Source File ENEE 440 PIC18 Alarm Clock Alarms asm ChangeCurAlarm Arguments gt gt gt O Description Changes the stack pointers FSR1 and FSR2 to point to the next alarm in the stack or rotate around Special Notes Should only be called from Review Alarm states states in side the larger circle in Figure 6 Variables Used Source File ENEE 440 PIC18 Alarm Clock Alarms asm DeleteAlarm Arguments Description Deletes current alarm pointed to by FSR1 and moves the rest of the alarms in the stack down one space Special Notes Should only be called from Review Alarm states states in side the larger circle in Figure 6 Variables Used FSR1 FSR2 Source File ENEE 440 PIC18 Alarm Clock Alarms asm 5 19 08 Page 48 of 64 PIC HEU QU Developer s Guide John Tooker PIC18F4550 Implementation qSpecial_Alarm_exist_q Arguments gt gt gt qSpecial_Alarm_full_q Arguments AlarmReset Return Value Description Sets up the alarms mainly the stack and default values for the daily and snooze alarm as well as their modifications Special Notes Variables Used Alarm_SP Alarm_mod_SP snooze_duration nap_
42. gments Green etc The display buffer itself can be accessed but should do write to buffer via LED_Write Constants DEFINE s We have several groups of definitions e Hardware reference cathode and anode addressed e Anodes which segments A through G decimal point apostrophe colon matrix of LED s will light when a zero is written to them e Cathodes which area one of four seven segments green LED s etc will light when a zero is written to them e Translate character values JSCII 0 9 A Z some characters these are logical combinations of which segments will light up Switches There are 12 switches on this board 8 arrow buttons corresponding to 4 seven segment displays and 4 mode buttons corresponding to their name in Figure 2 We can identify up to 2 switches being pressed at the same time If three or more are pressed we do not get any more info and only 2 will be reported as being pressed the leftmost two believe 5 19 08 Page 12 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation Updating Values Sw_State_16 page and Sw_State_7C page are updated to report which switches are currently being pressed at the time UpdateSwitches page is called UpdateSwitches is called on a low priority interrupt page 9 at a slow enough rate to avoid bouncing Sw_PState_16 7C holds the previous state of which switches were pressed This is used to see once a switch has been releas
43. h counter that indicates if a switch has been held down long enough is turned off page 15 Reset Calls Reset_LED page sets up the values that allow the display refresh routine to rotate through each display area This also prepares the boot up display Displays JOHN LowIntReset page allows us to use the interrupt dispatcher Reset_MT sets up the counter to the interrupt to overflow every 60 000 instructions page 11 and initial time date are stored these are arbitrary but have some rules see the Reset_MT code resetSwitches page prepares the switches to be updated on their interrupts RE_reset page initializes the rotary encoder state AlarmReset page sets up the alarm stack and modification stack pointers page as well as setting the modifications of the daily and snooze alarm Interrupts These need to be set after the reset calls are made since the interrupt routines rely on initialized registers These interrupts use the Tmrintrpt_setup macro to initialize themselves see the code ENEE 440 PIC18 Alarm Clock Main asm for a in depth explanation Timer 0 interrupt low is for the switches Need a low frequency so we use the 16 bit counter and set the prescale to 2 which yields 22 9 Hz Quick enough so the user cannot press the button too quickly they would have to hold it down for less that 40 ms and on the correct interval but slow enough to avoid bouncing Timer 1 interrupt high
44. h writing to Display_Buf n 4 7 2008 Query switches successfully and completely works for up to two switches pressed in different blocks 4 8 2008 Master time interrupt is working and accurate to 100Hz timer O and others are causing ghosting and incorrect display issues One of which was a previous test left in 4 10 2008 Found source of error talked about on 4 6 2008 had a test I was here that was causing Display_Buf 0 to get value of Display_Buf n s value when written to by my function LED_Write 4 11 2008 Can t get switches to work with HD and PR states Have opted to just update flags in a register when switches are active See notes on how to use switches in switch section Interrupt controls press and release of switch if any function that looks at pressing a switch clears that switches flag Interrupting at 45 7Hz too fast increments at about 34 Hz Can press and release and increment just once but that is hard to do Cut interrupt frequency in half on normal push increments by 2 Cut in half again now it a normal push sometimes doesn t increment at all FII leave it cut in half only once making it interrupt at about 23 Hz Function will have to manage it smarter Switch hold down counting seems to work well EXCETP causes red and green LED s to change Problem with any switch that hold down when associated with my test code Without test code or testing with that switch you can h
45. he daily alarm s modification data says that it will ring the next day but can be turned off through a separate routine state33 on page and viewed and altered easily states 03 13 pages The snooze alarm is never seen by the user and is set based on the current time plus the default snooze time when the snooze button is hit while alarm is ringing or we enter nap mode page It is not modified after it rings All the other alarms are called special alarms Data Structure of an Alarm 4 bytes each in compact BCD form date high date low hours minutes Structure of an Alarm Modification 4 bytes in varying forms modification modification used as modification value high value low pee aed code 5 19 08 Page 19 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation The modification codes determine what the higher two bytes mean Modification Result Code 0x00 Don t modify 0x01 Add larger two bytes to the alarm s date will only be 1 for this program 0x02 Use larger to bytes as flags for which day of the week to ring next look at MD to calculate not implemented OxFF Alarms has not been written yet As you can see other codes are available for implementation Right now we just use 0x00 0x01 and OxFF Alarm and Modification Stack Using the constant definitions actually implemented
46. here are some reasons why decided to implement all of these tasks in the interrupt 1 Increment the time takes time to calculate hundredths of seconds up to days of the week Want this to be done as soon as possible could set another flag and handle it the main loop but that would take more time and replicate handling the interrupt flag outside an interrupt 2 Decrement timers If updating the time occurred outside of an interrupt it would be natural to update the time based counters at the same place since it made sense outside the interrupt left the two together 3 Updating portions of the display These portions of the display rely on the time and the do not need to be updated every time the display refresh is called Overall we save operations 4 Reason for all of this We only call this function every 60 000 cycles it does not take too many cycles which does not effect the rotary encoder the next interrupt that needs to be completed before it s time roles around again There might be an issue with the sound that has not been addressed since did not complete that portion 5 19 08 Page 11 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Display Buffer and refreshment keep a buffer Display_Buf which holds hold the values of each of the 7 LED display components 4 seven segments 1 for the punctuation colon apostrophe and the one for the red and one for the green components of the LE
47. if alarm needs to start ringing page 22 e See if we are snoozing execute snoozing code if that is the case page 22 e See if we need to change states based on our delay counter page 23 e Goto the current state s code page 23 e Repeat Interrupts e Update the time date decrement time dependent counters page 11 e Update the switch state registers page 12 e Update the rotary encoder page 17 States e Everything else see all states on page 24 e Fill display buffer page 12 e Setup delay state system page 17 e Respond to user input page 15 e Update every other global status register not done by the main loop or interrupts Description of Source Files These files appear as they have when wrote them Comments were made at the time so they are genuine but may be a little disorganized The code has not been cleaned up either but should match the structure outlined on page 5 for the most part P18F4550 INC Contains the normal include information to compile assembly files for the PIC18F4550 18F4550 Ikr Modified for 0x1000 start use this one not the default linker file ENEE 440 SimOnly asm Include with making project for simulation purposes when program counter comes to address 0x0 0x10 0x18 the get redirected to where address 0x1000 0x1010 and 0x1018 direct to respectfully 5 19 08 Page 8 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation ENEE 44
48. ing an alarm and have not seen SET then we through away the current un added data Holding down d to delete the current alarm from state that view the alarm stack page 27 Similar to the previous except we are pointed to states that prompt the user to confirm that they want to delete their alarm The function called is QdeleteAlarmQ Responding to the yes or no question When YesOrNo page is called it returns a value saying whether the user has pressed the switches under the y or the n on the screen or has no pressed either buttons This function is different from the ones above it in that it also manages the display portion States that call this should not attempt to display update the display buffer themselves which ever is done second will be what is actually displayed but this wastes time Using the VA to change the current values This is used to change the values what is currently being displayed i e while viewing minutes and seconds we press the leftmost up arrow we will increment the tenth s place of the minutes This is done by the complex function MT_MD_Change_q page which can change minutes hours seconds and the date there is also one that behaves similarly but only changes the current day of the week MD_Change_q page 5 19 08 Page 18 of 64 PIC CHE QUIN Developer s Guide John Tooker PIC18F4550 Implementation MT_MD_Change_q takes a code as an argument and based on that c
49. le on the following page 5 19 08 Page 15 of 64 PIC L120 OLARI Developer s Guide John Tooker PIC18F4550 Implementation Example switch code this example will be using switch 9 switch 9 corresponds to button t time BTFSS Sw State 7C 9 6 O see if switch 9 pressed BRA State00 release t skip if not pressed BTFSC HDCount 7 O Set HDCount if it is CALL SetHDCount negative not running TSTESZ HDCount A if zero Held down t BRA State00_t _ undecided i ELSE go on with other stuff 7 if here t is being held down SETF HDCount 0 disable counter 7 Do what you want when t is being held down BRA State00_t undecided State00 release t 7 if here see if t was pressed AND not counting HD AN if so to goto state 01 BTFSS Sw_PState_7C 9 6 0 see if switch 9 was pressed BRA State00_t undecided if not ever pressed BTFSC HDCount 7 O see if HDCnt disabled BRA State00_t undecided 7 if not ever pressed 7 if here then we had pressed t and released it before it got to the held down state SETF HDCount 0 Since let go disable counter 7 Do what you want when t has been pressed and released 7 done looking at t right now State00 t undecided End of example 5 19 08 Page 16 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Rotary Encoder The rotary encoder is also updated via interrupt page 9 When RE_intrpt page is c
50. leteAlarmQ Arguments Description Similar to QreturnToState00Q except looks at d to see if it has been held down If so go to stateOD on a timer then to state1D Special Notes Should only be called from Review Alarm states states in side the larger circle in Figure 6 Variables Used Sw_State_7C MASTER_STATE delayCount delayState Source File ENEE 440 PIC18 Alarm Clock Main asm QchangeCurrentAlarmQ Return Value Description Same as QdeleteAlarmQ but change FSR1 FSR2 to point to the next alarm in the stack or first if we go over Special Notes Should only be called from Review Alarm states states in side the larger circle in Figure 6 Variables Used Sw_State_7C MASTER_STATE delayCount delayState Source File ENEE 440 PIC18 Alarm Clock Main asm YesOrNo Return Value WREG 1 nothing pressed O no selected 1 yes selected Description Displays yes or no Calls LED_Write as a subroutine Special Notes Calling state should not try to write to the LED buffer Variables Used Sw_State_16 Source File ENEE 440 PIC18 Alarm Clock Main asm 5 19 08 Page 46 of 64 PIC CHEV QU Developer s Guide John Tooker PIC18F4550 Implementation DisplayTest_A Return Value Description Displays a variety of items uncomment what you want displayed Calls LED_Write DisplayTest_B Return Value Description Displays which state you are in when called Calls LED_Write Special Notes For testing only not used in final product
51. ment out of the individual states Variables Used Source File ENEE 440 PIC18 Alarm Clock SW asm 5 19 08 Page 54 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Appendix D Macros Arguments Result Arguments Result Arguments Result Arguments Result 5 19 08 MOVLW literal file register 8 bit address REG literal SET_MS newState MASTER_STATE 5 0 newState 5 0 SET_DS nextState delayTime 16 bytes between 0 and 32767 time is in 1 100 of a second DelayState nextState DelayCount delayTime CLR_DS DelayState is default state DelayCount is disabled DelayCount 15 1 Tmrintrpt_setup Timer 0 through 3 eight_sixteen alarm specific prescale alarm specific interrupt 1 for yes O for no priority 1 for high interrupt O for low clear if we want to clear the counter Read the code for more details but sets up one of four timers as potential interrupts Page 55 of 64 5 19 08 PIC HEU MORI Developer s Guide John Tooker PIC18F4550 Implementation Arguments Result MOVWSPP WREG value to write address address to write to SPPEPS address SPPDATA WREG Safety integrity check performed to assure proper writing of value MOVSPPW Arguments address Result WREG value at that address Safety integrity check performed to assure proper reading of value JSCII_TO_DISPLAY Arguments Result Arguments area
52. needs to be slow as well we choose to its prescale to be 2 remember the interrupt code itself alters the counter to get 0 01 second period Timer 2 interrupt low is for the rotary encoder we scale it a bit pre and post to run quicker then a person can turn the dial since we don t have code to correct for both bit changing between readings Timer 3 interrupt low was for the sound but this was never finished 5 19 08 Page 21 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Main Loop called my main loop IdentidemVicis Latin As seen on page 8 the main loop s tasks are to refresh the display react to special alarm circumstances and pick which state code we will execute Some code gets executed every time refreshing the LED s and checking to see if the alarm is ringing everything else happens within separate states so the main loop needs to decide which state code to run including super states page 25 Refreshing the Display This has already been discussed in the display section on page 12 tried to do this in an existing interrupt but it did not refresh correctly but this was a quick trial may have done it incorrectly It stays in the main loop for now Managing Alarms The code in Manage_Alarms page checks to see if an alarm is ringing for the first time if so we set a bit in MASTER_STATE page that indicates the alarm is ringing the code that reports a new alarm ringin
53. ng a section of code they will be lined up with the instructions one tab When to the right of an instruction they will explain that instruction s significance Not as constant starts an idea followed on the next line s by then ideas end with For example if here the user has dialed the snooze down to zero Turn off snooze alarm change the day back 1 just to make sure it doesn t ring CLRF MASTER STATE 0 Turn off snooze alarm set state CLRF DelayState 0 since we ve used it for RE DECF INDFO 1 O ha points to SA date low from before GOTO IdentidemVicis ae Exit snooze stuff Buttons When you see x related to a button pressing x x button that character refers to a button as such 5 19 08 Page 5 of 64 PIC CHE OLORI Developer s Guide John Tooker PIC18F4550 Implementation a ALARM d DAILY ALARM s SET TIME VA Arrow buttons Other Notations Function names in the form QdescriptiveNameQ usually behave as if answering a question where the question is related to the descriptive name Un updated References am sure you the reader will notice many page cross references but also many pages referenced as did not have time to update all of these So when you see a it refers to that item s entry in the state list function list variable or macro lists in the appendices I apologize if you cannot tell the dif
54. ode responds differently when certain switches are pressed This list of codes in this function s description page Alarms There can be up to 32 alarms in the processor stored on a stack starting at Alarm_Base_addr Each alarm can ring once or every day initially we wanted to be able to ring certain days of the week but this was never accomplished but still achievable Alarm can be set to the minute and will ring when seconds go from 59 to 00 Alarms ring only when the date they are set to matches the current date and the time matching too This means when setting an alarm you set the date to the first time you want the alarm to ring But as you will see below we can only store one date per alarm so how to have that alarm repeat There is also a modification stack for the alarms starting at Alarm_Mod_Base_addr with entries corresponding to entries in the alarm stack When an alarm rings we look at its corresponding modification data to see if we need to repeat or not if we need to repeat we alter the alarm s date so that it rings the next time as of now this is updating the date to ring the next time This will be described a little better below Exceptional Alarms We have two exceptional alarms the daily alarm and the snooze nap alarm These are located at the base of the stack and each is modified outside of the normal alarm modification routine in fact they are not even viewable with the rest of the alarms page 25 T
55. old down any switch and LED s stay they same Also seems to change the 7seg display too Error doesn t happen if we don t call LED_Write Doesn t seem to be a problem with LED_Refresh subroutine either Usually changes display of all digits not just 1 or 1 color Places getting written to often in main loop seem to fix themselves see a flicker but stays correct So an issue with Display_Buf s integrity Doesn t seem to turn on lower left LED light This error only happens when hold down the button long enough to increment the test counter Seems to effect the LED s more then the 7seg Also seem to always be numbers displayed not random lights Seems to pick one of the nibbles usually lower in MAIN_TEST counter and write it to some of the spaces in Display_Buf after translation Sometimes error display is MSnibble of counter but not always Seems like LED_Write is being called randomly with random arguments 5 19 08 Page 59 of 64 PIC HEU OLORI Developers Guide John Tooker PIC18F4550 Implementation Not an issue with SetHDCount Was accidentally disabling HDCounter each time we hit zero changed that errors seem to happen less frequently Works when these two lines are commented out CALL SetHDCount 7 reset counter SETF HDCount 0 77 Since let go disable counter don t know why acts like PR test code AAAAHHHHH found the error in MT call was DECFSZ instead of DECF skipping backing up the FSROL registe
56. on to execute in the default state State00 Goes to Default state State00 Displa Current time s hours and minutes flash at 1 Hz PlaY CURRENT TIME LED is red Description idles until display count expires Comes from State00 Default State Comes from Many places Setting the current time toggle daily alarm view daily Goes to alarm view current date enter alarm menu enter nap mode States 10 33 03 01 OE 04 Current time s hours and minutes flash at 1 Hz CURRENT TIME LED is red Default state watches four mode buttons for being both pressed and released and held down Goes to different states depending on input Description This is where get when you escape from another state and how you start your path to any other state When the alarm is ringing only the display part of the code is executed Register Notes Doesn t effect HDCount while alarm is ringing Display State10 Set Current Time hh mm Comes from Viewing the current time State00 by holding down t Goes to Viewing the current time State00 by releasing t Displa Current time s hours and minutes flash at 1 Hz play CURRENT TIME LED is red SET TIME LED is red When holding down f pressing VA will change the D ription Se E escriptio current time s hours and minutes 5 19 08 Page 28 of 64 PIC HEU QUIN Developer s Guide John Tooker PIC18F4550 Implementation State01 View Current
57. one in the states this is where most of the work happens in this alarm clock Time is kept in MASTER_TIME page MASTER_DATE page and MASTER_DAY page which hold the current time hours minutes seconds hundredth of seconds current date 0000 through 9999 and current day of the week The first two use compact binary coded decimal compact BCD to store their numbers e Hundredth of seconds range from 00 to 99 in the lowest byte of MASTER_TIME e Seconds range from 00 to 59 in the second lowest byte of MASTER_TIME e Minutes range from 00 to 59 in the third lowest byte of MASTER_TIME 5 19 08 Page 10 of 64 PIC CHE OLORI Developer s Guide John Tooker PIC18F4550 Implementation e Hours range from 01 to 24 in the highest byte of MASTER_TIME e Date ranges from 0000 to 9999 in both bytes of MASTER_DATE e Day of the week is one bit that gets rotated in the 8 bits of MASTER_DAY 1 is not allowed Interrupts timing and latency The frequency which we update the time is 1 100 of a second This is accomplished via the high interrupt WREG STATUS and BSR registers are stored automatically and we get straight access to this code with minimal latency The interrupt fires when timer 1 s counter overflows this counter is a 2 number Our clock runs at 48 MHz and 48 MHz 2 100 Hz for any n So how do we get it to interrupt at 100 Hz adjust the timer s counter At 48 MHz we divide by 4 four cycles instruction to get then by 2
58. ontact your local John sales office or your distributor to obtain the latest specifications and before placing your product order Copies of documents which have an ordering number and are referenced in this document or other John literature may be obtained from John Corporation P O Box 7641 Mt Prospect IL 60056 7641 or visit John s website at http www jtooker com Copyright John Corporation 2008 Third party brands and names are the property of their respective owners 5 19 08 Page 2 of 64 PIC CHE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Table of Contents ABOUT THIS MANUAL dada dd 5 OVERVIEW OF THIS MANUAL S LAYOUT siii a dc ca 5 NOTATIONS AND GONVENTION aia 5 NUmbDertormalS caia ori 5 Programming CONSISTENCIES oocooconicccccnnicccccononcccnna nc RARE 5 OVERVIEW OF THE SYSTE M visa naaa 7 GENERAL DESCRIPTION c occccccccccnnnnnnnninn RR RR RR RR Ran Aarra aaa ariari 7 PROGRAM FLOW osa aaa nds ii lada 8 A E PAO A NA EEEE E osadessinske puted oa hee iaecaceevonraaex E E 8 Man LOOP A E E 8 E DUS E T A A A A A N 8 O O 8 DESCRIPTION OF SOURCE FILES 000s0seseeeesseeeeeseeeeeseeeseceeeeeeeeeeeeeeeeeeeeseseeeseeeseeeseseseeeseeeeeeeseeeeeeeeeeeeeeeseseeeeeeeeeeeeeseseseeeeeees 8 AIN CAMA N oo IIA 8 VOPFAS SONG ii daa 8 ENEE 440 SIMONIy ASMiiicniicnrai a a else a ede AE 8 ENEE 440 PIC 18 Alarm Clock Alarms SM cccccccccccccccecccesesesesesecececeseseseseseseseseseseseseseseses
59. r which is part of the display routine Too many hours used to figure that out those two lines enabled HDCount to be decremented inside of MT call 4 24 2008 After setting alarm to ring MS 7 it seems to get turned off automatically not sure where If you set it to ring in your main loop it rings properly LEDs flash green yellow red off at 1 Hz Also states 26 and 36 don t proceed properly have to hit t to get out of then are stuck can t move buttons for a couple seconds States 06 and 16 seem to still work 4 25 2008 Not an error got time date change functions to work off of pointers call with amount change in W and what to change in FSRO Makes minutes seconds the same code is really just the rules on the range of the value you want to change minutes can only be between 00 and 59 4 26 2008 Cannot have alarms at 0x800 restricted Putting them at 0x100 also placing alarm modifications at 0x180 so you will only be able to have 30 custom alarms as opposed to 64 LED s do not refresh correctly when rotary encode interrupt is established wasn t backing up FSRO did that and now can see what should be displayed except lots of flickering and buttons are not the same anymore Removal of earlier attempt to correct display corrected button use but still flicker Writing 1 s to the anodes fixed this Ghosting still seems to exist but don t think it is from the rotary encoder Strange rota
60. ry encoder error when turning from RE_POS gt 0 to RE_POS lt 0 we turn on the bottom two LED s to green depending on circumstance turns on all green LED s Ok not always turning on the same green LED s but most of them most of the time 4 30 2008 Had an error when turning on the buzz interrupt display was flickery adjusted the SPP macros then there was no display update Just undid it all 5 1 2008 Got the dial in the snooze to work but most of the snooze part does not Calculation of snooze alarm from MT Md and snooze_duration is not working correctly think it comes from DelayState being altered in state00 which needs to be partially run to see display but the button pressing part should be skipped over Seemed to have fixed that problem After took out the code to test the display made it look at switches in state00 it stared flickering a bunch and only in state00 everything else seems to work Pressing a while alarm is ringing still goes to snooze Tried putting display refresh in RE interrupt but display is very dim though should be at around 2kHz No real flicker though Tried in MT interrupt 100Hz very very flickery seizer Copied the call to Refresh_LED in the middle of State00 seemed to work well well maybe not left and right most 7seg look bright but others duller Sometimes it switches and the others are brighter seems to depend on MT min low put it in twice into MSOO and it
61. s accordingly if one is ringing includes responding to user input to turn it off Special Notes Self serving externally only alters MASTER_STATE Variables Used MASTER_STATE MASTER_TIME DATE alarm stack Source File ENEE 440 PIC18 Alarm Clock Main asm WN Feat le Mme oe Return Value Description Displays the time left of your snooze session the difference between the current time and the snooze alarm s time responds to the rotary encoder by updating the snooze alarm as well as exiting if nap is ended by rotary encoder Special Notes Regular states not arrived at while snooze bit is set Variables Used MASTER_STATE alarm stack MASTER_TIME DATE Source File ENEE 440 PIC18 Alarm Clock Main asm Time_Change_State Return Value Description Changes MASTER_STATE to DelayState when DelayCount is zero reset DelayCount Special Notes Variables Used MASTER_STATE DelayCount DelayState Source File ENEE 440 PIC18 Alarm Clock Main asm 5 19 08 Page 45 of 64 PIC HEU QU Developer s Guide John Tooker PIC18F4550 Implementation QreturnToState00Q Arguments Description Looks at the switched currently pressed if t is pressed then go to state30 set up delay state to take you to the default state in 0 2 seconds Special Notes Go to state30 first so pressing t not carried over to State00 Variables Used Sw_State_7C MASTER_STATE delayCount delayState Source File ENEE 440 PIC18 Alarm Clock Main asm Qde
62. seseeeueeaaaeaeeeeeeeeennaees 41 APPENDIXO FUNCTION API dica 45 AAPPENDIX Di MACROS 300 A A E A td tz 55 APPENDDGCE ISCIETABEES 22 ed a al a e e DA nel 57 APPEND A OTHER GONS TANTS tala lt da A dai aia 58 SPP AGOrOSS DOTINIIONS 0 da a A bn a oi oa A a A 58 Rotary Encoder Constants oociciiionicicnnnicnincinincccn cera 58 LED Segment Definitions Cathodes 00 cccscccceececeseceneneteneneseneeseneesenaneneneaseneacsaaeeseneneauaeesaeesenasesaeeseaeesaateseneneaeaseneas 58 LED Segment Bit Definitions Cathodes 0 1cccscccccsceccneseseneecseeeeseneecenaneseneeseaeecseaeetenenesanesseaeessaaseseneeseaeessateseneneseaeenaas 58 LED Area Definitions ANOG S oonmoociciniicononiccononnncccnorcrr AE A ARAE AEE AA AEA A EAA 58 To set the high bit of FSRO in LED_ Write argument to translate the WREG argument 1 cccccccceeeeeneeeteneteneeeenetens 58 Alarm Stack COnstants a a a a a a a a a aa aa aa aeaa e raea 58 Timing iN 1 100 Of a Second a ada Aia 58 APPENDIX G DEVELOPER S NOTES a a a a a a a a aaraa Ia ia 59 DT OM A E E EE TEE ETEA A E E E E E E EEA EE 62 INDE E E AE S A A R A AAE E A A A AE sus end E AE o E 63 5 19 08 Page 4 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation About this Manual Overview of this Manual s Layout This manual will start off with general explanations which will then lead into a deeper discussion of each structure and will end with appendices These list certain
63. shed Every hundredth of a second MASTER_TIME page gets incremented once Low Interrupt with dispatcher The low priority interrupt is divided into two sub priority interrupts in this implementation Up to eight sub priority interrupts can be managed with the two macros Sub_P_set_L and Sub_P_ex_L each interrupt executing both It may be helpful to be looking at ENEE 440 PIC18 Alarm Low Int asm now 5 19 08 Page 9 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation The former macro executed first looks to see if its interrupt has fired if so update Start_Srvs see page to acknowledge this states that we need to start servicing this interrupt Then we clear that interrupt s flag so we do not interrupt again The latter macro looks to see if we are in the middle of an interrupt if so resume IMPORTANT if this is the case a lower sub priority interrupt has interrupted a higher sub priority interrupt so we wish to continue with the higher sub priority interrupt which is why the macros should be written with the highest sub priority first Doing these out of order changes which bits correspond to the higher priority If this current interrupt in not in progress we check to see if it needs to be started looking at bit in Start_Srvs If so start it if two interrupts occur at the same time this will cause the higher priority to start before the other one does along with starting to servi
64. switches pressed you will need to mask of other bits Note these do not address the issue of ghosting the rotary encoder is similar but you do not need to write to the address first its address is the same as SWITHCES see the code comments for why which bits are which Clock Our processor is driven by a 40 MHz crystal but is divided multiplied to 48 MHz for the speed of the processor Actual instructions take this down by a factor of four 5 19 08 Page 38 of 64 PIC HE OLORI Developer s Guide John Tooker PIC18F4550 Implementation Non Obvious Methods This section is to address the not so apparent methods to my code State Jump Table In ENEE 440 PIC18 Alarm Clock Main asm line 1064 wanted a constant time function to get to a certain state We could compare to zero subtract 1 compare to zero can do the same thing with XOR but the last state on the list would not be reached for many step What I did was add the state value to the PC see page 23 for more detail about this and at the address the PC ended up at would be a GOTO to the correct state The downside to this is that we take up program memory with GOTO s that will never be reached I have pointed them to the default state in case of error Automatically Switching State By setting up a counter that decrements every 1 100 of a second we have pretty good and easy control of switching states every time main loop is called without taking too much extra
65. t states the green and red LED s are either off or are carry their previous value from the previous state What this state does Idle states use the delay state Description System to get out of their state Which registers are or are not effected by this state when Pediater Notes exception to their general use occurs 5 19 08 Page 24 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation State Diagram Page 0 Key PR Push and release Su Cr States HD Hold down W wait for a certain time to pass PRa O rotary dialed Run alarm MS 7 1 turn off code MS 7 0 Alarm is Ringing ex part of state00 d daily alarm button a alarm others buttons s set button VA corresponding up down button Ringing ex reg states Go to state 00 MT Alarm set snooze alarm based on MT amp AND L 0 L 1 1 L 1 1 LI L 1 L i t time button escape 1 1 L 1 LJ 0 0 LJ 0 1 0 1 i ser dialed to 0 1 User dialed to 0 Snooze Alarm n MS 6 0 Snooze NOT Set Nap Snooze time remaining 0 Guide for next two pages Actual State No mm Descrption of state Group relevent State No oe Description used before I assigned h actual state numbers display What will be diplayed State m Do this on the way to be done as we change Condition to states leave state
66. t to their state and they just stay in their state as long as that switch is being held down the set current time states do this Though most user input is state specific a couple types of user input occur in multiple states Pressing t to escape to default mode from just about ever state A function call to QreturnToState00Q page will check to see if the switch corresponding to the t button is pressed does not care if released only pressed If so the next state will be state30 page and the delay state is set to state00 page the default state We go to state30 so we can see the display of the default state but we do not register any button presses since t will still be held down it is noticed almost instantly and microseconds later we are in a different state before a human can physically release the button We are only in the phony state for a couple hundredths of a second but it is enough The user would have to act pretty quickly to want to press other buttons believing they are in the default state and find them not working Holding down a to change to next alarm in list from states that view the alarm stack page 27 A call to QchangeCurrentAlarmQ page will look to see if the a button is being held down if it is held down long enough we update the alarm pointers to view the next alarm in the list and are sent to the opening state of the alarm review sequence If we are add
67. tate State00 by holding down s Goes to While snooze nap is active we don t go to states This state doesn t display anything the snooze super ey state will display the time left in the snooze session Starts snooze session by turns on the snooze mode as Description well as setting the snooze nap alarm to current time date default nap time 5 19 08 Page 30 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation State03 View Daily Alarm Comes from Default state State00 by pressing d setting daily alarm s hours and minutes or the default Goes to state States 13 00 by holding down d pressing t or waiting 8 seconds Daily alarm s hours and minutes flash at 1 Hz REVIEW EDIT LED is red Description View the daily alarm s time Display State13 Set Daily Alarm Comes from Viewing daily alarm s time State03 by holding down d Goes to Viewing the daily alarm s time by releasing t Daily Alarm s time hours and minutes flash at 1 Hz REVIEW EDIT LED is red SET ALARM LED is red When holding down d pressing VA will change the daily alarm s time Display Description State0E Alarm Menu Comes from Default state State00 Review Alarms Add Alarms Snooze set Nap set or Goes to default state states 06 16 26 36 00 by pressing the corresponding AV or Y Display r A S n see above cell Presents a m
68. te If negative it means it should not be decremented Delay range 10 ms 5 46 min Time_Change_ State Many states ENEE 440 PIC18 Alarm Clock Main asm DOSEVEEIO Hold the number of a state when DelayCount becomes zero we should set MASTER_STATE to this registers value Time_Change_State Many states ENEE 440 PIC18 Alarm Clock Main asm HDCount 2 s compliment number Similar to DelayCount how many 0 01 seconds left until a switch has been held down as opposed to pressed and released Page 41 of 64 PIC CHEV QU Developer s Guide John Tooker PIC18F4550 Implementation When negative it is disabled Many States ENEE 440 PIC18 Alarm Clock Main asm 5 19 08 Page 42 of 64 PIC CHEV OLORI Developer s Guide John Tooker PIC18F4550 Implementation Alarm_SP IS AlarmRinging_g AddAlarm AddAlarmSpace ChangeCurAlarm DeleteAlarm Update ALARM_STATE gSpecial_Alarm_exist_q ENEE 440 PIC18 Alarm Clock Alarms asm Alarm_mod_SP bits AddAlarm AddAlarmSpace ChangeCurAlarm DeleteAlarm UpdateALARM_STATE qSpecial_Alarm_exist_q AlarmReset ENEE 440 PIC18 Alarm Clock Alarms asm snooze duration Is Holds the default length of snoozing after an alarm rings in hours and minutes compact BCD nap_duration Holds the default length of naps in hours and minutes compact BCD AlarmReset Set_Nap ENEE 440 PIC18 Alarm Clock Alarms asm Display_Buf NAAA CARTA LM LC RC RM GRN RED 7Seg 7Seg
69. uide John Tooker PIC18F4550 Implementation State02 View Current Time mm ss Comes from Viewing current day of the week State21 by pressing t setting current day of the time in minutes and seconds viewing current time in hours and minutes default state States 12 00 00 by holding down t pressing t or waiting 8 seconds respectfully Goes to Displ Current Time s Minutes and Seconds splay CURRENT TIME LED is red Description Displays the current time s minutes and seconds State12 Set Current Time mm ss Viewing current time s minutes and seconds State02 by Comes from Kolding down t Viewing the current time s minutes and seconds by Goes to releasing t Current Time s Minutes and Seconds flash at 5 Hz CURRENT TIME LED is red SET TIME LED is red When holding down t pressing VA will change the current time s minutes and seconds Display Description State33 Toggle Daily Alarm On Off Comes from Default state State00 by pressing and releasing s Goes to Delay to default state State30 Display Toggles the daily alarm on off Turns on by changing its date to the current date if alarm s time gt current time or Description changing its date to the next day of alarm s time lt current time To turn it off just put alarm s date to 0000 Only here for one cycle State04 Initiate Nap Snooze Mode Comes from Default s
70. unio AE 20 INITIALIZATION ci diia 21 REGISTER INITIALIZATION taos drovus cast ireland a adr reia idilio ere 21 RESET CAL ES o a o oe tN e OL oO 21 INTERRUPTS italiana ta data andes 21 MAIN LOOP e A E 22 REFRESHING THE DISPLAY santri ennta oi E E ceda NE 22 MANAGING ALARMS sonia aa A A ala 22 MANAGING IN SNOOZE MODE viseseds css ehedtshesewe fess ll Ad ts 22 5 19 08 Page 3 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation SWITCHED STATES BASED ON DEMAY TIME R a lili ts e add 23 GOING TOA STATES CODES till tant 23 STATES E E E E E E AE E E E R A A EA E E A E 24 EXPLANATION E A E AE E A A E A E E A e TE EEEIEE EEEE EEEE TEE EAT E 24 STATE LIST FORMAT A Cee ea edi e ok alc ee eel 24 HARDWARE ld 38 INPUTAND OUT PUT a cosh st av Le coc hake ii aa nd a red 38 ELO e cs ra ee be 38 NON OBVIOUS MET HODS lt a dio 39 STATE JUMPTABE cok Mice ie ola Nace tS sk Recital oa Sl och car alee ad ds olen Sea ea le ta cote Ratan ie oe ota 39 AUTOMATICALLY SWITCHING TA TEA tats aoe Sees a eae ti duageloabe dite currants 39 RESPONDING TO PRESSING OF A OV OH E a a er e aa a aa ea ea ra e a a a a eaa a nta ea ipai aiea 39 Pda N D E e E E P E A A eck ed 40 APPENDIX A ACRONYMS AND ABBREVIATIONS 00ccccccccesceeeeececceeseeueeesuuceeeceeeuueeuseceeeuaeuuaesseseeeeeeeuuaausseseeeseeauaeseseeeesueuaaees 40 APPENDIX B GLOBAL AND IMPORTANT VARIABLES ccccccccceeeeeeeeececsceeueeusceeeeeeeuueuueeceueseeuaueesceceeeeauaeaua
71. urrently Pressed 0 Currently Depressed Once a switch is read by a function that function may and should clear that bit to say that the switch press has been registered UpdateSwitches whoever need to know which switches are currently pressed ENEE 440 PIC18 Alarm Clock SW asm Sw_State_7C eee ON Pin Pin Pn Pin Pin Pin Same as Sw_State_16 except bit 1 is switch 7 bit 6 is switch C add 6 to the bit values to get the corresponding switch value UpdateSwitches whoever need to know which switches are currently pressed ENEE 440 PIC18 Alarm Clock SW asm 5 19 08 Sw_PState_16 Pin P n Pin Pin Pin Pin Before Sw_State_16 is updated it should be copied here so functions can see which switches were recently released UpdateSwitches whoever need to know which switches were pressed previously ENEE 440 PIC18 Alarm Clock SW asm Sw_PState_7C P n Pin Pin Pin Pin Pin Before Sw_State_7C is updated it should be copied here so functions can see which switches were recently released switches were pressed previously ENEE 440 PIC18 Alarm Clock SW asm Page 44 of 64 PIC HEU OLORI Developer s Guide John Tooker PIC18F4550 Implementation Appendix C Function API Main Arguments gt O Description Contains main loop initiates registers and interrupts Manage_Alarms Return Value Description Checks to see if an alarm is ringing and respond
72. which register we want to read or write from Reading from the SPPDATA for the switches rotary encoder is not filtered through a register but we can think of a register external that we get our data from We can see how to write an address write data and then read data from Microchip in Figure 7 cese atatatataenenestalseatatneneat nen JOE or Ce cee ce es ARA 1 1 j t 1 CK1SPP i l i T T T T T 1 1 1 1 1 1 1 1 1 1 1 CK2SPP i A 4 d 1 1 1 1 1 2 1 1 i 1 1 SPP lt 7 0 gt appr Y i X Daa Y DATA XK 4 I 1 1 1 A 1 1 1 1 A 1 1 1 Write Address Write Data Read Data MOVWF SPPEPS MOVWF SPPDATA MOVF SPPDATA W Figure 7 When reading and writing data we need to confirm that our transmission is complete This is done by checking the fourth bit of SPPEPS until it is clear Writing addresses data and reading data is done in two macros MOVWSPP MOVSPPW use MOVWSPP for both address and data see pages Here are the steps to writing to the 7seg set both anodes and cathodes MOVLW code to which area to write to all 1 s except area we want lit MOVWSPP ANODES a literal ee 0x02 MOVLW which lights are on off a letter number 1 off 0 on MOVWSPP CATHODES a literal ee OOL Here are the steps to read from the switches MOVLW code to which switches to allow 7 O for allow MOVWSPP SWITCHES a literal 77 0x00 MOVSPPW SWITCHES a literal 7 W holds O for

Download Pdf Manuals

image

Related Search

Related Contents

Final Report  MaxTech 28 Network Card User Manual    Atdec TH3070CTB2B User's Manual  SERVICE MANUAL I-0220 - Emerson Process Management  MCC104 user manual  R02 - 2004/01 ELEVATORE HOIST (1140623 - 110 V / 50 Hz  

Copyright © All rights reserved.
Failed to retrieve file