Picbasic Pro Serial Communication Example

On

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.

  1. Picbasic Pro Manual
  2. Picbasic Forum

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
blink1LED flashes on and off one time per second.PAUSE, GOTO, END

Schematic
5 v Voltage
Regulator

Code
PDF
file
blink1.pbp
blink2LED flashes on and off one time per half second using different commands.HIGH, LOW SchematicCode
PDF
file
blink2.pbp
railroadTwo LEDs flash like a railroad crossing.SchematicCode
PDF
file
railroad.pbp
blink3Turns one LED on and off 5 times.FOR..NEXTSchematicCode
PDF
file
blink3.pbp
led_piezoMany patterns of flashing SchematicCode
pdf
file
led_piezo.pbp
bounce1Eight LED's scroll on from the right then to the left then back again.SchematicCode
PDF
file
bounce1.pbp
bounce2Eight LED's scroll off from the right then to the left.SchematicCode
PDF
file
bounce2.pbp
count1Eight LEDs count in binary from 1 to 255 SchematicCode
PDF
file
count1.pbp
count2Display 8-bit
number as
decimal,
hexadecimal,
binary number
on LCD
SchematicCode
PDF
file
count2.pbp
LCD1Prints simple message to LCD.LCDOUTSchematicCode
PDF
file
LCD1.pbp
LCD2Demonstrates several commands to move LCD cursor.SchematicCode
PDF
file
LCD2.pbp
LCD3Display resistance readings from a potentiometer.POTSchematicCode
PDF
file
LCD3.pbp
pic_as_sourcePIC acts as a source output to light an LED SchematicCode PDF file pic_as_source
pic_as_sinkPIC acts as a sink output to light an LED SchematicCode PDF file Pic_as_sink
active_highLED turns on when active high switch is pressed SchematicCode PDF file active_high
active_lowLED turns on when active low switch is pressed SchematicCode PDF fileactive_low
array1Set up two 6 element arrays SchematicCode
PDF
file
array1
array2Find max value in 7 element array SchematicCode
PDF
file
array2
fiber-optic1Sends simple signal through fiber-optic cable SchematicCode PDF file fiber_optic1
shift1Use shift operator << to shift value to left Shift Operator SchematicCode
PDF
file
shift1.pbp
signed_num1Display signed numbers SchematicCode
PDF
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
servo1Rotates servo cwise and ccwise using PULSOUT PULSOUTSchematicCode
PDF
file
servo1.pbp
servo2Rotates servo cwise and ccwise using PAUSEUS PAUSEUSSchematicCode
PDF
file
servo2.pbp
servo3Rotates servo cwise and ccwise in incremental steps Schematic

Code
PDF
file

servo3.pbp
servo4Servo position determined by switch inputs SchematicCode
PDF
file
servo4.pbp
h_bridgePIC drives SN754410 1.5 A driver Schematic
Photo of
Breadboard
(large pdf file)
Code
PDF
file
h_bridge_
sn754410_ 1.pbp
pwm1Drives dc motors at different speeds. Jameco motor1motor2PWMSchematicCode
PDF
file
pwm1.pbp
hpwm1Shows HPWM working in program background HPWMSchematicCode
PDF
file
hpwm1.pbp
hpwm2Changes speeds using HPWM SchematicCode
PDF
file
hpwm2.pbp

pwm_

hpwm1

Runs PWM and HPWM commands together SchematicCode
PDF
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
PDF
file
pwm_
sn754410_1

16F88 Stepper Motor Programs:
Program
Description

New PICBASIC PRO Statements

Code as .pdf
file
step_mot1Drives stepper motor SchematicCode PDF filestep_mot1.pbp
step_mot_
hi_torque
Generates 1.4
times the torque as step_mot1
SchematicCode PDF filestep_mot_
hi_torque.pbp
step_mot_
half_step
Drives stepper motor with twice the steps as step_mot1SchematicCode PDF file step_mot_
half_step.pbp
step_mot_
one_rev
Drives stepper motor to rotate one revolution cw and ccw then stop LOOKUPSchematicCode 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
switch1Turn on/off LED's with button switch.IF..THENSchematicCode
PDF
file
switch1.pbp
switch2Switch drives LED and servo.IF..THEN.. ELSESchematicCode
PDF
file
switch2.pbp

switch_

limit1

Uses limit switches to reverse motor See VideoSchematicCode
PDF
file
switch_limit1
adc18-Bit Analog-to-Digital conversion ADCINSchematicadc1.pbp
adc210-bit Analog-to-Digital conversion SchematicCode
PDF
file
adc2.pbp
adc3Converts 10-Bit data to voltage SchematicCode
PDF
file
adc3.pbp
bend_sensor1Use flex sensor to rotate hobby servo See Video SchematicCode
PDF
file
bend_sensor1
read_voltageReads and displays 0 to +15 volts SchematicCode PDF fileread_voltage
sonar1Displays SRF04 sonar readings to LCD. PULSINSchematicCode
PDF
file
sonar1
sonar2LED's light up as sonar is brought close to an object. SchematicCode
PDF
File
sonar2
dinsmore1Reads Dinsmore 1490 Digital CompassSchematicCode PDF Filedinsmore1
MPX4250Measures water depth with MPX4250AP sensor SchematicCode PDF File mpx4250


