Simple Digital I/O in C

11
1 Simple Digital I/O in C Simple Digital I/O in C These lecture notes created by Dr. Alex Dean, NCSU

description

Simple Digital I/O in C. These lecture notes created by Dr. Alex Dean, NCSU. Simple Digital I/O Port Data Direction Data Drive Capacity Use Initialization Reading Writing Example Echo LEDs. In these notes. Digital Input/Output (I/O) Ports. The fundamental interfacing subsystem - PowerPoint PPT Presentation

Transcript of Simple Digital I/O in C

Page 1: Simple Digital I/O in C

1

Simple Digital I/O in CSimple Digital I/O in C

These lecture notes created by Dr. Alex Dean, NCSU

Page 2: Simple Digital I/O in C

2

In these notes . . . In these notes . . . • Simple Digital I/O

– Port• Data Direction

• Data

• Drive Capacity

– Use• Initialization

• Reading

• Writing

– Example• Echo LEDs

Page 3: Simple Digital I/O in C

3

Digital Input/Output (I/O) PortsDigital 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”

Page 4: Simple Digital I/O in C

4

Programmable I/O PortProgrammable I/O Port

Read port

Read port

Read direction

Page 5: Simple Digital I/O in C

5

Digital I/O Port as InputDigital I/O Port as Input

0

1

0

off

off

0disabled

enabled

1

enabled, turns on pull-upwhen input is 1

1Read port

Read port

Read direction

Page 6: Simple Digital I/O in C

6

Digital I/O Port as OutputDigital I/O Port as Output

1

1

01

disabled

enabled

0

disabled

1

enabled, behave as inverters

Read port

Read port

Read direction

Page 7: Simple Digital I/O in C

7

Pull-Up Resistors for InputsPull-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 devicewith 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 120k, min 66 k, max 500k– M16C62P Hardware Manual, “Programmable I/O Ports”.

Page 8: Simple Digital I/O in C

8

Example in Assembly: P6 Echoes Nibble DataExample 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

– Bottom 4 bits of P6 are outputs• Set bits 0-3 of PD6

Init: or.b #PU15, PUR1 mov.b #00001111b, PD6

Loop: mov.b P6, R0 ; read inputsshl.b #-4, R0 ; move bits 7-4 into 3-0mov.b R0, P6 ; write outputsjmp Loop ; repeat forever

Por

t 6

Bits 7-4

Bits 3-0

Page 9: Simple Digital I/O in C

9

C-Level Support for SFR InterfacingC-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;

Page 10: Simple Digital I/O in C

10

#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;

}

Example in C: P6 Echoes Nibble DataExample 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

a = ~a;

Por

t 6

Bits 7-4

Bits 3-0

Page 11: Simple Digital I/O in C

11

Example Application: Revisiting the Response TimerExample Application: Revisiting the Response Timer• Requirements

– Measure time delay between when program lights LED and user presses switch

– Display delay on LCD• Input

– Switch: active low, needs pull-up• Outputs

– LED: active low, triggers user– LCD: displays time measurement

• Development plan1. Get a simple program skeleton to compile – follow tutorial 1 or 2 if needed2. Verify debugger takes us to main()3. Configure and test LED outputs4. Configure and test switch inputs5. Configure and test LCD6. Create and test time delay measurement function