Anda di halaman 1dari 14

PIC course

1) Program design - Assignment hand-out 2) Microchip PICs and MPLAB 3) Input and Output Digital and serial I/O 4) Debugging using MPLAB simulator 5) Lab session 6) Analogue input 7) Interrupt programming external and timers 8) Simulator - stimulus and DCMI 9) Test 2 Assignment hand-in
1

PIC course Digital IO


Digital inputs: Switches, detectors (light / sound) etc. Digital outputs: Lights, motors, Digital to Analogue converter, etc.

PIC course Digital IO


PIC ports are multifunction. Port B normally used for digital IO Direction controlled by TRIS register TRISA for port A; TRISB for port B etc. Can set individual bits to in or out Set bit to 1 for input, 0 for output

PIC course Digital IO


Simple output program:
#include <p18cxxx.h> // TRISB and PORTB declarations int counter; void main (void) { counter = 0; TRISB = 0; // configure PORTB for output while (1) { PORTB = counter; // output to PORTB counter++; } }
4

PIC course Digital IO


Simple I/O
#include <p18cxxx.h>
// or #include <p18f452.h>

// for TRISB etc. declarations

int inpPortA; void main (void) { TRISB = 0; // configure port B for output TRISA = 0xFF; // configure port A for input while (1) { inpPortA = PORTA; // read port A PORTB = inpPortA; // output to port B } }
5

PIC course Digital IO


Reading or setting individual bits You can use: PORTBbits.RB2 and TRISBbits.TRISB2 (defined in MCC18/h/p18f452.h) TRISBbits.TRISB2 = 0; // Set PortB bit 2 (RB2) as output.

Or add a definition: #define portBbit2 PORTBbits.RB2 Code: TRISBbits.TRISB2 = 0; PORTBbits.RB2 = 0; portBbit2 = 1;

// Set PortB bit 2 (RB2) as output. // clear port B bit 2 // use definition

// Can give ports useful names e.g. leftMotor


6

PIC course Digital IO


Example: Define PORTB bit 0 as an input BIT -All other bits of PORTB to be output Write a program that reads portB input bit 0 and outputs it on portB bit 4. All other bits should be left alone and unaffected.
7

PIC course Digital IO


Pseudo (pretend) code:
Declare PortBbit0 as integer. Set up port B TRISB = 00000001; Read portB input bit 0 PortBbit0 = PORTBbits.RB0; // 0 or 1 PORTBbits.RB4 = PortBbit0; // output

PIC course Digital IO


Serial I/O

2 pins used. 1 for receive(rx), 1 for transmit (tx)


USART converts byte data to serial Data transmitted down a single wire USART converts back from serial to byte data USART has to be set-up: speed, no. of bits SFRs hold USART configuration, tx and rx data functions available in uart.h to help
9

PIC course Digital IO


When is USART ready?
Interrupt or bit set in SFR - Interrupts later

Receive: RCIF bit (PIR1,bit5) set when data


received Loop on this bit, then read data in RCREG

Transmit: TRMT (TXSTA, bit 1) bit cleared


when OK to transmit loop on this bit Then write data to TXREG
10

PIC course Digital IO


In library routines. Main ones:
OpenUSART - Configure the USART BusyUSART - Is the USART transmitting? DataRdyUSART - is data available? ReadUSART - Read a byte from the USART WriteUSART - Write a byte to the USART. CloseUSART - Disable the USART
11

PIC course Digital IO


Not concerned here with detail of configuring USART

Read data code:


while (!DataRdyUSART()); uartValue = ReadUSART(); // loop until data ready // read data

Write data code:


while(BusyUSART()) ; WriteUSART(TxData); // loop waiting for tx ready // send data
12

PIC course Digital IO


Summary
Digital IO TRIS register determines direction PORT register reads and writes to pins Can read and write individual pins Serial IO Needs to be set up library available to control serial port
13

PIC programming Digital IO


Exercise Do Serial IO exercises, tasks 1 to 3 Week4SerialIO.doc Uses simulator stimulus to inject values from file into serial port
14

Anda mungkin juga menyukai