ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 12.06.2025
Просмотров: 1364
Скачиваний: 4
Basic for PIC Microcontrollers |
86 |
||
Example: |
B0 var byte |
||
B1 var byte |
|||
W0 var byte |
|||
Main : |
|||
shiftout |
PORTA.0, PORTA.1, MSBFIRST, [B0, B1] |
||
‘ |
Sends the contents of variables B0 and B1 to output SHIFT register so that the |
||
first |
|||
‘ |
transferred bit is MSB |
||
shiftout |
PORTA.0, PORTA.1, MSBFIRST, [W0\4] |
||
‘ |
Sends 4 bits of variable W0 so that the first transferred bit is MSB |
||
Loop : goto Loop |
|||
End |
|||
4.58 SLEEP Turns off the processor for a given time period
Syntax: SLEEP Period
Description: Instruction puts the microcontroller to a state of low energy consumption for "Period" of seconds. "Period" is a 16-bit value allowing maximal delay of 65 535 seconds (about 18h). SLEEP uses the watchdog timer (WDT) with granularity about 2.3 seconds. RC oscillator is less temperature stable than system clock, making WDT somewhat less accurate.
Example: Main :
sleep 60 ‘ Go to low power mode for next 60 sec
Loop : goto Loop
End
Basic for PIC Microcontrollers |
87 |
4.59 SOUND |
Generates sound or white noise on a given pin |
Syntax: |
SOUND Pin, (Note, Duration{, Note, Duration}) |
Description: |
Instruction generates tone and/or noise on a given pin. For Note=0 there is no sound |
generated. If Note falls within range of 1-127 tones are generated, while range of 128- |
|
255 generates noise. |
|
Tones and noises are sorted in an ascending fashion (1 and 128 are the lowest |
|
frequencies, 127 and 255 are the highest). Duration ranges from 0 to 255 and defines |
|
sound duration in 12ms increments ("Note" and "Duration" don‘t have to be constants). |
|
Sound is being sent to output in form of sequence of TTL rectangle impulses. Thanks to |
|
the outstanding I/O features of PIC microcontrollers, a speaker can be driven directly |
|
trough electrolitical capacitor. Piezo speakers can be driven directly. |
|
Example: |
Main : |
sound PORTB.7, (100, 10, 50, 10) ‘ Sends 2 sounds in sequence to pin RB7
Loop : goto Loop
End
4.60 STOP Stops the program execution
Syntax: STOP
Description: Instruction stops the program execution by commencing the infinite loop. This instruction does not put the microcontroller to low power mode.
Example: Main :
STOP ‘ Stop the program execution in this line
Loop : goto Loop
End
4.61 SWAP Exchanges values of two variables
Syntax: |
SWAP Variable1, Variable1 |
Description: Instruction SWAP exchanges values of two variables. It can be used with variables of bit, byte and word types. SWAP can be used with strings, but only with those that have constant indexes.
Basic for PIC Microcontrollers |
88 |
||
Example: |
B0 var byte |
||
B1 var byte |
|||
temp var byte |
|||
Main : |
|||
temp = B0 |
|||
B0 = B1 |
|||
B1 = temp |
‘ |
classical way to do it |
|
swap B0, B1 |
‘ |
...and easier way to do it |
|
Loop : goto Loop |
|||
End |
|||
4.62 TOGGLE Inverts pin states
Syntax: |
TOGGLE Pin |
||
Description: |
Instruction inverts state of a specified pin. "Pin" is automatically designated output. |
||
Example: |
Main : |
||
low PORTB.0 |
‘ |
Set the state of pin RB0 to low level as starting condition |
|
toggle PORTB.0 |
‘ |
Change state of pin RB0 to high level |
|
Loop : goto Loop |
|||
End |
|||
4.63 WRITE |
Writes byte of data to data EEPROM |
Syntax: |
WRITE Address, Value |
Description: |
Instruction writes "Value" to a specified address of EEPROM. WRITE can only be used with |
PIC microcontrollers that have EEPROM built in chip. |
|
Basic for PIC Microcontrollers |
89 |
If 2-byte variable is being stored, two bytes are written in sequence : |
|
WRITE Address, Variable.BYTE0 |
|
WRITE Address, Variable.BYTE1 |
|
Example: |
B0 var byte |
Main : |
|
B0 = $EA |
|
write 5, B0 ‘ Writes value $EA to location 5 of EEPROM |
|
Loop : goto Loop |
|
End |
|
4.64 WRITECODE Writes two bytes (word) of data to program memory
Syntax: |
WRITECODE Address, Value |
Description: |
WRITECODE writes "Value" to a given address of program memory. This instruction can |
only be used with PIC microcontrollers that have FLASH memory in chip. Interrupts |
|
during the writing must be on. |
|
Example: |
W0 var byte |
Main : |
|
W0 = $12FE |
|
writecode 100, W0 ‘ Write value $12FE to location 100 of program FLASH |
|
memory |
|
Loop : goto Loop |
|
End |
|
4.65 WHILE-WEND Executes set of instructions while condition is fulfilled
Syntax : |
WHILE Condition |
Basic for PIC Microcontrollers |
90 |
|
Instructions... |
||
WEND |
||
Description: |
Purpose of this instruction is to keep executing set of instructions between WHILE and WHEN as long as |
|
"Condition" is fulfilled. |
||
Example: |
i Var byte |
|
Main : |
||
i = 1 |
||
WHILE i< 10 |
‘ when i reaches 10 program stops and port B has value of 9 |
|
i = i + 1 |
||
PORTB = i |
||
Pause 1000 |
||
WEND |
||
goto Main |
||
End |
||
Basic for PIC Microcontrollers |
91 |
Chapter 5
SAMPLE PROGRAMS FOR SUBSYSTEMS WITHIN THE MICROCONTROLLER
Introduction
5.1 Using the interrupt mechanism
5.2 Using the internal AD converter
5.3 Using the TMR0 timer
5.4 Using the TMR1 timer
5.5 Using the PWM subsystem
5.6 Using the hardware UART subsystem (RS-232 communication)
Introduction
Every microcontroller is supplied with at least a few integrated subsystems - commonly, these include timers, interrupt mechanisms and AD converters. More powerful microcontrollers can command greater number of built-in subsystems. Some of frequently encountered systems are detailed in this chapter.
5.1 Using the interrupt mechanism
Interrupts are mechanisms which enable instant microcontroller response to events such as : TMR0 counter overflow, state changes on RB0/INT pin, data is received over serial communication, etc. With bigger microcontrollers, number of interrupt sources is even greater. In normal mode, microcontroller executes the main program as long as there are no occurrences that would cause interrupt. When interrupt does take place microcontroller stops the execution of the main progra m and starts executing part of the program (interrupt routine) that will analyze and handle the interrupt. Analysis in necessary because PIC microcontrollers call the same interrupt routine in response to any of the mentioned events. Therefore, the first task is to determine which event caused the interrupt. After the analysis comes the interrupt handling, which is executing the appropriate part of program code tied to a certain event.
Basic for PIC Microcontrollers |
92 |
Button T is connected to the external interrupt input INT (pin RB0/INT) so that pressing the button is considered an interrupt occurrence. In order to see the change caused by interrupt LED diodes are connected to the pins RB6 and RB7. LED_run diode signalizes that the main program is being executed, while LED_ini diode signalizes the interrupt caused by pressing the button T. Following instructions are used in PIC BASIC programs which contain interrupt routine :
On Interrupt goto Address Defines the interrupt vector (address of interrupt routine)
Disable |
Disables the interrupts |
Enable |
Enables the interrupts |
Resume |
Return to the main program after handling the event |
Following example demonstrates usage of external interrupt INT located on pin RB0. At the same time, program gives an example how to handle multiple interrupt sources.
Basic for PIC Microcontrollers |
93 |
Basic for PIC Microcontrollers |
94 |
Program which handles interrupt must have the main loop (program) and an interrupt routine. Program in the main loop keeps
LED_run diode on and LED_int diode off. Pressing the button T causes the interrupt and the microcontroller will stop executing the main program and start executing the interrupt routine ISR marked by On interrupt instruction.
At the beginning of the interrupt routine there is instruction Disable. This instruction disables all interrupts until handling the current interrupt is over. ISR routine then analyses the interrupt by checking bits (flags) set on "1" with couple of if...then instructions, because there are several possible interrupt causes. In our case, an external interrupt took place (pin RB0/INT state changes) and therefore bit INTF in INTCON register is set and the microcontroller continues program execution from the label INTF. Part of the program code following the label INTF handles the interrupt and resets INTF bit in order to enable interrupts again. In this case, handling the external INTinterrupt changes state of diodes LED_int and LED_run : it turns off LED_run and turns on LED_int for half second period. After INTF is being reset, microcontroller continues executing the program from Exit_ISR label where interrupts are enabled (instruction Enable) and microcontroller returns to executing the main program (instruction Resume).
Why use interrupts at all ? In situations where the microcontroller must respond to events unrelated to the main program it is very useful to have an interrupt. Perhaps, one of the best examples is multiplexing the seven-segment display. If multiplexing code is part of the interrupt routine tied to timer interrupt the main program will be much less burdened because display refreshing will work in the background of the main program.
5.2 Using the internal AD converter
Certain microcontrollers have built in analog-digital converter (abbrev. ADC). Usually, these AD converters do not exceed 8 to 10 bits resolution allowing them voltage sensitivity of 19.5mV with 8-bit resolution and 4.8mV with 10-bit resolution (assuming that default