Tut 15 : Serial data communication

Serial data communication is a communication between microcontroller and other systems like RFID reader, GPS module, GSM module or computer. The communication is done via a RS232 interface. RS232 is a standard for a serial communication interface which allows to send and receive data. With the RS232 interface it is possible to setup a connection between a microcontroller and those units. Sometime the RS232 interface is used for communication between two microcontrollers.
 
 PIC and Computer communication
 
Old computers have their own serial ports (COM), thus the communication will go through these ports. Note that modern PCs don't have a serial port so you need to get a USB to serial converter. They are available at low cost. For serial communication the line used to transmit data is called TX and the line used to receive data is called RX. When sending or recieve commands from a PC to a microcontroller, or vice versa, the informations can be viewed on PC using any hyper termial program. Another way is to send the informations through visual basic.
 Communication blog diagram 
 
 USB to UART converter

PBP Program
'program to send and receive serial data 
 
DEFINE OSC 20
   @ device hs_osc  'jika guna crytal 20Mhz
 
INCLUDE "modedefs.bas"          ' Include serial modes
x VAR Byte(3)
 
loop:                                  
SerIn2 PORTC.7, 84,keyout,[WAIT("A"),STR x\3]    'receive data
SerOut2 PORTC.6,84,[x(0)]     ' Send 1st data
pause 100
SerOut2 PORTC.6,84,[x(1)]     ' Send 2nd data
pause 100
SerOut2 PORTC.6,84,[x(2)]     ' Send 3rd data
pause 100
GoTo loop
End   

Tut 14 : RFID

RFID (Radio-Frequency IDentification) is a small electronic devices that consist of a small chip and an antenna. The chip typically is capable of carrying 2K/4K/8K bytes of data or less. The RFID device serves the same purpose as a bar code or a magnetic strip on the back of a credit card or ATM card since it provides a unique identifier for that object. 

The RFID device must be scanned to retrieve the identifying information from the cards or tags. A significant advantage of RFID devices over the others mentioned above is that the RFID device does not need to be positioned precisely relative to the scanner. 

In contrast, RFID devices will work within a few feet (up to 20 feet for high-frequency devices) of the scanner. Some short distance RFID works below than 10 cm. Some common problems with RFID are reader collision and tag collision. Reader collision occurs when the signals from two or more readers overlap. The tag is unable to respond to simultaneous queries.Systems must be carefully set up to avoid this problem. Tag collision occurs when many tags are present in a small area. But since the read time is very fast, it is easier for vendors to develop systems that ensure that tags respond one at a time.
 
RFID tags may be used as:
  • Animal tracking tags, inserted beneath the skin.
  • Tags can be screw-shaped to identify items, trees or wooden items.
  • Credit-card shaped for use in access applications.
  • Smart attendance and parking system.

 
 RFID reader and tag(transponder)
 

System block diagram


Application of RFID in parking system
 
PBP Program
'program to use RFID reader and tag 
 
DEFINE OSC 20
   @ device hs_osc                         
INCLUDE "modedefs.bas"          ' Include serial modes
DEFINE HSER_RCSTA 90h           ' Definition serial modes utk Hserin
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 57600             ' 57600 Bauds rate
DEFINE HSER_SPBRG 21
DEFINE HSER_CLROERR 1
 
buf VAR BYTE(7) 

' -----[ EEPROM Data ]-----------------------------------------------------
Tag1 Data "AES8915"         ' ID 1
Tag2 Data "PGL9796"          ' ID 2
Tag3 Data "PJT4769"           ' ID 3
Tag4 Data "NAV8915"          ' ID 4
Tag5 Data "BFE6529"          ' ID 5
Tag6 Data "FE6529 "            ' ID 6
Tag7 Data "*BFE652"          ' ID 7
 
loop:
HSerin [WAIT("*"),STR buf\7]    
Check_List:
'checking commands  
Goto loop 


Tut 13 : RF module

RF module is widely used for wireless data transmission. It is a low cost module for short distance especially less than 5 meter . For long distance module, it comes with higher price. It is used in most of remote control applications, remote monitoring and sensing for example in controlling some machines or robots without connecting or use wires. Sometime we can use it for sending a string of codes or data wirelessly, wireless security alarm, digital signal transmission or for household electrical appliances . Normally the transmitter and receiver module operate at 315 MHz or 433 MHz. The common IC found in RF modules are PT2262(encoder/transmitter) and PT2272(decoder/receiver).



 315MHz Transmitter 

 315MHz Receiver

PBP Program
'program to transmite and receive a signal via RF module

DEFINE OSC 20
    @ device hs_osc
INCLUDE "modedefs.bas"          ' Include serial modes

x VAR BYTE
y VAR BYTE
Synk VAR BYTE
Synk=$55   
DEFINE CHAR_PACING 1000

