Download Interrupts, C Start-Up Module and Simple I/O

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Opto-isolator wikipedia , lookup

Scattering parameters wikipedia , lookup

Two-port network wikipedia , lookup

Power dividers and directional couplers wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Transcript
1
Simple Digital I/O in C
These lecture notes created by Dr. Alex Dean, NCSU
2
In these notes . . .
• Simple Digital I/O
– Port
• Data Direction
• Data
• Drive Capacity
– Use
• Initialization
• Reading
• Writing
– Example
• Echo LEDs
3
Digital Input/Output (I/O) Ports
• The fundamental interfacing
subsystem
– A port bit can be input or
output
– M30626 has 11 Programmable I/O
Ports (total of 87 digital I/O bits)
(P1 through P7 and P9 and P10 are
bidirectional 8-bit ports, while P8
has an an input-only bit)
– For some other MCUs some ports
may be limited to only input or
output
• Direction register sets bit
direction (PD1, etc.)
– 1: Output
– 0: Input (value of direction register
bits after reset)
• Data register holds actual data
– P1 etc.
• M16C62P Hardware Manual,
“Programmable I/O Ports”
4
Programmable I/O Port
Read direction
Read port
Read port
5
Digital I/O Port as Input
enabled, turns on pull-up
when input is 1
0
1
Read direction
Read port
Read port
disabled
enabled
0
1
off
0
1
off
6
Digital I/O Port as Output
disabled
1
0
Read direction
Read port
Read port
enabled
disabled
1
1
1
0
enabled, behave
as inverters
7
Pull-Up Resistors for Inputs
• Used to simplify interfacing with devices with limited signal swing
– M30626 is digital CMOS and is only designed to operate correctly with valid
input voltages (Everything else is illegal and is not guaranteed)
• Logic 1: 0.8 * VCC to VCC, Logic 0: 0V to 0.2 * VCC
– Resistor is used to pull up voltage of signal from device
with two states
• Low resistance to ground
• High resistance (essentially open circuit)
– Pull-up resistor is built into microcontroller to simplify circuit
design and eliminate external components (save money, space,
assembly effort)
• M30626 Pull-Ups
–
–
–
–
–
Controlled in blocks of 4 (upper and lower halves of each port)
Each block is enabled by a bit in PUR0, PUR1 or PUR2
Pull-ups disabled if a port bit is configured as an output
Value typically 120kW, min 66 kW, max 500kW
M16C62P Hardware Manual, “Programmable I/O Ports”.
8
Example in Assembly: P6 Echoes Nibble Data
• Configuring port to desired structure
– Top 4 bits (4-7) of P6 are inputs
• Clear bits 4-7 of PD6
– These inputs need pull-up resistors
• Set bit PU15 of special function register
(SFR) PUR1 to enable pull-ups
• Set bits 0-3 of PD6
Init:
Port 6
– Bottom 4 bits of P6 are outputs
Bits 7-4
or.b #PU15, PUR1
mov.b #00001111b, PD6
Loop: mov.b
shl.b
mov.b
jmp
P6, R0
#-4, R0
R0, P6
Loop
; read inputs
; move bits 7-4 into 3-0
; write outputs
; repeat forever
Bits 3-0
9
C-Level Support for SFR Interfacing
• Renesas has provided C support for MCU special function registers and special
bits in sfr62p.h
–
–
–
–
Original is in Renesas\QSK62P\Sample_Code\Common
File is copied into project directory when you create a new file
Note that these names are lower-case!
Can treat SFR’s (and some bits) as variables
• Examples
– To initialize a GPIO port’s direction, set the port data direction register
• pd0 is the name for SFR PD0
• Write all 0’s (0x00) to it to make it an input port
– pd0 = 0x00;
• Write all 1’s (0xff) to make it an output port
– pd1 = 0xff;
– To read from the port
• data = p0;
– To write to a port
• p1 = data;
– Can even access some bits (use names in sfr62p.h)
• p1_1 = 0;
• n = p0_7;
10
Example in C: P6 Echoes Nibble Data
• Reading and writing data
–
–
–
–
Load data from input port
Move top nibble to bottom
Write data to output port
Jump back to start
• Now let’s invert the data before
writing it out
Load data from input port
Move top nibble to bottom
Invert it (complement)
Write data to output port
Jump back to start
Bits 7-4
Port 6
–
–
–
–
–
#include “sfr62p.h”
#define DIR_OUT (1)
#define DIR_IN (0)
unsigned char a;
pu15 = 1;
/* pd6 = 0xf0; */
pd6_0 = pd6_1 = DIR_OUT;
pd6_2 = pd6_3 = DIR_OUT;
pd6_4 = pd6_5 = DIR_IN;
pd6_6 = pd6_7 = DIR_IN;
while (1) {
a = p6;
a >>= 4;
p6 = a;
}
a = ~a;
Bits 3-0
11
Example Application: Revisiting the Response Timer
•
Requirements
–
–
•
Input
–
•
Switch: active low, needs pull-up
Outputs
–
–
•
Measure time delay between when
program lights LED and user presses
switch
Display delay on LCD
LED: active low, triggers user
LCD: displays time measurement
Development plan
1.
2.
3.
4.
5.
6.
Get a simple program skeleton to compile – follow tutorial 1 or 2 if needed
Verify debugger takes us to main()
Configure and test LED outputs
Configure and test switch inputs
Configure and test LCD
Create and test time delay measurement function