Академический Документы
Профессиональный Документы
Культура Документы
Table of Contents
1.0 Introduction............................................................................................................. 1
2.0 Hardware Interfacing to an I/O Port ....................................................................... 1
3.0 Introduction to PWM with the AT90USB12xx ...................................................... 2
4.0 Introduction to Input Capture with the AT90USB12xx ......................................... 3
5.0 Hints on Using the Sonar and Thinking of Design ................................................. 4
1.0 Introduction
The purpose of this document is to provide some direction on using advanced I/O
features such that you should be able to use any I/O features you require with the help of
the Hardware Manual upon completion. This document will focus on Pulse Width
Modulation (PWM) and Input Capture features, as well as an introduction to interrupts
and hardware interfacing with the AT90USB’s I/O ports.
The order of ports on the AT90USB is a little counter intuitive. The following diagram
shows the order of ports on the device:
Figure 2: Port Locations on the AT90USB
As for using the ports, experimentation has proven that these ports will only work when
their power and ground pins are connected in some fashion to the devices they are driving.
When attempting to use the L293D H-Bridge (see the Hardware Interfacing Primer) DC
motor circuit, the AT90USB’s PWM output wasn’t working (nor were any of the ports
for that matter). Once the VDD and ground from PORTB were used to drive the external
circuitry (except for the motor) the circuit worked fine. In short, to properly use each I/O
port the VDD and ground must be somehow connected to the external devices. I speculate
this is for power saving functions, as well as to protect the port when the processor
decides there is nothing attached.
Since then, Input Capture and PWM have been tested and work just fine on the
AT90USB demo board.
Dr. Cheng has posted notes on his Wiki giving suggestions on how to start getting PWM
going. This is an excellent resource as it suggests different registers to become aware of
and quick calculations to determine you the speed and resolution of your PWM output.
Using TIMER0 included in the AT90USB (8-bit timer), the following example code
should give you a hand with how to get the timer working and select your duty cycle for
your pulse width modulation. If you want to use the timer, but not use pulse width
modulation (for example, for simply polling for a count and, say, cycling the onboard
LEDs, simply change the COM0A and COM0B registers to ‘0’ instead of ‘1’. For
TIMER0, PORTB7 is the OCRA (first PWM) output.
#include <avr/io.h>
/*********************************************************
* End initialization of Timer0 for PWM. Timer is now *
* running and PWM is working. To change the speed of the *
* motor, change the value of the OCR0A or OCR0B *
* registers
*
*********************************************************/
for (;;)
{
// Used for testing (keep the program running)
}
return 0;
}
As a more useful input method, we can use the input capture function of one of either
TIMER1 or TIMER3. These pins can be set to sense either a positive or negative edge of
a signal. When the desired edge is detected, a register ICRx (16-bit) saves the count of
the timer at the moment the edge was detected. Additionally, an interrupt can be raised to
signal the processor when the pulse edge has been detected. Sample code using this
technique is listed below.
IPC_LED.c
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
for(;;)
{
//run program forever
}
return 0;
}
ISR (TIMER1_CAPT_vect)
{
PORTD ^= _BV(PORTD7) ^ _BV(PORTD6) ^_BV(PORTD5);
//PORTD4 is the ICP
}
To test this code on your boards, take the ground and VDD leads from PORTA (the ones
with wires soldered in them); we will use them to simulate positive and negative clock
edges on the Input Capture Pin 1 ICP1 (PORTD4). Run and execute the code supplied.
Touch the ground lead from PORTA (PORTA1) to pin PORTD4. Now remove the
ground and touch the VDD lead to this pin (PORTA0). Three (3) LEDs should cycle on. If
you repeat this process (touch the ground and then VDD leads to this port) the LEDs
should cycle off. This is a simple ICP/Interrupt driven code sample.
As for the input capture algorithm needed to accurately time the width (period) of the
echo pulse, I have prepared code for this. As you should think about this for the lab
yourselves, I will supply it later in the week if need be. For now, however, the ISR
routine I have used has the following algorithm:
/******************************************************
* Pulse Detection Algorithm *
*******************************************************
* Detect +ve edge *
* ISR: if Edge Detector = +VE: set to -ve, reset clock*
* Detect -ve edge *
* ISR: if Edge Detector = -VE: set to +ve, get ICR1H/L*
* (length of pulse) *
******************************************************/
Keep in mind with these timing specifications what period you can cover with a clock set
to Fosc (no prescaler). This sonar will time out at 36ms if no signal is receive (out of
range of echo). At 8MHz, a 16bit counter can time up to 8ms maximum.
With this code and the use of the Hardware Manual, you should all be set to finish this
lab. Code can be provided near the deadline if need be (but keep in mind, it will not be
complete so do not count on the code).
Good luck!