Skip to main content

PicBasic mathematical and logical operations


PicBasic supports a number of mathematical and logical functions which make calculations easy in programs. The operations are performed on integer numbers only with 16-bit precision and there is no floating-point number format. Also, all math operations are performed strictly from left to right. The operators supported are

              addition
              subtraction
*              multiplication
**            most significant bit (MSB) of multiplication
/              division
//             remainder in a division MIN        limit to minimum value MAX      limit to maximum value
&            bitwise AND
|               bitwise OR
^              bitwise XOR
&/           bitwise AND NOT
|/              bitwise OR NOT
^/             bitwise XOR NOT

Multiplication is done on 16     16 bit numbers, resulting in a 32-bit result. The “* operator returns the lower 16-bits of the 32-bit result. Similarly, the “**operator returns the upper 16-bits of the result. For example,

W2     W1 * W0         Multiply W1 with W0. The lower 16-bits of the result
are placed in W2
or,


W2     W1 ** W0       Multiply W1 with W0. The upper 16-bits of the result
are placed in W2
or,


W2     W1 * 100         Multiply W1 with 100. Place the lower 16-bits of the
result in W2. Note that this is the multiplication
found in most programming languages

Similarly with division,

or,


W2     W1 / W0     ‘ Divide W1 by W0. The result is placed in W2

W2     W1 // W0    ‘ Divide W1 by W0. The remainder is placed in W2



MIN is used to limit the result to the minimum value defined. For example, B1     B0 MIN 100
Sets B1 to the smaller of B0 and 100, i.e. B1 cannot be greater than 100.

Similarly, MAX is used to limit the result to the maximum value defined. For example, B1     B0 MAX 100
sets B1 to the larger of B0 and 100, i.e. B1 will be between 100 and 255.

Bitwise logical operations operate on the entire byte and these operations can be used to extract bits from bytes or to set and clear bits of a byte. For example, to extract the least significant bit of B0 we can write

B0     B0 & 000001

Similarly, to set bit 2 of B1 to be 1 we can write

B1     B1 | 000100

To store the upper four bits of B2 in B1 we can write

B1     B2 & %11110000
 

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...

Other PicBasic commands 01

W e shall n o w brief l y look at the remaining PicBasic commands in alphabetical order w hich are useful during the pr o g ram d e v elopment. More details about these commands can be obtained from the PicBasic manual. EEP R OM EEP R OM Location, (constant, constant,….., constant) Thi s comman d store s constant s i n consecut i v e b yte s i n on-chi p EEP R O M memo r y . Th e command on l y w ork s wit h th e PI C microcontroller s tha t h a v e EEP R OM , suc h a s th e PIC16F84 , PIC16F877, etc . Locatio n i s optional , an d i f omitte d th e f irs t EEP R O M locatio n i s assumed . Constant s ca n be numeri c constant s o r strin g constants . String s ar e store d a s consecut i v e b yte s o f ASCI I v alues . An e xampl e i s g i v e n bel o w . EEP R OM 3, (5, 2, 8)        ‘ Store 5 in location 3, ‘ 2 in location 4, and 8 in ‘ location 5 END END St...

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...