[ Pobierz całość w formacie PDF ]
.There is another useful assembler directive, EQU, which is anabbreviation for equates or is equal to.This can be used to makeprograms more readable by replacing some of the numbers withwords.For example, register 86 is the PortB Data Direction register butthe program would be easier to read if we replaced the number by thename.This would be done adding the line: PortBDDR EQU 86 beforethe program listing so as soon as the assembler spots the namePortBDDR it would replace it with 86.This has no affect on the finalprogram but it makes life easier for us which has got to be a goodthing.If we add some other labels, the final program can now be writtenas:;EQUATESPortBDDR EQU 86 ; PortB data direction reg.; is register 86PortB EQU 06 ; PortB data register is; register 06Status EQU 03 ; Status register is register; 03RP0 EQU 05 ; Bank1 is selected by bit 5Data EQU 55 ; Data used is 55HBSF Status,RP0 ; Sets bit 5 of register 3 to; select Bank1225Introduction to Microprocessors and MicrocontrollersCLRW ; puts a zero into register; WMOVWF PortBDDR ; copies the zero into; register 86 which is the; PortB data direction; registerBCF Status,RP0 ; clears bit 5 of register 3MOVLW Data ; output data to give the; on, off sequenceagain MOVWF PortB ; this copies the data into; PortBGOTO again ; this line forces the micro; to return to the previous; lineEND ; the end of our code to be; assembledBy making full use of labels, we have rewritten our program withoutany numbers at all.This is just a matter of choice all labels, somelabels or no labels, whatever we like.Using a crystalThis gives a more accurate clock speed so that programs that involvereal can be written.It may be that we want a display sequence to runat a particular rate.To change to a crystal we need to set up the configuration bits in thePIC so that it knows that it is being controlled by a crystal instead of theRC method.This is most easily handled during the assembly processby clicking on configuration bits and selecting the clock source fromthe options offered.Figure 16.3A CrystalControlledClock226Using a PIC microcontroller for a real projectThe two capacitors shown in Figure 16.3 in the clock circuit alwayshave equal values and the range shown is suitable for 200 kHz and allclocks of 2 MHz and over.Other recommended values are: 32 kHz 68/100 pF and 100 kHz 100/150 pF.The higher values in each rangeresults in higher stability but slower startup times.A ceramic resonator can be used as a plug-in replacement for thecrystal.A modification to the programIn the last program we controlled the voltages to each of the PortBoutputs.With slight modifications we would be able to apply anycombinations of voltages to control any external circuits.Even this firstcircuit has significant control capabilities but now we are going toextend the capability by applying a counting sequence to the outputsignals.All programs are built on the backs of other programs that we haveused before so we can save considerable time by keeping copies of oursuccessful programs to be recycled whenever possible.This is welldemonstrated in this example.The program consists of three steps, two of which we have alreadydesigned and tested, so we know it works.If the new program refusesto work, we don t have to start from scratch, we know two thirds of itis OK.This is a very powerful method of designing programs andwhole libraries of programs are available so new developments can bereduced to slotting together ready-made program segments.When we make changes to a previously program, it is important tosave the new version under a new name so that, in the event of adisaster, we can retreat and start again.Here is the section that we have borrowed from our previous work:ORG 000BSF 3,5CLRWMOVWF 86 ; PortB data direction = outputBCF 3,5MOVLW 55MOVWF 06 ; PortB data set to a start valueAt this stage we can, of course, set the start value for the output to anyvalue between 00H to FFH which is binary B 00000000 toB 11111111.We have only one new instruction to worry about: INCF f,d.Itincrements or increases the value of a selected file f by 1, and where227Introduction to Microprocessors and Microcontrollersthe new value goes to is determined by the value of the d term.If dis 0 the new value is put into the W register but if it is 1, the new valueis put back into the register in use
[ Pobierz całość w formacie PDF ]