16F88 Serial Communications:

Program
Description

New PICBASIC PRO Statements

Code as .pdf
file
serout2_helloDisplay hello on PC terminal program
SEROUT2

Schematic
Code
PDF
file

serout2_hello
serout2_
potentiometer_
8bit
Display 8-bit
potentiometer
reading on PC terminal program

Schematic

Code
PDF
file

serout2_
potentiometer_
8bit
serout2_
potentiometer_
10bit
Display 10-bit
potentiometer
reading on PC terminal program

Schematic

Code
PDF
file

serout2_
pontentiometer_
10bit
serin2_led_on_
off
PC controls
LEDs on PIC
SERIN2
SELECT CASE
SchematicCode
PDF
file
serin2_led_on_off
serin2_pwmPC controls
motor speed
SERIN2 with
Timeout,
Label
SchematicCode
PDF
file
serin2_pwm
serin2_pwm_
wait
PC controls
motor speed
SERIN2 with
WAIT ()
Modifier
SchematicCode
PDF
file
serin2_pwm_wait

multiplex_tx1 &
multiplex_rx1

Control 4 LEDs over fiber-optic cable using 4 variables HSEROUT
HSERIN
Schematictx1.pdf
rx1.pdf
multiplex_tx1
multiplex_rx1
multiplex_tx2 &
multiplex_rx2
Control 4 LEDs over fiber-optic cable using 1 variableSchematictx2.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
Schematictx3.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
blink1LED flashes on and off one time per second.PAUSE, GOTO, END

Schematic
5 v Voltage
Regulator

Code
PDF
file
blink1.pbp
blink2LED flashes on and off one time per half second using different commands.HIGH, LOW SchematicCode
PDF
file
blink2.pbp
railroadTwo LED's flash like a railroad crossing.SchematicCode
PDF
file
railroad.pbp
blink3Turns one LED on and off 5 times.FOR..NEXTSchematicCode
PDF
file
blink3.pbp
bounce1Eight LED's scroll on from the right then to the left then back again.SchematicCode
PDF
file
bounce1.pbp
bounce2Eight LED's scroll off from the right then to the left.SchematicCode
PDF
file
bounce2.pbp
count1Eight LEDs count in binary from 1 to 255 SchematicCode
PDF
file
count1.pbp
LCD1Prints simple message to LCD.LCDOUTSchematicCode
PDF
file
LCD1.pbp
LCD2Demonstrates several commands to move LCD cursor.SchematicCode
PDF
file
LCD2.pbp
LCD3Display resistance readings from a potentiometer.POTSchematicCode
PDF
file
LCD3.pbp


16F877AServo and Motor Control Programs:

Program
Description

New PICBASIC PRO Statements

Code as .pdf
file
servo1Rotates servo cwise and ccwise using PULSOUT PULSOUTSchematicCode
PDF
file
servo1.pbp
servo2Rotates servo cwise and ccwise using PAUSEUS PAUSEUSSchematicCode
PDF
file
servo2.pbp
servo3Rotates servo cwise and ccwise in incremental steps Schematicservo3.pbp
servo4Servo position determined by switch inputs SchematicCode
PDF
file
servo4.pbp
h_bridgePIC drives SN754410 1.5 A driver Schematic
Code
PDF
file
h_bridge_
sn754410_ 1.pbp
pwm1Drives dc motors at different speeds. Jameco motor1motor2PWMSchematicCode
PDF
file
pwm1.pbp
hpwm1Shows HPWM working in program background HPWMSchematicCode
PDF
file
hpwm1.pbp
hpwm2Changes speeds using HPWM SchematicCode
PDF
file
hpwm2.pbp
pwm_
sn754410
Uses PAUSEUS
command to
create PWM
signal to drive
SN754410 1.5 A h-bridge driver
Schematic
Code
PDF
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_mot1Drives stepper motor SchematicCode PDF filestep_mot1.pbp
step_mot_
hi_torque
Generates 1.4
times the torque as step_mot1
SchematicCode PDF filestep_mot_
hi_torque.pbp
step_mot_
half_step
Drives stepper motor with twice the steps as step_mot1SchematicCode PDF file step_mot_
half_step.pbp
step_mot_
one_rev
Drives stepper motor to rotate one revolution cw and ccw then stop LOOKUPSchematicCode PDF file step_mot_
one_
revolution.pbp


16F877A Reading Sensor Programs:

Program
Description

New PICBASIC PRO Statements

Code as .pdf
file
switch1Turn on/off LED's with button switch.IF..THENSchematicCode
PDF
file
switch1.pbp
switch2Switch drives LED and servo.IF..THEN.. ELSESchematicCode
PDF
file
switch2.pbp
adc18-Bit Analog-to-Digital conversion ADCINSchematicadc1.pbp
adc210-bit Analog-to-Digital conversion SchematicCode
PDF
file
adc2.pbp
adc3Converts 10-Bit data to voltage SchematicCode
PDF
file
adc3.pbp
sonar1Displays SRF04 sonar readings to LCD. PULSINSchematicCode
PDF
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

Picbasic Pro Serial Communication Example
  • 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.

Picbasic manual

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:

Related