mikroPASCAL PRO for AVR May 2009. Reader’s note DISCLAIMER: mikroPASCAL PRO for AVR and this manual are owned by mikroElektronika and are Reader’s Note protected by copyright law and international copyright treaty. Therefore, you should treat this manual like any other copyrighted material (e.g., a book). The manual and the compiler may not be copied, partially or as a whole without the written consent from the mikroEelktronika.
Table of Contents CHAPTER 1 Introduction CHAPTER 2 mikroPASCAL PRO for AVR Environment CHAPTER 3 mikroPASCAL PRO for AVR Specifics CHAPTER 4 AVR Specifics CHAPTER 5 mikroPASCAL PRO for AVR Language Reference CHAPTER 6 mikroPASCAL PRO for AVR Libraries
mikroPASCAL PRO for AVR Table of Contents CHAPTER 1 Introduction to mikroPascal PRO for AVR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Where to Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 mikroElektronika Associates License Statement and Limited Warranty 4 IMPORTANT - READ CAREFULLY . . . . . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents Tools Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Project Menu Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Run Menu Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Tools Menu Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Help Menu Option . . . . .
Table of Contents mikroPASCAL PRO for AVR ROM Memory Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ROM Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Procedures Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Procedures Size Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Procedures Locations Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents Customizing Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Edit Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Managing Project Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Add/Remove Files from Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Project Level Defines . . . . . . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR Accessing Individual Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Accessing Individual Bits Of Variables . . . . . . . . . . . . . . . . . . . . . . . . . 96 sbit type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 bit type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Interrupts . . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents CHAPTER 4 Nested Calls Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Important notes: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 AVR Memory Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Program Memory (ROM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Data Memory . . . . . . . . . . . . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Comma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Semicolon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Colon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Dot . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents Multi-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 String Concatenating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Pointers . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents Nested if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Case statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Nested Case statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 For Statement . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents CANSPIGetOperationMode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 CANSPIInitialize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 CANSPISetBaudRate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 CANSPISetMask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 CANSPISetFilter . . . . . . . . . . . . . . . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR Cf_Fat_Delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Cf_Fat_Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Cf_Fat_Set_File_Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Cf_Fat_Get_File_Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Cf_Fat_Get_File_Size . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents Glcd_Set_Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Glcd_Write_Char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Glcd_Write_Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Glcd_Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Library Example . . . . . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR Multi Media Card Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Secure Digital Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 External dependencies of MMC Library . . . . . . . . . . . . . . . . . . . . . . . . 271 Library Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Mmc_Init . . . . . . . . . . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents Expander_Read_Byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Expander_Write_Byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Expander_Read_PortA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Expander_Read_PortB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Expander_Read_PortAB . . . . . . . . . . . . . . . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR Library Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 HW Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 PWM 16 bit Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Library Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents Soft_SPI_Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Library Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Software UART Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 External dependencies of Software UART Library . . . . . . . . . . . . . . . . 364 Soft_UART_Init . . . . . . . . . . . . . . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR Spi_Ethernet_getBytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 Spi_Ethernet_UserTCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 Spi_Ethernet_UserUDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Library Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 HW Connection . . . . . . . . . . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents SPI_Lcd_Cmd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Available SPI Lcd Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Library Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 HW Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 SPI Lcd8 (8-bit interface) Library . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR SPI_T6963C_SetBit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440 SPI_T6963C_NegBit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 SPI_T6963C_DisplayGrPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 SPI_T6963C_DisplayTxtPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 SPI_T6963C_SetGrPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents T6963C_DisplayTxtPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 T6963C_SetGrPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 T6963C_SetTxtPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 T6963C_PanelFill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 T6963C_GrFill . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Table of Contents mikroPASCAL PRO for AVR Button Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 External dependencies of Button Library . . . . . . . . . . . . . . . . . . . . . . . 484 Library Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Conversions Library . . . . . . . . . . . . .
mikroPASCAL PRO for AVR Table of Contents sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 tan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 tanh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 String Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Table of Contents XXVI mikroPASCAL PRO for AVR MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 1 Introduction to mikroPascal PRO for AVR Help version: 2009/05/18 The mikroPascal PRO for AVR is a powerful, feature-rich development tool for AVR microcontrollers. It is designed to provide the programmer with the easiest possible solution to developing applications for embedded systems, without compromising performance or control.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction Introduction to mikroPascal PRO for AVR Features - mikroPascal PRO for AVR allows you to quickly develop and deploy complex applications: - Write your Pascal source code using the built-in Code Editor (Code and Parameter Assistants, Code Folding, Syntax Highlighting, Auto Correct, Code Templates, and more.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction Where to Start - In case that you’re a beginner in programming AVR microcontrollers, read careful ly the AVR Specifics chapter. It might give you some useful pointers on AVR con straints, code portability, and good programming practices. - If you are experienced in Pascal programming, you will probably want to consult mikroPascal PRO for AVR Specifics first. For language issues, you can always refer to the comprehensive Language Reference.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction MIKROELEKTRONIKA ASSOCIATES LICENSE STATEMENT AND LIMITED WARRANTY IMPORTANT - READ CAREFULLY This license statement and limited warranty constitute a legal agreement (“License Agreement”) between you (either as an individual or a single entity) and mikroElektronika (“mikroElektronika Associates”) for software product (“Software”) identified above, including any software, media, and accompanying on-line or printed documentation.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction of the price paid, or (b) repair or replacement of Software that does not meet mikroElektronika Associates’ Limited Warranty and which is returned to mikroElektronika Associates with a copy of your receipt. DO NOT RETURN ANY PRODUCT UNTIL YOU HAVE CALLED MIKROELEKTRONIKA ASSOCIATES FIRST AND OBTAINED A RETURN AUTHORIZATION NUMBER. This Limited Warranty is void if failure of Software has resulted from an accident, abuse, or misapplication.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction GENERAL PROVISIONS This statement may only be modified in writing signed by you and an authorised officer of mikroElektronika Associates. If any provision of this statement is found void or unenforceable, the remainder will remain valid and enforceable according to its terms.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction TECHNICAL SUPPORT In case you encounter any problem, you are welcome to our support forums at www.mikroe.com/forum/. Here, you may also find helpful information, hardware tips, and practical code snippets. Your comments and suggestions on future development of the mikroPascal PRO for AVR are always appreciated — feel free to drop a note or two on our Wishlist. In our Knowledge Base www.mikroe.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction HOW TO REGISTER The latest version of the mikroPascal PRO for AVR is always available for downloading from our website. It is a fully functional software libraries, examples, and comprehensive help included. The only limitation of the free version is that it cannot generate hex output over 2 KB. Although it might sound restrictive, this margin allows you to develop practical, working applications with no thinking of demo limit.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction This will start your e-mail client with message ready for sending. Review the information you have entered, and add the comment if you deem it necessary. Please, do not modify the subject line. Upon receiving and verifying your request, we will send the license key to the e-mail address you specified in the form.
CHAPTER 1 mikroPASCAL PRO for AVR Introduction After Receving the License Key The license key comes as a small autoextracting file – just start it anywhere on your computer in order to activate your copy of compiler and remove the demo limit. You do not need to restart your computer or install any additional components. Also, there is no need to run the mikroPascal PRO for AVR at the time of activation. Notes: - The license key is valid until you format your hard disk.
CHAPTER 2 mikroPascal PRO for AVR Environment 11
CHAPTER 2 Environment mikroPASCAL PRO for AVR IDE OVERVIEW The mikroPascal PRO for AVR is an user-friendly and intuitive environment: - The Code Editor features adjustable Syntax Highlighting, Code Folding, Code Assistant, Parameters Assistant, Spell Checker, Auto Correct for common typos and Code Templates (Auto Complete). - The Code Explorer (with Keyboard shortcut browser and Quick Help browser) is at your disposal for easier project management.
CHAPTER 2 mikroPASCAL PRO for AVR Environment - Spell checker underlines identifiers which are unknown to the project. In this way it helps the programmer to spot potential problems early, much before the project is compiled. - Spell checker can be disabled by choosing the option in the Preferences dialog (F12).
CHAPTER 2 mikroPASCAL PRO for AVR Environment File Menu Options The File menu is the main entry point for manipulation with the source files. File Description Open a new editor window. Open source file for editing or image file for viewing. Reopen recently used file. Save changes for active editor. Save the active source file with the different name or change the file type. Close active source file. Print Preview. Exit IDE.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Edit Menu Options File Description Undo last change. Redo last change. Cut selected text to clipboard. Copy selected text to clipboard. Paste text from clipboard. Delete selected text. Select all text in active editor. Find text in active editor. Find next occurence of text in active editor. Find previous occurence of text in active editor. Replace text in active editor.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Find text in current file, in all opened files, or in files from desired folder. Goto to the desired line in active editor. Advanced Code Editor options Advanced » Description Comment selected code or put single line comment if there is no selection. Uncomment selected code or remove single line comment if there is no selection. Indent selected code. Outdent selected code. Changes selected text case to lowercase. Changes selected text case to uppercase.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Replace Text Dialog box for searching for a text string in file and replacing it with another text string. Find In Files Dialog box for searching for a text string in current file, all opened files, or in files on a disk. The string to search for is specified in the Text to find field. If Search in directories option is selected, The files to search are specified in the Files mask and Path fields.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Go To Line Dialog box that allows the user to specify the line number at which the cursor should be positioned. Regular expressions By checking this box, you will be able to advance your search, through Regular expressions.
CHAPTER 2 mikroPASCAL PRO for AVR Environment View Menu Options File Description Show/Hide toolbars. Show/Hide debug windows. Show/Hide Routine List in active editor. Show/Hide Project Settings window. Show/Hide Code Explorer window. Show/Hide Project Manager window. Show/Hide Library Manager window. Show/Hide Bookmarks window. Show/Hide Error Messages window. Show/Hide Macro Editor window. Show Window List window.
CHAPTER 2 mikroPASCAL PRO for AVR Environment TOOLBARS File Toolbar File Toolbar is a standard toolbar with following options: Icon Description Opens a new editor window. Open source file for editing or image file for viewing. Save changes for active window. Save changes in all opened windows. Close current editor. Close all editors. Print Preview. Edit Toolbar Edit Toolbar is a standard toolbar with following options: Icon Description Undo last change. Redo last change.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Advanced Edit Toolbar Advanced Edit Toolbar comes with following options: Icon Description Comment selected code or put single line comment if there is no selection Uncomment selected code or remove single line comment if there is no selection. Select text from starting delimiter to ending delimiter. Go to ending delimiter. Go to line. Indent selected code lines. Outdent selected code lines.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Project Toolbar Project Toolbar comes with following options: Icon Description Open new project wizard. wizard. Open Project Save Project Add existing project to project group. Remove existing project from project group. Add File To Project Remove File From Project Close current project. Build Toolbar Build Toolbar comes with following options: Icon Description Build current project. Build all opened projects. Build and program active project.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Debugger Debugger Toolbar comes with following options: Icon Description Start Software Simulator. Run/Pause debugger. Stop debugger. Step into. Step over. Step out. Run to cursor. Toggle breakpoint. Toggle breakpoints. Clear breakpoints. View watch window View stopwatch window Styles Toolbar Styles toolbar allows you to easily customize your workspace.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Tools Toolbar Tools Toolbar comes with following default options: Icon Description Run USART Terminal EEPROM ASCII Chart Seven segment decoder tool. The Tools toolbar can easily be customized by adding new tools in Options(F12) window.
CHAPTER 2 mikroPASCAL PRO for AVR Environment PROJECT MENU OPTIONS MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 25
CHAPTER 2 mikroPASCAL PRO for AVR Environment Project Description Build active project. Build all projects. Build and program active project. View Assembly. Edit search paths. Clean Project Folder Add file to project. Remove file from project. Open New Project Wizard Open existing project. Save current project. Open project group. Close project group. Save active project file with the different name. Open recently used project. Close active project.
CHAPTER 2 mikroPASCAL PRO for AVR Environment RUN MENU OPTIONS MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 27
CHAPTER 2 mikroPASCAL PRO for AVR Environment Run Description Start Software Simulator. Stop debugger. Pause Debugger. Step Into. Step Over. Step Out. Jump to interrupt in current project. Toggle Breakpoint. Breakpoints. Clear Breakpoints. Show/Hide Watch Window Show/Hide Stopwatch Window Toggle between Pascal source and disassembly.
CHAPTER 2 mikroPASCAL PRO for AVR Environment TOOLS MENU OPTIONS Tools Description Run mikroElektronika Programmer Run USART Terminal Run EEPROM Editor Run ASCII Chart Run 7 Segment Display Decoder Generate HTML code suitable for publishing source code on the web. Generate your own custom Lcd characters Generate bitmap pictures for Glcd UDP communication terminal.
CHAPTER 2 mikroPASCAL PRO for AVR Environment HELP MENU OPTION Help Description Open Help File. Quick Help. Check if new compiler version is available. Open mikroElektronika Support Forums in a default browser. Open mikroElektronika Web Page in a default browser. Information on how to register Open About window.
CHAPTER 2 mikroPASCAL PRO for AVR Environment KEYBOARD SHORTCUTS Below is a complete list of keyboard shortcuts available in mikroPascal PRO for AVR IDE. You can also view keyboard shortcuts in the Code Explorer window, tab Keyboard.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Ctrl+X Cut Ctrl+Y Delete entire line Ctrl+Z Undo Ctrl+Shift+Z Redo Advanced Editor Shortcuts 32 Ctrl+Space Code Assistant Ctrl+Shift+Space Parameters Assistant Ctrl+D Find declaration Ctrl+E Incremental Search Ctrl+L Routine List Ctrl+G Goto line Ctrl+J Insert Code Template Ctrl+Shift+.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Software Simulator Shortcuts F2 Jump To Interrupt F4 Run to Cursor F5 Toggle Breakpoint F6 Run/Pause Debugger F7 Step into F8 Step over F9 Debug Ctrl+F2 Reset Ctrl+F5 Add to Watch List Ctrl+F8 Step out Alt+D Dissasembly view Shift+F5 Open Watch Window MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 33
CHAPTER 2 Environment mikroPASCAL PRO for AVR IDE OVERVIEW The mikroPascal PRO for AVR is an user-friendly and intuitive environment: - The Code Editor features adjustable Syntax Highlighting, Code Folding, Code Assistant, Parameters Assistant, Spell Checker, Auto Correct for common typos and Code Tem plates (Auto Complete). - The Code Explorer (with Keyboard shortcut browser and Quick Help browser) is at your disposal for easier project management.
CHAPTER 2 mikroPASCAL PRO for AVR Environment - Like in any modern Windows application, you may customize the layout of mikroPascal PRO for AVR to suit your needs best. - Spell checker underlines identifiers which are unknown to the project. In this way it helps the programmer to spot potential problems early, much before the project is compiled. - Spell checker can be disabled by choosing the option in the Preferences dialog (F12).
CHAPTER 2 mikroPASCAL PRO for AVR Environment CUSTOMIZING IDE LAYOUT Docking Windows You can increase the viewing and editing space for code, depending on how you arrange the windows in the IDE. Step 1: Click the window you want to dock, to give it focus. Step 2: Drag the tool window from its current location. A guide diamond appears. The four arrows of the diamond point towards the four edges of the IDE.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Step 3: Move the pointer over the corresponding portion of the guide diamond. An outline of the window appears in the designated area. Step 4: To dock the window in the position indicated, release the mouse button. Tip: To move a dockable window without snapping it into place, press CTRL while dragging it. Saving Layout Once you have a window layout that you like, you can save the layout by typing the name for the layout and pressing the Save Layout Icon .
CHAPTER 2 mikroPASCAL PRO for AVR Environment Auto Hide Auto Hide enables you to see more of your code at one time by minimizing tool windows along the edges of the IDE when not in use. - Click the window you want to keep visible to give it focus. - Click the Pushpin Icon on the title bar of the window. When an auto-hidden window loses focus, it automatically slides back to its tab on the edge of the IDE. While a window is auto-hidden, its name and icon are visible on a tab at the edge of the IDE.
CHAPTER 2 mikroPASCAL PRO for AVR Environment ADVANCED CODE EDITOR The Code Editor is advanced text editor fashioned to satisfy needs of professionals. General code editing is the same as working with any standard text-editor, including familiar Copy, Paste and Undo actions, common for Windows environment.
CHAPTER 2 Environment mikroPASCAL PRO for AVR Code Assistant If you type the first few letters of a word and then press Ctrl+Space, all valid identifiers matching the letters you have typed will be prompted in a floating panel (see the image below). Now you can keep typing to narrow the choice, or you can select one from the list using the keyboard arrows and Enter.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Code Folding Code folding is IDE feature which allows users to selectively hide and display sections of a source file. In this way it is easier to manage large regions of code within one window, while still viewing only those subsections of the code that are relevant during a particular editing session. While typing, the code folding symbols ( and ) appear automatically. Use the folding symbols to hide/unhide the code subsections.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Parameter Assistant The Parameter Assistant will be automatically invoked when you open parenthesis “(” or press Shift+Ctrl+Space. If the name of a valid function precedes the parenthesis, then the expected parameters will be displayed in a floating panel. As you type the actual parameter, the next expected parameter will become bold.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Spell Checker The Spell Checker underlines unknown objects in the code, so they can be easily noticed and corrected before compiling your project. Select Tools › Options from the drop-down menu, or click the Show Options Icon and then select the Spell Checker Tab. Bookmarks Bookmarks make navigation through a large code easier. To set a bookmark, use Ctrl+Shift+number. To jump to a bookmark, use Ctrl+number.
CHAPTER 2 mikroPASCAL PRO for AVR Environment CODE EXPLORER The Code Explorer gives clear view of each item declared inside the source code. You can jump to a declaration of any item by right clicking it. Also, besides the list of defined and declared objects, code explorer displays message about first error and it's location in code. Icon Description Expand/Collapse all nodes in tree. Locate declaration in code.
CHAPTER 2 mikroPASCAL PRO for AVR Environment ROUTINE LIST Routine list diplays list of routines, and enables filtering routines by name. Routine list window can be accessed by pressing Ctrl+L. You can jump to a desired routine by double clicking on it.
CHAPTER 2 mikroPASCAL PRO for AVR Environment PROJECT MANAGER Project Manager is IDE feature which allows users to manage multiple projects. Several projects which together make project group may be open at the same time. Only one of them may be active at the moment. Setting project in active mode is performed by double click on the desired project in the Project Manager.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Following options are available in the Project Manager: Icon Description Save project Group. Open project group. Close the active project. Close project group. Add project to the project group. Remove project from the project group. Add file to the active project. Remove selected file from the project. Build the active project. Run mikroElektronika's Flash programmer. For details about adding and removing files from project see Add/Remove Files from Project.
CHAPTER 2 mikroPASCAL PRO for AVR Environment PROJECT SETTINGS WINDOW Following options are available in the Project Settings Window: - Device - select the appropriate device from the device drop-down list. - Oscillator - enter the oscillator frequency value.
CHAPTER 2 mikroPASCAL PRO for AVR Environment LIBRARY MANAGER Library Manager enables simple handling libraries being used in a project. Library Manager window lists all libraries (extencion .mcl) which are instantly stored in the compiler Uses folder. The desirable library is added to the project by selecting check box next to the library name. In order to have all library functions accessible, simply press the button Check All and all libraries will be selected.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Icon Description Refresh Library by scanning files in "Uses" folder.Useful when new libraries are added by copying files to "Uses" folder. Rebuild all available libraries. Useful when library sources are available and need refreshing. Include all available libraries in current project. No libraries from the list will be included in current project. Restore library to the state just before last project saving.
CHAPTER 2 mikroPASCAL PRO for AVR Environment ERROR WINDOW In case that errors were encountered during compiling, the compiler will report them and won’t generate a hex file. The Error Window will be prompted at the bottom of the main window by default. The Error Window is located under message tab, and displays location and type of errors the compiler has encountered. The compiler also reports warnings, but these do not affect the output; only errors can interefere with the generation of hex.
CHAPTER 2 mikroPASCAL PRO for AVR Environment STATISTICS After successful compilation, you can review statistics of your code. Click the Statistics Icon . Memory Usage Windows Provides overview of RAM and ROM usage in the form of histogram. RAM MEMORY Rx Memory Space Displays Rx memory space usage in form of histogram.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Data Memory Space Displays Data memory space usage in form of histogram. Special Function Registers Summarizes all Special Function Registers and their addresses.
CHAPTER 2 mikroPASCAL PRO for AVR Environment General Purpose Registers Summarizes all General Purpose Registers and their addresses. Also displays symbolic names of variables and their addresses. ROM MEMORY ROM Memory Usage Displays ROM memory usage in form of histogram.
CHAPTER 2 mikroPASCAL PRO for AVR Environment ROM Memory Allocation Displays ROM memory allocation. Procedures Windows Provides overview procedures locations and sizes. Procedures Size Window Displays size of each procedure.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Procedures Locations Window Displays how functions are distributed in microcontroller’s memory. HTML Window Display statistics in default web browser.
CHAPTER 2 mikroPASCAL PRO for AVR Environment INTEGRATED TOOLS USART Terminal The mikroPascal PRO for AVR includes the USART communication terminal for RS232 communication. You can launch it from the drop-down menu Tools › USART Terminal or by clicking the USART Terminal Icon from Tools toolbar.
CHAPTER 2 mikroPASCAL PRO for AVR Environment ASCII Chart The ASCII Chart is a handy tool, particularly useful when working with Lcd display. You can launch it from the drop-down menu Tools › ASCII chart or by clicking the View ASCII Chart Icon 58 from Tools toolbar.
CHAPTER 2 mikroPASCAL PRO for AVR Environment EEPROM Editor The EEPROM Editor is used for manipulating MCU's EEPROM memory. You can launch it from the drop-down menu Tools › EEPROM Editor. When Use this EEPROM definition is checked compiler will generate Intel hex file project_name.ihex that contains data from EEPROM editor. When you run mikroElektronika programmer software from mikroPascal PRO for AVR IDE - project_name.hex file will be loaded automatically while ihex file must be loaded manually.
CHAPTER 2 mikroPASCAL PRO for AVR Environment 7 Segment Display Decoder The 7 Segment Display Decoder is a convenient visual panel which returns decimal/hex value for any viable combination you would like to display on 7seg. Click on the parts of 7 segment image to get the requested value in the edit boxes. You can launch it from the drop-down menu Tools › 7 Segment Decoder or by clicking the Seven Segment Icon 60 from Tools toolbar.
CHAPTER 2 mikroPASCAL PRO for AVR Environment UDP Terminal The mikroPascal PRO for AVR includes the UDP Terminal. You can launch it from the drop-down menu Tools › UDP Terminal.
CHAPTER 2 Environment mikroPASCAL PRO for AVR Graphic Lcd Bitmap Editor The mikroPascal PRO for AVR includes the Graphic Lcd Bitmap Editor. Output is the mikroPascal PRO for AVR compatible code. You can launch it from the drop-down menu Tools › Glcd Bitmap Editor.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Lcd Custom Character mikroPascal PRO for AVR includes the Lcd Custom Character. Output is mikroPascal PRO for AVR compatible code. You can launch it from the drop-down menu Tools › Lcd Custom Character.
CHAPTER 2 mikroPASCAL PRO for AVR Environment MACRO EDITOR A macro is a series of keystrokes that have been 'recorded' in the order performed. A macro allows you to 'record' a series of keystrokes and then 'playback', or repeat, the recorded keystrokes. The Macro offers the following commands: Icon Description Starts 'recording' keystrokes for later playback. Stops capturing keystrokesthat was started when the Start Recordig command was selected. Allows a macro that has been recorded to be replayed.
CHAPTER 2 mikroPASCAL PRO for AVR Environment OPTIONS Options menu consists of three tabs: Code Editor, Tools and Output settings Code editor The Code Editor is advanced text editor fashioned to satisfy needs of professionals. Tools The mikroPascal PRO for AVR includes the Tools tab, which enables the use of shortcuts to external programs, like Calculator or Notepad. You can set up to 10 different shortcuts, by editing Tool0 - Tool9.
CHAPTER 2 Environment mikroPASCAL PRO for AVR Output settings By modifying Output Settings, user can configure the content of the output files. You can enable or disable, for example, generation of ASM and List file. Also, user can choose optimization level, and compiler specific settings, which include case sensitivity, dynamic link for string literals setting (described in mikroPascal PRO for AVR specifics). Build all files as library enables user to use compiled library (*.
CHAPTER 2 mikroPASCAL PRO for AVR Environment REGULAR EXPRESSIONS Introduction Regular Expressions are a widely-used method of specifying patterns of text to search for. Special metacharacters allow you to specify, for instance, that a particular string you are looking for, occurs at the beginning, or end of a line, or contains n recurrences of a certain character. Simple matches Any single character matches itself, unless it is a metacharacter with a special meaning described below.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Character classes You can specify a character class, by enclosing a list of characters in [], which will match any of the characters from the list. If the first character after the "[" is "^", the class matches any character not in the list. Examples: count[aeiou]r finds strings 'countar', 'counter', etc. but not 'countbr', 'countcr', etc. count[^aeiou]r finds strings 'countbr', 'countcr', etc. but not 'countar', 'counter', etc.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Examples: ^PORTA - matches string ' PORTA ' only if it's at the beginning of line PORTA$ - matches string ' PORTA ' only if it's at the end of line ^PORTA$ - matches string ' PORTA ' only if it's the only string in line PORT.r - matches strings like 'PORTA', 'PORTB', 'PORT1' and so on The "^" metacharacter by default is only guaranteed to match beginning of the input string/text, and the "$" metacharacter only at the end.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Metacharacters - Iterators Any item of a regular expression may be followed by another type of metacharacters - iterators. Using this metacharacters,you can specify number of occurences of previous character, metacharacter or subexpression.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Metacharacters - Alternatives You can specify a series of alternatives for a pattern using "|" to separate them, so that bit|bat|bot will match any of "bit", "bat", or "bot" in the target string as would "b(i|a|o)t)". The first alternative includes everything from the last pattern delimiter ("(", "[", or the beginning of the pattern) up to the first "|", and the last alternative contains everything from the last "|" to the next pattern delimiter.
CHAPTER 2 mikroPASCAL PRO for AVR Environment MIKROPASCAL PRO FOR AVR COMMAND LINE OPTIONS Usage: mPAvr.exe [- [-]] [ [-]] [-]] Infile can be of *.mpas and *.mcl type. The following parameters and some more (see manual) are valid: -P : MCU for which compilation will be done. -FO : Set oscillator [in MHz]. -SP : Add directory to the search path list. -N : Output files generated to file path specified by filename. -B : Save compiled binary files (*.mcl) to 'directory'.
CHAPTER 2 mikroPASCAL PRO for AVR Environment PROJECTS The mikroPascal PRO for AVR organizes applications into projects, consisting of a single project file (extension .mcpav) and one or more source files (extension ). mikroPascal PRO for AVR IDE allows you to manage multiple projects (see Project Manager). Source files can be compiled only if they are part of a project.
CHAPTER 2 mikroPASCAL PRO for AVR Environment New Project Wizard Steps Start creating your New project, by clicking Next button: Step One - Select the device from the device drop-down list.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Step Two - Enter the oscillator frequency value. Step Three - Specify the location where your project will be saved.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Step Four - Add project file to the project if they are avaiable at this point. You can always add project files later using Project Manager.
CHAPTER 2 mikroPASCAL PRO for AVR Environment CUSTOMIZING PROJECTS Edit Project You can change basic project settings in the Project Settings window. You can change chip and oscillator frequency. Any change in the Project Setting Window affects currently active project only, so in case more than one project is open, you have to ensure that exactly the desired project is set as active one in the Project Manager.
CHAPTER 2 mikroPASCAL PRO for AVR Environment The list of relevant source files is stored in the project file (extension .mppav). To add source file to the project, click the Add File to Project Icon . Each added source file must be self-contained, i.e. it must have all necessary definitions after preprocessing. To remove file(s) from the project, click the Remove File from Project Icon . See File Inclusion for more information.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Project Level Defines Project Level Defines (.pld) files can also be added to project. Project level define files enable you to have defines that are visible in all source files in the project. One project may contain several pld files. A file must contain one definition per line, for example: ANALOG DEBUG TEST There are some predefined project level defines.
CHAPTER 2 mikroPASCAL PRO for AVR Environment SOURCE FILES Source files containing Pascal code should have the extension .mpas. The list of source files relevant to the application is stored in project file with extension .mppav, along with other project information. You can compile source files only if they are part of the project. Managing Source Files Creating new source file To create a new source file, do the following: 1.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Saving file 1. Make sure that the window containing the file that you want to save is the active window. 2. Select File › Save from the drop-down menu, or press Ctrl+S, or click the Save File Icon from the File Toolbar. Saving file under a different name 1. Make sure that the window containing the file that you want to save is the active window. 2. Select File › Save As from the drop-down menu. The New File Name dialog will be displayed. 3.
CHAPTER 2 mikroPASCAL PRO for AVR Environment CLEAN PROJECT FOLDER Clean Project Folder This menu gives you option to choose which files from your current project you want to delete. Files marked in bold can be easily recreated by building a project. Other files should be marked for deletion only with a great care, because IDE cannot recover them.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Compilation When you have created the project and written the source code, it's time to compile it. Select Project › Build from the drop-down menu, or click the Build Icon from the Project Toolbar. If more more than one project is open you can compile all open projects by selecting Project › Build All from the drop-down menu, or click the Build All Icon from the Project Toolbar. Progress bar will appear to inform you about the status of compiling.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Compiler Error Messages: - "%s" is not valid identifier. - Unknown type "%s". - Identifier "%s" was not declared. - Syntax error: Expected "%s" but "%s" found. - Argument is out of range "%s". - Syntax error in additive expression. - File "%s" not found. - Invalid command "%s". - Not enough parameters. - Too many parameters. - Too many characters. - Actual and formal parameters must be identical. - Invalid ASM instruction: "%s".
CHAPTER 2 mikroPASCAL PRO for AVR Environment - Error occured while compiling "%s". - Recursive types are not allowed. - Adding strings is not allowed, use "strcat" procedure instead. - Cannot declare pointer to array, use pointer to structure which has array field. - Return value of the function "%s" is not defined. - Assignment to for loop variable is not allowed. - "%s" is allowed only in the main program. - Start address of "%s" has already been defined. - Simple constant cannot have fixed address.
CHAPTER 2 mikroPASCAL PRO for AVR Environment SOFTWARE SIMULATOR OVERVIEW The Source-level Software Simulator is an integral component of the mikroPascal PRO for AVR environment. It is designed to simulate operations of the AVR MCUs and assist the users in debugging Pascal code written for these devices.
CHAPTER 2 mikroPASCAL PRO for AVR Environment You can also expand/collapse complex variables, i.e. struct type variables, strings... Values are updated as you go through the simulation. Recently changed items are colored red. Double clicking a variable or clicking the Properties Button opens the Edit Value window in which you can assign a new value to the selected variable/register.
CHAPTER 2 mikroPASCAL PRO for AVR Environment An item's value can be also changed by double clicking item's value field and typing the new value directly. Stopwatch Window The Software Simulator Stopwatch Window is available from the drop-down menu, View › Debug Windows › Stopwatch. The Stopwatch Window displays a current count of cycles/time since the last Software Simulator action.
CHAPTER 2 mikroPASCAL PRO for AVR Environment RAM Window The Software Simulator RAM Window is available from the drop-down menu, View › Debug Windows › RAM. The RAM Window displays a map of MCU’s RAM, with recently changed items colored red. You can change value of any field by double-clicking it.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Software Simulator Options Name Description Function Key Start Debugger Start Software Simulator. [F9] Run/Pause Debugger Run or pause Software Simulator. [F6] Stop Debugger Stop Software Simulator. [Ctrl+F2] Toggle Breakpoints Toggle breakpoint at the current cursor position. To view all breakpoints, select Run > View Breakpoints from the drop–down menu. [F5] Double clicking an item in the Breakpoints Window List locates the breakpoint.
CHAPTER 2 mikroPASCAL PRO for AVR Environment CREATING NEW LIBRARY mikroBasic PRO for AVR allows you to create your own libraries. In order to create a library in mikroBasic PRO for AVR follow the steps bellow: 1. Create a new Pascal source file, see Managing Source Files 2. Save the file in one of the subfolders of the compiler's Uses folder (LTE64kW or GT64kW, see note on the end of the page): DriveName:\Program Files\Mikroelektronika\mikroPascal PRO for AVR\Uses\LTE64kW\__Lib_Example.mpas 3.
CHAPTER 2 mikroPASCAL PRO for AVR Environment Multiple Library Versions Library Alias represents unique name that is linked to corresponding Library .mcl file. For example UART library for ATMEGA16 is different from UART library for ATMEGA128 MCU. Therefore, two different UART Library versions were made, see mlk files for these two MCUs. Note that these two libraries have the same Library Alias (UART) in both mlk files.
CHAPTER 3 mikropascal PRO for AVR Specifics The following topics cover the specifics of mikroPascal PRO for AVR compiler: - Pascal Standard Issues - Predefined Globals and Constants - Accessing Individual Bits - Interrupts - AVR Pointers - Linker Directives - Built-in Routines - Code Optimization 93
CHAPTER 3 mikroPASCAL PRO for AVR Specifics PASCAL STANDARD ISSUES Divergence from the Pascal Standard - Function recursion is not supported because of no easily-usable stack and limited memory AVR Specific Pascal Language Extensions mikroPascal PRO for AVR has additional set of keywords that do not belong to the standard Pascal language keywords: - code data io rx sfr register at sbit bit Related topics: Keywords, AVR Specific 94 MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 3 mikroPASCAL PRO for AVR Specifics PREDEFINED GLOBALS AND CONSTANTS To facilitate programming of AVR compliant MCUs, the mikroPascal PRO for AVR implements a number of predefined globals and constants. All AVR SFR registers are implicitly declared as global variables of volatile word. These identifiers have an external linkage, and are visible in the entire project. When creating a project, the mikroPascal PRO for AVR will include an appropriate (*.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics ACCESSING INDIVIDUAL BITS The mikroPascal PRO for AVR allows you to access individual bits of 8-bit variables. It also supports sbit and bit data types Accessing Individual Bits Of Variables To access the individual bits, simply use the direct member selector (.) with a variable, followed by one of identifiers B0, B1, … , B7, or 0, 1, … 7, with 7 being the most significant bit : // Clear bit 0 on PORTA PORTA.B0 := 0; // Clear bit 5 on PORTB PORTB.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics bit type The mikroPascal PRO for AVR compiler provides a bit data type that may be used for variable declarations. It can not be used for argument lists, and function-return values. var bf : bit; // bit variable There are no pointers to bit variables: var ptr : ^bit; // invalid An array of type bit is not valid: var arr[5] : bit; // invalid Note : - Bit variables can not be initialized. - Bit variables can not be members of records.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics INTERRUPTS AVR derivates acknowledges an interrupt request by executing a hardware generated CALL to the appropriate servicing routine ISRs. ISRs are organized in IVT. ISR is defined as a standard function but with the org directive afterwards which connects the function with specific interrupt vector. For example org 0x000B is IVT address of Timer/Counter 2 Overflow interrupt source of the ATMEGA16.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics These principles can be explained on the picture below : Related topics: Pascal standard issues MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 99
CHAPTER 3 mikroPASCAL PRO for AVR Specifics LINKER DIRECTIVES mikroPascal PRO for AVR uses internal algorithm to distribute objects within memory. If you need to have a variable or a routine at the specific predefined address, use the linker directives absolute and org. Note: You must specify an even address when using the linker directives. Directive absolute Directive absolute specifies the starting address in RAM for a variable.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics Directive org Directive org specifies the starting address of a routine in ROM. It is appended to the declaration of a routine. For example: procedure proc(par : byte); org $200; begin // Procedure will start at address $200; ... end; org directive can be used with main routine too. For example: program Led_Blinking; procedure some_proc(); begin ...
CHAPTER 3 mikroPASCAL PRO for AVR Specifics BUILT-IN ROUTINES The mikroPascal PRO for AVR compiler provides a set of useful built-in utility functions. The Delay_us and Delay_ms routines are implemented as “inline”; i.e. code is generated in the place of a call, so the call doesn’t count against the nested call limit. The Vdelay_ms, Delay_Cyc and Get_Fosc_kHz are actual Pascal routines. Their sources can be found in Delays.mpas file located in the uses folder of the compiler.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics Hi Prototype function Hi(number: longint): byte; Returns Returns next to the lowest byte of number, bits 8..15. Function returns next to the lowest byte of number. Function does not interpret bit patterns of number – it merely returns 8 bits as found in register. Description This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit. Requires Arguments must be variable of scalar type (i.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics Inc Prototype procedure Inc(var par : longint); Returns Nothing. Description Increases parameter par by 1. Requires Nothing. Example p := 4; Inc(p); // p is now 5 Dec Prototype procedure Dec(var par : longint); Returns Nothing. Description Decreases parameter par by 1. Requires Nothing. Example p := 4; Dec(p); // p is now 3 Delay_us Prototype procedure Delay_us(time_in_us: const longword); Returns Nothing.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics Delay_ms Prototype procedure Delay_ms(time_in_ms: const longword); Returns Nothing. Creates a software delay in duration of time_in_ms milliseconds (a constant). Range of applicable constants depends on the oscillator frequency. Description This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit. Requires Nothing.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics Clock_KHz Prototype function Clock_KHz(): word; Returns Device clock in KHz, rounded to the nearest integer. Function returns device clock in KHz, rounded to the nearest integer. Description This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit. Requires Nothing.
CHAPTER 3 mikroPASCAL PRO for AVR Specifics CODE OPTIMIZATION Optimizer has been added to extend the compiler usability, cut down the amount of code generated and speed-up its execution. The main features are: Constant folding All expressions that can be evaluated in the compile time (i.e. are constant) are being replaced by their results.
CHAPTER 3 Specifics 108 mikroPASCAL PRO for AVR MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 4 AVR Specifics Types Efficiency First of all, you should know that AVR ALU, which performs arithmetic operations, is optimized for working with bytes. Although mikroPascal PRO is capable of handling very complex data types, AVR may choke on them, especially if you are working on some of the older models. This can dramatically increase the time needed for performing even simple operations. Universal advice is to use the smallest possible type in every situation.
CHAPTER 4 mikroPASCAL PRO for AVR AVR Specifics Nested Calls Limitations There are no Nested Calls Limitations, except by RAM size. A Nested call represents a function call to another function within the function body. With each function call, the stack increases for the size of the returned address. Number of nested calls is equel to the capacity of RAM which is left out after allocation of all variables.
CHAPTER 4 mikroPASCAL PRO for AVR AVR Specifics AVR Memory Organization The AVR microcontroller's memory is divided into Program Memory and Data Memory. Program Memory (ROM) is used for permanent saving program being executed, while Data Memory (RAM) is used for temporarily storing and keeping intermediate results and variables.
CHAPTER 4 mikroPASCAL PRO for AVR AVR Specifics Data Memory Data memory consists of : - Rx space I/O Memory Extended I/O Memory (MCU dependent) Internal SRAM Rx space consists of 32 general purpose working 8-bit registers (R0-R31). These registers have the shortest (fastest) access time, which allows single-cycle Arithmetic Logic Unit (ALU) operation. I/O Memory space contains addresses for CPU peripheral function, such as Control registers, SPI, and other I/O functions.
CHAPTER 4 mikroPASCAL PRO for AVR AVR Specifics Related topics: Accessing individual bits, SFRs, Memory type specifiers MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 113
CHAPTER 4 mikroPASCAL PRO for AVR AVR Specifics MEMORY TYPE SPECIFIERS The mikroPascal PRO for AVR supports usage of all memory areas. Each variable may be explicitly assigned to a specific memory space by including a memory type specifier in the declaration, or implicitly assigned. The following memory type specifiers can be used: - code data rx io sfr Memory type specifiers can be included in variable declaration.
CHAPTER 4 mikroPASCAL PRO for AVR AVR Specifics io Description This memory specifier allows user to access the I/O Memory space. Example // put io_buff in io memory space var io_buff : byte; io; sfr This memory specifier in combination with (rx, io, data) allows user to access Description special function registers. It also instructs compiler to maintain same identifier in Pascal and assembly.
CHAPTER 4 AVR Specifics 116 mikroPASCAL PRO for AVR MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 5 mikroPascal PRO for AVR Language Reference The mikroPascal PRO for AVR Language Reference describes the syntax,semantics and implementation of mikroPascal PRO for AVR Language reference. The aim of this referenceguide is to provide a more understandable description of the mikroPascal PRO for AVR language references to the user.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR MIKROPASCAL PRO FOR AVR LANGUAGE REFERENCE - Lexical Elements Whitespace Comments Tokens Literals Keywords Identifiers Punctuators - Program Organization Program Organization Scope and Visibility Units - Variables - Constants - Labels - Functions and Procedures Functions Procedures - Types Simple Types Arrays Strings Pointers Records Types Conversions Implicit Conversion Explicit Conversion - Operators Introduction to Operators Operators Precedence and
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Case Statement Iteration Statements (Loops) For Statement While Statement Repeat Statement Jump Statements Break and Continue Statements Exit Statement Goto Statement asm Statement Directives Compiler Directives Linker Directives MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 119
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR LEXICAL ELEMENTS OVERVIEW The following topics provide a formal definition of the mikroPascal PRO for AVR lexical elements. They describe different categories of word-like units (tokens) recognized by mikroPascal PRO for AVR. In the tokenizing phase of compilation, the source code file is parsed (i.e. broken down) into tokens and whitespace.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference parses into four tokens, including a single string literal token: some_string := 'mikro foo' ; Comments Comments are pieces of a text used to annotate a program, and are technically another form of whitespace. Comments are for the programmer’s use only. They are stripped from the source text before parsing. There are two ways to create comments in mikroPascal.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Tokens Token is the smallest element of the Pascal program that compiler can recognize. The parser separates tokens from the input stream by creating the longest token possible using the input characters in a left–to–right scan. mikroPascal PRO for AVR recognizes the following kinds of tokens: - keywords identifiers constants operators punctuators (also known as separators) Token Extraction Example Here is an example of token extraction.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Literals Literals are tokens representing fixed numeric or character values. The data type of a constant is deduced by the compiler using such clues as numeric value and format used in the source code. Integer Literals Integral values can be represented in decimal, hexadecimal, or binary notation.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference mikroPascal PRO for AVR limits floating-point constants to range ±1.17549435082 * 10-38 .. ±6.80564774407 * 1038. Here are some examples: 0. -1.23 23.45e6 2e-5 3E+10 .09E34 // // // // // // = = = = = = 0.0 -1.23 23.45 * 10^6 2.0 * 10^-5 3.0 * 10^10 0.09 * 10^34 Character Literals Character literal is one character from the extended ASCII character set, enclosed with apostrophes.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Keywords Keywords are the words reserved for special purposes and must not be used as normal identifier names. Beside standard Pascal keywords, all relevant SFRs are defined as global variables and represent reserved words that cannot be redefined (for example: W0, TMR1, T1CON, etc). Probe the Code Assistant for specific letters (Ctrl+Space in Editor) or refer to Predefined Globals and Constants.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference IDENTIFIERS Identifiers are arbitrary names of any length given to functions, variables, symbolic constants, user-defined data types and labels. All these program elements will be referred to as objects throughout the help (don't get confused about the meaning of object in object-oriented programming). Identifiers can contain the letters a to z and A to Z, underscore character “_”, and digits from 0 to 9.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference PUNCTUATORS The mikroPascal punctuators (also known as separators) are: - [ ] – Brackets ( ) – Parentheses , – Comma ; – Semicolon : – Colon . – Dot Brackets Brackets [ ] indicate single and multidimensional array subscripts: var alphabet : array[1..30] of byte; // ... alphabet[3] := 'c'; For more information, refer to Arrays.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Semicolon Semicolon (;) is a statement terminator. Every statement in Pascal must be terminated with a semicolon. The exceptions are: the last (outer most) end statement in the program which is terminated with a dot and the last statement before end which doesn't need to be terminated with a semicolon. For more information, see Statements. Colon Colon (:) is used in declarations to separate identifier list from type identifier.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference PROGRAM ORGANIZATION Pascal imposes quite strict program organization. Below you can find models for writing legible and organized source files. For more information on file inclusion and scope, refer to Units and Scope and Visibility. Organization of Main Unit Basically, the main source file has two sections: declaration and program body. Declarations should be in their proper place in the code, organized in an orderly manner.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference //******************************************************** //* Program body: //******************************************************** begin { write your code here } end. Organization of Other Units Units other than main start with the keyword unit. Implementation section starts with the keyword implementation.
CHAPTER 5 mikroPASCAL PRO for AVR { variables declarations } var Name[, Name2...] : [^]type; [volatile;] [register;] [sfr;] Language Reference [absolute 0x123;] [external;] { labels declarations } label ... { procedures declarations } procedure procedure_name([var] [const] ParamName : [^]type; [var] [const] ParamName2, ParamName3 : [^]type); [ilevel 0x123;] [overload;] [forward;] { local declarations } begin ...
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference SCOPE AND VISIBILITY Scope The scope of an identifier is a part of the program in which the identifier can be used to access its object. There are different categories of scope, which depends on how and where identifiers are declared: Place of declaration Scope Scope extends from the point where it is declared to the Identifier is declared end of the current block, including all blocks enclosed in the declaration of a within that scope.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference UNITS In mikroPascal PRO for AVR, each project consists of a single project file and one or more unit files. Project file, with extension .mppav contains information about the project, while unit files, with extension .mpas, contain the actual source code.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Main Unit Every project in mikroPascal PRO for AVR requires a single main unit file. The main unit file is identified by the keyword program at the beginning; it instructs the compiler where to “start”. After you have successfully created an empty project with the Project Wizard, the Code Editor will display a new main unit. It contains the bare-bones of the Pascal program: program MyProject; { main procedure } begin { Place program code here } end.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Implementation Section Implementation section hides all irrelevant innards from other units, allowing encapsulation of code. Everything declared below the keyword implementation is private, i.e. has its scope limited to the file. When you declare an identifier in the implementation section of a unit, you cannot use it outside the unit, but you can use it in any block or routine defined within the unit.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR VARIABLES Variable is object whose value can be changed during the runtime. Every variable is declared under unique name which must be a valid identifier. This name is used for accessing the memory location occupied by a variable. Variables are declared in the declaration part of the file or routine — each variable needs to be declared before being used.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Constants Constant is a data whose value cannot be changed during the runtime. Using a constant in a program consumes no RAM. Constants can be used in any expression, but cannot be assigned a new value. Constants are declared in the declaration part of a program or routine.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Labels Labels serve as targets for goto statements. Mark the desired statement with a label and colon like this: label_identifier : statement Before marking a statement, you must declare a label. Labels are declared in declaration part of unit or routine, similar to variables and constants. Declare labels using the keyword label: label label1, ..., labeln; Name of the label needs to be a valid identifier.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference FUNCTIONS AND PROCEDURES Functions and procedures, collectively referred to as routines, are subprograms (self-contained statement blocks) which perform a certain task based on a number of input parameters. When executed, a function returns a value while procedure does not. mikroPascal PRO for AVR does not support inline routines.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Function calls are considered to be primary expressions and can be used in situations where expression is expected. A function call can also be a self-contained statement and in that case the return value is discarded.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Example Here’s an example procedure which transforms its input time parameters, preparing them for output on Lcd: procedure begin sec := min := hr := end; time_prep(var sec, min, hr : byte); ((sec and $F0) shr 4)*10 + (sec and $0F); ((min and $F0) shr 4)*10 + (min and $0F); ((hr and $F0) shr 4)*10 + (hr and $0F); A function can return a complex type. Follow the example bellow to learn how to declare and use a function which returns a complex type.
CHAPTER 6 Language Reference mikroPASCAL PRO for PIC Forward declaration A function can be declared without having it followed by it's implementation, by having it followed by the forward procedure. The effective implementation of that function must follow later in the unit. The function can be used after a forward declaration as if it had been implemented already.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference TYPES Pascal is strictly typed language, which means that every variable and constant need to have a strictly defined type, known at the time of compilation. The type serves: - to determine correct memory allocation required, - to interpret the bit patterns found in the object during subsequent accesses, - in many type-checking situations, to ensure that illegal assignments are trapped.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference SIMPLE TYPES Simple types represent types that cannot be divided into more basic elements and are the model for representing elementary data on machine level. Basic memory unit in mikroPascal PRO for AVR has 16 bits. Here is an overview of simple types in mikroPascal PRO for AVR: Type Size Range byte, char 8–bit 0 .. 255 short 8–bit -127 .. 128 word 16–bit 0 .. 65535 integer 16–bit -32768 .. 32767 dword 32–bit 0 ..
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference ARRAYS An array represents an indexed collection of elements of the same type (called the base type). Because each element has a unique index, arrays, unlike sets, can meaningfully contain the same value more than once. Array Declaration Array types are denoted by constructions in the following form: array[index_start .. index_end] of type Each of the elements of an array is numbered from index_start through index_end.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Multi-dimensional Arrays Multidimensional arrays are constructed by declaring arrays of array type. These arrays are stored in memory in such way that the right most subscript changes fastest, i.e. arrays are stored “in rows”. Here is a sample 2-dimensional array: m : array[5] of array[10] of byte; 5x10 // 2-dimensional array of size A variable m is an array of 5 elements, which in turn are arrays of 10 byte each.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference STRINGS A string represents a sequence of characters equivalent to an array of char. It is declared like this: string_name : string[length] The specifier length is a number of characters the string consists of. String is stored internally as the given sequence of characters plus a final null character which is introduced to terminate the string. It does not count against the string’s total length.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR String Concatenating mikroPascal PRO for AVR allows you to concatenate strings by means of plus operator. This kind of concatenation is applicable to string variables/literals, character variables/literals. For control characters, use the non-quoted hash sign and a numeral (e.g. #13 for CR). Here is an example: var msg : string[20]; res_txt : string[5]; res, channel : word; begin //...
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference POINTERS A pointer is a data type which holds a memory address. While a variable accesses that memory address directly, a pointer can be thought of as a reference to that memory address. To declare a pointer data type, add a carat prefix (^) before type. For example, in order to create a pointer to an integer, write: ^integer; In order to access data at the pointer’s memory location, add a carat after the variable name.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR program Example; type TMyFunctionType = function (param1, param2: byte; param3: word) : word; // First, define the procedural type var MyPtr: ^TMyFunctionType; // This is a pointer to previously defined type Sample: word; function Func1(p1, p2: byte; p3: word): word;// Now, define few functions which will be pointed to.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Records A record (analogous to a structure in some languages) represents a heterogeneous set of elements. Each element is called a field. The declaration of the record type specifies a name and type for each field. The syntax of a record type declaration is type recordTypeName = record fieldList1 : type1; ...
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Accessing Fields You can access the fields of a record by means of dot (.) as a direct field selector. If we have declared variables circle1 and circle2 of previously defined type TCircle: var circle1, circle2 : TCircle; we could access their individual fields like this: circle1.radius := 3.7; circle1.center.x := 0; circle1.center.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference TYPES CONVERSIONS Conversion of variable of one type to a variable of another type is typecasting. mikroPascal PRO for AVR supports both implicit and explicit conversions for built-in types.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Explicit Conversion Explicit conversion can be executed at any point by inserting type keyword (byte, word, short, integer, longint or real) ahead of an expression to be converted. The expression must be enclosed in parentheses. Explicit conversion can be performed only on the operand right of the assignment operator. Special case is conversion between signed and unsigned types.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference OPERATORS Operators are tokens that trigger some computation when being applied to variables and other objects in an expression.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Operators Precedence and Associativity There are 4 precedence categories in mikroPascal PRO for AVR. Operators in the same category have equal precedence with each other. ), or right-to-left ( ). In the Each category has an associativity rule: left-to-right ( absence of parentheses, these rules resolve the grouping of expressions with operators of equal precedence.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Division by Zero If 0 (zero) is used explicitly as the second operand (i.e. x div 0), the compiler will report an error and will not generate code. But in case of implicit division by zero: x div y, where y is 0 (zero), the result will be the maximum integer (i.e 255, if the result is byte type; 65536, if the result is word type, etc.). Unary Arithmetic Operators Operator - can be used as a prefix unary operator to change sign of a signed value.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Bitwise Operators Use bitwise operators to modify individual bits of numerical operands. Operands need to be either both signed or both unsigned. Bitwise operators associate from left to right. The only exception is the bitwise complement operator not which associates from right to left.
CHAPTER 5 mikroPASCAL PRO for AVR $1234 and $5678 Language Reference // equals $1230 { because .. $1234 : 0001 0010 0011 0100 $5678 : 0101 0110 0111 1000 ---------------------------and : 0001 0010 0011 0000 .. that is, $1230 }// Similarly: $1234 or $5678 $1234 xor $5678 not $1234 // equals $567C // equals $444C // equals $EDCB Unsigned and Conversions If a number is converted from less complex to more complex data type, the upper bytes are filled with zeroes.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Bitwise Shift Operators Binary operators shl and shr move the bits of the left operand by a number of positions specified by the right operand, to the left or right, respectively. Right operand has to be positive and less than 255. With shift left (shl), left most bits are discarded, and “new” bits on the right are assigned zeroes.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference EXPRESSIONS An expression is a sequence of operators, operands and punctuators that returns a value. The primary expressions include: literals, constants, variables and function calls. More complex expressions can be created from primary expressions by using operators. Formally, expressions are defined recursively: subexpressions can be nested up to the limits of memory.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR STATEMENTS Statements define algorithmic actions within a program. Each statement needs to be terminated with a semicolon (;). In the absence of specific jump and selection statements, statements are executed sequentially in the order of appearance in the source code. The most simple statements are assignments, procedure calls and jump statements. These can be combined to form loops, branches and other structured statements.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Assignment Statements Assignment statements have the form: variable := expression; The statement evaluates expression and assigns its value to variable. All the rules of implicit conversion are applied. Variable can be any declared variable or array element, and expression can be any expression. Do not confuse the assignment with relational operator = which tests for equality.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR If Statement Use if to implement a conditional statement. The syntax of if statement has the form: if expression then statement1 [else statement2] If expression evaluates to true then statement1 executes. If expression is false then statement2 executes. The expression must convert to a boolean type; otherwise, the condition is ill-formed. The else keyword with an alternate statement (statement2) is optional.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Case statement Use the case statement to pass control to a specific program branch, based on a certain condition. The case statement consists of a selector expression (a condition) and a list of possible values. The syntax of the case statement is: case selector of value_1 : statement_1 ... value_n : statement_n [else default_statement] end; selector is an expression which should evaluate as integral value.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference In mikroPascal PRO for AVR, values in the case statement can be variables too: case byte_variable of byte_var1: opmode := 0; // this will be compiled correctly byte_var2: opmode := 1; // avoid this case, compiler will parse // a variable followed by colon sign as label byte_var3: // adding a comment solves the parsing problem opmode := 2; end; Nested Case statement Note that the case statements can be nested – values are then assigned to the innermos
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference ITERATION STATEMENTS Iteration statements let you loop a set of statements. There are three forms of iteration statements in mikroPascal PRO for AVR: - for - while - repeat You can use the statements break and continue to control the flow of a loop statement. break terminates the statement in which it occurs, while continue begins executing the next iteration of the sequence.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR For Statement The for statement implements an iterative loop and requires you to specify the number of iterations. The syntax of the for statement is: for counter := initial_value to final_value do statement // or for counter := initial_value downto final_value do statement counter is a variable which increments (or decrements if you use downto) with each iteration of the loop.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference While Statement Use the while keyword to conditionally iterate a statement. The syntax of the while statement is: while expression do statement statement is executed repeatedly as long as expression evaluates true. The test takes place before the statement is executed. Thus, if expression evaluates false on the first pass, the loop does not execute.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Repeat Statement The repeat statement executes until the condition becomes false. The syntax of the repeat statement is: repeat statement until expression statement is executed repeatedly as long as expression evaluates true. The expression is evaluated after each iteration, so the loop will execute statement at least once. Here is an example of calculating scalar product of two vectors, using the repeat statement: s := 0; i := 0; ...
CHAPTER 5 mikroPASCAL PRO forAVR Language Reference JUMP STATEMENTS A jump statement, when executed, transfers control unconditionally.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Break and Continue Statements Break Statement Sometimes, you might need to stop the loop from within its body. Use the break statement within loops to pass control to the first statement following the innermost loop (for, while, or repeat block). For example: Lcd_Out(1,1,'Insert CF card'); // Wait for CF card to be plugged; refresh every second while TRUE do begin if Cf_Detect() = 1 then break; Delay_ms(1000); end; // Now we can work with CF card ...
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference begin ... continue; ... // continue jumps here until condition; Exit Statement The exit statement allows you to break out of a routine (function or procedure). It passes the control to the first statement following the routine call. Here is a simple example: procedure Proc1(); var error: byte; begin ... // we're doing something here if error = TRUE then exit; ...
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR Goto Statement Use the goto statement to unconditionally jump to a local label — for more information, refer to Labels. Syntax of goto statement is: goto label_name; This will transfer control to the location of a local label specified by label_name. The goto line can come before or after the label. The label declaration, marked statement and goto statement must belong to the same block.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference asm Statement mikroPascal PRO for AVR allows embedding assembly in the source code by means of the asm statement. Note that you cannot use numerals as absolute addresses for register variables in assembly instructions. You may use symbolic names instead (listing will display these names as well as addresses).
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR DIRECTIVES Directives are words of special significance which provide additional functionality regarding compilation and output. The following directives are available for use: - Compiler directives for conditional compilation, - Linker directives for object distribution in memory.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Compiler Directives mikroPascal PRO for AVR treats comments beginning with a “$” immediately following an opening brace as a compiler directive; for example, {$ELSE}. The compiler directives are not case sensitive. You can use a conditional compilation to select particular sections of code to compile, while excluding other sections. All compiler directives must be completed in the source file in which they have begun.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference The processed section can contain further conditional clauses, nested to any depth; each $IFDEF must be matched with a closing $ENDIF.
CHAPTER 5 mikroPASCAL PRO for AVR Language Reference Linker Directives mikroPascal PRO for AVR uses internal algorithm to distribute objects within memory. If you need to have a variable or a routine at the specific predefined address, use the linker directives absolute and org. Note: You must specify an even address when using the linker directives. Directive absolute Directive absolute specifies the starting address in RAM for a variable.
CHAPTER 5 Language Reference mikroPASCAL PRO for AVR org directive can be used with main routine too. For example: program Led_Blinking; procedure some_proc(); begin ... end; org 0x800; begin DDRB := 0xFF; // main procedure starts at 0x800 while TRUE do begin PORTB := 0x00; Delay_ms(500); PORTB := 0xFF; Delay_ms(500); end; end. Note: You must specify an even address when using the org directive.
CHAPTER 6 mikroPascal PRO for AVR Libraries mikroPascal PRO for AVR provides a set of libraries which simplify the initialization and use of AVR compliant MCUs and their modules: Use Library manager to include mikroPascal PRO for AVR Libraries in you project.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Hardware AVR-specific Libraries - ADC Library - CANSPI Library - Compact Flash Library - EEPROM Library - Flash Memory Library - Graphic Lcd Library - Keypad Library - Lcd Library - Manchester Code Library - Multi Media Card library - OneWire Library - Port Expander Library - PS/2 Library - PWM Library - PWM 16 bit Library - RS-485 Library - Software I2C Library - Software SPI Library - Software UART Library - Sound Library - SPI Library - SPI Ethernet Library -
CHAPTER 6 mikroPASCAL PRO for AVR Libraries LIBRARY DEPENDENCIES Certain libraries use (depend on) function and/or variables, constants defined in other libraries. Image below shows clear representation about these dependencies. For example, SPI_Glcd uses Glcd_Fonts and Port_Expander library which uses SPI library. This means that if you check SPI_Glcd library in Library manager, all libraries on which it depends will be checked too.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Related topics: Library manager, AVR Libraries 184 MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 6 mikroPASCAL PRO for AVR Libraries ADC LIBRARY ADC (Analog to Digital Converter) module is available with a number of AVR micros. Library function ADC_Read is included to provide you comfortable work with the module in single-ended mode. ADC_Read Prototype function ADC_Read( channel : byte ) : word; Returns 10-bit or 12-bit (MCU dependent) unsigned value from the specified channel. Initializes AVR ’s internal ADC module to work with XTAL frequency prescaled by 128.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection ADC HW connection 186 MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPI LIBRARY The SPI module is available with a number of the AVR compliant MCUs. The mikroPascal PRO for AVR provides a library (driver) for working with mikroElektronika's CANSPI Add-on boards (with MCP2515 or MCP2510) via SPI interface. The CAN is a very robust protocol that has error detection and signalization, self–checking and fault confinement. Faulty CAN data and remote frames are re-transmitted automatically, similar to the Ethernet.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines - CANSPISetOperationMode CANSPIGetOperationMode CANSPIInitialize CANSPISetBaudRate CANSPISetMask CANSPISetFilter CANSPIread CANSPIWrite The following routines are for an internal use by the library only: - RegsToCANSPIID - CANSPIIDToRegs Be sure to check CANSPI constants necessary for using some of the functions. CANSPISetOperationMode Prototype procedure CANSPISetOperationMode(mode: byte; WAIT: byte); Returns Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPIGetOperationMode Prototype function CANSPIGetOperationMode(): byte; Returns Current operation mode. The function returns current operation mode of the CANSPI module. Check Description CANSPI_OP_MODE constants (see CANSPI constants) or device datasheet for operation mode codes. The CANSPI routines are supported only by MCUs with the SPI module.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Global variables : Requires CanSpi_CS: Chip Select line CanSpi_Rst: Reset line CanSpi_CS_Bit_Direction: Direction of the Chip Select pin CanSpi_Rst_Bit_Direction: Direction of the Reset pin must be defined before using this function. The CANSPI routines are supported only by MCUs with the SPI module. The SPI module needs to be initialized. See the SPI1_Init and SPI1_Init_Advanced routines.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPISetBaudRate Prototype procedure CANSPISetBaudRate(SJW: byte; BRP: byte; PHSEG1: byte; PHSEG2: byte; PROPSEG: byte; CAN_CONFIG_FLAGS: byte); Returns Nothing. Sets the CANSPI module baud rate. Due to complexity of the CAN protocol, you can not simply force a bps value. Instead, use this function when the CANSPI module is in Config mode. SAM, SEG2PHTS and WAKFIL bits are set according to CAN_CONFIG_FLAGS value. Refer to datasheet for details.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPISetMask Prototype procedure CANSPISetMask(CAN_MASK: byte; val: longint; CAN_CONFIG_FLAGS: byte); Returns Nothing. Configures mask for advanced filtering of messages. The parameter value is bit-adjusted to the appropriate mask registers. Parameters: - CAN_MASK: CANSPI module mask number. Valid values: CANSPI_MASK constants (see CANSPI constants) Description - val: mask register value - CAN_CONFIG_FLAGS: selects type of message to filter.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPISetFilter Prototype procedure CANSPISetFilter(CAN_FILTER: byte; val: longint; CAN_CONFIG_FLAGS: byte); Returns Nothing. Configures message filter. The parameter value is bit-adjusted to the appropriate filter registers. Parameters: - CAN_FILTER: CANSPI module filter number. Valid values: CANSPI_FILTER constants (see CANSPI constants) Description - val: filter register value - CAN_CONFIG_FLAGS: selects type of message to filter.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPIRead Prototype function CANSPIRead(var id: longint; var rd_data: array[8] of byte; data_len: byte; var CAN_RX_MSG_FLAGS: byte): byte; Returns - 0 if nothing is received - 0xFF if one of the Receive Buffers is full (message received) If at least one full Receive Buffer is found, it will be processed in the following way: - Message ID is retrieved and stored to location provided by the id parameter - Message data is retrieved and stored to a buffer provid
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPIWrite Prototype function CANSPIWrite(id: longint; var wr_data: array[8] of byte; data_len: byte; CAN_TX_MSG_FLAGS: byte): byte; Returns - 0 if all Transmit Buffers are busy - 0xFF if at least one Transmit Buffer is available If at least one empty Transmit Buffer is found, the function sends message in the queue for transmission. Parameters: Description - id:CAN message identifier.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPI_OP_MODE The CANSPI_OP_MODE constants define CANSPI operation mode. Function CANSPISetOperationMode expects one of these as it's argument: const CANSPI_MODE_BITS = 0xE0; // Use this to access opmode CANSPI_MODE_NORMAL = 0x00; CANSPI_MODE_SLEEP = 0x20; CANSPI_MODE_LOOP = 0x40; CANSPI_MODE_LISTEN = 0x60; CANSPI_MODE_CONFIG = 0x80; bits CANSPI_CONFIG_FLAGS The CANSPI_CONFIG_FLAGS constants define flags related to the CANSPI module configuration.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries You may use bitwise and to form config byte out of these values. For example: init := CANSPI_CONFIG_SAMPLE_THRICE CANSPI_CONFIG_PHSEG2_PRG_ON CANSPI_CONFIG_STD_MSG CANSPI_CONFIG_DBL_BUFFER_ON CANSPI_CONFIG_VALID_XTD_MSG CANSPI_CONFIG_LINE_FILTER_OFF; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPI_RX_FILTER_2 CANSPI_RX_FILTER_3 CANSPI_RX_FILTER_4 CANSPI_RX_FILTER_5 CANSPI_RX_FILTER_6 CANSPI_RX_OVERFLOW CANSPI_RX_INVALID_MSG CANSPI_RX_XTD_FRAME CANSPI_RX_RTR_FRAME CANSPI_RX_DBL_BUFFERED double-buffered = = = = = 0x01; 0x02; 0x03; 0x04; 0x05; = = = = = 0x08; // Set if Overflowed else cleared 0x10;// Set if invalid else cleared 0x20; // Set if XTD message else cleared 0x40; // Set if RTR message else cleared 0x80; // Set if this message was hardwar
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example This is a simple demonstration of CANSPI Library routines usage. First node initiates the communication with the second node by sending some data to its address. The second node responds by sending back the data incremented by 1. First node then does the same and sends incremented data back to second node, etc.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries CANSPIInitialize(1,3,3,3,1,Can_Init_Flags); // Initialize external CANSPI module CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF); // set CONFIGURATION mode CANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG); // set all mask1 bits to ones CANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG); // set all mask2 bits to ones CANSPISetFilter(_CANSPI_FILTER_B2_F4,3,_CANSPI_CONFIG_XTD_MSG); // set id of filter B1_F1 to 3 CANSPISetOperationMode(_CANSPI_MODE_NO
CHAPTER 6 mikroPASCAL PRO for AVR Libraries var CanSpi_CS : sbit at PORTB.B0; CanSpi_CS_Direction : sbit at DDRB.B0; CanSpi_Rst : sbit at PORTB.B2; CanSpi_Rst_Direction : sbit at DDRB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries begin Msg_Rcvd := CANSPIRead(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags); // receive message if ((Rx_ID = 12111) and Msg_Rcvd) then // if message received check id begin PORTC := RxTx_Data[0]; // id correct, output data at PORTC Inc(RxTx_Data[0]) ; // increment received data CANSPIWrite(Tx_ID, RxTx_Data, 1, Can_Send_Flags); // send incremented data back end; end; end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries COMPACT FLASH LIBRARY The Compact Flash Library provides routines for accessing data on Compact Flash card (abbr. CF further in text). CF cards are widely used memory elements, commonly used with digital cameras. Great capacity and excellent access time of only a few microseconds make them very attractive for the microcontroller applications. In CF card, data is divided into sectors. One sector usually comprises 512 bytes.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries External dependencies of Compact Flash Library The following variables must be defined in all projects using Compact Flash Library: Example : var CF_Data_Port : byte; sfr; external; Compact Flash Data Port. var CF_Data_Port : byte at PORTD; var CF_Data_Port_Direction : byte; sfr; external; Direction of the Compact Flash Data Port. var CF_Data_Port_Directio n : byte at DDRD; var CF_RDY : sbit; sfr; Ready signal line. external; var CF_RDY : sbit at PINB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines - Cf_Init Cf_Detect Cf_Enable Cf_Disable Cf_Read_Init Cf_Read_Byte Cf_Write_Init Cf_Write_Byte Cf_Read_Sector Cf_Write_Sector Routines for file handling: - Cf_Fat_Init Cf_Fat_QuickFormat Cf_Fat_Assign Cf_Fat_Reset Cf_Fat_Read Cf_Fat_Rewrite Cf_Fat_Append Cf_Fat_Delete Cf_Fat_Write Cf_Fat_Set_File_Date Cf_Fat_Get_File_Date Cf_Fat_Get_File_Size Cf_Fat_Get_Swap_File Cf_Init Prototype procedure Cf_Init() ; Returns Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Requires - CF_A1 : Address pin 1 CF_A0 : Address pin 0 CF_Data_Port_direction : Direction of the Compact Flash data direction port CF_RDY_direction : Direction of the Ready pin CF_WE_direction : Direction of the Write enable pin CF_OE_direction : Direction of the Output enable pin CF_CD1_direction : Direction of the Chip detect pin CF_CE1_direction : Direction of the Chip enable pin CF_A2_direction : Direction of the Address 2 pin CF_A1_direction : Direction o
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Detect Prototype function CF_Detect() : byte ; Returns - 1 - if CF card was detected - 0 - otherwise Description Checks for presence of CF card by reading the chip detect pin. Requires The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. Example // Wait until CF card is inserted: while (Cf_Detect() = 0) do nop; Cf_Enable Prototype procedure Cf_Enable(); Returns Nothing. Enables the device.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Read_Init Prototype procedure Cf_Read_Init(address : dword; sector_count : byte); Returns Nothing. Initializes CF card for reading. Description Parameters : - address: the first sector to be prepared for reading operation. - sector_count: number of sectors to be prepared for reading operation. Requires The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Write_Init Prototype procedure Cf_Write_Init(address : dword; sectcnt : byte); Returns Nothing. Initializes CF card for writing. Description Parameters : - address: the first sector to be prepared for writing operation. - sectcnt: number of sectors to be prepared for writing operation. Requires The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Read_Sector Prototype procedure Cf_Read_Sector(sector_number : dword; var buffer : array[512] of byte); Returns Nothing. Reads one sector (512 bytes). Read data is stored into buffer provided by the buffer parameter. Description Parameters : - sector_number: sector to be read. - buffer: data buffer of at least 512 bytes in length. Requires The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Init Prototype function Cf_Fat_Init(): byte; Returns - 0 - if CF card was detected and successfully initialized - 1 - if FAT16 boot sector was not found - 255 - if card was not detected Description Initializes CF card, reads CF FAT16 boot sector and extracts data needed by the library. Requires Nothing. Example //init the FAT library if (Cf_Fat_Init() = 0) then begin ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Assign Prototype Returns function Cf_Fat_Assign(var filename: array[12] of char; file_cre_attr: byte): byte; - 0 if file does not exist and no new file is created. - 1 if file already exists or file does not exist but a new file is created. Assigns file for file operations (read, write, delete...). All subsequent file operations will be applied to the assigned file. Parameters : - filename: name of the file that should be assigned for file operations.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Reset Prototype procedure Cf_Fat_Reset(var size: dword); Returns Nothing. Opens currently assigned file for reading. Description Parameters : - size: buffer to store file size to. After file has been open for reading its size is returned through this parameter. CF card and CF library must be initialized for file operations. See Cf_Fat_Init. Requires File must be previously assigned. See Cf_Fat_Assign. Example var size : dword; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Rewrite Prototype procedure Cf_Fat_Rewrite(); Returns Nothing. Description Opens currently assigned file for writing. If the file is not empty its content will be erased. CF card and CF library must be initialized for file operations. See Cf_Fat_Init. Requires The file must be previously assigned. See Cf_Fat_Assign. Example // open file for writing Cf_Fat_Rewrite(); Cf_Fat_Append Prototype procedure Cf_Fat_Append(); Returns Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Write Prototype procedure Cf_Fat_Write(var fdata: array[512] of byte; data_len: word); Returns Nothing. Writes requested number of bytes to currently assigned file opened for writing. Description Parameters : - fdata: data to be written. - data_len: number of bytes to be written. CF card and CF library must be initialized for file operations. See Cf_Fat_Init. Requires File must be previously assigned. See Cf_Fat_Assign.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Set_File_Date Prototype procedure Cf_Fat_Set_File_Date(year: word; month: byte; day: byte; hours: byte; mins: byte; seconds: byte); Returns Nothing. Sets the date/time stamp. Any subsequent file writing operation will write this stamp to currently assigned file's time/date attributs. Parameters : Description - year: year attribute. Valid values: 1980-2107 month: month attribute. Valid values: 1-12 day: day attribute.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Get_File_Date Prototype procedure Cf_Fat_Get_File_Date(var year: word; var month: byte; var day: byte; var hours: byte; var mins: byte); Returns Nothing. Reads time/date attributes of currently assigned file. Parameters : - year: buffer to store year attribute to. Upon function execution year attribute is returned through this parameter. - month: buffer to store month attribute to.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Cf_Fat_Get_Swap_File Prototype function Cf_Fat_Get_Swap_File(sectors_cnt: longint; var filename : string[11]; file_attr : byte): dword; Returns - Number of the start sector for the newly created swap file, if there was enough free space on CF card to create file of required size. - 0 - otherwise. This function is used to create a swap file of predefined name and size on the CF media.
CHAPTER 6 mikroPASCAL PRO for AVR Description Libraries Bit Mask Description 0 0x01 Read Only 1 0x02 Hidden 2 0x04 System 3 0x08 Volume Label 4 0x10 Subdirectory 5 0x20 Archive 6 0x40 Device (internal use only, never found on disk) 7 0x80 Not used Note: Long File Names (LFN) are not supported. Requires CF card and CF library must be initialized for file operations. See Cf_Fat_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The following example is a simple demonstration of CF(Compact Flash) Library which shows how to use CF card data accessing routines. program CF_Fat16_Test; var // set compact flash pinout Cf_Data_Port : byte at PORTD; Cf_Data_Port_Direction : byte CF_RDY CF_WE CF_OE CF_CD1 CF_CE1 CF_A2 CF_A1 CF_A0 : : : : : : : : sbit sbit sbit sbit sbit sbit sbit sbit at at at at at at at at at DDRD; PINB.B7; PORTB.B6; PORTB.B5; PINB.B4; PORTB.B3; PORTB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries while ostr[i] <> 0 do begin UART1_Write(ostr[i]); Inc(i); end; UART1_Write($0A); end; //-------------- Creates new file and writes some data to it procedure Create_New_File; begin filename[7] := 'A'; Cf_Fat_Assign(filename, 0xA0); // Will not find file and then create file Cf_Fat_Rewrite(); // To clear file and start with new data for loop:=1 to 90 do // We want 5 files on the MMC card begin PORTC := loop; file_contents[0] := loop div 10 + 48; file_contents[1] :
CHAPTER 6 Libraries mikroPASCAL PRO for AVR for loop := 1 to 55 do begin file_contents[0] := byte(loop div 10 + 48); file_contents[1] := byte(loop mod 10 + 48); Cf_Fat_Write(file_contents, 38); // write data to the assigned file end; end; //-------------- Opens an existing file and appends data to it // (and alters the date/time stamp) procedure Open_File_Append; begin filename[7] := 'B'; Cf_Fat_Assign(filename, 0); Cf_Fat_Set_File_Date(2005,6,21,10,35,0); Cf_Fat_Append; file_contents := ' for mikroElektr
CHAPTER 6 mikroPASCAL PRO for AVR Libraries // and then deleted.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries // sectors (see Help for details) procedure M_Create_Swap_File; var i : word; begin for i:=0 to 511 do Buffer[i] := i; size := Cf_Fat_Get_Swap_File(5000, 'mikroE.txt', 0x20); help on this function for details // see if (size <> 0) then begin LongIntToStr(size, fat_txt); Write_Str(fat_txt); for i:=0 to 4999 do begin Cf_Write_Sector(size, Buffer); size := size+1; UART1_Write('.'); end; end; end; //-------------- Main.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Create_Multiple_Files(); //----- test group #2 Open_File_Rewrite(); Open_File_Append(); Delete_File; //----- test group #3 Open_File_Read(); Test_File_Exist('F'); M_Create_Swap_File(); //--- Test termination UART1_Write(0xAA); end else begin UART1_Write_Text(FAT_TXT); end; //--- signal end-of-test UART1_Write_Text(':End:'); end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection Pin diagram of CF memory card 226 MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 6 mikroPASCAL PRO for AVR Libraries EEPROM LIBRARY EEPROM data memory is available with a number of AVR family. The mikroPascal PRO for AVR includes a library for comfortable work with MCU's internal EEPROM. Note: EEPROM Library functions implementation is MCU dependent, consult the appropriate MCU datasheet for details about available EEPROM size and constrains.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries EEPROM_Write Prototype procedure EEPROM_Write(address: word; wrdata: byte); Returns Nothing. Writes wrdata to specified address. Parameters : Description - address: address of the EEPROM memory location to be written. - wrdata: data to be written. Note: Specified memory location will be erased before writing starts. Requires Nothing. Example var eeWrite : byte; wrAddr : word; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Delay_ms(1000); PORTA := 0x00; PORTB := 0x00; Delay_ms(1000); PORTA address PORTB address := EEPROM_Read(0x02); 2 and display it on PORT0 := EEPROM_Read(0x150); 0x150 and display it on PORT1 // Read data from // Read data from Delay_ms(1000); for counter := 0 to 31 do block from address 0x100 begin PORTC := EEPROM_Read(0x100+counter); data on PORT2 Delay_ms(100); end; end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries FLASH MEMORY LIBRARY This library provides routines for accessing microcontroller Flash memory. Note that prototypes differ for MCU to MCU due to the amount of Flash memory. Note: Due to the AVR family flash specifics, flash library is MCU dependent. Since some AVR MCU's have more or less than 64kb of Flash memory, prototypes may be different from chip to chip. Please refer to datasheet before using flash library.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries FLASH_Read_Bytes // for MCUs with 64kb of Flash memory or less procedure FLASH_Read_Bytes(address : word; buffer : ^byte; NoBytes : word); Prototype // for MCUs with Flash memory larger than 64kb procedure FLASH_Read_Bytes(address : dword; buffer : ^byte; NoBytes : word) Returns Nothing. Description Reads number of data bytes defined by NoBytes parameter from the specified address in Flash memory to variable pointed by buffer. Requires Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries FLASH_Read_Words // for MCUs with 64kb of Flash memory or less procedure FLASH_Read_Words(address : word; buffer : ^word; NoWords : word); Prototype // for MCUs with Flash memory larger than 64kb procedure FLASH_Read_Words(address : dword; buffer : ^word; NoWords : word); Returns Nothing. Description Reads number of data words defined by NoWords parameter from the specified address in Flash memory to variable pointed by buffer. Requires Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries counter := 0; while ( counter < 64 ) do // reading 64 bytes in loop begin PORTD := FLASH_Read_Byte(F_ADDRESS + counter); // demonstration of reading single byte Inc(counter); PORTB := FLASH_Read_Byte(F_ADDRESS + counter); // demonstration of reading single byte Inc(counter); Delay_ms(200); end; FLASH_Read_Bytes(F_ADDRESS, @dat_buff, 64); // demonstration of reading 64 bytes for counter := 0 to 31 do begin PORTD := dat_buff[counter]; // output low byte to PORTD P
CHAPTER 6 mikroPASCAL PRO for AVR Libraries GRAPHIC LCD LIBRARY The mikroPascal PRO for AVR provides a library for operating Graphic Lcd 128x64 (with commonly used Samsung KS108/KS107 controller). For creating a custom set of Glcd images use Glcd Bitmap Editor Tool. External dependencies of Graphic Lcd Library The following variables must be defined in all projects using Sound Library: var GLCD_DataPort : byte; sfr; external; Glcd Data Port.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines Basic routines: - Glcd_Init Glcd_Set_Side Glcd_Set_X Glcd_Set_Page Glcd_Read_Data Glcd_Write_Data Advanced routines: - Glcd_Fill Glcd_Dot Glcd_Line Glcd_V_Line Glcd_H_Line Glcd_Rectangle Glcd_Box Glcd_Circle Glcd_Set_Font Glcd_Write_Char Glcd_Write_Text Glcd_Image MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 235
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Init Prototype procedure Glcd_Init(); Returns Nothing. Description Initializes the Glcd module. Each of the control lines is both port and pin configurable, while data lines must be on a single port (pins <0:7>).
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Set_Side Prototype procedure Glcd_Set_Side(x_pos: byte); Returns Nothing. Selects Glcd side. Refer to the Glcd datasheet for detailed explaination. Parameters : - x_pos: position on x-axis. Valid values: 0..127 Description The parameter x_pos specifies the Glcd side: values from 0 to 63 specify the left side, values from 64 to 127 specify the right side. Note: For side, x axis and page layout explanation see schematic at the bottom of this page.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Set_Page Prototype procedure Glcd_Set_Page(page: byte); Returns Nothing. Selects page of the Glcd. Parameters : Description - page: page number. Valid values: 0..7 Note: For side, x axis and page layout explanation see schematic at the bottom of this page. Requires Glcd needs to be initialized, see Glcd_Init routine. Example Glcd_Set_Page(5); Glcd_Read_Data Prototype function Glcd_Read_Data(): byte; Returns One byte from Glcd memory.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Write_Data Prototype procedure Glcd_Write_Data(ddata: byte); Returns Nothing. Writes one byte to the current location in Glcd memory and moves to the next location. Description Parameters : - ddata: data to be written Glcd needs to be initialized, see Glcd_Init routine. Requires Example Glcd side, x-axis position and page should be set first. See functions Glcd_Set_Side, Glcd_Set_X, and Glcd_Set_Page. var data: byte; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Dot Prototype procedure Glcd_Dot(x_pos: byte; y_pos: byte; color: byte); Returns Nothing. Draws a dot on Glcd at coordinates (x_pos, y_pos). Parameters : - x_pos: x position. Valid values: 0..127 - y_pos: y position. Valid values: 0..63 Description - color: color parameter. Valid values: 0..2 The parameter color determines a dot state: 0 clears dot, 1 puts a dot, and 2 inverts dot state.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_V_Line Prototype procedure Glcd_V_Line(y_start: byte; y_end: byte; x_pos: byte; color: byte); Returns Nothing. Draws a vertical line on Glcd. Parameters : Description - y_start: y coordinate of the line start. Valid values: 0..63 y_end: y coordinate of the line end. Valid values: 0..63 x_pos: x coordinate of vertical line. Valid values: 0..127 color: color parameter. Valid values: 0..
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Rectangle Prototype procedure Glcd_Rectangle(x_upper_left: byte; y_upper_left: byte; x_bottom_right: byte; y_bottom_right: byte; color: byte); Returns Nothing. Draws a rectangle on Glcd. Parameters : - x_upper_left: x coordinate of the upper left rectangle corner. Valid values: 0..127 - y_upper_left: y coordinate of the upper left rectangle corner. Valid values: 0..63 - x_bottom_right: x coordinate of the lower right rectangle corner.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Circle Prototype procedure Glcd_Circle(x_center: integer; y_center: integer; radius: integer; color: byte); Returns Nothing. Draws a circle on Glcd. Parameters : Description - x_center: x coordinate of the circle center. Valid values: 0..127 y_center: y coordinate of the circle center. Valid values: 0..63 radius: radius size color: color parameter. Valid values: 0..
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Write_Char Prototype procedure Glcd_Write_Char(chr: byte; x_pos: byte; page_num: byte; color: byte); Returns Nothing. Prints character on the Glcd. Parameters : - chr: character to be written - x_pos: character starting position on x-axis. Valid values: 0..(127-FontWidth) - page_num: the number of the page on which character will be written. Valid Description values: 0..7 - color: color parameter. Valid values: 0..
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Glcd_Write_Text Prototype procedure Glcd_Write_Text(var text: array[20] of char; x_pos: byte; page_num: byte; color: byte); Returns Nothing. Prints text on Glcd. Parameters : - text: text to be written - x_pos: text starting position on x-axis. - page_num: the number of the page on which text will be written. Valid values: Description 0..7 - color: color parameter. Valid values: 0..
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The following example demonstrates routines of the Glcd library: initialization, clear(pattern fill), image displaying, drawing lines, circles, boxes and rectangles, text displaying and handling.
CHAPTER 6 mikroPASCAL PRO for AVR Glcd_Box(62,40,124,63,1); Glcd_Rectangle(5,5,84,35,1); Glcd_Line(0, 0, 127, 63, 1); Delay2S(); counter := 5; Libraries // Draw box // Draw rectangle // Draw line while (counter <= 59) do // Draw horizontal and vertical lines begin Delay_ms(250); Glcd_V_Line(2, 54, counter, 1); Glcd_H_Line(2, 120, counter, 1); Counter := counter + 5; end; Delay2S(); Glcd_Fill(0x00); // Clear Glcd Glcd_Set_Font(@Character8x7, 8, 7, 32); // Choose font "Character8x7" Glcd_Write_Text('mikro
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection Glcd HW connection 248 MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 6 mikroPASCAL PRO for AVR Libraries KEYPAD LIBRARY The mikroPascal PRO for AVR provides a library for working with 4x4 keypad. The library routines can also be used with 4x1, 4x2, or 4x3 keypad. For connections explanation see schematic at the bottom of this page. Note: Since sampling lines for AVR MCUs are activated by logical zero Keypad Library can not be used with hardwares that have protective diodes connected with anode to MCU side, such as mikroElektronika's Keypad extra board HW.Rev v1.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Keypad_Key_Press Prototype function Keypad_Key_Press(): byte; The code of a pressed key (1..16). Returns If no key is pressed, returns 0. Description Reads the key from keypad when key gets pressed. Requires Port needs to be initialized for working with the Keypad library, see Keypad_Init. Example var kp : byte; ... kp := Keypad_Key_Press(); Keypad_Key_Click Prototype function Keypad_Key_Click(): byte; The code of a clicked key (1..16).
CHAPTER 6 mikroPASCAL PRO for AVR Libraries program Keypad_Test; var kp, cnt, oldstate : byte; txt : array[6] of byte; // Keypad module connections var keypadPort : byte at PORTB; var keypadPort_Direction : byte at DDRB; // End Keypad module connections // Lcd pinout definition var LCD_RS : sbit at PORTD.B2; LCD_EN : sbit at PORTD.B3; LCD_D4 : sbit at PORTD.B4; LCD_D5 : sbit at PORTD.B5; LCD_D6 : sbit at PORTD.B6; LCD_D7 : sbit at PORTD.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries 1: kp := 49; // 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 1 kp kp kp kp kp kp kp kp kp kp kp kp kp kp kp := := := := := := := := := := := := := := := 50; 51; 65; 52; 53; 54; 66; 55; 56; 57; 67; 42; 48; 35; 68; // // // // // // // // // // // // // // // // Uncomment this block for keypad4x4 2 3 A 4 5 6 B 7 8 9 C * 0 # D end; if (kp <> oldstate) then // Pressed key differs from previous begin cnt := 1; oldstate := kp; end else Inc(cnt); // Pre
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection 4x4 Keypad connection scheme MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 253
CHAPTER 6 mikroPASCAL PRO for AVR Libraries LCD LIBRARY The mikroPascal PRO for AVR provides a library for communication with Lcds (with HD44780 compliant controllers) through the 4-bit interface. An example of Lcd connections is given on the schematic at the bottom of this page. For creating a set of custom Lcd characters use Lcd Custom Character Tool.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Lcd_Init Prototype procedure Lcd_Init() Returns Nothing. Description Initializes Lcd module.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Lcd_Out Prototype procedure Lcd_Out(row: byte; column: byte; var text: array [20] of char); Returns Nothing. Prints text on Lcd starting from specified position. Both string variables and literals can be passed as a text. Description Parameters : - row: starting position row number - column: starting position column number - text: text to be written Requires The Lcd module needs to be initialized. See Lcd_Init routine.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Lcd_Chr Prototype procedure Lcd_Chr(row: byte; column: byte; out_char: byte); Returns Nothing. Prints character on Lcd at specified position. Both variables and literals can be passed as a character. Description Parameters : - row: writing position row number - column: writing position column number - out_char: character to be written Requires The Lcd module needs to be initialized. See Lcd_Init routine.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Lcd_Cmd Prototype procedure Lcd_Cmd(out_char: byte); Returns Nothing. Sends command to Lcd. Parameters : Description - out_char: command to be sent Note: Predefined constants can be passed to the function, see Available Lcd Commands. Requires The Lcd module needs to be initialized. See Lcd_Init table.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The following code demonstrates usage of the Lcd Library routines: program Lcd_Test; // Lcd module connections var LCD_RS : sbit at PORTD.B2; var LCD_EN : sbit at PORTD.B3; var LCD_D4 : sbit at PORTD.B4; var LCD_D5 : sbit at PORTD.B5; var LCD_D6 : sbit at PORTD.B6; var LCD_D7 : sbit at PORTD.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries // Moving text for i:=0 to 3 do // Move text to the right 4 times begin Lcd_Cmd(LCD_SHIFT_RIGHT); Move_Delay(); end; while TRUE do // Endless loop begin for i:=0 to 6 do // Move text to the left 7 times begin Lcd_Cmd(LCD_SHIFT_LEFT); Move_Delay(); end; for i:=0 to 6 do // Move text to the right 7 times begin Lcd_Cmd(LCD_SHIFT_RIGHT); Move_Delay(); end; end; end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW connection Lcd HW connection MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 261
CHAPTER 6 mikroPASCAL PRO for AVR Libraries MANCHESTER CODE LIBRARY The mikroPascal PRO for AVR provides a library for handling Manchester coded signals.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines - Man_Receive_Init Man_Receive Man_Send_Init Man_Send Man_Synchro Man_Break The following routines are for the internal use by compiler only: - Manchester_0 - Manchester_1 - Manchester_Out Man_Receive_Init Prototype function Man_Receive_Init(): word; Returns - 0 - if initialization and synchronization were successful. - 1 - upon unsuccessful synchronization.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Man_Receive Prototype function Man_Receive(var error: byte): byte; Returns A byte read from the incoming signal. The function extracts one byte from incoming signal. Description Parameters : - error: error flag. If signal format does not match the expected, the error flag will be set to non-zero. Requires Example To use this function, the user must prepare the MCU for receiving. See Man_Receive_Init. var data, error : byte ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Man_Send Prototype procedure Man_Send(tr_data: byte); Returns Nothing. Sends one byte. Parameters : Description - tr_data: data to be sent Note: Baud rate used is 500 bps. Requires To use this function, the user must prepare the MCU for sending. See Man_Send_Init. Example var msg : byte; ... Man_Send(msg); Man_Synchro Prototype function Man_Synchro(): word; Returns - 0 - if synchronization was not successful.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Man_Break Prototype procedure Man_Break(); Returns Nothing. Description Man_Receive is blocking routine and it can block the program flow. Call this routine from interrupt to unblock the program execution. This mechanism is similar to WDT. Note: Interrupts should be disabled before using Manchester routines again (see note at the top of this page). Requires Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The following code is code for the Manchester receiver, it shows how to use the Manchester Library for receiving data: program Manchester_Receiver; // Lcd module connections var LCD_RS : sbit at PORTD.B2; LCD_EN : sbit at PORTD.B3; LCD_D4 : sbit at PORTD.B4; LCD_D5 : sbit at PORTD.B5; LCD_D6 : sbit at PORTD.B6; LCD_D7 : sbit at PORTD.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries break; if (error <> 0) then break; end; // We got the starting sequence // Exit so we do not loop forever repeat begin temp := Man_Receive(error); // Attempt byte receive if (error <> 0) then // If error occured begin Lcd_Chr_CP('?'); // Write question mark on Lcd Inc(ErrorCount); // Update error counter if (ErrorCount > 20) then // In case of multiple errors begin temp := Man_Synchro(); // Try to synchronize again //Man_Receive_Init(); // Alternative, try to I
CHAPTER 6 mikroPASCAL PRO for AVR begin s1 := 'mikroElektronika'; Man_Send_Init(); while TRUE do begin Man_Send(0x0B); Delay_ms(100); Libraries // Initialize transmitter // Endless loop // Send "start" byte // Wait for a while character := s1[0]; // Take first char from string index := 0; // Initialize index variable while (character <> 0) do // String ends with zero begin Man_Send(character); // Send character Delay_ms(90); // Wait for a while Inc(index); // Increment index variable character := s1[inde
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Simple Receiver connection 270 MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 6 mikroPASCAL PRO for AVR Libraries MULTI MEDIA CARD LIBRARY The Multi Media Card (MMC) is a flash memory card standard. MMC cards are currently available in sizes up to and including 1 GB, and are used in cell phones, mp3 players, digital cameras, and PDA’s. mikroPascal PRO for AVR provides a library for accessing data on Multi Media Card via SPI communication.This library also supports SD(Secure Digital) memory cards.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines - Mmc_Init Mmc_Read_Sector Mmc_Write_Sector Mmc_Read_Cid Mmc_Read_Csd Routines for file handling: - 272 Mmc_Fat_Init Mmc_Fat_QuickFormat Mmc_Fat_Assign Mmc_Fat_Reset Mmc_Fat_Read Mmc_Fat_Rewrite Mmc_Fat_Append Mmc_Fat_Delete Mmc_Fat_Write Mmc_Fat_Set_File_Date Mmc_Fat_Get_File_Date Mmc_Fat_Get_File_Size Mmc_Fat_Get_Swap_File MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Init Prototype function Mmc_Init(): byte; Returns - 0 - if MMC/SD card was detected and successfully initialized - 1 - otherwise Initializes MMC through hardware SPI interface. Description Parameters: - port: chip select signal port address. - cspin: chip select pin. Global variables : Requires - Mmc_Chip_Select: Chip Select line - Mmc_Chip_Select_Direction: Direction of the Chip Select pin must be defined before using this function.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Read_Sector Prototype function Mmc_Read_Sector(sector: longint; var dbuff: array[512] of byte): byte; Returns - 0 - if reading was successful - 1 - if an error occurred The function reads one sector (512 bytes) from MMC card. Description Parameters: - sector: MMC/SD card sector to be read. - data: buffer of minimum 512 bytes in length for data storage. Requires MMC/SD card must be initialized. See Mmc_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Read_Cid Prototype function Mmc_Read_Cid(var data_cid: array[16] of byte): byte; Returns - 0 - if CID register was read successfully - 1 - if there was an error while reading The function reads 16-byte CID register. Description Parameters: - data_cid: buffer of minimum 16 bytes in length for storing CID register content. Requires MMC/SD card must be initialized. See Mmc_Init. Example var error : byte; dataBuffer : array[16] of byte; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Init Prototype function Mmc_Fat_Init(): byte; Returns - 0 - if MMC/SD card was detected and successfully initialized - 1 - if FAT16 boot sector was not found - 255 - if MMC/SD card was not detected Description Initializes MMC/SD card, reads MMC/SD FAT16 boot sector and extracts necessary data needed by the library. Note: MMC/SD card has to be formatted to FAT16 file system.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_QuickFormat Prototype function Mmc_Fat_QuickFormat(var port : word; pin : word; var mmc_fat_label : string[11]) : byte; Returns - 0 - if MMC/SD card was detected, successfully formated and initialized - 1 - if FAT16 format was unseccessful - 255 - if MMC/SD card was not detected Formats to FAT16 and initializes MMC/SD card. Parameters: - port: chip select signal port address. - pin: chip select pin.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Assign Prototype Returns function Mmc_Fat_Assign(var filename: array[12] of char; file_cre_attr: byte): byte; - 1 - if file already exists or file does not exist but a new file is created. - 0 - if file does not exist and no new file is created. Assigns file for file operations (read, write, delete...). All subsequent file operations will be applied on an assigned file.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Reset Prototype procedure Mmc_Fat_Reset(var size: dword); Returns Nothing. Opens currently assigned file for reading. Description Parameters: - size: buffer to store file size to. After file has been open for reading, its size is returned through this parameter. Requires MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. Example var size : dword; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Rewrite Prototype procedure Mmc_Fat_Rewrite(); Returns Nothing. Description Opens the currently assigned file for writing. If the file is not empty its content will be erased. Requires MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Write Prototype procedure Mmc_Fat_Write(var fdata: array[512] of byte; data_len: word); Returns Nothing. Writes requested number of bytes to the currently assigned file opened for writing. Description Parameters: - fdata: data to be written. - data_len: number of bytes to be written. MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. Requires The file must be previously assigned. See Mmc_Fat_Assign.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Set_File_Date Prototype procedure Mmc_Fat_Set_File_Date(year: word; month: byte; day: byte; hours: byte; mins: byte; seconds: byte); Returns Nothing. Sets the date/time stamp. Any subsequent file writing operation will write this stamp to the currently assigned file's time/date attributs. Parameters: Description - year: year attribute. Valid values: 1980-2107 month: month attribute. Valid values: 1-12 day: day attribute.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Get_File_Date Prototype procedure Mmc_Fat_Get_File_Date(var year: word; var month: byte; var day: byte; var hours: byte; var mins: byte); Returns Nothing. Reads time/date attributes of the currently assigned file. Parameters: - year: buffer to store year attribute to. Upon function execution year attribute is returned through this parameter. - month: buffer to store month attribute to.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Mmc_Fat_Get_Swap_File Prototype function Mmc_Fat_Get_Swap_File(sectors_cnt: longint; var filename : string[11]; file_attr : byte) : dword; Returns - Number of the start sector for the newly created swap file, if there was enough free space on the MMC/SD card to create file of required size. - 0 - otherwise. This function is used to create a swap file of predefined name and size on the MMC/SD media.
CHAPTER 6 mikroPASCAL PRO for AVR Description Libraries Bit Mask Description 0 0x01 Read Only 1 0x02 Hidden 2 0x04 System 3 0x08 Volume Label 4 0x10 Subdirectory 5 0x20 Archive 6 0x40 Device (internal use only, never found on disk) 7 0x80 Not used Note: Long File Names (LFN) are not supported. Requires MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The following example demonstrates MMC library test. Upon flashing, insert a MMC/SD card into the module, when you should receive the "Init-OK" message. Then, you can experiment with MMC read and write functions, and observe the results through the Usart Terminal. // if defined, we have a debug messages on PC terminal program MMC_Test; {$DEFINE RS232_debug} var MMC_chip_select : sbit at PORTB.B2; var MMC_chip_select_direction : sbit at DDRB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries {$IFDEF RS232_debug} UART1_Write_Text('PIC-Started'); // If PIC present report UART1_Write(13); UART1_Write(10); {$ENDIF} // Before all, we must initialize a MMC card SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV2, _SPI_CLK_LO_LEADING); Spi_Rd_Ptr := @SPI1_Read; i := Mmc_Init(); PORTC := i; {$IFDEF RS232_debug} if(i = 0) then begin UART1_Write_Text('MMC Init-OK'); // If MMC present report UART1_Write(13); UART1_Write(10); end; if(i) then begin UART1_Write_Text('M
CHAPTER 6 mikroPASCAL PRO for AVR Libraries printhex(data_for_registers[k]); if(k <> 15) then UART1_Write('-'); end; UART1_Write(13); end else begin UART1_Write_Text('CID-error'); end; i := Mmc_Read_Csd(data_for_registers); if(i = 0) then begin for k:=0 to 15 do begin printhex(data_for_registers[k]); if(k <> 15) then UART1_Write('-'); end; UART1_Write(13); UART1_Write(10); end else begin UART1_Write_Text('CSD-error'); end; {$ENDIF} end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries size : longint; buffer : array[512] of byte; //-------------- Writes string to USART procedure Write_Str(var ostr: array[2] of byte); var i : byte; begin i := 0; while ostr[i] <> 0 do begin UART1_Write (ostr[i]); Inc(i); end; UART1_Write($0A); end;//~ //-------------- Creates new file and writes some data to it procedure Create_New_File; begin filename[7] := 'A'; // Set filename for single-file tests Mmc_Fat_Assign(filename, 0xA0); // Will not find file and then
CHAPTER 6 mikroPASCAL PRO for AVR Libraries end;//~ //-------------- Opens an existing file and rewrites it procedure Open_File_Rewrite; begin filename[7] := 'C'; // Set filename for single-file tests Mmc_Fat_Assign(filename, 0); Mmc_Fat_Rewrite; for loop := 1 to 55 do begin file_contents[0] := byte(loop div 10 + 48); file_contents[1] := byte(loop mod 10 + 48); Mmc_Fat_Write(file_contents, 42); // write data to the assigned file end; end;//~ //-------------- Opens an existing file and appends data to it /
CHAPTER 6 mikroPASCAL PRO for AVR Libraries procedure Delete_File; begin filename[7] := 'F'; Mmc_Fat_Assign(filename, 0); Mmc_Fat_Delete; end;//~ //-------------- Tests whether file exists, and if so sends its creation date // and file size via USART procedure Test_File_Exist; var fsize: longint; year: word; month, day, hour, minute: byte; outstr: array[12] of byte; begin filename[7] := 'B'; if Mmc_Fat_Assign(filename, 0) <> 0 then begin //--- file has been found - get its date Mmc_Fat_Get_File_Date(year,
CHAPTER 6 mikroPASCAL PRO for AVR Libraries begin for i:=0 to 511 do Buffer[i] := i; size := Mmc_Fat_Get_Swap_File(5000, 'mikroE.txt', 0x20); help on this function for details // see if (size <> 0) then begin LongIntToStr(size, fat_txt); UART1_Write_Text(fat_txt); for i:=0 to 4999 do begin Mmc_Write_Sector(size, Buffer); size := size + 1; UART1_Write('.'); end; end; end; //-------------- Main.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Open_File_Rewrite; Open_File_Append; Open_File_Read; Delete_File; Test_File_Exist; M_Create_Swap_File(); UART1_Write('e'); end else begin UART1_Write_Text(FAT_TXT); end; //--- signal end-of-test PORTC := $0F; UART1_Write_Text('End'); end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection Pin diagram of MMC memory card 294 MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
CHAPTER 6 mikroPASCAL PRO for AVR Libraries ONEWIRE LIBRARY The OneWire library provides routines for communication via the Dallas OneWire protocol, e.g. with DS18x20 digital thermometer. OneWire is a Master/Slave protocol, and all communication cabling required is a single wire. OneWire enabled devices should have open collector drivers (with single pull-up resistor) on the shared data line. Slave devices on the OneWire bus can even get their power supply from data line.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Ow_Reset Prototype function Ow_Reset(): word; Returns - 0 if the device is present - 1 if the device is not present Issues OneWire reset signal for DS18x20. Description Parameters : - None. Devices compliant with the Dallas OneWire protocol. Global variables : Requires - OW_Bit_Read: OneWire read line - OW_Bit_Write: OneWire write line. - OW_Bit_Direction: Direction of the OneWire pin must be defined before using this function.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Ow_Read Prototype function Ow_Read(): byte; Returns Data read from an external device over the OneWire bus. Description Reads one byte of data via the OneWire bus. Devices compliant with the Dallas OneWire protocol. Global variables : Requires - OW_Bit_Read: OneWire read line - OW_Bit_Write: OneWire write line. - OW_Bit_Direction: Direction of the OneWire pin must be defined before using this function.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Ow_Write Prototype procedure Ow_Write(par: byte); Returns Nothing. Writes one byte of data via the OneWire bus. Description Parameters : - par: data to be written Devices compliant with the Dallas OneWire protocol. Global variables : Requires - OW_Bit_Read: OneWire read line - OW_Bit_Write: OneWire write line. - OW_Bit_Direction: Direction of the OneWire pin must be defined before using this function.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries LCD_D5_Direction : sbit at DDRD.B5; LCD_D6_Direction : sbit at DDRD.B6; LCD_D7_Direction : sbit at DDRD.B7; // End Lcd module connections // OneWire pinout var OW_Bit_Write : sbit at PORTB.B2; OW_Bit_Read : sbit at PINB.B2; OW_Bit_Direction : sbit at DDRB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries temp_fraction temp_fraction temp_fraction := word(temp2write shl (4-RES_SHIFT)); := temp_fraction and 0x000F; := temp_fraction * 625; // convert temp_fraction to characters text[4] := word(temp_fraction div 1000) + 48; Extract thousands digit text[5] := word((temp_fraction div 100)mod 10 + 48); Extract hundreds digit text[6] := word((temp_fraction div 10)mod 10 + 48); Extract tens digit text[7] := word(temp_fraction mod 10) + 48; Extract ones digit // // // //
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Display_Temperature(temp); Delay_ms(520); end; end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Port Expander Library The mikroPascal PRO for AVR provides a library for communication with the Microchip’s Port Expander MCP23S17 via SPI interface. Connections of the AVR compliant MCU and MCP23S17 is given on the schematic at the bottom of this page. Note: Library uses the SPI module for communication. The user must initialize SPI module before using the Port Expander Library.
CHAPTER 6 mikroPASCAL PRO for AVR - Libraries Expander_Set_DirectionPortA Expander_Set_DirectionPortB Expander_Set_DirectionPortAB Expander_Set_PullUpsPortA Expander_Set_PullUpsPortB Expander_Set_PullUpsPortAB Expander_Init Prototype procedure Expander_Init(ModuleAddress : byte); Returns Nothing. Initializes Port Expander using SPI communication.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Read_Byte Prototype function Expander_Read_Byte(ModuleAddress : byte; RegAddress : byte) : byte; Returns Byte read. The function reads byte from Port Expander. Parameters : Description - ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page - RegAddress: Port Expander's internal register address Requires Port Expander must be initialized. See Expander_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Read_PortA Prototype function Expander_Read_PortA(ModuleAddress: byte): byte; Returns Byte read. The function reads byte from Port Expander's PortA. Description Parameters : - ModuleAddress: Port Expander hardware address, see schematic at the bot tom of this page Port Expander must be initialized. See Expander_Init. Requires Example Port Expander's PortA should be configured as input.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Read_PortAB Prototype function Expander_Read_PortAB(ModuleAddress: byte): word; Returns Word read. The function reads word from Port Expander's ports. PortA readings are in the higher byte of the result. PortB readings are in the lower byte of the result. Description Parameters : - ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page Port Expander must be initialized. See Expander_Init.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Write_PortB Prototype procedure Expander_Write_PortB(ModuleAddress: byte; Data_: byte); Returns Nothing. The function writes byte to Port Expander's PortB. Parameters : Description - ModuleAddress: Port Expander hardware address, see schematic at the bot tom of this page - Data_: data to be written Port Expander must be initialized. See Expander_Init. Requires Port Expander's PortB should be configured as output.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Write_PortAB Prototype procedure Expander_Write_PortAB(ModuleAddress: byte; Data_: word); Returns Nothing. The function writes word to Port Expander's ports. Parameters : Description - ModuleAddress: Port Expander hardware address, see schematic at the bot tom of this page - Data_: data to be written. Data to be written to PortA are passed in Data's higher byte.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Set_DirectionPortB Prototype procedure Expander_Set_DirectionPortB(ModuleAddress: byte; Data_: byte); Returns Nothing. The function sets Port Expander's PortB direction. Parameters : Description - ModuleAddress: Port Expander hardware address, see schematic at the bot tom of this page - Data_: data to be written to the PortB direction register. Each bit corresponds to the appropriate pin of the PortB register.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Set_PullUpsPortA Prototype procedure Expander_Set_PullUpsPortA(ModuleAddress: byte; Data_: byte); Returns Nothing. The function sets Port Expander's PortA pull up/down resistors. Parameters : Description - ModuleAddress: Port Expander hardware address, see schematic at the bot tom of this page - Data_: data for choosing pull up/down resistors configuration. Each bit corre sponds to the appropriate pin of the PortA register.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Expander_Set_PullUpsPortAB Prototype procedure Expander_Set_PullUpsPortAB(ModuleAddress: byte; PullUps: word); Returns Nothing. The function sets Port Expander's PortA and PortB pull up/down resistors. Parameters : Description - ModuleAddress: Port Expander hardware address, see schematic at the bot tom of this page - PullUps: data for choosing pull up/down resistors configuration.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries // // If Port Expander Library uses SPI2 module // SPI2_Init(); // Initialize SPI module used with PortExpander // Spi_Rd_Ptr := @SPI2_Read; // Pass pointer to SPI Read function of used SPI module Expander_Init(0); // Initialize Port Expander Expander_Set_DirectionPortA(0, 0x00); be output // Set Expander's PORTA to Expander_Set_DirectionPortB(0,0xFF); be input Expander_Set_PullUpsPortB(0,0xFF); the Expander's PORTB pins // Set Expander's PORTB to while (
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection Port Expander HW connection MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 313
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PS/2 LIBRARY The mikroPascal PRO for AVR provides a library for communication with the common PS/2 keyboard. Note: The library does not utilize interrupts for data retrieval, and requires the oscillator clock to be at least 6MHz. Note: The pins to which a PS/2 keyboard is attached should be connected to the pull-up resistors. Note: Although PS/2 is a two-way communication bus, this library does not provide MCU-to-keyboard communication; e.g.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Ps2_Config Prototype procedure Ps2_Config(); Returns Nothing. Description Initializes the MCU for work with the PS/2 keyboard. Global variables : Requires - PS2_Data: Data signal line PS2_In_Clock: Clock signal line in PS2_Out_Clock: Clock signal line out PS2_Data_Direction: Direction of the Data pin PS2_Clock_Direction: Direction of the Clock pin must be defined before using this function. Example // PS2 pinout definition var PS2_Data : sbit at PINC.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Ps2_Key_Read Prototype function Ps2_Key_Read(var value: byte; var special: byte; var pressed: byte): byte; Returns - 1 if reading of a key from the keyboard was successful - 0 if no key was pressed The function retrieves information on key pressed. Parameters : Description 316 - value: holds the value of the key pressed. For characters, numerals, punctua tion marks, and space value will store the appropriate ASCII code.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Special Function Keys Key Value returned Scroll Lock 28 F1 1 Num Lock 29 F2 2 Left Arrow 30 F3 3 Right Arrow 31 F4 4 Up Arrow 32 F5 5 Down Arrow 33 F6 6 Escape 34 F7 7 Tab 35 F8 8 F9 9 F10 10 F11 11 F12 12 Enter 13 Page Up 14 Page Down 15 Backspace 16 Insert 17 Delete 18 Windows 19 Ctrl 20 Shift 21 Alt 22 Print Screen 23 Pause 24 Caps Lock 25 End 26 Home 27 MIKROELEKTRONIKA - SOFTWARE AND HA
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example This simple example reads values of the pressed keys on the PS/2 keyboard and sends them via UART. program PS2_Example; var keydata, special, down : byte; var PS2_Data PS2_Clock_Input PS2_Clock_Output : sbit at PINC.B0; : sbit at PINC.B1; : sbit at PORTC.B1; PS2_Data_Direction : sbit at DDRC.B0; PS2_Clock_Direction : sbit at DDRC.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection Example of PS2 keyboard connection MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 319
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM LIBRARY CMO module is available with a number of AVR MCUs. mikroPascal PRO for AVR provides library which simplifies using PWM HW Module. Note: For better understanding of PWM module it would be best to start with the example provided in Examples folder of our mikroPascal PRO for AVR compiler. When you select a MCU, mikroPascal PRO for AVR automatically loads the correct PWM library (or libraries), which can be verified by looking at the Library Manager.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries _PWM_PRESCALER_64 Sets prescaler value to 64. _PWM_PRESCALER_128 Sets prescaler value to 128 (this value is not available on every MCU. Please use Code Assistant to see if this value is available for the given MCU. _PWM_PRESCALER_256 Sets prescaler value to 256. _PWM_PRESCALER_1024 Sets prescaler value to 1024. _PWM1_PRESCALER_1 Sets prescaler value to 1 on second PWM library (if it exists in Library Manager).
CHAPTER 6 Libraries mikroPASCAL PRO for AVR Note: Not all of the MCUs have both PWM and PWM1 library included. Sometimes, like its the case with ATmega8515, MCU has only PWM library. Therefore constants that have in their name PWM1 are invalid (for ATmega8515) and will not be visible from Code Assistant. It is highly advisable to use this feature, since it handles all the constants (available) and eliminates any chance of typing error.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM_Init Prototype procedure PWM_Init(wave_mode : byte; prescaler : byte; inverted : byte; duty : byte); Returns Nothing. Initializes the PWM module. Parameter wave_mode is a desired PWM mode. There are two modes: Phase Correct and Fast PWM. Parameter prescaler chooses prescale value N = 1,8,64,256 or 1024 (some modules support 32 and 128, but for this you will need to check the datasheet for the desired MCU).
CHAPTER 6 mikroPASCAL PRO for AVR Libraries The N variable represents the prescaler factor (1, 8, 64, 256, or 1024). Some modules also support 32 and 128 prescaler value, but for this you will need to Description check the datasheet for the desired MCU) PWM_Init must be called before using other functions from PWM Library. You need a CMO on the given MCU (that supports PWM). Requires Before calling this routine you must set the output pin for the PWM (according to the datasheet): DDRB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM_Stop Prototype procedure PWM_Stop(); Returns Nothing. Description Stops the PWM. Requires MCU must have CMO module to use this library. PWM_Init and PWM_Start must be called before using this routine using this routine, otherwise it will have no effect as the PWM module is not running. Example PWM_Stop(); Note: Not all the AVR MCUs support both PWM and PWM1 library. The best way to verify this is by checking the datasheet for the desired MCU.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Description The N variable represents the prescaler factor (1, 8, 64, 256, or 1024). Some modules also support 32 and 128 prescaler value, but for this you will need to check the datasheet for the desired MCU) PWM1_Init must be called before using other functions from PWM Library. You need a CMO on the given MCU (that supports PWM). Requires Before calling this routine you must set the output pin for the PWM (according to the datasheet): DDRB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM1_Set_Duty Prototype procedure PWM1_Set_Duty(duty : byte); Returns Nothing. Changes PWM duty ratio. Parameter duty takes values from 0 to 255, where 0 Description is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100. Requires PWM module must to be initialised (PWM1_Init) before using PWM_Set_Duty function.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The example changes PWM duty ratio on PB3 and PB7 pins continually. If LED is connected to PB3 and PB7, you can observe the gradual change of emitted light. program PWM_Test; var current_duty : byte; current_duty1 : byte; begin DDRB.B0 := 0; DDRB.B1 := 0; // Set PORTB pin 0 as input // Set PORTB pin 1 as input DDRC.B0 := 0; DDRC.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries else if (PINC.0 <> 0) then // Detect if PORTC pin 0 is pressed begin Delay_ms(40); // Small delay to avoid deboucing effect Inc(current_duty1); // Increment duty ratio PWM1_Set_Duty(current_duty1); // Set incremented duty end else if (PINC.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM 16 BIT LIBRARY CMO module is available with a number of AVR MCUs. mikroPascal PRO for AVR provides library which simplifies using PWM HW Module. Note: For better understanding of PWM module it would be best to start with the example provided in Examples folder of our mikroPascal PRO for AVR compiler. When you select a MCU, mikroPascal PRO for AVR automaticaly loads the correct PWM-16bit library, which can be verified by looking at the Library Manager.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries _TIMER1 Selects the Timer/Counter1 (used with PWM16bit_Start and PWM16bit_Stop. _TIMER3 Selects the Timer/Counter3 (used with PWM16bit_Start and PWM16bit_Stop. _TIMER1_CH_A Selects the channel A on Timer/Counter1 (used with PWM16bit_Change_Duty). _TIMER1_CH_B Selects the channel B on Timer/Counter1 (used with PWM16bit_Change_Duty). _TIMER1_CH_C Selects the channel C on Timer/Counter1 (used with PWM16bit_Change_Duty).
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM16bit_Init Prototype procedure PWM16bit_Init(wave_mode : byte; prescaler : byte; inverted : byte; duty : word; timer : byte); Returns Nothing. Initializes the PWM module. Parameter wave_mode is a desired PWM-16bit mode.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Description The N variable represents the prescaler factor (1, 8, 64, 256, or 1024). PWM16bit_Init must be called before using other functions from PWM Library. You need a CMO on the given MCU (that supports PWM-16bit). Requires Before calling this routine you must set the output pin for the PWM (according to the datasheet): DDRB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM16bit_Change_Duty Prototype procedure PWM16bit_Change_Duty(duty : word; channel : byte); Returns Nothing. Changes PWM duty ratio. Parameter duty takes values shown on the table below. Where 0 is 0%, and TOP value is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*TOP)/100.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries PWM16bit_Stop Prototype procedure PWM16_Stop(timer : byte); Returns Nothing. Description Stops the PWM-16bit module, connected to Timer/Counter set in this stop function. Requires MCU must have CMO module to use this library. Like in PWM16bit_Start before, PWM16bit_Init must be called before using this routine , otherwise it will have no effect as the PWM module is not running.
CHAPTER 6 Libraries mikroPASCAL PRO for AVR Delay_ms(40); // Small delay to avoid deboucing effect Inc(current_duty); // Increment duty ratio PWM16bit_Set_Duty(current_duty); // Set incremented duty end else if (PINC.B1 <> 0) then // Detect if PORTC pin 1 is pressed begin Delay_ms(40); // Small delay to avoid deboucing effect Dec(current_duty); // Decrement duty ratio PWM16bit_Set_Duty(current_duty); // Set decremented duty ratio end else if (PINC.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection PWM demonstration MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 337
CHAPTER 6 mikroPASCAL PRO for AVR Libraries RS-485 LIBRARY RS-485 is a multipoint communication which allows multiple devices to be connected to a single bus. The mikroPascal PRO for AVR provides a set of library routines for comfortable work with RS485 system using Master/Slave architecture. Master and Slave devices interchange packets of information. Each of these packets contains synchronization bytes, CRC byte, address byte and the data.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries External dependencies of RS-485 Library The following variables must be defined in all projects using RS-485 Library: Description: Example : var RS485_rxtx_pin : sbit; sfr; external; Control RS-485 Transmit/Receive operation mode var RS485_rxtx_pin : sbit at PORTD.B2; var RS485_rxtx_pin_direction : sbit; sfr; external; Direction of the RS-485 Transmit/Receive pin var RS485_rxtx_pin_direction : sbit at DDRD.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries RS485Master_Init Prototype procedure RS485Master_Init(); Returns Nothing. Description Initializes MCU as a Master for RS-485 communication. Global variables : Requires - RS485_rxtx_pin - this pin is connected to RE/DE input of RS-485 transceiver(see schematic at the bottom of this page). RE/DE signal controls RS-485 transceiver operation mode.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries RS485Master_Receive Prototype procedure RS485Master_Receive(var data_buffer: array[5] of byte); Returns Nothing. Receives messages from Slaves. Messages are multi-byte, so this routine must be called for each byte received. Parameters : Description - data_buffer: 7 byte buffer for storing received data, in the following manner: data[0..
CHAPTER 6 mikroPASCAL PRO for AVR Libraries RS485Master_Send Prototype procedure RS485Master_Send(var data_buffer: array[20] of byte; datalen: byte; slave_address: byte); Returns Nothing. Sends message to Slave(s). Message format can be found at the bottom of this page. Description Parameters : - data_buffer: data to be sent - datalen: number of bytes for transmition. Valid values: 0...3. - slave_address: Slave(s) address MCU must be initialized as a Master for RS-485 communication.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries RS485Slave_Init Prototype procedure RS485Slave_Init(slave_address: byte); Returns Nothing. Initializes MCU as a Slave for RS-485 communication. Description Parameters : - slave_address: Slave address Global variables : Requires - RS485_rxtx_pin - this pin is connected to RE/DE input of RS-485 transceiver(see schematic at the bottom of this page). RE/DE signal controls -- RS-485 transceiver operation mode.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries RS485slave_Receive Prototype procedure RS485Slave_Receive(var data_buffer: array[20] of byte); Returns Nothing. Receives messages from Master. If Slave address and Message address field don't match then the message will be discarded. Messages are multi-byte, so this routine must be called for each byte received. Parameters : Description - data_buffer: 6 byte buffer for storing received data, in the following manner: data[0..
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example This is a simple demonstration of RS485 Library routines usage. Master sends message to Slave with address 160 and waits for a response. The Slave accepts data, increments it and sends it back to the Master. Master then does the same and sends incremented data back to Slave, etc. Master displays received data on PORTB, while error on receive (0xAA) and number of consecutive unsuccessful retries are displayed on PORTC.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries UART_TX_Idle_Ptr := @UART1_TX_Idle; UART1_Init(9600); Delay_ms(100); // initialize UART1 module RS485Master_Init(); dat[0] := 0xAA; dat[1] := 0xF0; dat[2] := 0x0F; dat[4] := 0; dat[5] := 0; dat[6] := 0; // initialize MCU as Master // ensure that message received flag is 0 // ensure that error flag is 0 RS485Master_Send(dat,1,160); SREG_I RXCIE := 1; := 1; // enable global interrupt // enable interrupt on UART receive while (TRUE) do begin // // upon comp
CHAPTER 6 mikroPASCAL PRO for AVR Libraries RS485 Slave code: program RS485_Slave_Example; uses __Lib_RS485; var dat : array[20] of byte; i, j : byte; // buffer for receving/sending messages var rs485_rxtx_pin : sbit at PORTD.B2; // set transcieve pin rs485_rxtx_pin_direction : sbit at DDRD.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries dat[5] := 0; end; if (dat[4] <> 0) then begin dat[4] := 0; j := dat[3]; // upon completed valid message receive // for i := 1 to dat[3] do PORTB := dat[i-1]; dat[0] := dat[0]+1; Delay_ms(1); RS485Slave_Send(dat,1); end; data[4] is set to 0xFF // show data on PORTB // increment received dat[0] // and send it back to master end; end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection Example of interfacing PC to AVR MCU via RS485 bus with LTC485 as RS-485 transceiver MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 349
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Message format and CRC calculations Q: How is CRC checksum calculated on RS485 master side? START_BYTE := 0x96; // 10010110 STOP_BYTE := 0xA9; // 10101001 PACKAGE: -------START_BYTE 0x96 ADDRESS DATALEN [DATA1] [DATA2] [DATA3] CRC STOP_BYTE 0xA9 // if exists // if exists // if exists DATALEN bits -----------bit7 := 1 MASTER SENDS 0 SLAVE SENDS bit6 := 1 ADDRESS WAS XORed with 1, IT WAS EQUAL STOP_BYTE 0 ADDRESS UNCHANGED bit5 := 0 FIXED bit4 := 1 DATA3 (if exi
CHAPTER 6 mikroPASCAL PRO for AVR Libraries SOFTWARE I²C LIBRARY The mikroPascal PRO for AVR provides routines for implementing Software I2C communication. These routines are hardware independent and can be used with any MCU. The Software I2C library enables you to use MCU as Master in I2C communication. Multi-master mode is not supported. Note: This library implements time-based activities, so interrupts need to be disabled when using Software I2C.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines - Soft_I2C_Init Soft_I2C_Start Soft_I2C_Read Soft_I2C_Write Soft_I2C_Stop Soft_I2C_Break Soft_I2C_Init Prototype procedure Soft_I2C_Init(); Returns Nothing. Description Configures the software I2C module.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_I2C_Start Prototype procedure Soft_I2C_Start(); Returns Nothing. Description Determines if the I2C bus is free and issues START signal. Requires Software I2C must be configured before using this function. See Soft_I2C_Init routine. Example // Issue START signal Soft_I2C_Start(); Soft_I2C_Read Prototype function Soft_I2C_Read(ack: word): byte; Returns One byte from the Slave. Reads one byte from the slave.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_I2C_Write Prototype function Soft_I2C_Write(_data: byte): byte; Returns - 0 if there were no errors. - 1 if write collision was detected on the I2C bus. Sends data byte via the I2C bus. Description Parameters : - _Data: data to be sent Soft I2C must be configured before using this function. See Soft_I2C_Init routine. Requires Example Also, START signal needs to be issued in order to use this function. See Soft_I2C_Start routine.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_I2C_Break Prototype procedure Soft_I2C_Break(); Returns Nothing. All Software I2C Library functions can block the program flow (see note at the top of this page). Call this routine from interrupt to unblock the program execuDescription tion. This mechanism is similar to WDT. Note: Interrupts should be disabled before using Software I2C routins again (see note at the top of this page). Requires Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The example demonstrates Software I2C Library routines usage. The AVR MCU is connected (SCL, SDA pins) to PCF8583 RTC (real-time clock). Program reads date and time are read from the RTC and prints it on Lcd.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries month := Soft_I2C_Read(0); Soft_I2C_Stop(); end; // Read weekday/month byte} // Issue stop signal} //-------------------- Formats date and time procedure Transform_Time() ; begin seconds := ((seconds and 0xF0) shr 4)*10 + // Transform seconds minutes := ((minutes and 0xF0) shr 4)*10 + // Transform months hours := ((hours and 0xF0) shr 4)*10 // Transform hours year := (day and 0xC0) shr 6; day := ((day and 0x30) shr 4)*10 // Transform day month := ((month and 0
CHAPTER 6 mikroPASCAL PRO for AVR Libraries LCD_Chr(1,11,':'); LCD_Out(2,1,'Time:'); LCD_Chr(2,8,':'); LCD_Chr(2,11,':'); LCD_Out(1,12,'200'); end; //----------------- Main procedure begin Init_Main(); // Perform initialization while TRUE do begin Read_Time(); Transform_Time(); Display_Time(); Delay_ms(1000); end; end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries SOFTWARE SPI LIBRARY The mikroPacal PRO for AVR provides routines for implementing Software SPI communication. These routines are hardware independent and can be used with any MCU. The Software SPI Library provides easy communication with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc. Library configuration: - SPI to Master mode Clock value = 20 kHz. Data sampled at the middle of interval. Clock idle state low.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines - Soft_SPI_Init - Soft_SPI_Read - Soft_SPI_Write Soft_SPI_Init Prototype procedure Soft_SPI_Init(); Returns Nothing. Description Configures and initializes the software SPI module.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_SPI_Read Prototype function Soft_SPI_Read(sdata: byte): word; Returns Byte received via the SPI bus. This routine performs 3 operations simultaneously. It provides clock for the Software SPI bus, reads a byte and sends a byte. Description Parameters : - sdata: data to be sent. Requires Soft SPI must be initialized before using this function. See Soft_SPI_Init routine. Example var data_read : word; data_send : byte; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example This code demonstrates using library routines for Soft_SPI communication. Also, this example demonstrates working with Microchip's MCP4921 12-bit D/A converter. program Soft_SPI; // DAC module connections var Chip_Select : sbit at SoftSpi_CLK : sbit at SoftSpi_SDI : sbit at SoftSpi_SDO : sbit at PORTB.B0; PORTB.B7; PINB.B6; PORTB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries begin InitMain(); // Perform main initialization value := 2048; // while (TRUE) do begin // When program starts, DAC gives the output in the mid-range // Endless loop if ((PINA.B0) and (value < 4095)) then is pressed Inc(value) // else begin if ((PINA.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries SOFTWARE UART LIBRARY The mikroPascal PRO for AVR provides routines for implementing Software UART communication. These routines are hardware independent and can be used with any MCU. The Software UART Library provides easy communication with other devices via the RS232 protocol. Note: The Software UART library implements time-based activities, so interrupts need to be disabled when using it.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_UART_Init Prototype function Soft_UART_Init(baud_rate: dword; inverted: byte): byte; Returns - 2 - error, requested baud rate is too low - 1 - error, requested baud rate is too high - 0 - successfull initialization Configures and initializes the software UART module. Parameters : - baud_rate: baud rate to be set. Maximum baud rate depends on the MCU’s clock and working conditions. - inverted: inverted output flag.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_UART_Read Prototype function Soft_UART_Read(var error: byte): byte; Returns Byte received via UART. The function receives a byte via software UART. This is a blocking function call (waits for start bit). Programmer can unblock it by calling Soft_UART_Break routine. Description Parameters : - error: Error flag. Error code is returned through this variable.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_UART_Write Prototype procedure Soft_UART_Write(udata: byte); Returns Nothing. This routine sends one byte via the Software UART bus. Description Parameters : - udata: data to be sent. Software UART must be initialized before using this function. See the Soft_UART_Init routine. Requires Be aware that during transmission, software UART is incapable of receiving data – data transfer protocol must be set in such a way to prevent loss of information.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Soft_UART_Break Prototype procedure Soft_UART_Break(); Returns Nothing. Soft_UART_Read is blocking routine and it can block the program flow. Call this routine from interrupt to unblock the program execution. This mechanism is simDescription ilar to WDT. Note: Interrupts should be disabled before using Software UART routines again (see note at the top of this page). Requires Nothing.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example This example demonstrates simple data exchange via software UART. If MCU is connected to the PC, you can test the example from the mikroPascal PRO for AVR USART Terminal Tool. program Soft_UART; // Soft UART connections var Soft_UART_Rx_Pin : sbit at Soft_UART_Tx_Pin : sbit at Soft_UART_Rx_Pin_Direction Soft_UART_Tx_Pin_Direction // End Soft UART connections var error : byte; counter, byte_read : byte; PIND.B0; PORTD.B1; : sbit at DDRD.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries SOUND LIBRARY The mikroPascal PRO for AVR provides a Sound Library to supply users with routines necessary for sound signalization in their applications. Sound generation needs additional hardware, such as piezo-speaker (example of piezo-speaker interface is given on the schematic at the bottom of this page).
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Sound_Play Prototype procedure Sound_Play(freq_in_Hz: word; duration_ms: word); Returns Nothing. Generates the square wave signal on the appropriate pin. Description Parameters : - freq_in_Hz: signal frequency in Hertz (Hz) - duration_ms: signal duration in miliseconds (ms) Requires In order to hear the sound, you need a piezo speaker (or other hardware) on designated port.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Tone1(); Tone1(); Tone1(); Tone3(); end; Tone2(); Tone2(); Tone2(); Tone3(); Tone3(); Tone3(); Tone3(); Tone3(); Tone2(); Tone2(); Tone1(); procedure ToneA(); begin Sound_Play(1250, 20); end; // Tones used in Melody2 function procedure ToneC(); begin Sound_Play(1450, 20); end; procedure ToneE(); begin Sound_Play(1650, 80); end; procedure Melody2(); var counter : byte; begin for counter := 9 downto 1 do begin ToneA(); ToneC(); ToneE(); end; end; // Plays Mel
CHAPTER 6 mikroPASCAL PRO for AVR Libraries while (PINB.B6 <> 0) do nop; // Wait for but- ton to be released end; if (PINB.B5 <> 0) then // If PORTB.5 is pressed play Tone3 begin Tone3(); while (PINB.B5 <> 0) do nop; // Wait for button to be released end; if (PINB.B4 <> 0) then // If PORTB.4 is pressed play Melody2 begin Melody2(); while (PINB.B4 <> 0) do nop; // Wait for button to be released end; if (PINB.B3 <> 0) then // If PORTB.3 is pressed play Melody begin Melody(); while (PINB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries SPI LIBRARY mikroPascal PRO for AVR provides a library for comfortable with SPI work in Master mode. The AVR MCU can easily communicate with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc. Note: Some AVR MCU's have alternative SPI ports, which SPI signals can be redirected to by setting or clearing SPIPS (SPI Pin Select) bit of the MCUCR register. Please consult the appropriate datasheet.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries SPI1_Init_Advanced Prototype procedure SPI1_Init_Advanced(mode : byte; fcy_div : byte; and_edge : byte) Returns Nothing. Configures and initializes SPI. SPI1_Init_Advanced or SPI1_Init needs to be called before using other functions of SPI Library.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Requires MCU must have SPI module. Example // Set SPI to the Master Mode, clock = Fosc/32 , clock idle level is high, data sampled on falling edge: SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV32, _SPI_CLK_HI_TRAILING); SPI1_Read Prototype function SPI1_Read(buffer: byte): byte; Returns Received data. Reads one byte from the SPI bus.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Example The code demonstrates how to use SPI library functions for communication between SPI module of the MCU and Microchip's MCP4921 12-bit D/A converter program SPI; // DAC module connections var Chip_Select : sbit at PORTB.B0; Chip_Select_Direction : sbit at DDRB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries // the output in the mid-range while ( TRUE ) do begin // Endless loop if ((PINA.B0) and (value < 4095)) then // If PA0 button is pressed Inc(value) // increment value else begin if ((PINA.B1) and (value > 0)) then // If PA1 button is pressed Dec(value); // decrement value end; DAC_Output(value); Delay_ms(1); end; // Send value to DAC chip // Slow down key repeat pace end.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries HW Connection SPI HW connection MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD 379
CHAPTER 6 mikroPASCAL PRO for AVR Libraries SPI ETHERNET LIBRARY The ENC28J60 is a stand-alone Ethernet controller with an industry standard Serial Peripheral Interface (SPI™). It is designed to serve as an Ethernet network interface for any controller equipped with SPI. The ENC28J60 meets all of the IEEE 802.3 specifications. It incorporates a number of packet filtering schemes to limit incoming packets.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries External dependencies of SPI Ethernet Library The following variables must be defined in all projects using SPI Ethernet Library: Description: Example : var SPI_Ethernet_CS : var SPI_Ethernet_CS : ENC28J60 chip select pin. sbit at PORTB.B4; sbit; sfr; external; var SPI_Ethernet_RST : ENC28J60 reset pin. sbit; sfr; external; var SPI_Ethernet_RST : sbit at PORTB.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Library Routines - Spi_Ethernet_Init Spi_Ethernet_Enable Spi_Ethernet_Disable Spi_Ethernet_doPacket Spi_Ethernet_putByte Spi_Ethernet_putBytes Spi_Ethernet_putString Spi_Ethernet_putConstString Spi_Ethernet_putConstBytes Spi_Ethernet_getByte Spi_Ethernet_getBytes Spi_Ethernet_UserTCP Spi_Ethernet_UserUDP Spi_Ethernet_Init Prototype procedure Spi_Ethernet_Init(mac: ^byte; ip: ^byte; fullDuplex:byte); Returns Nothing. This is MAC module routine.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Parameters: Description Requires - mac: RAM buffer containing valid MAC address. - ip: RAM buffer containing valid IP address. - fullDuplex: ethernet duplex mode switch. Valid values: 0 (half duplex mode) and 1 (full duplex mode). The appropriate hardware SPI module must be previously initialized.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Parameters: - enFlt: network traffic/receive filter flags. Each bit corresponds to the appropriate network traffic/receive filter: Bit Mask 0 0x01 MAC Broadcast traffic/receive Spi_Ethernet_BROADCAST filter flag. When set, MAC broadcast traffic will be enabled. 1 0x02 MAC Multicast traffic/receive filSpi_Ethernet_MULTICAST ter flag. When set, MAC multicast traffic will be enabled.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Spi_Ethernet_Disable Prototype procedure Spi_Ethernet_Disable(disFlt: byte); Returns Nothing. This is MAC module routine. This routine disables appropriate network traffic on the ENC28J60 module by the means of it's receive filters (unicast, multicast, broadcast, crc). Specific type of network traffic will be disabled if a corresponding bit of this routine's input parameter is set.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Requires Ethernet module has to be initialized. See Spi_Ethernet_Init. Example Spi_Ethernet_Disable(Spi_Ethernet_CRC or Spi_Ethernet_UNICAST); // disable CRC checking and Unicast traffic Spi_Ethernet_doPacket Prototype function Spi_Ethernet_doPacket(): byte; Returns - 0 - upon successful packet processing (zero packets received or received packet processed successfully). - 1 - upon reception error or receive buffer corruption.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Spi_Ethernet_putByte Prototype procedure Spi_Ethernet_putByte(v: byte); Returns Nothing. This is MAC module routine. It stores one byte to address pointed by the current ENC28J60 write pointer (EWRPT). Description Parameters: - v: value to store Requires Ethernet module has to be initialized. See Spi_Ethernet_Init. Example var data as byte; ...
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Spi_Ethernet_putConstBytes Prototype procedure Spi_Ethernet_putConstBytes(const ptr : ^byte; n : byte); Returns Nothing. This is MAC module routine. It stores requested number of const bytes into ENC28J60 RAM starting from current ENC28J60 write pointer (EWRPT) location. Description Parameters: - ptr: const buffer containing bytes to be written into ENC28J60 RAM. - n: number of bytes to be written. Requires Ethernet module has to be initialized.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Spi_Ethernet_putConstString Prototype function Spi_Ethernet_putConstString(const ptr : ^byte): word; Returns Number of bytes written into ENC28J60 RAM. This is MAC module routine. It stores whole const string (excluding null termination) into ENC28J60 RAM starting from current ENC28J60 write pointer (EWRPT) location. Description Parameters: - ptr: const string to be written into ENC28J60 RAM. Requires Ethernet module has to be initialized.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Spi_Ethernet_getBytes Prototype procedure Spi_Ethernet_getBytes(ptr : ^byte; addr : word; n : byte); Returns Nothing. This is MAC module routine. It fetches equested number of bytes from ENC28J60 RAM starting from given address. If value of 0xFFFF is passed as the address parameter, the reading will start from current ENC28J60 read pointer (ERDPT) location. Description Parameters: - ptr: buffer for storing bytes read from ENC28J60 RAM.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Requires Ethernet module has to be initialized. See Spi_Ethernet_Init. Example This function is internally called by the library and should not be called by the user's code. Spi_Ethernet_UserUDP Prototype function Spi_Ethernet_UserUDP(remoteHost : ^byte; remotePort : word; destPort : word; reqLength : word) : word; Returns - 0 - there should not be a reply to the request. - Length of UDP reply data field - otherwise. This is UDP module routine.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries Main program code : program enc_ethernet; uses eth_enc28j60_utils ; //this is where you should write implementation for UDP and HTTP {*********************************** * RAM variables *} var myMacAddr : array[6] of byte ; // my MAC address myIpAddr : array[4] of byte ; // my IP address gwIpAddr : array[4] of byte ; // gateway (router) IP address ipMask : array[4] of byte ; // network mask (for example : 255.255.255.
CHAPTER 6 mikroPASCAL PRO for AVR Libraries ipMask[1] := 255; ipMask[2] := 255; ipMask[3] := 0; dnsIpAddr[0] dnsIpAddr[1] dnsIpAddr[2] dnsIpAddr[3] := := := := 192; 168; 20; 1; {* * starts ENC28J60 with : * reset bit on PORTB.B4 * CS bit on PORTB.
CHAPTER 6 Libraries mikroPASCAL PRO for AVR const httpMimeTypeHTML : string[13] = 'text/html'+#10+#10 ; // HTML MIME type const httpMimeTypeScript : string[14] = 'text/plain'+#10+#10 ; // TEXT MIME type const httpMethod : string[5] = 'GET /'; {* * web page, splited into 2 parts : * when coming short of ROM, fragmented data is handled more efficiently by linker * * this HTML page calls the boards to get its status, and builds itself with javascript *} const indexPage : string[513] = 'PAGE 421
CHAPTER 6 mikroPASCAL PRO for AVR 'This is HTTP #