0% found this document useful (0 votes)
134 views

Embedded: C and The PIC Microcontroller

http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051

Uploaded by

Samuel Taylor
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Embedded: C and The PIC Microcontroller

http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051http://www.scribd.com/doc/7031769/8051

Uploaded by

Samuel Taylor
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

cembedded

Part 3
C and the PIC Microcontroller
PIC18F4520 (40 Pin PDIP)
Port A
Port B

Port E

PORT D
Port A

Port C Port C

PORT D PORT D
PIC PORTS
• Port A – Digital I/O and Analogue in
• Port B – Digital I/O and Analogue in
• Port C – Serial (RS-232 / SPI/ I2C)
• Port D – Digital I/O
• Port E – More Analogue
Analogue to Digital Convertor
(ADC)
• Converts an analogue voltage to a digital
number.
– Resolution – the number of bits used to hold the
resulting number. 8 bit, 10 bit, 12 bit, 16 bit, 24 bit.
(fixed for a particular ADC)
– Sampling rate – how frequently the analogue
voltage is sampled. (set by micro)
• The ADC on the PIC has a resolution of 10 bits
(how many possible states)
ADC examples
1. your input voltage that can vary in the range 0-
5 volts. your 10-bit ADC reads the value 512.
What voltage is it reading?
2. you have a voltage that can vary in the range
0-5 volts. your 10-bit ADC reads the value 12.
What voltage is it reading?
3. What number would you expect to come out of
the ADC in the above examples if you put in a
d.c. value of 1.00 volts?
Ted’s ADC code
void configure_analogue_ports(void)
{ // Configure analog inputs
TRISA = 0b11111111; //Pins 2,3,8,9,10 as AN0,AN1,AN5,AN6,AN7
ADCON1 = 0b00000111; // 00 for unimplemented bits, 0 for Vref- //= Vss, 0 for Vref+ = Vdd,
0111 for AD0-7 enabled as analog //inputs
}

unsigned int read_analog_channel(unsigned int n)


{ ADCON0 = n << 2; // Set ADCON0 to specified channel, unset //GO/NOTDONE bit, unset ADON
bit
ADCON0bits.ADON = 1; // Turn on A/D module
for (n=0 ; n<100 ; ++n); // Manual wait acquisition time
ADCON0bits.GO = 1; // Start conversion
while (ADCON0bits.GO); // Wait for conversion to finish
return ADRESH; // Just return 8 MSBs of left justified //sample from ADRESH:ADRESL
}
Main ()
{
configure_analogue_ports();
...
While(1){
X=read_analogue_channel(1);
}
}
Step 1: Testing the ADC
5V

10kΩ
Connecting a resistive sensor
5V

10kΩ

220Ω
Input voltage clamping using
Zener diode
+12V
+5V

0V

Zener
diode
-12V
QRB 1134 Light Sensor
• The phototransistor
responds to radiation
from the emitting diode
only when a reflective
object passes within its
field of view. Optimum Reflective surface (tape)
distance
• The area of the optimum 5mm
response approximates a
circle .200” in diameter
infrared NPN silicon
emitting phototransistor
diode
QRB1134 Light Sensor
5v

220Ω 10kΩ

coloured
material Port C
switches

3v

10k

Port C
Sharp range finder
Output voltage vs distance
3.0

2.5

2.0
Output
Voltage 1.5
[v]
1.0

0.5

0
0 10 20 30 40 50 60 70 80 90 100 110 120 130

Distance [cm]
Sharp Rangefinders
Presentations
• Weight , mass centre of balance / gravity (Jonathan, Conor)
• Wheel positioning and steering (Liam, stephen reilly)
• Combining batteries (John Brady)
• Measuring speed (John Byrne, John Oneill)
• Pressure / touch sensor ideas (Aidan, Martin)
• Finding centre of arena (Paul Devitt, Mark McConnell)
• Measuring load on motor (Illa, Paul)
• Tilt switches and interfacing them to PIC (Ali, Alex)
• Finding your opponent (Gary, Darren)
• Gearing - Billy Purcell, Yumi
• Non destructive weapons (Gra???
joystick interface
1 potentiometer common (Joy A)
2 button 1 (Joy A)
3 X coordinate potentiometer (Joy A)
4 button common (Joy A)
5 button common (Joy B)
6 Y coordinate potentiometer (Joy A)
7 button 2 (Joy A)
8 unused
9 potentiometer common (Joy B)
10 button 1 (Joy B)
11 X coordinate potentiometer (Joy B)
12 MIDI TXD (transmit) (computer ->
midi)
13 Y coordinate potentiometer (Joy B)
14 button 2 (Joy B)
15 MIDI RXD (midi -> computer) J
Multiple ADC inputs
• You can wire as many analog inputs to the PIC but you may notice some strange effects when you do so. The ADC
circuits will affect each others' readings, because all the circuits are drawing from the same +5V source. One thing you
can do to minimize this is by using decoupling capacitors ( 0.1µF to 1µF) for each analog input, to smooth out dips and
surges in the current caused by the other ADC's. Place the capacitors between power and ground, as physically close
to the ADC input as you can get.
• The effect is also reduced if you increase the sampling time and the delay between ADCIN commands
• Finally, it helps to sample at a lower resolution. Sampling at 8 bits instead of 10 will improve the speed and stability of
a multiple ADC program – calculate the resolution that you need.
• You can also (if you have a voltage regulator powering the PIC – which you don’t for the robo sumo but you do for
instrumentation) decouple the power supply using the circuit shown below on your 7805 regulator.
Ranges of different rangefinder
sensors in Sharp Range
The GP2XX detectors come in several derivatives.  The table below helps to
characterize each type by minimum and maximum ranges, as well as whether the
sensor returns a varying distance value or a boolean detection signal:

GPDP12
GPDPD12D
GP2Y0A02YK
GP2Y0A21YK
GP2Y0A700K
GP2D15
GP2Y0D02YK
Pulse Width Modulation
Duty cycle (%)
• Can be used to control
power to a motor
• By adding an
appropriate low pass
filter can be used to
produce slowly varying This signal has been filtered but either the LPF cutoff
needs to b reduced or the period of PWM increased
analogue output from
a digital output pin.

This filtered
Signal would provide
An analogue value which is an
average of the original PWM signal
20% duty cycle-> V =20% of full scale etc
Pin setup for PWM example
5V

10kΩ
Ted’s analogue example with PWM
main() {
configure_pins();
while(1) {
forever
n=read_analogue_channel(0);
set_pwm(1,n);
set_pwm(2,255-n);
}
}

void configure_pins(void)
{configure pins code here}

unsigned char read_analogue_channel(unsigned int channel)


{code to do conversion and get analogue value here}

set_pwm(unsigned char channel, unsigned char value)


{stuff to change PWM duty cycle and send out to pins here}

You might also like