1 / 11

Simple Digital I/O in C

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

Download Presentation

Simple Digital I/O in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Simple Digital I/O Port Data Direction Data Drive Capacity Use Initialization Reading Writing Example Echo LEDs In these notes . . .

  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. Read direction Read port Read port Programmable I/O Port

  5. Digital I/O Port as Input enabled, turns on pull-up when input is 1 0 1 Read direction 1 off 0 Read port 0 disabled 1 off Read port enabled

  6. Digital I/O Port as Output disabled 1 0 Read direction 1 Read port 1 enabled 0 Read port 1 enabled, behave as inverters disabled

  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 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 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 • Bottom 4 bits of P6 are outputs • Set bits 0-3 of PD6 Bits 7-4 Port 6 Bits 3-0 Init: or.b #PU15, PUR1 mov.b #00001111b,PD6 Loop: mov.b P6, R0 ; read inputs shl.b #-4, R0 ; move bits 7-4 into 3-0 mov.b R0, P6 ; write outputs jmp Loop ; repeat forever

  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 #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; } • 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 a = ~a; Port 6 Bits 3-0

  11. Example 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 plan • 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

More Related