Skip to main content

Other PicBasic commands 01



We shall now briefly look at the remaining PicBasic commands in alphabetical order which are useful during the program development. More details about these commands can be obtained from the PicBasic manual.

EEPROM

EEPROM Location, (constant, constant,….., constant)

This command stores constants in consecutive bytes in on-chip EEPROM memory. The command only works with the PIC microcontrollers that have EEPROM, such as the PIC16F84, PIC16F877,
etc. Location is optional, and if omitted the first EEPROM location is assumed. Constants can be numeric constants or string constants. Strings are stored as consecutive bytes of ASCII values. An example is given below.

EEPROM 3, (5, 2, 8)      ‘ Store 5 in location 3,
‘ 2 in location 4, and 8 in
‘ location 5



END



END



Stops execution and enters low power mode. The command has no parameters.



HIGH



HIGH Pin



Makes the specified pin an output pin and sets it to logic 1. Pin only applies to PORTB pins and it can take values from 0 to 7. In the following example, bit 1 of PORTB is configured as an out- put pin and is set to logic 1:

HIGH 1



I2CIN



I2CIN Control, Address, Var, (,Var)



This command is used to read data from serial EEPROMs with a 2-wire I2C interface. A list of some compatible devices is given in Table 4.4. The lower 7 bits of the Control byte contain a 4-bit control code, followed by the chip select or additional address information, depending on the device used. As shown in Table 4.4, the 4-bit control code for EEPROMs is “1010”. The high-order bit (MSB) of the Control byte is a flag indicating whether the Address is to be sent as 8 bits or 16 bits. If the flag is low, the Address is sent as 8 bits, and if it is high, the Address is sent as 16 bits. (,Var) shown in the command list is used only for 1-bit information. The I2C data and clock lines are predefined in the PicBasic library as bit 0 of PORTA (RA0) and bit 1 of PORTA (RA1), respectively.

For example, when communicating with a 24LC02B EEPROM, the required Address is 8 bits, the control code is “1010” and chip select or additional address information is not required and can be assumed to be 0. The required Control byte is then “01010000”.

Figure 4.2 shows how the 24LC02B (or any other serial EEPROM) can be connected to a PIC microcontroller. In this example, a PIC16F84 is used and pin RA0 and RA1 are connected to the data and clock pins of the EEPROM, respectively. These are the only connections required to communicate with an I2C-compatible device. As shown in the figure, the I2C lines should be con- nected to Vdd (   5 supply) with 4.7K resistors.


Some I2C compatible EEPROM


Device
Capacity
Control
Address size
24LC01B
128 bytes
01010xxx
8 bits
24LC02B
256 bytes
01010xxx
8 bits
24LC04B
512 bytes
01010xxb
8 bits
24LC08B
1 K bytes
01010xbb
8 bits
24LC16B
2 K bytes
01010bbb
8 bits
24LC32B
4 K bytes
11010ddd
16 bits
24LC65B
8 K bytes
11010ddd
16 bits

bbb     block select bits (each block is 256 bytes)
ddd     device select bits
 xxx     dont care




I2C Connections to a PIC microcontroller



In the following example, a data byte is read from address 20 of the serial EEPROM and stored in variable B1. Note that the Control byte is set to “01010000”, Address is assigned variable B0 and value 20 stored in it, and the byte read from the EEPROM is stored in data register B1.

Symbol con     %01010000
Symbol addr     B0
addr     20                        Set address to 20
I2CIN con, addr, B1       Read from address 20 to B1
 





I2COUT



I2COUT Control, Address, Value (,Value)



This command is used to send data to an I2C compatible device such as a serial EEPROM

described in command I2CIN. The (,Value) in the command is used for 16-bit information.



When writing data to an EEPROM, it is necessary to wait about 10 ms (device dependent) for the write operation to complete before attempting to write again. In the example given below, data byte

10 is written to address 30, and also data byte in variable B5 is written to address 31 of an EEPROM.



Symbol con     %01010000

Symbol addr     B0



addr     30                              Set address to 30

I2COUT con, addr, (10)        Write byte 10 to address 30

PAUSE 10                              Wait 10ms



addr     31                              Set address to 31

I2COUT con, addr, (B5)       Write byte in B5 to address 31

PAUSE 10                              Wait 10 ms



INPUT



INPUT Pin



This makes the specified PORTB pin an input. Pin is from 0 to 7. For example, INPUT 2                                             Make RB2 an input pin

LOOKDOWN



LOOKDOWN Search, (Constant, Constant,…..), Var



This command provides a look-up table. It looks down a list of Constants and compares each one with the Search value. If a match is found, the position of the match is stored in Var. Note that the first Constant is assumed to be at position 0. The Constant list can be numeric or string constants. In the following example, if we assume that variable B0 has value 5 then variable B1 will contain

3 which is the position of 5 in the table: LOOKDOWN B0, (0, 8, 9, 5, 12, 0, 1), B1

LOOKUP



LOOKUP Index, (Constant, Constant,….), Var

This command is used to retrieve values from a table. When Index is 0, Var is loaded with the first
Constant; when Index is 1, Var is loaded with the second Constant and so on. In the following
example, if we assume that variable B0 has value 3, variable B1 will be loaded with 8 which is the 3rd element in the table starting from 0:

LOOKUP B0, (0, 9, 0, 8, 12, 32), B1



LOW





LOW Pin



This command makes the specified pin an output pin and clears it to logic 0. Pin only applies to PORTB pins and it can take values from 0 to 7. In the following example, bit 2 of PORTB is con- figured as an output pin and is cleared to logic 0:

LOW 2



NAP



NAP Period




The NAP command places the PIC microcontroller in low-power mode for a while to save power in battery applications. The Period is a variable from 0 to 7 and the approximate delay is given in Table



 Delay in NAP command

Period
Delay (s, approx)
0
18     10   3
1
36     10   3
2
72     10   3
3
144     10   3
4
288     10   3
5
576     10   3
6
1.152
7
2.304

In the following example the microcontroller is put into low power mode for just over 1 s: NAP 6

OUTPUT



OUTPUT Pin



This command makes the specified pin of PORTB an output pin. Pin can take values from 0 to 7. In the following example, bit 2 of PORTB (RB2) is made an output pin:



OUTPUT 2
 


PAUSE



PAUSE Period



This is one of the commonly used commands to delay a program by a specified amount. Period is in milliseconds and can range from 1 to 65,535 ms (i.e. just over one minute). PAUSE does not put the microcontroller into low-power mode. In the following example, the program is delayed by 1 s:



PAUSE 1000




PEEK



PEEK Address, Var



This command is used to read the value of a RAM register at the specified Address and then put the value into variable Var. The PEEK command can be used to access all registers of the PIC microcontroller including the Port registers, A/D converter registers, etc.
In the following example, the 8-bit value of PORTB is read and stored in variable B0: Symbol PORTB     6        ‘ PORTB register address
PEEK PORTB, B0            ‘ Read PORTB into B0



POKE



POKE Address, Var



This command is used to send data to a RAM register at the specified Address. The POKE com- mand can be used to send data to all accessible registers of the PIC microcontroller, including the PORT registers, PORT direction registers, A/D converter registers, etc.

In the following example, TRISB is cleared to 0 so that all PORTB pins are outputs. The hexa- decimal value 24 is sent to PORTB.

Symbol TRISB     $86       TRISB register address
Smbol PORTB     6           ‘ PORTB register address

POKE TRISB, 0                ‘ Clear TRISB
POKE PORTB, $24          ‘ Send $24 to PORTB



POT



POT Pin, Scale, Var




This command could be useful to read an analogue voltage if the microcontroller has no built-in A/D converter. Pin is a PORTB pin and can take a value between 0 and 7. For this command to work, a resistor and a capacitor are serially connected to a port pin as shown in Figure 4.3. When a voltage is applied to a resistor–capacitor circuit, the voltage across the capacitor rises exponen- tially as the capacitor is charged through the resistor. The charge time is dependent on the value of the resistor and the capacitor.





Resistor and capacitor connected to an I/O pin

When the POT command is used, the capacitor is initially discharged by the I/O pin by placing the pin in output mode. After that, the I/O port is changed to an input port and starts timing the voltage across the capacitor until the voltage reaches the threshold value of the I/O pin. When this happens, the calculated charge time is converted into a number between 0 and 255 and is stored in Var. The Scale value should be set experimentally. To do this, set the device to maximum resistance and set the Scale to 255. The value returned in Var will be the proper scale value for the chosen compo- nents. An example is given below where the resistor–capacitor are connected to pin 1 of PORTB, the Scale value is set to 255 and the output value is stored in B0.

POT 1, 255, B0

PULSIN

PULSIN Pin, State, Var

