Sie sind auf Seite 1von 3

University of Portsmouth, Department of Electronic and Computer Engineering B222L Microcontrollers and Programmable Logic - Lecture 1 Branislav Vuksanovic

First PIC18F252 Program

//************************************************** // File Name: first.c // Purpose: // This program endlessly toggles the state of // pin 0 of port B (i.e. RB0 pin). // No delay built in yet, so a very high // frequency square wave generated. // B. Vuksanovic 28/09/2010 //**************************************************

#include <p18F252.h>

void main (void) { TRISB = 0x00; while(1) { PORTBbits.RB0 = 1; PORTBbits.RB0 = 0; } }

// configure Port B as output // do it forever // set RB0 to 1 // set RB0 to 0

PIC18F252 Pin Diagram

University of Portsmouth, Department of Electronic and Computer Engineering B222L Microcontrollers and Programmable Logic - Lecture 1 Branislav Vuksanovic

Some remarks to go with pin diagram: Each port has three main registers to control its operation. These registers are: 1. PORT register (reads the logic levels on the pins of the device in input mode, outputs the logic levels on the pins of the device in output mode) 2. The I/O pins direction (input or output) is controlled by the data direction register, called the TRIS register. TRIS<x> controls the direction of PORT<x>. A 1 in the TRIS bit corresponds to that pin being an input, while a 0 corresponds to that pin being an output. An easy way to remember is that a 1 looks like an I (input) and a 0 looks like an O (output). 3. LAT register (output latch)

In output mode, the PORT register is the latch for the data to be output. In input mode, when the PORT is read, the device reads the logic levels present on the I/O pins. This means that care should be taken with read-modify-write commands on the ports and changing the direction of a pin from an input to an output on the fly. If a port pin is configured as output, reading the corresponding port pin using the PORT register, will read the output logic level present on that pin, which will normally be the logic level written to the PORT register in the last write operation. Writing to PORT<x> when it is configured as an input, does not change anything.

Individual Port Details PORTA is a 7-bit wide, bi-directional port (pins RA6 (MSB) to RA0 (LSB)). On Reset, RA5 and RA3:RA0 are configured as analog inputs (reading PORTA will return logic level 0). RA6 and RA4 are configured as digital inputs. The TRISA register controls the direction of the RA pins, even when they are being used as analog inputs. The user must ensure the bits in the TRISA register are maintained set (logic 1) when using them as analog inputs. The RA4 pin is multiplexed with the Timer0 module clock input to become the RA4/T0CKI pin. PORTB is an 8-bit wide, bi-directional port (pins RB7 (MSB) to RB0 (LSB)). On Reset, these pins are configured as digital inputs. PORTC is an 8-bit wide, bi-directional port (pins RC7 (MSB) to RC0 (LSB)). On a Power-on Reset, these pins are configured as digital inputs. Examples of C18 Program Statements (not a complete programs)

All ports can be configured, read or written to, either as groups of bits or as individual bits. C18 compiler allows the use of decimal, binary, octal or hexadecimal constants in the program statements. Example 1: Configure RB7:RB4 pins as digital inputs and RB3:RB0 pins as digital outputs. TRISB = 0xF0; // F0 is hexadecimal constant (11110000 in 8 bit binary) Example 2: Configure RB3 pin as digital input (configuration of other PORTB pins remains unchanged). TRISBbits.TRISB3 = 1; Example 3: Set RB7:RB3 output pins to LOW (logic 0) and RB2:RB0 output pins to HIGH (logic 1). PORTB = 7; // 7 is a decimal constant (00000111 in 8 bit binary)

University of Portsmouth, Department of Electronic and Computer Engineering B222L Microcontrollers and Programmable Logic - Lecture 1 Branislav Vuksanovic

Example 4: Read PORTB input logic levels into a one byte variable result (declared as type char earlier in the program). result = PORTB; Example 5: Configure RC2 and RC1 pins as digital outputs and all other PORTC pins as digital inputs. TRISC = 0b11111001; // 11111001 is a binary constant Example 6: Configure RC0 pin as digital output (configuration of other PORTC pins remains unchanged). TRISCbits.TRISC0 = 0; Example 7: Set RC0 output pin to LOW (logic 0). Toggle RC1 output pin using read-modify-write operation. In the second line below; PORTCbits.RC1 on the right side of =, is the read operation, ~ is the modification and PORTCbits.RC1 on the left is the write operation. PORTCbits.RC0 = 0; PORTCbits.RC1 = ~PORTCbits.RC1; // ~ is the logic complement operator

Accessing the port pins on PIC18F252 bit and byte addressing


(taken from p18F252.h file, a standard C18 compiler header file that defines I/O port names and alternate peripheral pin function names.)

extern volatile near unsigned char extern volatile near union { struct { unsigned RB0:1; unsigned RB1:1; unsigned RB2:1; unsigned RB3:1; unsigned RB4:1; unsigned RB5:1; unsigned RB6:1; unsigned RB7:1; }; struct { unsigned INT0:1; unsigned INT1:1; unsigned INT2:1; unsigned CCP2:1; }; } PORTBbits;

PORTB;

Das könnte Ihnen auch gefallen