embedded c lecture 3 2008

Upload: samuel-taylor

Post on 08-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Embedded C Lecture 3 2008

    1/21

    embedded

    Part 3

    C and the PIC Microcontroller

  • 8/6/2019 Embedded C Lecture 3 2008

    2/21

  • 8/6/2019 Embedded C Lecture 3 2008

    3/21

    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

  • 8/6/2019 Embedded C Lecture 3 2008

    4/21

    Analogue to Digital Convertor

    (ADC) Converts an analogue voltage to a digital

    number.

    Resolution the number of bits used to hold theresulting 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)

  • 8/6/2019 Embedded C Lecture 3 2008

    5/21

    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?

  • 8/6/2019 Embedded C Lecture 3 2008

    6/21

    Teds ADC codevoid configure_analogue_ports(void)

    { // Configure analog inputs

    TRISA = 0b11111111; //Pins 2,3,8,9,10 as AN0,AN1,AN5,AN6,AN7ADCON1 = 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

  • 8/6/2019 Embedded C Lecture 3 2008

    7/21

  • 8/6/2019 Embedded C Lecture 3 2008

    8/21

    Connecting a resistive sensor

    5 V

    10k

    220

  • 8/6/2019 Embedded C Lecture 3 2008

    9/21

    Input voltage clamping using

    Zener diode+12V

    -12V

    Zener

    diode

    +5V

    0V

  • 8/6/2019 Embedded C Lecture 3 2008

    10/21

    QRB 1134 Light Sensor

    The phototransistor

    responds to radiation

    from the emitting diode

    only when a reflectiveobject passes within its

    field of view.

    The area of the optimum

    response approximates a

    circle .200 in diameter

    Reflective surface (tape)

    infrared

    emitting

    diode

    NPN silicon

    phototransistor

    Optimum

    distance

    5mm

  • 8/6/2019 Embedded C Lecture 3 2008

    11/21

    Port C

    5v

    220 10k

    C

    QRB1134 Light Sensor

    coloured

    material

  • 8/6/2019 Embedded C Lecture 3 2008

    12/21

    Port C

    switches

    3v

    10k

  • 8/6/2019 Embedded C Lecture 3 2008

    13/21

    Sharp range finder

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

    3.0

    2.5

    2.0

    1.5

    1.0

    0.5

    0

    Output voltage vs distance

    Distance [cm]

    Output

    Voltage

    [v]

  • 8/6/2019 Embedded C Lecture 3 2008

    14/21

    Sharp Rangefinders

  • 8/6/2019 Embedded C Lecture 3 2008

    15/21

    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???

  • 8/6/2019 Embedded C Lecture 3 2008

    16/21

    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

  • 8/6/2019 Embedded C Lecture 3 2008

    17/21

    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.1F to 1F) 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 dont for the robo

    sumo but you do for instrumentation) decouple the power supply using the circuit shown

    below on your 7805 regulator.

  • 8/6/2019 Embedded C Lecture 3 2008

    18/21

    Ranges of different rangefinder

    sensors in Sharp RangeThe GP2XX detectors come in several derivatives. The table below helps tocharacterize 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

  • 8/6/2019 Embedded C Lecture 3 2008

    19/21

    Pulse Width Modulation

    Can be used to control

    power to a motor

    By adding an

    appropriate low passfilter can be used to

    produce slowly varying

    analogue output from

    a digital output pin.

    Duty cycle (%)

    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

    This signal has been filtered but either the LPF cutoff

    needs to b reduced or the period of PWM increased

  • 8/6/2019 Embedded C Lecture 3 2008

    20/21

    Pin setup for PWM example5 V

    10k

  • 8/6/2019 Embedded C Lecture 3 2008

    21/21

    Teds analogue example with PWMmain() {

    configure_pins();while(1) {

    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}

    forever