USING LEDs, LCDs AND GLCDs IN MICROCONTROLLER PROJECTS USING LEDs, LCDs AND GLCDs IN MICROCONTROLLER PROJECTS DoganIbrahim NearEastUniversity,Cyprus Thiseditionfirstpublished2012 #2012,JohnWiley&Sons,Ltd Registeredoffice JohnWiley&SonsLtd,TheAtrium,SouthernGate,Chichester,WestSussex,PO198SQ,UnitedKingdom Fordetailsofourglobaleditorialoffices,forcustomerservicesandforinformationabouthowtoapplyfor permissiontoreusethecopyrightmaterialinthisbookpleaseseeourwebsiteatwww.wiley.com. TherightoftheauthortobeidentifiedastheauthorofthisworkhasbeenassertedinaccordancewiththeCopyright, DesignsandPatentsAct1988. Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,inany formorbyanymeans,electronic,mechanical,photocopying,recordingorotherwise,exceptaspermittedbytheUK Copyright,DesignsandPatentsAct1988,withoutthepriorpermissionofthepublisher. Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsinprintmaynotbe availableinelectronicbooks. Designationsusedbycompaniestodistinguishtheirproductsareoftenclaimedastrademarks.Allbrandnames andproductnamesusedinthisbookaretradenames,servicemarks,trademarksorregisteredtrademarksoftheir respectiveowners.Thepublisherisnotassociatedwithanyproductorvendormentionedinthisbook.This publicationisdesignedtoprovideaccurateandauthoritativeinformationinregardtothesubjectmattercovered. Itissoldontheunderstandingthatthepublisherisnotengagedinrenderingprofessionalservices.Ifprofessional adviceorotherexpertassistanceisrequired,theservicesofacompetentprofessionalshouldbesought. LibraryofCongressCataloging-in-PublicationData Ibrahim,Dogan. UsingLEDs,LCDs,andGLCDsinmicrocontrollerprojects/DoganIbrahim. p.cm. Includesbibliographicalreferencesandindex. ISBN978-1-119-94070-8(cloth) 1. Informationdisplaysystems. 2. Liquidcrystaldevices–Automatic control. 3. Lightemittingdiodes–Automaticcontrol. 4. Microcontrollers. I.Title. TK7882.I6I1852012 629.8’9–dc23 2012009481 AcataloguerecordforthisbookisavailablefromtheBritishLibrary. PrintISBN:9781119940708 Setin10/12ptTImesbyThomsonDigital,Noida,India Contents Preface xiii Acknowledgements xv 1 IntroductiontoMicrocontrollersandDisplaySystems 1 1.1 MicrocontrollersandMicroprocessors 2 1.2 EvolutionoftheMicrocontroller 3 1.3 PartsofaMicrocontroller 4 1.3.1 Address 4 1.3.2 ALU 5 1.3.3 AnalogueComparator 5 1.3.4 Analogue-to-DigitalConverter 5 1.3.5 Brown-outDetector 5 1.3.6 Bus 5 1.3.7 CAN 6 1.3.8 CISC 6 1.3.9 Clock 6 1.3.10 CPU 6 1.3.11 EEPROM 6 1.3.12 EPROM 6 1.3.13 Ethernet 7 1.3.14 FlashMemory 7 1.3.15 HarvardArchitecture 7 1.3.16 IdleMode 7 1.3.17 Interrupts 7 1.3.18 LCDDrivers 8 1.3.19 Pipelining 8 1.3.20 Power-onReset 8 1.3.21 PROM 8 1.3.22 RAM 8 1.3.23 Real-timeClock 8 1.3.24 Register 9 1.3.25 Reset 9 1.3.26 RISC 9 1.3.27 ROM 9 vi Contents 1.3.28 SerialInput-Output 9 1.3.29 SleepMode 9 1.3.30 SupplyVoltage 10 1.3.31 Timers 10 1.3.32 USB 10 1.3.33 Watchdog 10 1.4 DisplayDevices 10 1.4.1 LED 10 1.4.2 7-SegmentLED 11 1.4.3 OLED 12 1.4.4 LCD 12 1.5 Summary 15 Exercises 15 2 PIC18FMicrocontrollers 17 2.1 ThePIC18F2410Microcontroller 18 2.2 PIC18F2410Architecture 19 2.2.1 TheProgramMemory 21 2.2.2 TheDataMemory 21 2.2.3 PowerSupplyRequirements 22 2.2.4 OscillatorConfigurations 24 2.2.5 TheReset 30 2.2.6 ParallelI/OPorts 31 2.2.7 TimerModules 38 2.2.8 Analogue-to-DigitalConverterModule 43 2.2.9 SpecialFeaturesoftheCPU 48 2.2.10 Interrupts 49 2.2.11 PulseWidthModulatorModule 53 2.3 Summary 56 Exercises 56 3 CProgrammingLanguage 59 3.1 CLanguagesforMicrocontrollers 59 3.2 YourFirstmikroCProforPICProgram 61 3.2.1 Comments 61 3.2.2 BeginningandEndingaProgram 62 3.2.3 WhiteSpaces 63 3.2.4 VariableNames 63 3.2.5 ReservedNames 64 3.2.6 VariableTypes 64 3.2.7 Constants 66 3.2.8 EscapeSequences 68 3.2.9 VolatileVariables 69 3.2.10 AccessingBitsofaVariable 69 3.2.11 sbitType 70 3.2.12 bitType 70 Contents vii 3.2.13 Arrays 70 3.2.14 Pointers 73 3.2.15 Structures 76 3.2.16 Unions 80 3.2.17 OperatorsinmikroCProforPIC 80 3.2.18 TheFlowofControl 90 3.3 FunctionsinmikroCProforPIC 101 3.3.1 FunctionPrototypes 102 3.3.2 voidFunctions 103 3.3.3 PassingParameterstoFunctions 104 3.3.4 PassingArraystoFunctions 106 3.3.5 InterruptProcessing 106 3.4 mikroCProforPICBuilt-inFunctions 108 3.5 mikroCProforPICLibraries 109 3.5.1 ANSICLibrary 109 3.5.2 MiscellaneousLibrary 111 3.6 UsingthemikroCProforPICCompiler 111 3.6.1 mikroCProforPICIDE 112 3.6.2 CreatingaNewSourceFile 118 3.6.3 CompilingtheSourceFile 122 3.7 UsingthemikroCProforPICSimulator 123 3.7.1 SettingaBreak-Point 124 3.8 OthermikroCProforPICFeatures 126 3.8.1 ViewStatistics 126 3.8.2 ViewAssembly 127 3.8.3 ASCIIChart 127 3.8.4 USARTTerminal 127 3.8.5 SevenSegmentEditor 127 3.8.6 Help 128 3.9 Summary 128 Exercises 129 4 PICMicrocontrollerDevelopmentTools–IncludingDisplay DevelopmentTools 131 4.1 PICHardwareDevelopmentBoards 132 4.1.1 SuperBundleDevelopmentKit 132 4.1.2 PIC18ExplorerBoard 132 4.1.3 PIC18F4XK20StarterKit 134 4.1.4 PICDEM4 135 4.1.5 PIC16F887DevelopmentKit 135 4.1.6 FUTURLECPIC18F4550DevelopmentBoard 137 4.1.7 EasyPIC6DevelopmentBoard 137 4.1.8 EasyPIC7DevelopmentBoard 139 4.2 PICMicrocontrollerDisplayDevelopmentTools 140 4.2.1 DisplayHardwareTools 140 4.2.2 DisplaySoftwareTools 143 viii Contents 4.3 UsingtheIn-CircuitDebuggerwiththeEasyPIC7DevelopmentBoard 145 4.4 Summary 149 Exercises 149 5 LightEmittingDiodes(LEDs) 151 5.1 ATypicalLED 151 5.2 LEDColours 153 5.3 LEDSizes 154 5.4 Bi-ColourLEDs 154 5.5 Tri-ColourLEDs 155 5.6 FlashingLEDs 155 5.7 OtherLEDShapes 155 5.8 7-SegmentLEDs 156 5.8.1 DisplayingNumbers 157 5.8.2 Multi-digit7-SegmentDisplays 159 5.9 AlphanumericLEDs 159 5.10 mikroCProforPIC7-SegmentLEDEditor 163 5.11 Summary 163 Exercises 164 6 LiquidCrystalDisplays(LCDs)andmikroCProforPICLCDFunctions 165 6.1 HD44780Controller 165 6.2 DisplayingUserDefinedData 168 6.3 DDRAMAddresses 169 6.4 DisplayTimingandControl 171 6.4.1 ClearDisplay 172 6.4.2 ReturnCursortoHome 172 6.4.3 CursorMoveDirection 172 6.4.4 DisplayON/OFF 172 6.4.5 CursorandDisplayShift 173 6.4.6 FunctionSet 173 6.4.7 SetCGRAMAddress 173 6.4.8 SetDDRAMAddress 173 6.4.9 ReadBusyFlag 174 6.4.10 WriteDatatoCGRAMorDDRAM 174 6.4.11 ReadDatafromCGRAMorDDRAM 174 6.5 LCDInitialisation 174 6.5.1 8-bitModeInitialisation 175 6.5.2 4-bitModeInitialisation 175 6.6 ExampleLCDDisplaySetupProgram 177 6.7 mikroCProforPICLCDFunctions 180 6.7.1 Lcd_Init 180 6.7.2 Lcd_Out 181 6.7.3 Lcd_Out_Cp 181 6.7.4 Lcd_Chr 181 Contents ix 6.7.5 Lcd_Chr_Cp 181 6.7.6 Lcd_Cmd 182 6.8 Summary 182 Exercises 183 7 GraphicsLCDDisplays(GLCD) 185 7.1 The128(cid:2)64PixelGLCD 185 7.2 OperationoftheGLCDDisplay 187 7.3 mikroCProforPICGLCDLibraryFunctions 189 7.3.1 Glcd_Init 189 7.3.2 Glcd_Set_Side 190 7.3.3 Glcd_Set_X 190 7.3.4 Glcd_Set_Page 190 7.3.5 Glcd_Write_Data 190 7.3.6 Glcd_Fill 190 7.3.7 Glcd_Dot 191 7.3.8 Glcd_Line 191 7.3.9 Glcd_V_Line 191 7.3.10 Glcd_H_Line 191 7.3.11 Glcd_Rectangle 192 7.3.12 Glcd_Rectangle_Round_Edges 192 7.3.13 Glcd_Rectangle_Round_Edges_Fill 192 7.3.14 Glcd_Box 193 7.3.15 Glcd_Circle 193 7.3.16 Glcd_Circle_Fill 194 7.3.17 Glcd_Set_Font 194 7.3.18 Glcd_Set_Font_Adv 194 7.3.19 Glcd_Write_Char 195 7.3.20 Glcd_Write_Char_Adv 195 7.3.21 Glcd_Write_Text 195 7.3.22 Glcd_Write_Text_Adv 195 7.3.23 Glcd_Write_Const_Text_Adv 196 7.3.24 Glcd_Image 196 7.4 ExampleGLCDDisplay 196 7.5 mikroCProforPICBitmapEditor 198 7.6 AddingTouch-screentoGLCDs 199 7.6.1 TypesofTouch-screenDisplays 200 7.6.2 ResistiveTouchScreens 200 7.7 Summary 203 Exercises 204 8 MicrocontrollerProgramDevelopment 205 8.1 UsingtheProgramDescriptionLanguageandFlowcharts 205 8.1.1 BEGIN–END 206 8.1.2 Sequencing 206 x Contents 8.1.3 IF–THEN–ELSE–ENDIF 206 8.1.4 DO–ENDDO 207 8.1.5 REPEAT–UNTIL 209 8.1.6 CallingSubprograms 209 8.1.7 SubprogramStructure 209 8.2 Examples 211 8.3 RepresentingforLoopsinFlowcharts 216 8.4 Summary 218 Exercises 218 9 LEDBasedProjects 219 9.1 PROJECT9.1–FlashingLED 219 9.2 PROJECT9.2–BinaryCountingUpLEDs 226 9.3 PROJECT9.3–RotatingLEDs 229 9.4 PROJECT9.4–WheelofLuckyDay 231 9.5 PROJECT9.5–RandomFlashingLEDs 239 9.6 PROJECT9.6–LEDDice 240 9.7 PROJECT9.7–ConnectingmorethanoneLEDtoaPortPin 246 9.8 PROJECT9.8–ChangingtheBrightnessofLEDs 250 9.9 PROJECT9.9–LEDCandle 264 9.10 Summary 267 Exercises 267 10 7-SegmentLEDDisplayBasedProjects 269 10.1 PROJECT10.1–SingleDigitUpCounting7-SegmentLEDDisplay 269 10.2 PROJECT10.2–DisplayaNumberon2-Digit7-SegmentLEDDisplay 271 10.3 PROJECT10.3–DisplayLotteryNumberson2-Digit7-SegmentLED Display 278 10.4 PROJECT10.4–EventCounterUsing4-Digit7-SegmentLEDDisplay 285 10.5 PROJECT10.5–ExternalInterruptBasedEventCounterUsing4-Digit 7-SegmentLEDDisplaywithSerialDriver 292 10.6 Summary 302 Exercises 303 11 TextBasedLCDProjects 305 11.1 PROJECT11.1–DisplayingTextonLCD 305 11.2 PROJECT11.2–MovingTextonLCD 307 11.3 PROJECT11.3–CountingwiththeLCD 310 11.4 PROJECT11.4–CreatingCustomFontsontheLCD 315 11.5 PROJECT11.5–LCDDice 317 11.6 PROJECT11.6–DigitalVoltmeter 325 11.7 PROJECT11.7–TemperatureandPressureDisplay 327 11.8 PROJECT11.8–TheHigh/LowGame 333 11.9 Summary 344 Exercises 345 Contents xi 12 GraphicsLCDProjects 347 12.1 PROJECT12.1–CreatingandDisplayingaBitmapImage 347 12.2 PROJECT12.2–MovingBallAnimation 355 12.3 PROJECT12.3–GLCDDice 357 12.4 PROJECT12.4–GLCDX-YPlotting 372 12.5 PROJECT12.5–PlottingTemperatureVariationontheGLCD 374 12.6 PROJECT12.6–TemperatureandRelativeHumidityMeasurement 385 12.7 OperationoftheSHT11 386 12.8 Acknowledgement 389 12.9 Summary 400 Exercises 400 13 TouchScreenGraphicsLCDProjects 401 13.1 PROJECT13.1–TouchScreenLEDON-OFF 401 13.2 PROJECT13.2–LEDFlashingwithVariableRate 410 13.3 Summary 418 Exercises 418 14 UsingtheVisualGLCDSoftwareinGLCDProjects 419 14.1 PROJECT14.1–ToggleLED 420 14.2 PROJECT14.2–TogglemorethanOneLED 425 14.3 PROJECT14.3–MiniElectronicOrgan 426 14.4 PROJECT14.4–UsingtheSmartGLCD 430 14.5 PROJECT14.5–DecimaltoHexadecimalConverterusingtheSmartGLCD 444 14.6 Summary 452 Exercises 452 15 UsingtheVisualTFTSoftwareinGraphicsProjects 453 15.1 PROJECT15.1–CountdownTimer 454 15.2 PROJECT15.2–ElectronicBook 462 15.3 PROJECT15.3–PictureShow 467 15.4 Summary 472 Exercises 472 Bibliography 473 Index 475