The PULSIN command measures the pulse width of any signal connected to a PORTB pin. With a 4 MHz crystal or resonator, the pulse width will be measured in 10   s units. If State is 0, the width of a low pulse is measured; if Scale is 1, the width if a high pulse is measured. The meas- ured value in 10   s units is stored in variable Var. Var can be a byte or a word. If a word is used, it can take values 1 to 65,535, i.e. the minimum pulse width that can be measured is 10   s and the maximum is 655,350   s. If a byte is used, the range of the measurement is 10 to 2550   s.

PULSOUT

PULSOUT Pin, Period

This command generates a pulse on a PORTB pin (Pin can be 0 to 7) of specified Period in 10   s units. The Period is a word and thus pulses of up to 655,350   s can be generated. The specified pin is automatically made an output pin.

For example, to generate a 500-  s pulse on pin 1 of PORTB, we need the command

PULSOUT 1, 50



PWM





PWM Pin, Duty, Cycle




This command outputs a Pulse-Width-Modulated (PWM) signal on the specified PORTB pin (Pin can be 0 to 7). The Duty is the pulse duty-cycle and can range from 0 to 255. 0 corresponds to a 0% duty-cycle, and 255 corresponds to a 100% duty-cycle. The generated PWM pulse is repeated Cycle times. The specified port Pin is made an output just before the command is executed and reverts to an input after the pulse is generated.

In the following example, a 200-cycle PWM signal is generated on bit 0 of PORTB with a duty- cycle of 50%:

PWM 0, 127, 200

Another use of this command is to generate an analogue signal by sending the output to a resistor–capacitor circuit as shown in Figure 4.4. In this circuit, the voltage across the capacitor will vary depending on the Duty and the Cycle of the pulses.





Using PWM signal for D/A conversion

RANDOM

RANDOM Var

This command generates a random number and stores in word variable Var. For example, to gen- erate a random number and store in W1 use the command:

RANDOM W1



READ



READ Address, Var




This command is used to read a byte from the specified Address of the built-in EEPROM mem- ory. The byte read is stored in variable Var. This command can only be used with PIC microcon- trollers that have built-in EEPROM memory (such as PIC16F84, or PIC16F877).



In the following example, the byte at address 10 of EEPROM is read and stored in variable B1:



READ 10, B1       Read byte at address 10

and store in B1



REVERSE



REVERSE Pin



This command reverses the mode of a PORTB pin (Pin can be from 0 to 7). If the pin is an input, it is made an output. Similarly, if the pin is an output, it is made an input.



In the following example, bit 2 of PORTB is first made an output pin, then changed to an input pin:

OUTPUT 2          RB2 is output pin
REVERSE 2        RB” is an input pin



































Comments

Popular posts from this blog

ANALOGUE to DIGITAL (A/D) Converter in PIC16F87X Series

ANALOGUE to DIGITAL (A/D) Converter What is a Sample & Sample rate? A "sample" is a single measurement of amplitude. Sample rate is simply the number of samples (or measurements of amplitude) taken per second. Sampling Intervals Quantization The samples are assigned a binary number approximating their sampled value. Quantizing divides up the sampled voltage range into 2n-1 quantizing intervals, where “n” is the number of bits per sample (the sampling resolution). For example, an 8-bit system can identify 28 (256) discrete sampled signal values (255 quantizing intervals). The amplitude of such a signal can occupy the entire quantizing range. A/D Reference Voltages Successive Approximation A/D Example 1 Example 2  Example 3 A/D Converter Modules in PIC16F87X series 28-pin devices has 5 modules. (AN0-AN4)         PIC16F873        PIC16F876 4...

Voltmeter

This code to read an analog temperature sensor, convert the reading to a digital value using the ADC, and display the temperature on a 4-digit 7-segment display. The display is updated periodically based on a timer interrupt. 1.Initialization: Setting up ports, configuring ADC, and Timer0. 2.Infinite Loop: Continuously reads analog input and updates the display. 3.ADC Conversion: Reads analog voltage from a sensor. 4.Voltage Calculation: Converts ADC value to voltage. 5.Digit Extraction: Extracts individual digits from the temperature/voltage value. 6.Display Function: Displays the digits on a 4-digit 7-segment display with slight delays. --------------------------------------------- #include <pic.h> // Include the header file for PIC microcontroller family. unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Array containing values for 7-segment display. unsigned int a2d_value,temp; // Variables for analog to digital conversion and temporary sto...