Skip to main content

Other PicBasic commands 02

SERIN



SERIN Pin, Mode, (Qual, Qual,, ), Item, Item,



This command is used to receive RS232 serial asynchronous data on a PORTB pin (pin is between 0 and 7) using 8-bit data, no parity bit, and one stop bit. As shown in Table 4.6, Mode defines the baud rate and whether or not the pin data is inverted. For example, if Mode is N9600, the data is inverted and the selected baud rate is 9600.




Selecting the baud rate with Mode


Symbol
Value
Baud rate
Mode
T2400
0
2400
True
T1200
1
1200
True
T9600
2
9600
True
T300
3
300
True
N2400
4
2400
Inverted
N1200
5
1200
Inverted
N9600
6
9600
Inverted
N300
7
300
Inverted





The RS232 signal levels are    12 V and level converter circuits (such as MAX232) are normally used to convert the RS232 signal levels to TTL and the TTL levels back to RS232 levels. The I/O specifications of PIC microcontrollers allow RS232 signals to be directly connected to a port pin. As shown in Figure 4.5, a resistor is all that is needed to receive RS232-compatible signals on a pin. When used in this mode, the data is to be inverted (i.e. use the “N” versions of the mode sig- nals in Table )




Connecting a RS232 signal to a port pin

A number of qualifiers, enclosed in brackets, can be used with the SERIN command such that these bytes must be received before receiving the data items. Once the qualifiers are satisfied, SERIN receives the serial data and stores in Items. The Item variable may be preceded by the hash character (“#”). This will convert the decimal number received into ASCII equivalent and store it in Item.




In the following example, pin 1 of PORTB (RB1) is defined as the serial I/O pin and the port pin is connected to the RS232 serial line using a resistor. The baud rate is assumed to be 4800. The microcontroller waits until the character “X” is received from the line and then stores the next byte in variable B0:



SERIN 1, N4800, (“X”), B0



SEROUT



SEROUT Pin, Mode, (Item, Item,…)


This command is similar to the SERIN command but is used to send RS232 asynchronous serial data to a pin of PORTB (Pin can be between 0 and 7). As before, Mode is used to set the commu- nications baud rate. In addition to the standard inverted and non-inverted modes, it is also possi- ble to set Open-Drain and Open-Collector modes where a pull-up resistor will be required at the output of the pin.s

 Selecting the baud rate with Mode


Symbol                Value         Baud rate       Mode
T2400                     0               2400             True    
T1200                     1               1200             True
T9600                     2               9600             True
T300                       3                 300             True
N2400                     4                2400          Inverted
N1200                     5                1200          Inverted
N9600                     6                9600          Inverted
N300                       7                  300         Inverted
OT2400                   8                2400         Open Drain
OT1200                   9                1200         Open Drain
OT9600                 10                 9600         Open Drain
OT300                   11                  300         Open Drain
ON2400                 12                 2400        Open Source
ON1200                 13                 1200        Open Source
ON9600                 14                 9600        Open Source
ON300                   15                  300         Open Source

 



Data byte Item is sent to the specified port pin in serial format. The Item can be a string constant or a numeric value. A string constant consists of characters and each character of the string is sent out. For example, the string “COMPUTER is sent out as 8 individual characters. A numeric value will send the corresponding ASCII character. For example, 13 is the carriage-return character, 65 is character A and so on. A numeric value can be preceded by the hash character “# and this will send out the ASCII representation of its decimal value. For example, #345 will be sent as “3”, “4”, and “5”.

In the following example, it is assumed that pin 1 of PORTB (RB1) is used as the serial I/O pin and it is configured for 4800 baud. ASCII value of variable B0 is sent out from this pin, followed by a carriage-return.



SEROUT 1, N4800, (#B0, 13)



SLEEP



SLEEP Period

The SLEEP command is used to put the microcontroller in low-power mode and stops the micro- controller running for the specified Period. The Period is a word and can range from 1 to 65,535 and represents increments of 2.3 s. For example, a value of 1 will make the microcontroller sleep for 2.3 s, a value of 2 will make the microcontroller sleep for 4.6 s and so on. The maximum value of 65,535 makes the microcontroller sleep just over 18 h.

In the following example, the microcontroller sleeps for 23 s: SLEEP 10
SOUND

SOUND Pin, (Note, Duration, Note, Duration,…..)

This command is used to generate sound on a specified PORTB pin of the microcontroller (Pins are between 0 and 7). Note can take values from 0 to 255 and these values do not correspond to the musical notes. A 0 represents silence. Values from 1 to 127 are tones (1 is lower frequency than 127), and values from 128 to 255 are white noise (128 is lower frequency than 255). The sound continues for a length of time specified by Duration. Duration is measured in milliseconds and it can take values between 0 and 255. The SOUND command produces TTL level square waves and it is possible to connect a speaker to the output pin  



Connecting a speaker for the SOUND command

In the following example, a sound with note 20 and duration 100 ms is sent to pin 0 of PORTB. Then, another sound with note 23 and duration 200 ms is sent out from the same port pin.


SOUND 0, (20, 100, 23, 200)


TOGGLE


TOGGLE Pin


This command makes the specified Pin an output pin and inverts the state of this pin (Pin can take values from 0 to 7).



In the following example, bit 0 of PORTB (RB0) is first made low, and then changed to high using the TOGGLE command:


LOW 0

TOGGLE 0


WRITE


WRITE Address, Value


The WRITE command writes the Value byte to the specified EEPROM address. This command is only valid for the PIC microcontrollers which have built-in EEPROM memories.


In the following example byte in variable B0 is written to EEPROM address 2: WRITE 2, B0





 

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