View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
As the peripheral signals usually are substantially different from the ones that microcontroller can understand (zero and one), they have to be converted into a pattern which can be comprehended by a microcontroller.
This task is performed by a block for analog to digital conversion or by an ADC.
This block is responsible for converting an information about some analog value to a binary number and for follow it through to a CPU block so that CPU block can further process it.
Up to now, we have considered inputs as being digital in operation, i.e. the input is either a 0 or 1. But suppose we wish to make temperature measurement, but not hot or cold (1 or 0). We may, for example, require to:
Sound a buzzer if the temperature drops below freezing.
Turn a heater on if the temperature is below 18 C.
Turn on a fan if the temperature goes above 25 C.
Turn on an alarm if the temperature goes above 30 C
The microcontroller would read these voltages and convert them to an 8-bit number where 0V is 0 and 5V is 255. i.e. a reading of 51(0x33H) per volt (1 volt) or a resolution or 1/5V, i.e. 1 bit is (5V/255) = 19.6mV
Switch the A/D converter on with ADON, bit 0. This bit turns the A/D on when set and off when clear.
Instruct the PIC to executes a conversion by setting the GO/DONE bit, bit 2. When the GO/DONE bit is set the PIC does an A/D conversion. When the conversion is complete the hardware clears the GO/DONE bit.
Set the particular input to make the measurement from. This is done with two Channel Select bits, CHS0 and CHS1, bit 3 and 4.
Analog to digital conversion (cont..) ADCON0 register ADCS1 ADCS0 - CHS1 CHS0 GO/DONE - ADON Bit 7 Bit 0 1 = A/D on 2 = A/D off 1 = A/D in progress 0 = A/D finished Analog channel select 00 = channel 0, AN0 01 = channel 1, AN1 10 = channel 2, AN2 11 = channel 3, AN3
In ADCON1, A to D Conversion register 1, only bits 0 and 1 are used.
They are the Port Configuration bits, PCFG0 and PCFG1, that determine which of the pins on PORTA will be analog inputs and which will be digital.
To provide a mixture of analog and digital inputs, we have to configure the 16C711 header with ADCON1 = 10, i.e. B’00000010’, to give two analog and two digital inputs
Analog to digital conversion (cont..) Bit 7 Bit 0 A/D Port Configuration bits ADCON1 Register ADCON1 Port configuration - - - - - - PCFG1 PCFG0 Vdd D D D D 11 Vdd D D A A 10 A3 Vref A A A 01 Vdd A A A A 00 Ref A3 A2 A1 A0 PCFG
Example polling program to wait the A/D conversion.
Analog to digital conversion (cont..) bsf ADCON0, ADON ;A/D converter is on bsf ADCON0, GO ;Start A/D conversion ADLoop btfsc ADCON0, GO ;has conversion finished? goto ADLoop ;no, so keep looping etc…. ;yes, so exits loop
SCI is an abbreviation for Serial Communication Interface and, as a special subsystem, it exists on most microcontrollers. When it is not available, as is the case with PIC16F84, it can be created in software.
In order to connect a microcontroller to a serial port on a PC computer, we need to adjust the level of the signals so communicating can take place.
The signal level on a PC is -10V for logic zero, and +10V for logic one. Since the signal level on the microcontroller is +5V for logic one, and 0V for logic zero, we need an intermediary stage that will convert the levels.
One chip specially designed for this task is MAX232. This chip receives signals from -10 to +10V and converts them into 0 and 5V.
Serial Communication (cont..) Connecting a microcontroller to a PC via a MAX232 line interface chip Program Example
rs232init Macro for initializing the pin for transmitting data (TX-pin). Example: RS232init Sendw Sending ASCII value of data found in W register. Example: movlw 't' call Sendw rs232text Sending ASCII value of a specified text Example: rs232 “HELLO WORLD" rs232byte Sending ASCII value of decimal digits of 8-bit variable Example: movlw .123 movwf TXdata rs232byte TXdata ;Send '1', '2', '3'
When rs232.inc file is used, it is necessary to declare variables Rstemp and TXD at the beginning of the main program.
As a demonstration of a serial communication, we have an example which sends text and the content of variable cnt . Text is sent via macro rs232text, while variable cnt is sent via macro rs232byte. This operation repeats itself after 500ms, with incrementing cnt in the process. This example can be easily modified to have button(s) for sending specified data.
A PWM signal is simply a pulse of varying length, in effect a rectangular wave. This is illustrated in Figure 1, which also shows how a servo might react to different PWM inputs.
For example circuit, the maximum voltage outputted will be +5 VDC, and the minimum will be 0 VDC. The length of the pulse generated is some times characterized by a duty cycle.
The duty cycle is the percentage of the signal that the output remains high. For instance, a constant +5V would be equivalent to a 100% duty cycle. A typical square wave output from a function generator has a 50% duty cycle. 0V would correspond to a 0% duty cycle.
Pulse Width Modulation (PWM) – (Cont..) list p= 16f84 ;************************************************************* PORTB EQU 0x06 ; port b equate duty EQU 0x0c ; length of duty cycle temp EQU 0x0d ; length of duty cycle ;************************************************************* c EQU 0 ; status bit to check after subtraction ;************************************************************* ORG 0x000 The only equate of signifficance here is PWM. This register will be used to store the length of the PWM signal to be generated.
Pulse Width Modulation (PWM) – (Cont..) The next portion of code contains the actual instructions that tell the PIC what to do. start MOVLW 0x00 ;load W with 0x00 make port B output TRIS PORTB ;copy W tristate to port B outputs MOVLW 0x00 ;fill w with zeroes MOVWF PORTB ;set port b outputs to low These lines ;set up port B as outputs. ;All outputs are then set to low. rstrt MOVLW d'0' MOVWF PORTB MOVLW d'157' ;Duty cycle length movwf duty After setting up the ports, the main loop is begun. At the beginning of the main loop, all port b pins are set to low just incase they are high when they shouldn't be. The duty cycle is then set to 157 (a 50% duty cycle. 255 corresponds to 100% and 0 corresponds to 0%).
Pulse Width Modulation (PWM) – (Cont..) The next bit of code is the loop for the PWM signal generated at pin B0. The pwm1a loop generates the high portion of the PWM signal. The duty cycle is stored in temp and then the pin is set high. after a pause, temp is decremented and so long as it doesnt reach zero the pause is repeated and temp is decremented again. After temp reaches zero, the code continues.
Pulse Width Modulation (PWM) – (Cont..) DECFSZ temp GOTO pwmb GOTO rstrt END The next portion of code generates the low part of the PWM signal. The value 255 is stored in temp, and the duty cycle is subtracted from this. This gives the remaining length of signal to be generated. Temp is then decremented in the same manner as above, this time with B0 set to low. Once the entire PWM signal has been generated, the code repeats. This code causes a PWM signal to be generated with a duty cycle proportional to the value set. The frequency of the signal can also be adjusted by varying the delay (the number of nop's used).
One common application of pwm signals is motor control.
By varying the duty cycle of a pwm signal sent to a motor, you can vary the effective power of the signal and thereby slow the motor down or speed the motor up depending on how long of a pulse you send to the motor.
The signal generated by the PIC can not be directly connected to the motor, however, because the PIC is unable to handle the power required by the motor.
It is therefore necessary to use a transistor to regulate the flow of current to the motor. A transistor is like an electric switch. When you send a logic high (+5V) to the transistor, it allows current to flow.
When a logic low (0V) is sent, it restricts the flow of current. For digital signals, this means that the signal can be reproduced exactly, except the new signal is scaled up to a much larger current. Figure 2 shows a schematic for controlling a motor using a TIP31 NPN transistor.
As the schematic shows, the output from the pick is wired to the base. The negative terminal of the motor is then connected to the base and the collector is connected to ground. When the PWM otuput from the
PIC is sent to the transistor, it will flip the transistor on and off and subsequently generate the same PWM signal to the motor, allowing you to control the motor with a PWM signal.