loop:
SerOut PORTC.6,T2400,[Synk,Synk,Synk,Synk,Synk,Synk,9,x]     'transmit
Pause 100
SerIn PORTC.7, T2400,[9],y     'receive
Pause 100
Goto loop 

Tut 12 : PWM (Pulse Width Modulation)

Pulse width modulation (PWM) is a technique for controlling analog circuits with microccontroller digital outputs. PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion. PWM is measured in percent (%) and normally this percentage is also called duty cycle. The PWM strength or duty cycle can be controlled from 0% to 100%.
 
In microcontroller, PWM is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts or 100%) and off (0 Volts or 0%) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you may change or modulate that pulse width.

 
The Interfacing PWM with PIC controller program is very simple and straight forward, which generates a pulse pattern in a particular frequency.

PBP Program
 'program to generate a PWM signal
 
DEFINE OSC 20
@ device hs_osc               'jika guna crytal 20Mhz
 
i VAR BYTE

loop:
FOR i = 1 TO 50
HPwm 1, 64, 1000            '25%, 1kHz PWM signal at PortC.2
PAUSE 100
NEXT i

FOR i = 1 TO 50
HPwm 1, 191, 1000             '75%, 1kHz PWM signal at PortC.2
PAUSE 100
NEXT i

GOTO loop

Tut 11 : Sound in PIC


8 ohm speaker

Circuit setup
 

PBP Program
'program PIC to sound speaker
 
DEFINE OSC 20
   @ device hs_osc                         'jika guna crytal 20Mhz
 
i var byte
j var byte
 
sound1:
 FOR i = 1 TO 100
    SOUND PORTB.3, [120,2,124,2,123,2,121,2]            ' first sound
 NEXT i

 FOR j = 200 to 210 step 1
 SOUND PORTB.3,[100,10,60,15,120,10,73,10]          'second sound alarm
 PAUSE 400
 SOUND PORTB.3,[120,10]
 PAUSE 400
 SOUND PORTB.3,[120,10]
 PAUSE 400
 SOUND PORTB.3,[120,10]
 NEXT j
GOTO sound1

Tut 10 : Servo motor

