140 likes | 156 Views
Peripherals & I/O lines. All the on-chip peripherals are configured and controlled through Special Function Registers (SFR) Many of the SFR’s are bit addressable The 111 I/O lines are arranged into Ports
E N D
Peripherals & I/O lines • All the on-chip peripherals are configured and controlled through Special Function Registers (SFR) • Many of the SFR’s are bit addressable • The 111 I/O lines are arranged into Ports • Some port lines have multiple functions which must be configured for correct operation of the required function.
Infineon 167 I/O Ports • 111 I/O lines with individual bit addressability • Tri-stated in input mode • Selectable input thresholds (not on all pins) • Push/pull or open drain output mode • Programmable port driver control
Digital I/O Ports • Port is where data enters/leaves the system • Digital I/O lines are normally grouped into 8-bit ports or possibly 16-bit ports • Ports must be configured for input or output before they are used • Most ports allow a mixture of inputs and outputs on the same port • Some microcontrollers have instructions that access individual bits of a port
M167 digital I/O ports • Associated with a port is a DIRECTION register and on some a Port output control • Port direction - '0' = input, '1' = output • Output control – '0' = Push/pull, '1' = open-drain • In Keil 'C' all the SFR's are defined in the header file "c167cs.h" with the Infineon standard names • Px – is Port x • DPx – is the Direction for Port x • ODPx = Open Drain Control for Port x
Output Driver Types Push/Pull Output Driver Open-Drain Output Driver
Using all bits of ports • Outputs • DP4 = 0xff; // Direction all outputs • P4 = value8; // output value to all 8-bits • DP2 = 0xffff; • P2 = value16; • Inputs • DP6 = 0x00; //Direction all inputs • x = P6; // Read all 8-bits into variable x • x would normally be an unsigned char but could be unsigned int
Bit Manipulation in 'C' • 'C' bit operators (binary operators) OP symbol Description & AND | OR ^ XOR >> n shift bits right n places << n shift bits left n places • y = y OP z; can be written as y OP= z; E.g.'s y = y & 0x80; y = y >> 4; y &= 0x80; y >>= 4; • unary operator ~ complements (invert all bits) e.g. y = ~y;
Masking to achieve bit manipulations • Masking uses AND and OR operators • Use OR ('|') to set bits to '1' • Use AND ('&') to set bits to 0
Controlling output bits – Output Masking • Setting bits to 1 using OR (|) • General format: Port = Port | mask; • Mask has '1' to set a bit, '0' to leave bit unchanged. Example: Set bit 3 to '1' P4 = P4 | 0x08; // 0x08 = 00001000 binary • Setting bits to 0 using AND (&) • General format: Port = Port & mask; • Mask has '0' to clear a bit, and '1' to leave bit unchanged. Example: Set bit 3 to '0' P4 = P4 & 0xf7; // 0xf7 = 11110111 binary
Input Masking • Use AND (&) and a mask to isolate a selected bit • Used in input Polling to test an individual input bit • Loop until a bit becomes logic '1' while( (P4 & 0x08) == 0) { ; } • Two possible results 00000000 or 00001000 • I.e. zero or non-zero • Loop until a bit becomes logic '0' while( (P4 & 0x08) != 0) { ; } • Often written as while( P4 & 0x80); // in 'C' zero is FALSE, non-zero is TRUE
bit variables • The Keil C compiler supports the bit addressable features of Infineon microcontrollers • General declaration format sbit bitname = Portx^y; // port x bit y E.g. sbit motor_dir = DP2^10; sbit motor = P2^10; • MUST declare sbit's before 'C' main() function • Using sbit's motor_dir = 1; // Set bit to output motor = 1; //turn on motor