Picbasic Pro Serial Communication Example
PICBASIC PRO Compiler (3.0 and later) Working Code Examples; If this is your first visit, be sure to check out the Forum Help pages by clicking the Help link. You are required to register before you can post: click the sign-up link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. Example for CSU's MECH307 mechatronics course illustrating how to use Microchip PIC microcontrollers and.
PicBasic Pro Programs
PICBASIC PRO PROGRAMS FOR 16F88:
Beginning Programs
Servo and Motor Control Programs
Stepper Motor Programs
Reading Sensor Programs
Serial Communications Programs
Visual Basic.NET Programs
Robotics Programs
DS1620 Thermometer Programs
Making Music with PIC16F88
PICBASIC PRO PROGRAMS FOR 16F877A:
Beginning Programs
Servo and Motor Control Programs
Stepper Motor Programs
Reading Sensor Programs
PICBASIC PRO PROGRAMS FOR 18F4331:
Encoder Programs
These fundamental PicBasic Pro programs were written to introduce new statements that periodically would be combined into a program with a robotic application. Most of the programs are very short, focusing on one new command or a new application of a command. Check Technical Lessons for Year 2 for more background information.
The microcontroller used for the most part is the PIC16F88-I/P from Microchip Technology,
http://microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=64. The data sheet is available at: http://ww1.microchip.com/downloads/en/devicedoc/30487c.pdf. Jameco stocks the PIC16F88-I/P at a cost of $3.25 each for quantities less than 10. Microchipdirect lists the PIC16F88-I/P at $2.60 each for quantities less than 26.
Some of the programs have adapted to the PIC16F877A.
The programs are written in PicBasic Pro from micro-Engineering Labs, Inc., melabs.com. We use the melabs U2 Programmer. The cost for both the PicBasic Pro Compiler 3.0 and the U2 Programmer with Accessories ranges from $174.90 to $394.90 USD depending upon which version of PicBasic Pro Compiler 3.0 purchased.
16F88 Beginning Programs:
Program Description | New PICBASIC PRO Statements | Code as .pdf file | |||
blink1 | LED flashes on and off one time per second. | PAUSE, GOTO, END | Schematic | Code file | blink1.pbp |
blink2 | LED flashes on and off one time per half second using different commands. | HIGH, LOW | Schematic | Code file | blink2.pbp |
railroad | Two LEDs flash like a railroad crossing. | Schematic | Code file | railroad.pbp | |
blink3 | Turns one LED on and off 5 times. | FOR..NEXT | Schematic | Code file | blink3.pbp |
led_piezo | Many patterns of flashing | Schematic | Code file | led_piezo.pbp | |
bounce1 | Eight LED's scroll on from the right then to the left then back again. | Schematic | Code file | bounce1.pbp | |
bounce2 | Eight LED's scroll off from the right then to the left. | Schematic | Code file | bounce2.pbp | |
count1 | Eight LEDs count in binary from 1 to 255 | Schematic | Code file | count1.pbp | |
count2 | Display 8-bit number as decimal, hexadecimal, binary number on LCD | Schematic | Code file | count2.pbp | |
LCD1 | Prints simple message to LCD. | LCDOUT | Schematic | Code file | LCD1.pbp |
LCD2 | Demonstrates several commands to move LCD cursor. | Schematic | Code file | LCD2.pbp | |
LCD3 | Display resistance readings from a potentiometer. | POT | Schematic | Code file | LCD3.pbp |
pic_as_source | PIC acts as a source output to light an LED | Schematic | Code PDF file | pic_as_source | |
pic_as_sink | PIC acts as a sink output to light an LED | Schematic | Code PDF file | Pic_as_sink | |
active_high | LED turns on when active high switch is pressed | Schematic | Code PDF file | active_high | |
active_low | LED turns on when active low switch is pressed | Schematic | Code PDF file | active_low | |
array1 | Set up two 6 element arrays | Schematic | Code file | array1 | |
array2 | Find max value in 7 element array | Schematic | Code file | array2 | |
fiber-optic1 | Sends simple signal through fiber-optic cable | Schematic | Code PDF file | fiber_optic1 | |
shift1 | Use shift operator << to shift value to left | Shift Operator | Schematic | Code file | shift1.pbp |
signed_num1 | Display signed numbers | Schematic | Code file | signed_num1 |
16F88 Servo and Motor Control Programs:
Program Name | New PICBASIC PRO Statements | Schematic for Code | Download PicBasic Pro Code as .pbp File | ||
servo1 | Rotates servo cwise and ccwise using PULSOUT | PULSOUT | Schematic | Code file | servo1.pbp |
servo2 | Rotates servo cwise and ccwise using PAUSEUS | PAUSEUS | Schematic | Code file | servo2.pbp |
servo3 | Rotates servo cwise and ccwise in incremental steps | Schematic | Code | servo3.pbp | |
servo4 | Servo position determined by switch inputs | Schematic | Code file | servo4.pbp | |
h_bridge | PIC drives SN754410 1.5 A driver | Schematic Photo of Breadboard (large pdf file) | Code file | h_bridge_ sn754410_ 1.pbp | |
pwm1 | Drives dc motors at different speeds. Jameco motor1motor2 | PWM | Schematic | Code file | pwm1.pbp |
hpwm1 | Shows HPWM working in program background | HPWM | Schematic | Code file | hpwm1.pbp |
hpwm2 | Changes speeds using HPWM | Schematic | Code file | hpwm2.pbp | |
pwm_ hpwm1 | Runs PWM and HPWM commands together | Schematic | Code file | pwm_hpwm1 | |
pwm_ sn754410 | Uses PAUSEUS command to create PWM signal to drive SN754410 1.5 A h-bridge driver | Schematic Photo of Breadboard (large pdf file) | Code file | pwm_ sn754410_1 |
16F88 Stepper Motor Programs:
Program Description | New PICBASIC PRO Statements | Code as .pdf file | |||
step_mot1 | Drives stepper motor | Schematic | Code PDF file | step_mot1.pbp | |
step_mot_ hi_torque | Generates 1.4 times the torque as step_mot1 | Schematic | Code PDF file | step_mot_ hi_torque.pbp | |
step_mot_ half_step | Drives stepper motor with twice the steps as step_mot1 | Schematic | Code PDF file | step_mot_ half_step.pbp | |
step_mot_ one_rev | Drives stepper motor to rotate one revolution cw and ccw then stop | LOOKUP | Schematic | Code PDF file | step_mot_ one_ revolution.pbp |
16F88 Reading Sensor Programs:
Program Name | New PICBASIC PRO Statements | Schematic for Code | Download PicBasic Pro Code as .pbp File | ||
switch1 | Turn on/off LED's with button switch. | IF..THEN | Schematic | Code file | switch1.pbp |
switch2 | Switch drives LED and servo. | IF..THEN.. ELSE | Schematic | Code file | switch2.pbp |
switch_ limit1 | Uses limit switches to reverse motor See Video | Schematic | Code file | switch_limit1 | |
adc1 | 8-Bit Analog-to-Digital conversion | ADCIN | Schematic | adc1.pbp | |
adc2 | 10-bit Analog-to-Digital conversion | Schematic | Code file | adc2.pbp | |
adc3 | Converts 10-Bit data to voltage | Schematic | Code file | adc3.pbp | |
bend_sensor1 | Use flex sensor to rotate hobby servo See Video | Schematic | Code file | bend_sensor1 | |
read_voltage | Reads and displays 0 to +15 volts | Schematic | Code PDF file | read_voltage | |
sonar1 | Displays SRF04 sonar readings to LCD. | PULSIN | Schematic | Code file | sonar1 |
sonar2 | LED's light up as sonar is brought close to an object. | Schematic | Code File | sonar2 | |
dinsmore1 | Reads Dinsmore 1490 Digital Compass | Schematic | Code PDF File | dinsmore1 | |
MPX4250 | Measures water depth with MPX4250AP sensor | Schematic | Code PDF File | mpx4250 |
16F88 Serial Communications:
Program Description | New PICBASIC PRO Statements | Code as .pdf file | |||
serout2_hello | Display hello on PC terminal program | SEROUT2 | Schematic | Code file | serout2_hello |
serout2_ potentiometer_ 8bit | Display 8-bit potentiometer reading on PC terminal program | Schematic | Code file | serout2_ potentiometer_ 8bit | |
serout2_ potentiometer_ 10bit | Display 10-bit potentiometer reading on PC terminal program | Schematic | Code file | serout2_ pontentiometer_ 10bit | |
serin2_led_on_ off | PC controls LEDs on PIC | SERIN2 SELECT CASE | Schematic | Code file | serin2_led_on_off |
serin2_pwm | PC controls motor speed | SERIN2 with Timeout, Label | Schematic | Code file | serin2_pwm |
serin2_pwm_ wait | PC controls motor speed | SERIN2 with WAIT () Modifier | Schematic | Code file | serin2_pwm_wait |
multiplex_tx1 & | Control 4 LEDs over fiber-optic cable using 4 variables | HSEROUT HSERIN | Schematic | tx1.pdf rx1.pdf | multiplex_tx1 multiplex_rx1 |
multiplex_tx2 & multiplex_rx2 | Control 4 LEDs over fiber-optic cable using 1 variable | Schematic | tx2.pdf rx2.pdf | multiplex_tx2 multiplex_rx2 | |
multiplex_tx3 & multiplex_rx3 | Control 2 motors' speed over fiber-optic cable. One tx PIC, two rx PICs | Schematic | tx3.pdf rx3x.pdf rx3y.pdf | multiplex_tx3 multiplex_rx3x multiplex_rx3y |
16F877A Beginning Programs:
Program Name | New PICBASIC PRO Statements | Schematic for Code | Download PicBasic Pro Code as .pbp File | ||
blink1 | LED flashes on and off one time per second. | PAUSE, GOTO, END | Schematic | Code file | blink1.pbp |
blink2 | LED flashes on and off one time per half second using different commands. | HIGH, LOW | Schematic | Code file | blink2.pbp |
railroad | Two LED's flash like a railroad crossing. | Schematic | Code file | railroad.pbp | |
blink3 | Turns one LED on and off 5 times. | FOR..NEXT | Schematic | Code file | blink3.pbp |
bounce1 | Eight LED's scroll on from the right then to the left then back again. | Schematic | Code file | bounce1.pbp | |
bounce2 | Eight LED's scroll off from the right then to the left. | Schematic | Code file | bounce2.pbp | |
count1 | Eight LEDs count in binary from 1 to 255 | Schematic | Code file | count1.pbp | |
LCD1 | Prints simple message to LCD. | LCDOUT | Schematic | Code file | LCD1.pbp |
LCD2 | Demonstrates several commands to move LCD cursor. | Schematic | Code file | LCD2.pbp | |
LCD3 | Display resistance readings from a potentiometer. | POT | Schematic | Code file | LCD3.pbp |
16F877AServo and Motor Control Programs:
Program Description | New PICBASIC PRO Statements | Code as .pdf file | |||
servo1 | Rotates servo cwise and ccwise using PULSOUT | PULSOUT | Schematic | Code file | servo1.pbp |
servo2 | Rotates servo cwise and ccwise using PAUSEUS | PAUSEUS | Schematic | Code file | servo2.pbp |
servo3 | Rotates servo cwise and ccwise in incremental steps | Schematic | servo3.pbp | ||
servo4 | Servo position determined by switch inputs | Schematic | Code file | servo4.pbp | |
h_bridge | PIC drives SN754410 1.5 A driver | Schematic | Code file | h_bridge_ sn754410_ 1.pbp | |
pwm1 | Drives dc motors at different speeds. Jameco motor1motor2 | PWM | Schematic | Code file | pwm1.pbp |
hpwm1 | Shows HPWM working in program background | HPWM | Schematic | Code file | hpwm1.pbp |
hpwm2 | Changes speeds using HPWM | Schematic | Code file | hpwm2.pbp | |
pwm_ sn754410 | Uses PAUSEUS command to create PWM signal to drive SN754410 1.5 A h-bridge driver | Schematic | Code file | pwm_ sn754410_1 |
16F877A Stepper Motor Programs:
Program Name | New PICBASIC PRO Statements | Schematic for Code | Download PicBasic Pro Code as .pbp File | ||
step_mot1 | Drives stepper motor | Schematic | Code PDF file | step_mot1.pbp | |
step_mot_ hi_torque | Generates 1.4 times the torque as step_mot1 | Schematic | Code PDF file | step_mot_ hi_torque.pbp | |
step_mot_ half_step | Drives stepper motor with twice the steps as step_mot1 | Schematic | Code PDF file | step_mot_ half_step.pbp | |
step_mot_ one_rev | Drives stepper motor to rotate one revolution cw and ccw then stop | LOOKUP | Schematic | Code PDF file | step_mot_ one_ revolution.pbp |
16F877A Reading Sensor Programs:
Program Description | New PICBASIC PRO Statements | Code as .pdf file | |||
switch1 | Turn on/off LED's with button switch. | IF..THEN | Schematic | Code file | switch1.pbp |
switch2 | Switch drives LED and servo. | IF..THEN.. ELSE | Schematic | Code file | switch2.pbp |
adc1 | 8-Bit Analog-to-Digital conversion | ADCIN | Schematic | adc1.pbp | |
adc2 | 10-bit Analog-to-Digital conversion | Schematic | Code file | adc2.pbp | |
adc3 | Converts 10-Bit data to voltage | Schematic | Code file | adc3.pbp | |
sonar1 | Displays SRF04 sonar readings to LCD. | PULSIN | Schematic | Code file | sonar1 |
Bluetooth:
Bluetooth® wireless technology is becoming a popular standard in the communication. it is one of the fastest growing fields in the wireless technologies. It is convenient, easy to use and has the bandwidth to meet most of today’s demands for mobile and personal communications. Bluetooth technology handles the wireless part of the communication channel; it transmits and receives data wirelessly between these devices. It delivers the received data and receives the data to be transmitted to and from a host system through a host controller interface (HCI). The most popular host controller interface today is either a UART or a USB .Here, I will only focus on the UART interface, it can be easily show how a Bluetooth module can be integrated on to a host system through a UART connection and provide the designer an optimal solution for Bluetooth enabled systems.
The bluetooth module I will use today is HC-05 which is so familiar and cheap. It is easily available in local indian markets. You can go either offline or online. You may find in about ~600 ₹. May be if you search better, you can get as low as 450 ₹.
HC-05 Bluetooth Module is one of the most popular bluetooth module used in embedded projects. It can be easily interfaced with Arduino Board, Raspberry Pi, Microcontrollers through serial UART interface. HC-05 module is an easy to use Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup.
Serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps Modulation with complete 2.4GHz radio transceiver and baseband. It uses CSR Bluecore 04-External single chip Bluetooth system with CMOS technology and with AFH(Adaptive Frequency Hopping Feature). It has the footprint as small as 12.7mmx27mm. Hope it will simplify your overall design/development cycle.
Specifications
- Frequency : 2.4GHz ISM Band
- Bluetooth Protocol : Bluetooth Specification v2.0 + EDR
- Emission Power : ≤4dBm, Class 2
- Operating Voltage : 3.3V
- Input Power Supply : 3.6V ~ 6V
- Security : Authentication & Encryption
- Working Temperature : -20°C ~ +75°C
- Dimensions : 35.7mm x 15.2mm x 5.6mm
Features
- 3.3V TTL Compatible
- Wide Input Voltage Range : 3.3V ~ 6V
- Supported Baud Rates : 9600, 19200, 38400, 57600, 115200, 230400, 460800
- Auto Connect to Last Device on power as default
- Supports Master and Slave Modes
This module works on AT Commands. Lets have a look at important AT commands.
- KEY: if set to HIGH module goes into command mode for configuration
- RXD: the RXD signal line – 5V safe
- TXD: the TXD signal line – 5V safe
- Vcc: connection for 5V supply voltage
- GND: Ground
- State : Not connected
The Module has two modes of operation namely,
- AT Command Mode.
It is a mode of the module where a set of commands (AT Commands) are used to setup and configure the module. In this mode, the module can’t be detected by other Bluetooth Devices. For this mode take Key pin to HIGH. All the Commands are sent to the module serially as a string. This string must be capital letters (case sensitive) and each command should be ended by “rn”. If the command is successfully understood by the module, the module will reply back with a response string usually contains the string “OK”. Otherwise, the module returns back the error string “ERROR()” with a specific code that defines the type of this error.
- Connection Mode.
In this mode the device can directly communicate with other devices. We can search this device on other devices and can connect via entering the passkey. The default passkey is ‘1234’. In this mode we cannot change its configuration serially, but can only communicate with it. Key pin should be NC for this case.
Microcontroller section:
You can use any of your favorite microcontrollers, but the condition is it should be having atleast one serial port. I am taking widely used PIC microcontroller as an example here. The device code is PIC 16F887.
For that purpose i needed to configure my HC-05 first to be able to communicate with microcontrollers. Because the default baudrate of module was 38400 hence not easy to direct communicate with microcontroller. So i changed few of settings of HC-05 and changed its baudrate. I did it via my Laptop and a USB to TTL converter. You may use any it of any type such as FTDI, Prolific PL2303 or Scilab CP2021. Then we need a terminal program at Laptop side. For the i used RealTerm. You can use Putty, Terminal or HyperTerminal even. If you want download links for them here are some of them:
- RealTerm: http://realterm.sourceforge.net/
- Terminal: https://sites.google.com/site/terminalbpp/
Then connect as per shown below
The connections between HC05 module & the USB converter are simple as below :
HC – 05 MODULE USB-TTL MODULE
VCC ——> 5.0V
GND ——> GND
TXD ——> RXD
RXD ——> TXD
KEY ——> 5 V
So now we will be entered into the AT mode.
Then Open real term and connect the respective COM port with 38400 Baudrate.
Enable CR and LF after each line. Then in transmit window
Send ‘AT’. Then it should reply as OK if everything works fine.
Further you can check its version with command ‘AT+VERSION’. You can get its device address with ‘AT+ADDR?’. But our priority is to check the baudrate first. So enter ‘AT+UART?’ Then it replies as
+UART=<Param>,<Param2>,<Param3>
Picbasic Pro Manual
OK
Param1: baud rate( bits/s) value (Decimal)
Param2:stop bit: 0 —-> 1 bit
1 —-> 2 bits
Param3: parity bit
0 —-> None
1 —-> Odd parity
2 —-> Even parity
We have to set as 9600 baud rate 1 stop bit hence now we will set this configuration.
To set baudrate the syntax is ‘AT+UART=<Param>,<Param2>,<Param3>’
Picbasic Forum
Hence we will send ‘AT+UART=9600,0,0’
So now our module is ready to be connected to microcontroller so remove HIGH logic at Key pin to exit AT mode. Now we are ready to work with microcontroller. Here the schematic is shown with one 16×2 LCD and a serial port connection.
Our task is to type a message on Android device and it should be displayed on LCD of our Hardware. So Lets see the PIC microcontroller program for that below. Later on we will download app for Android to communicate.
void main()
{
//Initialize USART with baud rate 9600
TRISD = 0x00;
USARTInit(9600);
LCDEnable();
__delay_ms(1000);
LCDInitialize();
__delay_ms(1000);
LCDWriteString(“welcome”);
__delay_ms(1000);
USARTWriteString(“Hii.. Bluetooth is readyrn”);
LCDClear();
__delay_ms(100);
while(1)
{
//Get the amount of data waiting in USART queue
uint8_t n= USARTDataAvailable();
//If we have some data
if(n!=0)
{
//Read it
char ch=USARTReadData();
if(ch ‘n’)
{
LCDRow2();
}
LCDWriteData(ch);
}
}
}
Mobile application:
Like we use the Hyperterminal utility to send data over serial in PC, there are similar kind of very useful apps for smartphones also. But they send this data over Bluetooth to other devices. Most of platforms are having this kind of apps. I am focusing on Android here. Phone which i tested was Micromax Canvas HD (Model No: A116) It runs on Android Jelly Bean. But generally speaking most of android phones are capable to do it via these apps.
I used the app called ‘Bluetooth Terminal’ Checkout at PlatStore: https://play.google.com/store/apps/details?id=ptah.apps.bluetoothterminal&hl=en . Check the logo below
Download the app and follow on screen instructions. Now search devices, You will find device named as ‘HC-05’. Connect it with.
Now reset your microcontroller.On your mobile app ,you should see the text as ‘Hii.. Bluetooth is ready’.
Now type any message on your screen and the press send the text will be displayed on the LCD of our hardware board.
See the final view in the app: