ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1352

Скачиваний: 3

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Basic for PIC Microcontrollers

63

nap 7 ‘ take a nap for 2.304 seconds

Loop: goto Loop

End

4.36 OUTPUT Designates I/O pin as output

Syntax: OUTPUT pin

Description: Designates specified pin as output.

Example: Main:

output PORTB.7

‘ Pin RB7 is output

TRISB.0 = 0

‘ Same effect as above

Loop : goto Loop

End

4.37 OWIN Receives data via one-wire communication

Syntax:

OWIN Pin, Mode, [Var1, Var2...]

Description: Parameter “Pin” is a variable containing the microcontroller pin connected to the element

which has one-wire communication.

Parameter “Mode” is value defined by parameters of communication.

"Mode" bit

How it works

0

1

= sending the reset signal ahead of data

1

1

= sending the reset signal after data

0 = 8-bit data

2

1= 1 -bit data


Basic for PIC Microcontrollers

64

Parameters “Var1” and “Var2” are variables for containing the read data.

Example:

Temperature var byte

Main:

OWIN PORTC.0, 0, [Temperature]

‘ read the temp.

PORTB=Temperature

‘ display temperature on port B diodes

goto Main

End

4.38 OWOUT

Transmits data via one -wire communication

Syntax:

OWOUT Pin, Mode, [Var1, Var2...]

Description:

Parameter “Pin” is variable containing the microcontroller pin connected to the element which

has one-wire communication.

Parameter “Mode” is value defined by parameters of communication.

"Mode" bit

How it works

0

1 = sending the reset signal ahead of data

1

1 = sending the reset signal after data

0 = 8-bit data

2

1= 1 -bit data

Parameters “Var1” and “Var2” are variables for containing the read data.

Example:

Main :

OWOUT PORTC.0, 1, [$CC, $BE] ‘ sends reset signal and 2 values afterwards

goto Main

End


Basic for PIC Microcontrollers

65

4.39 PAUSE Pause (in miliseconds)

Syntax:

PAUSE Period (in miliseconds)

Description:

Instruction pauses the program for “Period” miliseconds. Period is 16-bit, allowing delay

to be as long as 65 535ms (a bit over a minute). Unlike other delay instruc tions (NAP and

SLEEP), PAUSE does not put the microcontroller to low power mode. Thus, PAUSE

consumes more energy, but gets more accurate timing (it has precision of a system

clock).

Example:

TRISB = 0

Main:

PORTB = 255

pause 1000

Delay execution of next instruction line for 1 sec.

PORTB = 0

pause 2000

Delay execution of next instruction line for 2 sec.

goto Main

End

4.40 PAUSEUS Pause (in microseconds)

Syntax:

PAUSEUS Period (in miliseconds)

Description:

PAUSEUS stops the program for “Period” miliseconds. Period is 16-bit (WORD), allowing

delay to be as long as 65 535ms (a bit over a minute). Unlike other delay instructions

(NAP and SLEEP), PAUSE does not put the microcontroller to low power mode. PAUSEUS

consumes more energy than PAUSE, but gets much more accurate timing. Minimal delay

of PAUSEUS depends on the crystal frequency.

OSC

Minimal delay


Basic for PIC Microcontrollers

66

3 (3.58)

20 us

4

24 us

8

12 us

10

8 us

12

7 us

16

5 us

20

3 us

PAUSEUS works with default 4MHz crystal frequency. If frequency differs from default it is

necessary to modify it with directive: DEFINE Osc.

Example:

TRISB = 0

Main:

PORTB = 255

pauseus

100 ‘

Delay execution of next instruction line for 100 microsec

PORTB = 0

pauseus

3450‘

Delay execution of next instruction line for 3.450 ms

goto Main

End

4.41 POT Returns value of OTPORNOST connected to the pin

Syntax:

POT Pin, Scale, Var

Description: Instruction POT measures value of potentiometer on a given pin. Resistance can be calculated using the time of condenser discharge through resistor (usually 5K to 50K). Scale is used for setting various RC constants. For higher RC constants, scale should be set to the lowest value (minimally 1). For lower RC constants, scale should be set to the highest value (maximally 255). If the scale is set properly, "Var" should have near zero value, close to the minimum of resistance (unfortunately, scale value has to be determined experimentally).

In order to set the "Scale" parameter, potentiometer should be set to maximum resistance and measured with scale set to 127. Next, "Scale" parameter should be adjusted until value of "Var" reaches 255. Program below does it automatically.

Example:

B0 var byte


Basic for PIC Microcontrollers

67

skala var byte Main :

FOR skala=1 TO 255

pot PORTA.0, scale, B0 ‘ read value of potentiometer on RA0

IF B0>253 Then Over

NEXT skala

Over : PORTB=scale

‘ display value of the scale on port B diodes

goto Main

End

4.42 PULSIN Measures impulse duration on input pin

Syntax:

PULSIN Pin, Level, Var

Description: Instruction measures impulse duration with 10us resolution (when 4MHz oscillator is used) on a given pin. If level is zero it measures duration of low impulse and if level is one it measures duration of high impulse. Measured value of duration is put to variable "Var". Measuring can last from 10 to 65 535 microseconds for 16-bit variables. If impulse doesn‘t appear at all or it‘s duration is too long to be measured variable is set to zero.

In case of 8-bit variable only lower 8 bits of a 16-bit word are used. Resolution depends

on oscillator frequency. 4MHz oscillator has 10us resolution, while 20MHz oscillator has

2us resolution.

Example:

W0 var word

Main :

pulsin PORTB.0, 1, W0

‘ Measures high impulse on RB0 pin with 10us resolution

and puts

it to variable W0

goto Main

End

Basic for PIC Microcontrollers

68

4.43 PULSOUT Generates impulse on output pin

Syntax:

PULSOUT Pin, Period

Description:

Instruction generates impulse of specific duration in tens of microseconds (when 4MHz

oscillator is used) on a pin. Impulse is generated by double change of level on a pin, so

that former state of pin defines polarity of an impulse. Chosen pin is automatically

designated output.

Resolution depends on oscillator frequency. 4MHz oscillator has 10us resolution, while

20MHz oscillator has 2us resolution.

Example:

Main :

pulsout PORTB.7, 100

‘ Generate 1ms impulse to RB7 pin

goto Main

End

4.44 PWM Generates PWM signal on pin

Syntax:

PWM Pin, Ratio, Cycle

Description: Instruction sends PWM (Pulse-Width Modulation) impulses Ratio to pin defined with parameter "Pin" (for each PWM signal, cycle goes from 0 (0%) to 255 (100%)). This PWM cycle repeats itself for number of times defined with "Cycle" parameter. Pin direction is set to output just before generating PWM impulse and is set back to input afterwards.

Cycle duration depends on the oscillator used. In case of 4MHz oscillator cycle duration is

5ms, while in case of 8MHz oscillator cycle duration is 1ms. Instruction PWM allows simple R/C circuit to be used for generating DC voltage like a simple D/A converter.

Example: Main :