The servo motor is widely used in airplane remote controller for moving the rudder and control the acceleration in the car remote controller as well as for steering. The servo motor basically is a high quality geared DC motor equipped with electronic circuit for controlling the DC motor rotation direction and position. Normally, a servo motor can rotate to maximum (clockwise or counterclockwise) of 180 degrees in both direction.

 
The servo motor use PWM signal for controlling the DC motor; unlike normal PWM usually used in ordinary DC motor; this PWM signal is not use for controlling the rotation speed, instead it is use for controlling the motor direction or position. Most servo motor will work well on 50 Hz of PWM frequency; this mean the PWM signal should have a period of 20ms.

 
The electronic circuit inside the servo motor will response to the PWM signal width; the 1ms to 1.5ms PWM width will make the servo motor to turn clockwise (CW), the 1.5ms to 2ms PWM width will make the servo motor to turn counterclockwise (CCW and the 1.5ms PWM width will turn the servo motor to its center.

PBP Program
'Program to run a servo motor clockwise, counterclockwise and center

DEFINE OSC 20 ' using 20MHz clock oscillator
@ device hs_osc

i VAR BYTE
data VAR PORTD.0

loop:
data=0
For i=1 TO 100            'CW
PULSOUT data,500
PAUSE 19
Next i
 
For i=1 TO 100                'CCW
PULSOUT data,1000
PAUSE 18
Next i
 
For i=1 TO 100                            'Center
PULSOUT data,750
PAUSE 18
Next i
 
Goto loop
End

 

Tut 9 : Stepper motor

A stepper motor is an electromechanical device which converts electrical pulses into discrete mechanical movements. The shaft or spindle of a stepper motor rotates in discrete step increments when electrical command pulses are applied to it in the proper sequence. The motors rotation has several direct relationships to these applied input pulses. The sequence of the applied pulses is directly related to the direction of motor shafts rotation. The speed of the motor shafts rotation is directly related to the frequency of the input pulses and the length of rotation is directly related to the number of input pulses applied. 

A stepper motor rotates with a fixed step angle, with a basic step angle of 0.9° and 1.8° for each clock cycle. A system configuration for high accuracy positioning is shown below. The rotation angle and speed of the stepper motor can be controlled with precise accuracy by using pulse signals from the controller. 
A stepper motor is used to achieve precise positioning via digital control. The motor operates by accurately synchronizing with the pulse signal output from the controller to the driver. Stepper motors, with their ability to produce high torque at a low speed while minimizing vibration, are ideal for applications requiring quick positioning over a short distance.

PBP Program
'Program to run a stepper motor forward and reverse

DEFINE OSC 20                             ' using 20MHz clock oscillator
@ device hs_osc
 
TRISB=0 
i VAR BYTE 
loop:
For i=0 TO 50                                          'forward direction
PORTB=101000
PAUSE 100
PORTB=011000
PAUSE 100
PORTB=010100
PAUSE 100
PORTB=100100
PAUSE 100
Next i
 
For i=0 TO 50                                          'reverse direction
PORTB=100100
PAUSE 100
PORTB=010100
PAUSE 100
PORTB=011000
PAUSE 100
PORTB=101000
PAUSE 100
Next i

Goto loop
End



Tut 8 : Switch and button

For using swith and button, we would like to understand the concept of pull-up and pull-down resistor. A pull up or pull down resistor are used on Input pins to define a state in the case an input does not have anything connected or the connected part is in the define (high impedance) state. Inputs without a defined state have the problem that the input value can be anything (0 or 1), called floating.

It is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (resistor to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K being a common value.

Pull-up resistor
 
Pull-down resistor 

 
PBP Program
'Program to use pull-up resistor(press switch 1) to ON LED1 and pull-down resistor(press switch 2) to ON LED2
 
DEFINE OSC 20 ' using 20MHz clock oscillator
@ device hs_osc
sw1 var PORTD.2
sw2 var PORTD.3 
LED1 var PORTB.6  
LED2 var PORTB.7        

loop:
If (sw1=0) Then                   'sw1 is pressed, pull-up resistor
High LED1
Pause 100                       
End If
 
If (sw2=1) Then                  'sw2 is pressed, pull-down resistor             
High LED2 
Pause 100
End If
 
Goto loop
End
 

Tut 1 : Basic Laws and Assignment 1


Tutorial 1


Assignement 1



Tutorial Schedule and Room

Schedule
Group 5 : Every Friday  10am to 11am
Group 6 : Every Friday   11am to 12pm


Location
Power Electronic Lab (Formerly CEDEC Lab, next to Power Lab)
Ground Floor, PPKEE


Please be in tutorial room at the right time and the right place.

Tut 7 : Array

An array is a finite and arranged list of variables of the same type called elements. This type is called the base type. Each element is assigned a unique index so that different elements may have the same value. An array is declared by specifying the type of its elements (called array type), its name and the number of its elements enclosed within brackets:
array VAR BYTE[3]
Elements of an array are identified by their position. Indices go from 0 (the first element of an array) to N-1 (N is the number of elements contained in an array). The compiler must know how many memory locations to allocate when an array is declared and because of that the array size can’t be variable.

Elements of array      Contents of element
      array[0]                           5
      array[1]                  20
      array[2]                   25

In constant arrays, elements can be assigned their contents during array declaration. In the following example, an constant array named VOLTAGE is declared and each element is assigned specific number of volts:

 VOLTAGE VAR BYTE[5] = (5,20,25,16,12)

The number of assigned values must not exceed the specified array length, but can be less. In this case, the trailing ‘excess’ elements will be assigned zeroes.
PBP Program
' Example program to detect the maximum value in array

DEFINE OSC 20
@ device hs_osc 'jika guna crytal 20Mhz

Vin VAR BYTE[5]
i VAR BYTE
Vin_max VAR BYTE

Vin[0]=2
Vin[1]=150
Vin[2]=50
Vin[3]=245
Vin[4]=25

Vin_max=0

loop:
For i = 0 to 4
If Vin[i] > Vin_max THEN
Vin_max = Vin[i]
Pause 1000
End If
Next i

Goto loop
End
      


Tut 6 : Analog to Digital Conversion ADC

Analog to digital conversion (ADC) is necessary when dealing with analog values, they need to be converted to digital values so that the PIC may understand the signals. The signals typically involve many analog signals such as, temperature, speed, pressure, the output of a microphone, etc. Below, we will see how to read an external analog signal using a PIC microcontroller and display the conversion output (a digital number) on a bargraph LED. The input analog signal will be a varying voltage between 0-5V derived using a potentiometer. The output will be the equivalent signal shown in 8-bit binary sequence. The minimum output is 0 (binary 00000000) and the maximum output is 255 (11111111) as shown below.
Circuit Setup
PBP Program
' Example program to display analog value on bargraph LED

DEFINE OSC 20
   @ device hs_osc  'jika guna crytal 20Mhz

' -----[ Define ADC ports]-----------
define adc_bits 8       'define number of bits in result
define adc_clock 3      'clock source3=RC
DEFINE ADC_SAMPLEUS 50  'set delay 50us for every adc inputs

TRISB=0

'Allocate variables
x VAR Byte
ADCON0 = %10000010
ANSEL = 000001
  
loop:
 
ADCIN 0, x           ' Read voltage of the voltage sensor circuit port A.0 ,voltage divider pot 5k
PORTB=x
Pause 10

GoTo loop             ' Do it forever
End