ECE 477 Digital Systems Senior Design Project Spring 2009 Homework 9: Software Design Considerations Due: Friday, March 27, at NOON Team Code Name: _The Magic Wand____________________________ Group No. __5___ Team Member Completing This Homework: _Nina Vintila___________________________ E-mail Address of Team Member: _____mvintila _________ @ purdue.edu NOTE: This is the last in a series of four “design component” homework assignments, each of which is to be completed by one team member. The body of the report should be 3-5 pages, not including this cover sheet, references, attachments or appendices. Evaluation: SCORE DESCRIPTION Excellent – among the best papers submitted for this assignment. Very few corrections needed for version submitted in Final Report. Very good – all requirements aptly met. Minor additions/corrections needed for 9 version submitted in Final Report. Good – all requirements considered and addressed. Several noteworthy 8 additions/corrections needed for version submitted in Final Report. Average – all requirements basically met, but some revisions in content should 7 be made for the version submitted in the Final Report. Marginal – all requirements met at a nominal level. Significant revisions in 6 content should be made for the version submitted in the Final Report. Below the passing threshold – major revisions required to meet report * requirements at a nominal level. Revise and resubmit. * Resubmissions are due within one week of the date of return, and will be awarded a score of “6” provided all report requirements have been met at a nominal level. 10 Comments: ECE 477 Digital Systems Senior Design Project Spring 2009 1.0 Introduction The Magic Wand is a hand writing recognition device that uses changes in acceleration to determine character writing patterns and displays interpreted characters to a display. The product consists of two separate parts: the Magic Wand, a pen shaped device, and a base station that performs the motion analysis to determine characters. The portable device will be able to collect motion and battery related information and wirelessly send it to the base station. It will sample accelerometer data around 120 Hz and transmit the data along with battery status to the base station. The base station will buffer incoming acceleration data and wait for an ‘end of character’ signal to begin analyzing the buffered data and look for patterns that correspond to specific characters. 2.0 Software Design Considerations The Magic Wand product requires two different software design styles due to the fact that the Wand itself differs in functionality from the base station. While the portable device merely collects data from accelerometers, the base station processes the data and provides feedback tot the user. The Wand is primarily a data acquisition device and transmitter. The highest priority process on the pen is the acquisition and conversion of the analog data from the accelerometer. This process is done with a frequency of 120 Hz. The pen also uses an external interrupt from the LTC4150 fuel gauge to monitor battery use. The aggregate information (accelerometer and battery information) is formed into a structured packet and transmitted to the base station. The base station’s main purpose is to buffer the data received from the pen, provide nonvolatile storage for the battery information, and analyze the buffered data from the pen to look for character patterns. The MRF24J40MA transceiver [1] triggers an interrupt when data is received, communicating the data to the dsPIC33 via SPI. The dsPIC33 parses the transmitted data and stores the accelerometer data to RAM and stores the battery information from the pen to non-volatile storage. A periodic interrupt triggers an LCD update routine that looks at the current battery counts and analyzed characters in the display buffer and displays them on the LCD. -1- ECE 477 Digital Systems Senior Design Project Spring 2009 2.1 Memory Mapping The PIC18F2320 [2] microcontroller was chosen for use on the portable device due to its convenient compatibility with the transceiver module. The memory map for the PIC18F2320 is displayed in Figure 2.1.1. Program memory will begin at 18h and continue until 1FFFh. This allows for 8KB of memory to hold the main program along with variables. The MiWi protocol requires 3336 Bytes of program memory and 150 Bytes of RAM. Thus, special care must be given during the development stage that the program memory is used efficiently due to its limited amount of space. The return address stack allows for up to 31 program calls and is stored before the reset vector at location 0000h. From location 2000h on the memory is unimplemented in the PIC18F2320. Figure 2.1.1 PIC18 Memory Map [2] -2- ECE 477 Digital Systems Senior Design Project Spring 2009 The dsPIC33FJ128GP204 microcontroller [3] was chosen for the base station due to its compatibility with the WiMi communication protocol, while providing more memory to work with for processing the motion information. The dsPIC33FJ128GP204 has 128KB of user memory with 85.5KB of available program memory. This will provide ample space for the main program, along with space to store the buffered character data. A total of 5KB of flash memory will be reserved to store the data received from the pen. This reserved space provides plenty of space for a single character. If the buffer begins to be full an error message will be displayed on the LCD screen and the buffer will be reset. Figure 2.1.2 dsPIC33 Memory Map -3- ECE 477 Digital Systems Senior Design Project Spring 2009 2.2 Mapping of external interfaces The pen microcontroller has three ports, named PORTA, PORTB and PORTC. PORTA is used for input of analog accelerometer signals. PORTB contains the external interrupt pins that will be connected to the fuel gauge and transceiver respectively, as well as the programming pins and the connection to the pushbutton at the tip of the pen. PORTC interfaces the SPI communication between the transceiver and the microcontroller. A detailed table of these mappings is included in table 1 in Appendix C of this document. The base station microcontroller also has three ports named in an identical fashion. PORTA is only connected to the polarity signal of the fuel gauge, which indicates whether the battery is charging or discharging. PORTB is connected to all of the signals to and from the transceiver, as well as the programming pins. Lastly, all of the pins of PORTC will be used as digital outputs to LCD screen. Table 2 in Appendix C contains a detailed listing of the pin usage. 2.3 Utilization of integrated peripherals 2.3.1 PIC18F2320 The Analog to Digital convertor will be used to take input data from the accelerometers and store it into a buffer for transmission to the base station. This module uses 5 registers: ADRESH, ADRESL, ADCON0, ADCON1 and ADCON2. The first two registers hold the result of the conversion, the high and low byte respectively. The program will trigger a 10 bit, left justified conversion, and thus only the high byte will be used as a result, discarding the two least significant bits stored in the low byte. ADCON0 is used to select the channels for the conversion (0, 1 and 2 in our case), to enable the ATD module and to indicate status of the conversion. ADCON1 sets the types of inputs that the ATD pins serve as (analog or digital) to allow the unused channels to function as general purpose input/output. ADCON2 sets right/left justified option, the acquisition time and conversion clock. The timer module will be used in interrupt mode to sample the pushbutton. The registers pertinent to its functionality are the INTCON and T0CON. The 5th bit of INTCON enable TIM module interrupts, while T0CON sets the timer period after which and interrupt will be generated. The final integrated peripheral used by the pen microcontroller is the SPI module in master mode, which will be used for communication to the WiMi transceiver. It will be set up -4- ECE 477 Digital Systems Senior Design Project Spring 2009 using the SSPCON1 control register which contains clock polarity, master mode and clock frequency information, and SSPSTAT status register which includes a buffer full flag and a clock edge selection. The serial receive/transmit buffer is stored in register SSPBUFF, and the shift register is called SSPSR. 2.3.2 dsPIC33FJ128GP204 The dsPIC33 will be using two integrated peripherals the SPI and timer. The SPI module consists of a 16-bit shift register (SPI1SR), a buffer register (SPI1BUF), a control register (SPI1CON), along with a status register (SPI1STAT). The SPI on the dsPIC will operate in master mode. The timer on the dsPIC33 will be used in the updating and refreshing of the LCD. The T2CON register is used to set up the timer and control its functionality. The dsPIC33 provides the capability to mush 2 timers together and create a 32 bit counter, but for our purposes we will not need to do that. Address x00001A will be used in the interrupt vector table to specify how the interrupt will be handled. 2.4 Overall Organization of Application code Both the pen and base station will implement a ‘hybrid’ style of operation. This works best for our operation as the pen and base station use external interrupts along with timers and some program driven controls. A flowchart of the main program for the pen and base station and included in appendix A, figures A.1 and A.2 respectively. The pen will use a continuous loop to check and see if the timers or external interrupts have set any flags. The timer’s expiration (every 8 ms) will trigger an interrupt that will check the status of the pressure sensor pen tip (a simple push button will accomplish this function) and set a corresponding flag. The status of the pushbutton determines wither or not a 3 channel ATD conversion will be run on the next iteration through the main loop and the information will be placed in a transmission buffer. The external interrupt from the coulomb counter [4] will trigger the current count to be incremented in the designated memory location. When the buffer is filled, the accelerometer information is sent to the base station along with the battery status. If a sufficiently long period of time has passed without any characters being drawn, a packet will be transmitted containing only and update on battery discharge status. -5- ECE 477 Digital Systems Senior Design Project Spring 2009 The base station will also use a continuous loop to check flags and determine if timers or external interrupts have transpired. An external interrupt will be triggered from the transceiver to indicate that data is ready to be read. The coulomb counter will also use an external interrupt to indicate to the microcontroller that 0.307 Coulombs (0.085 mAh) of charge has been drained or, in the case of the base with a rechargeable battery, charged to the battery. A timer will be used to trigger an LCD update. When the main loop sees the LCD update flag set it will call a routine to display the current battery life of each devices, display if the two devices are connected, and display the current buffer of characters that have been recognized. The main debugging tool that we will be using throughout the development and testing stages will be the use of the LCD that will already be connected and set up on the board as a way to observe the changes in internal variables during operation of the device. 3.0 Software Design Narrative 3.1 Pen code hierarchy This section includes a detailed narrative of each module for the portable device software. The general hierarchical arrangement of the program is included in figure B.1 in appendix B. Except for the ATD conversion routine (which has been tested on the development board) and the MiWi protocol routines (provided by Microchip), the other modules are in development, with the pseudo-code finalized. The main module (main.c) contains the continuous polling loop illustrated in the functional flowchart (figure A.1). This loop continuously checks if the push button or data ready flags have been set since the previous iteration. If one of them is set, it clears it before calling the appropriate functions to service that instance of a signaled flag. Before starting the infinite loop, however, the main function calls a series of initialization functions. IOinit( ) sets the data direction registers for ports A, B and C according to the intended use, as indicated in table 1 of appendix C. Var_init( ) initializes any variables as needed for the operation of the program. Periph_init( ) initializes the control registers for the SPI and ATD module, while timer_init( ) fixes the value of the timer period and enables timer interrupts. If the push button flag is set, it means that at the last probing of the push button value tip it was found to be depressed, and thus the pb_handle( ) function is called. If the push button flag is cleared, but the previous push button state was pressed, then an end-of-character condition is -6- ECE 477 Digital Systems Senior Design Project Spring 2009 generated and added to the transmission buffer. If the data ready flag is set, then it is first cleared before the ATD conversion results are added to the transmission buffer. If the buffer is full, the transmit_axes( ) function is called. Lastly, if the data ready flag is not set, but the coulomb cont value is higher than a fixed threshold, then the transmit_battery( ) function is called. The pb_handle( ) function first clears the push button flag, before initializing a three channel ATD conversion on channels 0, 1, and 2, and stores each of these results in memory. Finally, it sets a data ready flag to be used by the main loop. The transmit_axes( ) function forms and sends a MiWi transmission packet using the modules provided by Microchip [5] to the base station. In this packet will include all the data in the ATD buffer and additionally the current value of the coulomb counter variable. This variable will then be cleared to avoid overflow, as the base station will be keeping track of the overall state of the pen battery. The transmit_battery( ) function simply forms a packet with only the coulomb counter value and transmits it to the base station, clearing the variable. This function is anticipated for the situation when the pen is on for a longer period of time without any characters being written, and this can cause and overflow in the counter variable. The timer interrupt subroutine will be used to sample to push button value and set the flag if the button is found to be pressed. The int0( ) will be generated as part of the handshaking protocol with the transceiver and will be handled by the corresponding modules, while int1( ) will simply increment the local coulomb counter value. 3.2 Base code hierarchy This section includes a detailed narrative of each module for the base station software. The general hierarchical arrangement of the program is included in figure B.2 in appendix B. While the routines for the MiWi protocol have been provided by Microchip, the rest of the code is currently under development from pseudo-code phase into c modules. The main module (main.c) contains the continuous polling loop illustrated in the functional flowchart (figure A.2). This loop continuously checks if the receiver or end-of-character flag has been set since the previous iteration. If one of them is set, it clears it before calling the appropriate functions to service that instance of a signaled flag. -7- ECE 477 Digital Systems Senior Design Project Spring 2009 Before starting the infinite loop, however, the main function calls a series of initialization functions. IOinit( ) sets the data direction registers for ports A, B and C according to the intended use, as indicated in table 1 of appendix C. Var_init( ) initializes any variables as needed for the operation of the program. Periph_init( ) initializes the control registers for the SPI module, while timer_init( ) fixes the value of the timer period and enables timer interrupts. If the receiver flag is set, then the pen is attempting to send information to the base, and the Receive_MiWi( ) function is called after the flag is cleared. If the end-of-character flag is set, then it is cleared and then the char_analysis( ) function is called. The Receive_MiWi( ) function will use the provided MiWi modules to read the data from the transceiver through the SPI lines, and parse this data. The local pen battery coulomb counter will be incremented with the value of the corresponding variable included in the transmission stack by the pen, and the end-of-character flag will be set if this condition is indicated in the transmission. Finally, if any ATD data is included in the stack, then it is stored into a circular buffer. If the buffer is full, and no end of character condition has been transmitted, then an error message is stored into the LCD buffer, the ATD data buffer is cleared and an error flag is raised. This flag will only be cleared when an end of character condition is received from the pen. This provision will prevent the data buffer to be overfilled by random “doodling” by the user, which provides much more accelerometer data that any character can generate. The char_analysis( ) function will use the data stored in the buffer to attempt a character match. This algorithm has not yet been finalized, and will be developed in further detail after the data from the PCB mounted accelerometer can be observed on an oscilloscope to get a better understanding of the nature of the signals received. If a match has been found, then this character is stored in the LCD buffer. On the contrary, if the data yields no clear results for a match, a ‘?’ will be displayed to indicate no match was found. The timer interrupt subroutine will call for an update on the LCD display. The first line out of the four available will contain battery life information. The values for the coulomb counters for the two batteries will be proportional to the number of “bars” on each of the batteries. The last three lines will be reserved for characters and/or error messages. If the buffer is filled almost completely, the first line will be deleted and the following two will be scrolled up to allow for an easy-to-follow display. -8- ECE 477 Digital Systems Senior Design Project Spring 2009 The int0( ) subroutine will be associated to the transceiver connection, and will thus set the receiver flag, while the int1( ) subroutine will simply increment or decrement the local coulomb counter flag depending on whether the battery is charging or discharging as indicated by the polarity signal connected to port A. 4.0 Summary In this report, a detailed overview of software design considerations was presented, followed by an in-depth description of the code functionality and structure. Both microcontrollers will run in a continuous polling loop that will be checking for flags set in interrupt subroutines. Additional features then the ones already described, that enhance user experience and reliability of the product, may be implemented if time permits. -9- ECE 477 Digital Systems Senior Design Project Spring 2009 List of References [1] Microchip MRF24J40MA Data Sheet, [Online Document], 2008 Nov, [cited 2009 Mar 27], Avaliable HTTP: http://ww1.microchip.com/downloads/en/DeviceDoc/70329b.pdf [2] Microchip PIC18F2220/2320/4220/4320 Data Sheet, [Online Document], 2007 Dec, [cited 2008 March 27], Available HTTP: http://ww1.microchip.com/downloads/en/DeviceDoc/39599g.pdf [3] Microchip dsPIC33FJ32GP302/304, dsPIC33FJ64GPX02/X04, and dsPIC33FJ128GPX02/X04 Data Sheet, [Online Document], 2008 Mar, [cited 2008 March 27], Available HTTP: http://ww1.microchip.com/downloads/en/DeviceDoc/70292B.pdf [4] Linear Technology LTC4150 Coulomb Counter/ Battery Gas Gauge, [Online Document], 2003, [cited 2008 march 27], http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1037,C1134,P235 4,D1556 [5] MiWi™ P2P for PIC18, PIC24, dsPIC, PIC32, [Online Document], 2008 Nov, [cited 2008 March 27], Available HTTP: http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2680&dDocN ame=en538363, [Individual module files: MSPI.c; P2P.c; SymbolTime.c; MRF24J40.h; MSPI.h; P2P.h; SymbolTime.h; GenericTypeDefs.h] -10- ECE 477 Digital Systems Senior Design Project Appendix A: Flowchart/Pseudo-code for Main Program Figure A.1 Functional Flowchart for Pen Device -11- Spring 2009 ECE 477 Digital Systems Senior Design Project Figure A.2 Functional Flowchart for Base Station -12- Spring 2009 ECE 477 Digital Systems Senior Design Project Spring 2009 Appendix B: Hierarchical Block Diagram of Code Organization ISR main.c timer( ) Initialization function pb_handle( ) transmit_axes( ) IOinit( ) var_init( ) periph_init( ) timer_init( ) int1( ) int0( ) transmit_battery( ) MiWi modules [5] Figure B.1 : Pen Hierarchical Design ISR main.c timer( ) Initialization function IOinit( ) var_init( ) periph_init( ) timer_init( ) Char_analysis( ) Receive_MiWi( ) MiWi modules [5] Figure B.2: Base Hierarchical Design -13- int1( ) UpdateLCD( ) int0( ) ECE 477 Digital Systems Senior Design Project Appendix C: Port Pins and Use PEN PORT USAGE Register PORTA PORTB PORTC PIN AN0 AN1 AN2 RA3 RA4 RA5 RA6 RA7 INT0 INT1 RB2 RB3 RB4 RB5 RB6 RB7 RC0 RC1 RC2 SCK SDI RDO RC6 RC7 Description Accelerometer Z Axis Accelerometer Y Axis Accelerometer X Axis Not Used Not Used Not Used Not Used Not Used MiWi Transceiver Interrupt Fuel Gauge Interrupt Not Used Pen Tip Push Button Not Used PGM PGC PGD GPIO Debug Pin Not Used MiWi Wake SPI Clock SPI Data In SPI Data Out Chip Select Transceiver Reset Table 1 – Port Mapping on PIC18 -14- Spring 2009 ECE 477 Digital Systems Senior Design Project Table 2 – Port Mapping on dsPIC33 -15- Spring 2009
© Copyright 2026 Paperzz