mechatronics lab 1 group b

Upload: chrisvanloon

Post on 30-May-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Mechatronics Lab 1 Group B

    1/21

    1

    SUNY BuffaloCreating a Stop Watch with the Basic Stamp 2

    MAE 476 - Mechatronics2/22/2010

    Group B

    Ilya GartseevChris Van Loon

    Jenna CurryIan Clark

    Ian Duncan

  • 8/9/2019 Mechatronics Lab 1 Group B

    2/21

    2

    Table of Contents

    Abstract - pg. 3

    Introduction - pg. 3

    Theoretical Background - pg. 3-6

    Testing - pg. 6

    Problems Encountered - pg. 6-7

    User Instructions - pg. 8

    Schematics and Diagrams - pg. 9-13

    Results - pg. 14

    Description of Code - pg. 14-15

    Conclusion - pg. 15

    Contributions - pg. 15-16

    Appendix - pg 17-22

  • 8/9/2019 Mechatronics Lab 1 Group B

    3/21

  • 8/9/2019 Mechatronics Lab 1 Group B

    4/21

    4

    bouncing the circuit takes multiple samples of the button state successively to confirm that itstays with the same reading for a prescribed amount of time. This was demonstrated inexperiment 14 with the following code:

    Get_Buttons:btns = %1111 ' enable all four inputsFOR idx = 1 TO 5

    btns = btns & ~BtnBus ' test inputsPAUSE 5 ' delay between tests

    NEXTRETURN

    This code simply puts a pause between each sample and ensures that each state is the sameas the one before it.

    ASCII Code

    ASCII (American Standard Code for Information Interchange) is a method in which youcan encode symbols (such as letters) by simple numbers. This standard has assignednumbers to each symbol (given in a table below). As you can see from the table, digits 0-9 arerepresented by 48 to 57 respectively. This makes the task of conversion very simple (simplyadd 48 to the original digit).

  • 8/9/2019 Mechatronics Lab 1 Group B

    5/21

    5

    Table 1. AscII printable characters

    Pull Down Switches Pull-down switches are one method that allows input from a user to determine the state of theprogram. Pull down circuits have a switch connected to ground that is normally open. Usuallythe circuit has a voltage across it, which is detected as a high, logic 1. When the button ispressed, the connection to ground is completed. Now both ends of the circuit are connectedto ground and there is no voltage across the circuit, and the output is a low, logic zero. Hence,the pull-down circuit is active logic low. This type of circuit is more resistance to noise than apull-up circuit, since in order to have a change in the state the voltage most change from 5Vall the way to 1.5 V, whereas the pull-up circuit can have a change in logic state if the voltagechanges from 0V to only 1.5 V.

    Built -in Components The Basic Stamp board that was used in this lab had many built in features to protect themfrom high resistances and requires fewer external circuits that need to be wired. For example,the 7-segment LEDs have built in resistors. Also, the pull-down switches are internally wiredwith the necessary switch and resistors. Thus, only the inputs and outputs to the switchesneed to be wired.

    LEDs Light Emitting Diodes are components that emit light when current flows across them in only

  • 8/9/2019 Mechatronics Lab 1 Group B

    6/21

    6

    one direction. With LEDs, it is, therefore, very important to connect them with the right polaritysince the LEDs will only allow current to flow in the correct direction, positive anode tonegative cathode, and the will break down the LED if wired incorrectly. Resistors are used tolimit the current across the diode to prevent it from burning out. LEDs function by releasing

    energy when positive and negative charges recombine across a p-n junction.Testing

    We tested our system in a number of ways. We used sample programs and wiring diagramsfrom the Stamp Works Manual. These helped determine if different pieces of hardware werefunctioning properly. Experiments 1 through 3 were used to test the LEDs and learn how toprogram for them. The LEDs lit correctly when the programs were run. Experiment 6 showedus the commands necessary to use the buttons. The buttons were pressed and worked finewith this code. Experiments 8 and 11 involved the seven segment LED and LCD respectively.These codes demonstrated that both displays were working correctly and gave us the codesneeded to initialize them for our project.

    We began our project by wiring the led to a pin and running the part of the program we wrotefor it. This presented no problems so we moved on to implementing hte buttons. Our firstattempt at the code had some bugs. Because we used the pause command with our buttoncommand, short presses of the button to stop the timer were not registering. The code wasonly checking for a button press every half second. We then decided to base the timer on thelength of time it took the code to process. This took some calibration, using a debugstatement in the loop and an existing stopwatch, but worked well. The seven segment andLCD displays were added next. The initializations and a few other parts of the codes from theexperiments were used. The seven segment display showed the number of each button wepushed. The LCD did not display the character we intended though. This led us to look into

    the documentation for the device. We found the display used ASII code. We then changedthe code to reflect this and it worked. Debug statements were also used to make sure thatthe code was moving to and from the proper subroutines. The statements were outputted toa PC as the program ran so we could track the code's progress.

    Problems Encountered

    People working on projects of any nature ultimately run into problems. Solving the problem isessential to getting a final product that works. Listed and described below are examples of

    what problems our group faced while completing the lab. Also discussed is how weeventually solved them.One of the problems encountered dealt with writing to the 7 segment display. The example labusing the MAX 7219 chip utilized all four 7 segment displays included on the board. Our labonly required use with one of the 7 segment displays. We were getting unexpected results inregard to the number that was being displayed. For example, a 4 was being displayed, butthe top LED segment would be very dimly lit. We eventually realized that the initialization inthe experiment had the scan limit register to scan all four digits, when we only needed to scanone. This is shown by the bolded constant (set to 1) in the serial transmission of data:

  • 8/9/2019 Mechatronics Lab 1 Group B

    7/21

    7

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [ c7sOnlyDig i t , 1]

    Another problem encountered was the physical connection of the LCD display. We originally

    connected the LCD in accordance with version 2.1 of the Stamp Works manual. This had aslightly different connection than the connection described in the original documentation for the LCD in version 1.2 The comparison is shown below:

    Version 1.2

    Version 2.1

    As you can see these two schematics are slightly different. Our group didnt originally hook itup with a 4.7 K resistor. After this we could use the example experiment to enter in data to theLCD.

    Another one of the main issues encountered also dealt with the LCD and how exactlyto output data to it. At the beginning we assumed that you would have to simply output thenumber you wished to write to the LCD. This would work but it didnt give the character wewished. We may have tried something like 64 and get the symbol @. After thinking about itwe realized that this was simply ASCII representation. To convert, we output each digitindividually at its value added to 48 (See attached ASCII table)

    The rest of the lab was fairly straight forward in regard to problems encountered.These were the main holdups and how we dealt with them. Overall the lab was successfuland every member learned a lot about some of the inner workings of a lot of the components.

  • 8/9/2019 Mechatronics Lab 1 Group B

    8/21

    8

    User Instructions

    The set of user instructions below are to operate Group Bs BSII setup. It assumes theprogram has been loaded to the modules memory and the power supply is plugged in.

    Audible beeps will be heard to allow the user to identify which button was pressed. STEP 1-Flip the power switch on the stop watch- The green on LED should be lit. This indicates power is being suppliedto our project. If this is not lit, please the check connections. Also thesingle red LED should not be lit this indicates the LCD counter has notbeen initiated.

    STEP 2- Press Button 1 (Start) [ Location: D0 ]- This starts the counting on the LCD screen. You should also see the 1

    displayed on the seven segment display. This indicates the start buttona.k.a. button one has been pressed. Concurrently you should see singlered LED blinking every second to match the counter with LCD screen.

    STEP 3- Press Button 2 (Stop)[ Location: D1 ]- This buttons stops the counting on the LCD screen and leaves theupdated elapsed time on the display. The seven segments should displaya 2 to indicate button two has been pressed. At the same time thesingle red LED should be off.

    STEP 4- Press Button 3 (Reset)[ Location: D2 ]

    - This final button resets the time on the LCD screen and displays zeroesconsecutively. The seven segment display now displays a 3 to indicatebutton three is pressed. The single red LED should remain lit to indicateno time is being counted.

    Repeat steps 1-3 to record elapsed time of various events. Also note that the stop buttondoes not have to be pressed in order to reset the LCD.

  • 8/9/2019 Mechatronics Lab 1 Group B

    9/21

    9

    Schematic Drawings

    The schematic of Lab1 hardware is shown on Figure 1. The design is made on theStampWorks TM Experimental Board NX-1000 1 that is shown on Figure 2.

    The dashed boxes highlight electrical components built into the NX-1000 board. Theintegrated circuit U1 depicts Basic Stamp II (BS2) module 2. Module LS1 is built-in sounder.D2 is a one of the built-in LEDs. SWITCH1, SWITCH2, SWITCH3 are built-in buttons.

    The integrated circuit MAX7219 3 is a serial input/output common-cathode display driver that interfaces the BS2 to the 7-segment numeric LED display. Included on-chip are a BCDdecoder, segment and digit drivers, and an 8x8 static RAM that stores each digit. The externalresistor R6 is required to set the segment current for all LEDs. The external resistor R4 isrecommended by documentation.

    The module COMPUTER shows RS-232/USB connection with personal computer andcan be used for the debug purpose.

    Module LCD1 (LM020L) depicts parallel liquid crystal display with 2 rows and 16

    columns. The module is connected to the NX-1000 board by 14-pin header. The modulecontains built-in Hitachi HD44780 controller.For integrated circuits, signals GROUND and POWER are not shown.

    Parts list

    Table 2. Components used for the device

    1 The documentation for the NX-1000 board can be found on the CD that board goes along with.2 http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/bs2/List/0/SortField/4/ProductID/1/Default.aspx3 http://datasheets.maxim-ic.com/en/ds/MAX7219-MAX7221.pdf

    Quantity References Value Order Code3 Resistors

    1 R3 4.7K, 1/2W, 5% 271-11242 R4,R6 10K, 1/2W, 5% 271-1126

    2 Integrated Circuits

    1 U1 BS2-IC1 U3 MAX7219 603-00001

    2 Miscellaneous

    1 28135

    1 LCD1 603-00006

    BS2-IC, BasicStamp II module

    NX-1000,

    StampWorks TM ExperimentalBoardLM020L, 2x16Parallel LCD

    http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/bs2/List/0/SortField/4/ProductID/1/Default.aspxhttp://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/bs2/List/0/SortField/4/ProductID/1/Default.aspxhttp://datasheets.maxim-ic.com/en/ds/MAX7219-MAX7221.pdfhttp://datasheets.maxim-ic.com/en/ds/MAX7219-MAX7221.pdfhttp://datasheets.maxim-ic.com/en/ds/MAX7219-MAX7221.pdfhttp://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/bs2/List/0/SortField/4/ProductID/1/Default.aspx
  • 8/9/2019 Mechatronics Lab 1 Group B

    10/21

    10

    General idea of software algorithm

    The flowchart of software algorithm is shown on Figure 3 (pg 13).The block Start runs once after reset of the board. It contains calls to subroutine for

    initializing LCD and 7-segment indicator. After done, control is given to blockMain

    .The block Main contains main program cycle. It polls switches on every pass; updatestime counter, LCD, blinking LED if corresponding flags are set. The block Main gets controlafter every other block is done.

    The blocks lSw1Pressed, lSw2Pressed, lSw3Pressed runs when block Main detectsswitching on of Switches 1, 2, 3, consequently. The block lSw1Pressed sets flag 'Counting'and shows '1' on 7-segment indicator. The block lSw2Pressed clears flag 'Counting', shows '2'on 7-segment indicator,and turns off the blinking LED. The block lSw3Pressed clears flag'Counting', shows '3' on 7-segment indicator, turns on blinking LED, clears current time, andupdates the LCD (with zero value).

    B S 2

    P05

    P16

    P27

    P38

    P49

    P510

    P611

    P712

    P813

    P914

    P1015

    P1116

    P1217

    P1318

    P1419

    P1520

    ATN3

    RES22

    TX1

    RX2

    U1

    BS2-IC

    RXD

    RTS

    TXD

    CTS

    COMPUTER

    GND

    VDD

    510built-inD2LEDGreen

    GND

    SWITCH1

    LEDisblinking,Timeriscounting

    10kbuilt-in

    GND

    VDD

    SWITCH2

    LEDisOFFTimerisstopped

    10kbuilt-in

    GND

    VDD

    SWITCH3

    LEDgoesONTimerisreset

    10kbuilt-in

    Lab.BaictamIIiitaI/O--ImemetiadiitatoatchGroupB

    LS1

    SOUNDER7din7clk7load

    lcd4

    lcd5lcd6lcd7lcdElcdRWlcdRS

    Verion4

    A14

    B16

    C20

    D23

    E21

    F15

    G17

    DP22

    DIG02

    DIG111

    I26

    I37

    I43

    I510

    I65

    I78

    DIN1

    LOAD12

    CLK13

    IET18

    UT24

    U3

    AX721

    6

    10krn-lk-rg

    7clk

    7din

    R410kBrn-Blk-Org

    7load

    l c d 4

    l c d 5

    l c d 6

    l c d 7

    lcdRSlcdRWlcdE

    R34k7yel-vio-red

    (P7)(P5)(P6)

    (P14)(P13)(P12)

    ( P 8 )

    ( P 9 )

    ( P 1 0 )

    ( P 1 1 )

    D 7

    1 4

    D 6

    1 3

    D 5

    1 2

    D 4

    1 1

    D 3

    1 0

    D 2

    9

    D 1

    8

    D 0

    7

    E

    6

    R W

    5

    R S

    4

    V S S

    1

    V D D

    2

    V E E

    3

    LCD1LM020L

    sw1sw2sw3

    sw1sw2sw3

  • 8/9/2019 Mechatronics Lab 1 Group B

    11/21

    11

    Figure 1. Schematic drawing

  • 8/9/2019 Mechatronics Lab 1 Group B

    12/21

    12

    Figure 2. Assembled hardware

  • 8/9/2019 Mechatronics Lab 1 Group B

    13/21

    13

    Figure 3. Flowchart of software algorithm

    Results

    The purpose of this experiment, the creation of a digital stopwatch, was successfullyaccomplished. When button 1 is pressed, the circuit for the pull up switch has zero voltageacross it, hence transmits a logic zero. The pressing of the button is detected by the basicstamp and a digital signal that corresponds to a 1 is transmitted to the MAX7219 whichcontrols the LED display by outputting a high to the correct segments of the LED display (in

  • 8/9/2019 Mechatronics Lab 1 Group B

    14/21

    14

    this case to a, e, and f.) When button 1 is pressed, the program also begins counting. This isdone by counting the number of times that the system runs through the program, which loopsthrough the code until it detects that another button is pressed. The number of cycles that theloop runs through is converted to real time by dividing by calibration number, which is the

    number of times the loop is completed in 1 sec. This calibration number was measured usingreal time and the number of cycles of the loop. The time is transmitted in AscII code to theLCD display, which is updated every second to show the correct time. When button 2 ispressed, a 2 is displayed on the LED display and the time stops counting. The last number outputted before the 2 was pressed remains on the LCD display. Button 3 allows the systemto be reset, by setting all of the counters and the time to zero. Button is also displayed on theLCD display. All of the buttons have unique frequency sound that is created using the piezo-beepers.

    In this lab many different components needed to be integrated, including switches, A piezo-beeper, an LCD display, an LED display, and a microcontroller. In addition, there was a lot of interfacing that needed to be accomplished. Having unique sounds for each of the buttonsallows the user to easily identify the button that was present. Creating a debugging screenallows the problems to be understood and fixed if they occur. All of these concepts areextremely useful and can be built upon to create more functional and complicated systems.

    Description of the Code

    This section will thoroughly describe the function of the code found in the appendix of the report. Also included is a block diagram to its basic function found in the schematicssection.

    The program begins by assigning the pins of the basic stamp names specific to their function. Also assigned are constants for LCD functions and MAX 7219 functions. In the midstof these constants being declared are the declaration of variables dealing with time.

    The program begins executing code when it starts by going to subroutines dedicated tothe initialization of the LCD and the 7-segment chip. The LCD initialization sets the LCD in 4bit mode (takes one nibble of data at a time), clears the screen, then displays a message Lab1 Group B . The 7 segment initialization sets the seven segments intensity, decode modesetting, etc. It then tests the 7 segment display by outputting high on all segments for 300 ms,followed by clearing them. Both the LCD and 7 segment display are now ready for use.

    In the main loop, many conditions are tested, which determines where the program willbranch. First, all three states of the button are tested. If either of the three buttons arepressed, the program veers to a separate subroutine which will be described later. Theprogram then continues to an if statement testing to see whether or not the stop watch is infact counting. If the stop watch is counting, another if statement tests to see if the LED shouldbe off or on. The LED is on for 20 cycles and off for 164 cycles (totaling 184 cycles which isthe amount of cycles calibrated to one second). The blinking counter resets every 184 cyclesin order to repeat the blinking cycle. The program then increments both the blinking counter (too keep track of the LED) and the temporary time counter. The program then checks to seewhether or not the temporary time counter has reached a calibrated value of how many cyclesof the loop it takes to reach 1 second. This value (as mentioned) was found to be 184 bymeasuring the counter value after 5 minutes and comparing them. The stop watch is actually

  • 8/9/2019 Mechatronics Lab 1 Group B

    15/21

    15

    fast 1 second after about 16 minutes of use. If one second is reached, then the temporarytime is reset to 0 in order to be incremented again. The program in this situation also updatesthe LCD screen with an incremented time (this subroutine will also be described later). After all of this comparing and math completed, the program returns to the beginning to do it all

    again. The subroutine dedicated to the event of switch 3 (reset) being pressed executes thefollowing tasks. First, the counting variable is set to 0 so the program knows that thestopwatch is no longer counting. The LED is turned on, and a 3 is sent to the 7 segment digit

    (signifying the 3 rd switch being pressed). The time variables are all reset to 0, in order toprime the stop watch to restart. Finally, the Update LCD subroutine is called.The subroutine dedicated to the event of switch 2 (stop) being pressed executes the followingtasks. First, the counting variable is set to 0, and the LED is turned off. The seven segment isset to 2 as well. The program then returns to main.The subroutine dedicated to the event of switch 1 (start timer) being pressed executes thefollowing tasks. The counter variable is set to 1 allowing the controller to know that the stop

    watch has begun counting. The LCD is updated, followed by the LED blinking variable beingreset. The seven segment display is set to 1 and the program returns to main.The update LCD subroutine is a precursor to the write LCD subroutine. This subroutine firstclears the LCD. It then enters a 4 cycle loop where each cycle takes an individual digit of thetime variable and assigns it (+48 to convert to ASCII) to the character variable. In each of these cycles the LCD write subroutine is also called, where the program sends 2 thecharacter variable in 2 nibbles to the LCD.

    Conclusion

    Overall, our experiment was successful. The stopwatch is fairly accurate and has the ability

    to be calibrated. The LED, displays, speaker, and buttons were implemented and worked asintended. The code functioned correctly with no known bugs.

    Contributions

    Jenna Report: Theoretical background, resultsHardware and Software: Code for LED, timing and buttons, general code structure

    Ilya Report: Schematics, diagramsHardware and Software: Wiring of components, initialization code, LCD output

    Chris Report: Problems encountered, code descriptionHardware and Software: Wiring of components, initialization code, LCD output

    Ian Duncan - Report: Abstract, introductionHardware and Software: Code for LED, timing and buttons, general codestructure

    Ian Clark Report: Assembly, editing and formating of the report, testing the deviceHardware and Software: Code for LED, timing and buttons, general code structure

    Appendix

    '*****************************************************************************'/**

  • 8/9/2019 Mechatronics Lab 1 Group B

    16/21

    16

    '* @file lab1.bs2'* @hardware BasicStamp II'* @lab 1. Digital I/O - Implementing a digital stopwatch'* @part All'* @see Schematic: lab1bv3.dsn'* @date 12 feb 2010'* @author Team B'* @desc'*/'*****************************************************************************' {$STAMP BS2}' {$PBASIC 2.5}

    '*****************************************************************************' Variables and constants'*****************************************************************************

    ' Pin namesspeaker PIN 3pinSw1 PIN 0pinSw2 PIN 1pinSw3 PIN 2pinLED PIN 15pin7sData PIN 7 ' after change these values, necessary to change init of 7SEGpin7sClock PIN 6pin7sLoad PIN 5E CON 12 ' LCD Enable pin (1 = enabled)RS CON 14 ' Register Select (1 = char)LCDbus VAR OUTC ' 4-bit LCD data busmsg DATA "LAB 1 - GROUP B "' ------------------------------------------------------------------------------' Constants for LCD' ------------------------------------------------------------------------------ClrLCD CON $01 ' clear the LCDCrsrHm CON $02 ' move cursor to home position'CRSRLF CON $10 ' move cursor left'CRSRRT CON $14 ' move cursor rightDispLf CON $18 ' shift displayed chars leftDispRt CON $1C ' shift displayed chars rightDDRam CON $80 ' Display Data RAM control

    ' Temp variables for switchesvSw1 VAR BytevSw2 VAR BytevSw3 VAR Byte

  • 8/9/2019 Mechatronics Lab 1 Group B

    17/21

    17

    ' Constants for switchescSwitchDownState CON 0cNoAutoRepeat CON 255cAutoRepeatRate CON 0cStateForBranch CON 1

    ' Blink parameterscBlinkingT1 CON 20 ' time for OFF during blinkingcBlinkingT2 CON 184 ' time for ON during blinking

    ' VariablesvCounting VAR Bit ' 1 - we are counting nowvBlinkingCnt VAR Word ' counter for blinking (look at Blink parameters)vTimeCnt VAR Word ' counter for timevTimeCntTmp VAR Word ' temporary counter for time

    char VAR Byte ' character sent to LCDindex VAR Byte ' loop counter

    ' Registers of MAX7219 (LED driver) and constants for itr7sDecode CON $09 ' bcd decode registerr7sBright CON $0A ' intensity registerr7sScan CON $0B ' scan limit registerr7sShutDn CON $0C ' shutdown register (1 = on)r7sTest CON $0F ' display test mode

    c7sBlankDigit CON %1111 ' blank a digit

    c7sOnlyDigit CON 1 ' digit for showing last pushed switch

    cTimeCalibration CON 184

    '*****************************************************************************

    Start:DEBUG "Group B", CR, "Lab 1", CR

    GOSUB Init7s ' Init of 7-segment indicatorGOSUB InitLCD ' Init of LCD

    vCounting = 0

    Main:' BUTTON PIN, DownState, Delay, Rate, Workspace, TargetState, Address

    BUTTON pinSw3, cSwitchDownState, cNoAutoRepeat, cAutoRepeatRate, vSw3, cStateForBranch,lSw3Pressed

    BUTTON pinSw2, cSwitchDownState, cNoAutoRepeat, cAutoRepeatRate, vSw2, cStateForBranch,

  • 8/9/2019 Mechatronics Lab 1 Group B

    18/21

    18

    lSw2PressedBUTTON pinSw1, cSwitchDownState, cNoAutoRepeat, cAutoRepeatRate, vSw1, cStateForBranch,

    lSw1Pressed

    IF vCounting = 1 THENIF vBlinkingCnt = cBlinkingT1 THEN

    LOW pinLEDELSEIF vBlinkingCnt = cBlinkingT2 THEN

    HIGH pinLEDvBlinkingCnt = -1

    ENDIFvBlinkingCnt = vBlinkingCnt + 1vTimeCntTmp = vTimeCntTmp + 1IF vTimeCntTmp = cTimeCalibration THEN

    vTimeCnt = vTimeCnt + 1IF vTimeCnt = 10000 THEN vTimeCnt = 0vTimeCntTmp = 0GOSUB UpdateLCD

    ENDIFENDIF

    GOTO Main

    END

    '*****************************************************************************

    lSw3Pressed:DEBUG "LED is On, Time is reset.",CRFREQOUT speaker, 250, 1046vCounting = 0

    HIGH pinLED

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [c7sOnlyDigit, 3]PULSOUT pin7sLoad,5

    vTimeCnt = 0vTimeCntTmp = 0GOSUB UpdateLCD

    GOTO Main

    '*****************************************************************************lSw2Pressed:

    DEBUG "LED is Off, Timer is stopped.",CR

  • 8/9/2019 Mechatronics Lab 1 Group B

    19/21

    19

    FREQOUT speaker, 250, 1318vCounting = 0

    LOW pinLED

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [c7sOnlyDigit, 2]PULSOUT pin7sLoad,5

    GOTO Main

    '*****************************************************************************lSw1Pressed:

    DEBUG "LED is blinking, Timer is counting.",CRFREQOUT speaker, 250, 1568vCounting = 1

    GOSUB UpdateLCD

    vBlinkingCnt = 0

    HIGH pinLED

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [c7sOnlyDigit, 1]PULSOUT pin7sLoad,5

    GOTO Main

    '*****************************************************************************' Function for initializing LCD.'*****************************************************************************InitLCD:

    DIRH = %11111111 ' setup pins for LCDLCD_Init:PAUSE 500 ' let the LCD settleLCDbus = %0011 ' 8-bit modePULSOUT E, 1

    PAUSE 5PULSOUT E, 1PULSOUT E, 1LCDbus = %0010 ' 4-bit modePULSOUT E, 1char = %00001100 ' disp on, crsr off, blink offGOSUB LCD_Commandchar = %00000110 ' inc crsr, no disp shiftGOSUB LCD_Command

  • 8/9/2019 Mechatronics Lab 1 Group B

    20/21

    20

    char = ClrLCD ' clear the LCDGOSUB LCD_Commandindex=msgFOR index = 0 TO 16

    READ index, char ' get character from EEGOSUB LCD_Write

    NEXTRETURN

    '*****************************************************************************' Function for updating LCD.' Current time (in cycles) is in variable vTimeCnt'*****************************************************************************UpdateLCD:

    char = ClrLCD ' clear the LCDGOSUB LCD_CommandFOR index = 3 TO 0

    char = (vTimeCnt DIG index) + 48GOSUB LCD_Write

    NEXT'DEBUG DEC vTimeCnt, CR

    RETURN

    '*****************************************************************************

    ' Initialization of 7-segment indicator'*****************************************************************************Init7s:

    DIR5 = 1 ' pins of 7SEG control should beOUTPUT

    DIR6 = 1DIR7 = 1

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [r7sScan, 0] ' how many digits are displayed (0 -only one, 7 - all)

    PULSOUT pin7sLoad,5 ' lenght of LOAD pulse (from

    example)

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [r7sBright, 5] ' intensity (0 - low, 15 - high)PULSOUT pin7sLoad,5

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [r7sDecode, 1] ' Decode-mode for digit 0 (pg.7 ofdatasheet)

    PULSOUT pin7sLoad,5

  • 8/9/2019 Mechatronics Lab 1 Group B

    21/21

    21

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [r7sShutDn, 1] ' 1 - normal operation, 0 - shutdownPULSOUT pin7sLoad,5

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [c7sOnlyDigit, c7sBlankDigit] ' makes all segments off

    for futurePULSOUT pin7sLoad,5

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [r7sTest, 1] ' test - all segments go on for somems

    PULSOUT pin7sLoad,5

    PAUSE 300

    SHIFTOUT pin7sData, pin7sClock, MSBFIRST, [r7sTest, 0] ' test mode goes off, now 7SEG isready for use

    PULSOUT pin7sLoad,5

    RETURN'*****************************************************************************

    ' ------------------------------------------------------------------------------' Subroutines' ------------------------------------------------------------------------------

    LCD_Command:LOW RS ' enter command mode

    LCD_Write:LCDbus = char.HIGHNIB ' output high nibblePULSOUT E, 1 ' strobe the Enable lineLCDbus = char.LOWNIB ' output low nibblePULSOUT E, 1HIGH RS ' return to character modeRETURN