BAHÇEŞEHİR UNIVERSITY
Upcoming SlideShare
Loading in...5
×
 

BAHÇEŞEHİR UNIVERSITY

on

  • 1,333 views

 

Statistics

Views

Total Views
1,333
Views on SlideShare
1,333
Embed Views
0

Actions

Likes
0
Downloads
14
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

BAHÇEŞEHİR UNIVERSITY BAHÇEŞEHİR UNIVERSITY Document Transcript

  • BAHÇEŞEHİR UNIVERSITY INTERNSHIP TUTORIAL Programming PIC MCUs with PIC BASIC PRO Compiler PIC Microcontrollers Currently, the best choice for application development, using PIC BASIC PRO are microcontrollers from the family : PIC16F87x, PIC16F62X and of course the famous PIC16F84. With this family of PIC microcontrollers, program memory is created using FLASH technology which provides fast erasing and reprogramming, thus allowing faster debugging. By a single mouse click in the programming software, microcontroller program can be instantly erased and then reloaded without removing chip from device. Also, program loaded in FLASH memory can be stored after power supply has been turned off. The older PIC microcontroller series (12C67x, 14C000, 16C55x, 16C6xx, 16C7xx and 16C92x) have program memory created using EPROM/ROM technology, so they can either be programmed only once (OTP version with ROM memory) or have glass window (JW version with EPROM memory), which allows erasing by few minutes exposure to UV light. OTP versions are usually cheaper and are used for manufacturing large series of products. Besides FLASH memory, microcontrollers of PIC16F87x and PIC16F84 series also contain 64-256 bytes of internal EEPROM memory, which can be used for storing program data and other parameters when power is off. PIC BASIC PRO has built-in READ and WRITE instructions that can be used for loading and saving data to EEPROM. Brief Introduction to PIC16F84 MCU The PIC 16F84 microcontroller is shown in Figure 1 It is a versatile microcontroller with flash memory. Flash memory is the terminology used to describe “rewriteable” memory. The 1K x 14bit onboard flash memory can endure a minimum of 1000 erase/write cycles. Figure 1: PIC16F84 Pin Diagram General features: RISC CPU 35 singleword instructions; operating speed dc, 10MHz clock input; 1K program memory; 14bitwide instructions; 8bitwide data path; direct, indirect, and relative addressing; 1000 erase/write cycles. Peripheral features: 13 I/O pins with individual direction control; highcurrent sink/source for direct LED drive (25mA sink max. per pin, 20mA source max. per pin); TMRO—8bit timer/counter with 8bit programmable prescaler.
  • So you can reprogram and reuse the PIC chip at least 1000 times. The program retention time between erase/write cycles is approximately 40 years. The 18pin chip devotes 13 pins to I/O. Each pin may be programmed individually for input or output. The pin status (I/O direction control) may be changed on the fly via programming. Other features include power on reset, power saving sleep mode, power up timer, and code protection. Additional features and architectural details of the PIC 16F84 will be given as we continue. Figure 2: PIC16F84 Features RA0-RA3 and RB0-RB7 pins are I/O (input/output) ports. You can access MCU by sending digital signals to these ports and MCU process that commands then it sends digital signals from I/O ports. PIC 16F84 is produced with CMOS Technology (complementary metal-oxide-semiconductor). CMOS chips include microprocessor, microcontroller, static RAM, and other digital logic circuits. The central characteristic of the technology is that it only uses significant power when its transistors are switching between on and off states. Consequently, CMOS devices use little power and do not produce as much heat as other forms of logic. CMOS also allows a high density of logic functions on a chip. Pin Descriptions PIC16F84 has a 13 I/O ports. 5 of the I/O ports are RA0 to RA5 and 8 of them are RB0 to RB7. You can use these 13 ports input or output. It has a special TRIS register is used for ports I/O directions. You can find pin descriptions in Figure 3.
  • Figure 3: PIC16F84 Pin Descriptions Memory Organization There are two memory blocks in the PIC16F8X. These are the program memory and the data memory. Each block has its own bus, so that access to each block can occur during the same oscillator cycle. The data memory can further be broken down into the general purpose RAM and the Special Function Registers (SFRs). The SFRs used to control the peripheral modules. The data memory area also contains the data EEPROM memory. Program Memory Organization PIC16F84 has 1 Kbytes program memory. Memory cells are between 000h and 3FFF. In each of the cell, 14 bit program instructions are stored. Program memory has a Flash technology so it can be erased or written electrically but while program is working it can just read. Program memory in a PIC is actually 14 bits wide even though PICs are considered 8-bit microcontrollers. The 8-bit terminology comes from the data memory buss, which is 8 bits wide.
  • Figure 3: PIC16F84 Program Memory Organization Data Memory Organization Data memory is where all your variables are stored. This is RAM (Random Access Memory), which means when the PIC is disconnected from power all the data memory is lost. The data memory is 8 bits wide, which is why the PICs are considered 8-bit microcontrollers. The PIC data memory is also banked just like the program memory. The first section of data memory is reserved for special function registers. These registers are the key locations that control most of functions within the PIC. Some of the registers are located in Bank 0 while some are located in Bank 1. Some are even repeated so they are available in both banks. To select which bank to control, a bit is set or cleared in the STATUS register. Because you need access to the STATUS register from both banks, it is located in both banks. Figure 4 shows PIC16F84’s data memory organization and register file map.
  • Figure 4: PIC16F84 Register File Map
  • PIC Programming Overview Programming PIC microcontrollers is a simple three step process: Write the code, compile the code, and upload the code into a microcontroller. The PicBasic Pro compiler both function is, saved program code (text file) is run through a PicBasic Pro compiler. The compiler reads through the text file and creates (compiles) an equivalent machine code instruction listing (.hex file) of the program. The machine code (.hex file) is a list of hexadecimal numbers that represent the PicBasic Pro program. The list of hexadecimal numbers (.hex file) is uploaded (programmed) into the microcontroller. When the microcontroller is started, its CPU will run through the programmed list of hexadecimal numbers, running the PicBasic Pro program. Uploading the machine code (.hex file) into the microcontroller is the job of the Programmer board and software. The PicBasic Pro version offers enhanced and richer basic command syntax. The compiler program may be run manually in DOS or in an “MSDOS Prompt” window but I offer Microchip’s MPLAB IDE program in my graduation project I used Pic Basic Pro and MPLAB together. Figure 5: PIC programming overview. The PicBasic Pro Compiler Micro Engineering Lab’s Pic Basic Pro Compiler gives us powerful and flexible features. You can write your PIC code very fast instead of assembly programming. Additionally, that is easy to learn PBP’s commands and properties because it is very similar to basic programming language if you are familiar with high level language programming, especially basic programming you can learn PBP in a short time period. The PicBasic Pro Compiler makes it even quicker and easier for you to program Microchip Technology’s powerful PICmicro microcontrollers (MCUs). The English-like BASIC language is much easier to read and write than the quirky Microchip assembly language. Decisions were made that we hope improve the language overall. One of these was to add a real IF..THEN..ELSE..ENDIF instead of the IF..THEN(GOTO).
  • Here is a list of the current PBPro commands: @: Insert one line of assembly language code. ASM..ENDASM: Insert assembly language code section. ADCIN: Read on-chip analog to digital converter. BRANCH: Computed GOTO (equivalent to ON..GOTO). BRANCHL: BRANCH out of page (long BRANCH). BUTTON: Debounce and auto-repeat input on specified pin. CALL: Call assembly language subroutine. CLEAR: Zero all variables. CLEARWDT: Clear Watchdog Timer. COUNT: Count number of pulses on a pin. DATA: Define initial contents of on-chip EEPROM. DEBUG: Asynchronous serial output to fixed pin and baud. DEBUGIN: Asynchronous serial input from fixed pin and baud. DISABLE: Disable ON INTERRUPT processing. DISABLE DEBUG: Disable ON DEBUG processing. DISABLE INTERRUPT: Disable ON INTERRUPT processing. DTMFOUT: Produce touch-tones on a pin. EEPROM: Define initial contents of on-chip EEPROM. ENABLE: Enable ON INTERRUPT processing. ENABLE DEBUG: Enable ON DEBUG processing. ENABLE INTERRUPT: Enable ON INTERRUPT processing. END: Stop execution and enter low power mode. FOR..NEXT: Repeatedly execute statements. FREQOUT: Produce up to 2 frequencies on a pin. GOSUB: Call BASIC subroutine at specified label. GOTO: Continue execution at specified label. HIGH: Make pin output high. HSERIN: Hardware asynchronous serial input. HSEROUT: Hardware asynchronous serial output. I2CREAD: Read bytes from I2C device. I2CWRITE: Write bytes to I2C device. IF..THEN..ELSE..ENDIF: Conditionally execute statements. INPUT: Make pin an input. {LET}: Assign result of an expression to a variable. LCDIN: Read RAM on LCD. LCDOUT: Display characters on LCD. LOOKDOWN: Search constant table for value. LOOKDOWN2: Search constant/variable table for value. LOOKUP: Fetch constant value from table. LOOKUP2: Fetch constant/variable value from table. LOW: Make pin output low. NAP: Power down processor for short period of time. ON INTERRUPT: Execute BASIC subroutine on an interrupt. OUTPUT: Make pin an output. PAUSE: Delay (1 millisecond (msec) resolution). PAUSEUS: Delay (1 microsecond (_sec) resolution). PEEK: Read byte from register. POKE: Write byte to register. POT: Read potentiometer on specified pin. PULSIN: Measure pulse width on a pin.
  • PULSOUT: Generate pulse to a pin. PWM: Output pulse width modulated pulse train to pin. RANDOM: Generate pseudo-random number. RCTIME: Measure pulse width on a pin. READ: Read byte from on-chip EEPROM. READCODE: Read word from code memory. RESUME: Continue execution after interrupt handling. RETURN: Continue execution at statement following last executed GOSUB. REVERSE: Make output pin an input or an input pin an output. SERIN: Asynchronous serial input (8N1) (BS1 style with timeout.) SERIN2: Asynchronous serial input (BS2 style.) SEROUT: Asynchronous serial output (8N1) (BS1 style.) SEROUT2: Asynchronous serial output (BS2 style.) SHIFTIN: Synchronous serial input. SHIFTOUT: Synchronous serial output. SLEEP: Power down processor for a period of time. SOUND: Generate tone or white noise on specified pin. STOP: Stop program execution. SWAP: Exchange the values of two variables. TOGGLE: Make pin output and toggle state. WHILE..WEND: Execute code while condition is true. WRITE: Write byte to on-chip EEPROM. WRITECODE: Write word to code memory. XIN: X-10 input. XOUT: X-10 output. Note: In PicBasic Pro Compiler Manual, you can find extensive information about PicBasic Pro, Microchips MPLAB IDE What is MPLAB IDE? MPLAB Integrated Development Environment (IDE) is a free, integrated toolset for the development of embedded applications employing Microchip's PICmicro® and dsPIC® microcontrollers. MPLAB IDE runs as a 32-bit application on MS Windows®, is easy to use and includes a host of free software components for fast application development and super-charged debugging. MPLAB IDE also serves as a single, unified graphical user interface for additional Microchip and third party software and hardware development tools. You can use PicBasic Pro compiler with MPLAB IDE.
  • Figure 6: MPLAB user interface Using MPLAB IDE with PicBasic Pro Compiler I will explain to setting up MPLAB and PBP step by step, please follow up these steps respectively. 1) First, Install MPLAB and its components. Please use default directory. In the following examples, the default installation directory (C:Program FilesMicrochip...) will be used. 2) Install the PICBASIC or PICBASIC PRO Compiler. Please just copy and paste PBP folder into your dirve C. Some versions will have problems if installed in C:Program FilesPBP. In the following examples, we assume that PICBASIC PRO has been installed in C:PBP. 3) In "C:PBP", run the file "PBregister.bat" (Start - Run - C:PBPPBregister.bat). Restart MPLAB for the changes to take effect. (Note: In C:PBP folder there are two PBP plug-in, if you run "PBregister.bat", these plug-ins will be activated.) 4) For Windows 2000/XP; The path (C:PBP;C:Program FilesMicrochipMPASM Suite) statement is found in the Environment Variables dialog box. 1- To get there, right-click the My Computer icon on your desktop and select Properties. 2- Click the Advanced tab, then the button labeled Environment Variables....
  • 3- Find the Path variable in the System Variables window, highlight the line, and click Edit. 4- Add the paths to MPLAB and PICBASIC PRO to the end of the line in the Variable Values text box, separating each path with a semicolon. For example path statement is: %SystemRoot%System32;C:PBP;C:Program FilesMicrochipMPASM Suite 5- If necessary, restart your computer after making changes to the path variable. 6- After, all steps just run P245A1.EXE Selecting PICBASIC as the language tool within MPLAB Start MPLAB and select Set Language Tool Locations under the Project menu. Select the appropriate Toolsuite Name (PICBASIC or PICBASIC PRO Compiler). Use the browse button to select PBC.EXE or PBPW.EXE within the subdirectory where it was installed.
  • Creating a BASIC project within MPLAB Run the Project Wizard to create a project in MPLAB. Select the desired processor. Select appropriate microEngineering Labs, Inc. Tool Suite. Name the project. Choose the directory where the source file exists. Add the source file to the project.
  • Click Next, then Finish. You should be able to edit the source and build the project. Sample PicBasic Pro programs 1) Blinking a Led Task: Connect a led to PIC16F84’s port B (any of the pin you can select). You will try to accomplish blinking your led (turn on, then turn off, then turn on….). Your pause time will be 1sec. Program your MCU with PBP. After all steps will be done, your led must blink. Schematic:
  • PBP Program: '----------------------Simple Blinking Program--------------------- OUTPUT PORTB.7 ' Our output is Led. (or you can write like this --> "TRISB.7 = 0") Loop: ' Generally all PBP programs started with a loop. HIGH PORTB.7 ' We make portb's 7. pin high, so portb's 7. pin's voltage level is 5V. PAUSE 1000 ' Wait 1000ms (1 sec) LOW PORTB.7 ' We make portb's 7. pin low, so portb's 7. pin's voltage level is 0V. PAUSE 1000 ' Wait 1000ms (1 sec) GOTO Loop ' Go to loop and do it forever. END ' End of our blinking program. (-: I think our first program is so easy.
  • 2) Led Show Task: Connect leds to PIC16F84’s port Bs all ports. Initially our leds are off state then firstly, turn on the leds from right to left respectively then turn on the leds left to right respectively after that turn on two middle leds, then turn on the leds from inside to outside, then turn on them outside to inside. For transitions, turn on the all leds 3 seconds. Schematic:
  • PBP Program: '------------------------------LED SHOW---------------------------- TRISB = %00000000 ' Make Port B all pins output PORTB = 0 ' Make Port B all pins 0 Volt Left_Counter VAR BYTE Right_Counter VAR BYTE Middle_Counter VAR BYTE Left_Counter = 0 ' Left_Counter holds how many times left transitions occured. Right_Counter = 0 ' Right_Counter holds how many times right transitions occured. Middle_Counter = 0 ' Right_Counter holds how many times middle transitions occured. loop: ' Main Subroutine left: ' Left transition subroutine PORTB = %00000001 ' Initially Port B's first pin voltage is 5 Volt and led that connected this pin turns on. leftcontinue: ' Inner left transition Subroutine PAUSE 300 ' Wait 300 ms PORTB=PORTB<<1 ' LEFT SHIFTING IF PORTB = %10000000 THEN PORTB = %00000001 ' Initial State for left transition Left_Counter = Left_Counter + 1 ' Increase left transition counter IF Left_Counter = 3 THEN PORTB = %11111111 ' Turn on port B's all leds. PAUSE 3000 ' Wait 3 seconds. GOTO right ' Go to right subroutine ENDIF ENDIF GOTO leftcontinue ' Go to left continue subroutine right: ' Right transition subroutine PORTB = %10000000 ' Initially Port B's last pin voltage is 5 Volt and led that connected this pin turns on. rightcontinue: ' Inner right transition Subroutine PAUSE 300 ' Wait 300 ms PORTB=PORTB>>1 ' RIGHT SHIFTING IF PORTB = %00000001 THEN PORTB = %10000000 ' Initial State for right transition Right_Counter = Right_Counter + 1 ' Increase right transition counter IF Right_Counter = 3 THEN PORTB = %11111111 ' Turn on port B's all leds. PAUSE 3000 ' Wait 3 seconds. GOTO middle ' Go to middle subroutine ENDIF ENDIF GOTO rightcontinue ' Go to right continue subroutine
  • middle: ' Right transition subroutine PORTB = %00011000 ' Port B's middle 4. and 5. pins are turns on. PAUSE 300 ' Wait 300 ms PORTB = %00100100 ' Port B's middle 3. and 6. pins are turns on. PAUSE 300 ' Wait 300 ms PORTB = %01000010 ' Port B's middle 2. and 7. pins are turns on. PAUSE 300 ' Wait 300 ms PORTB = %10000001 ' Port B's middle 1. and 8. pins are turns on. PAUSE 300 ' Wait 300 ms PORTB = %01000010 ' Port B's middle 2. and 7. pins are turns on. PAUSE 300 ' Wait 300 ms PORTB = %00100100 ' Port B's middle 3. and 6. pins are turns on. PAUSE 300 ' Wait 300 ms PORTB = %00011000 ' Port B's middle 4. and 5. pins are turns on. PAUSE 300 ' Wait 300 ms IF PORTB = %00011000 THEN Middle_Counter = Middle_Counter + 1 ' Increase middle transition counter IF Middle_Counter=3 THEN PORTB = %11111111 ' Turn on port B's all leds. PAUSE 3000 ' Wait 3 seconds. GOTO left ' Go to left subroutine ENDIF ENDIF GOTO middle ' Go to middle continue subroutine END ' End of program.
  • 2) Generating Sound Task: Buttons are connected to pins RA0, RA1 and RA2. Pressing any of these executes part of the code for generating impulse sequence on RA3 pin, which can be heard as different sounds or a melody on buzzer. Use FOR NEXT loop for generating different sounds. In the following program, pressing buttons T0, T1 and T2 generate different sounds on a buzzer.
  • PBP Program: '-------------------------------SOUND PROGRAM---------------------------- INPUT PORTA.0 'Set PORTA pin 0 to an input. For button T0.(or -> TRISA.0 = 1) INPUT PORTA.1 'Set PORTA pin 1 to an input. For button T1.(or -> TRISA.1 = 1) INPUT PORTA.2 'Set PORTA pin 2 to an input. For button T2.(or -> TRISA.2 = 1) OUTPUT PORTA.3 'Set PORTA pin 3 to an output. For BUZZER.(or -> TRISA.2 = 1) counter VAR BYTE ' Counter for different sounds. 'Or you can define I/O port like this; TRISA = %00000111 which means first three pins are inputs. loop: 'Our main loop 'For Button T0 IF PORTA.0 = 0 THEN ' If button T0 is pressed then generate different sounds. Counter increment rate is 1. FOR counter=10 to 130 SOUND PORTA.3, [counter,10,60,10] ' Generate Sound NEXT counter ENDIF 'For Button T1 IF PORTA.1 = 0 THEN ' If button T1 is pressed then generate different sounds. Counter increment rate is 10. FOR counter=10 to 130 STEP 10 SOUND PORTA.3, [counter,10,60,10] ' Generate Sound NEXT counter ENDIF 'For Button T2 IF PORTA.2 = 0 THEN ' If button T2 is pressed then generate different sounds. Counter increment rate is 20. FOR counter=10 to 130 STEP 20 SOUND PORTA.3, [counter,10,60,10] ' Generate Sound NEXT counter ENDIF GOTO loop ' Do it forever END ' End of our program.