Serial Port Utilities SPBRG =

Similar documents
PIC17C7XX. PIC17C7XX Data Sheet Errata. Voltage. Frequency. Voltage. Frequency. Clarifications/Corrections to the Data Sheet:

Serial Port Utilities

AN602. How to get 10 Million Cycles out of your Microchip Serial EEPROM 10 MILLION CYCLE GUARENTEE INTRODUCTION ENDURANCE. Thi d t t d ith F M k 4 0 4

PIC16C54C/55A/56A/57C/58B

PIC16F872 Rev. A0 Silicon Errata Sheet. As with any windowed EPROM device, please cover the window at all times, except when erasing.

Optimizing Serial Bus Operations with Proper Write Cycle Times

PIC16F87X. PIC16F87X Rev. B3 Silicon Errata Sheet DC SPECIFICATION CHANGES FROM DATA SHEET

PIC16F84A. PIC16F84A Errata Sheet

PIC16C745/765. PIC16C745/765 Rev. A2 Silicon/Data Sheet Errata

ICSP SOCKET MODULE USER S GUIDE

AN514. Software Interrupt Techniques CREATING CONSTANT TIME POLLING INTRODUCTION THEORY OF OPERATION

SEEVAL 32 Quick Start Guide

AN767. Interfacing Microchip's Fan Speed Controllers to a SPI Port STANDARD IMPLEMENTATION INTRODUCTION

PIC16F872 Rev. A2 Silicon Errata Sheet. As with any windowed EPROM device, please cover the window at all times, except when erasing.

PS4200EV. PS4200 Evaluation Kit 1.0 INTRODUCTION. 1.1 Evaluation Kit Contents

M Using Timer1 in Asynchronous Clock Mode

Techniques to Disable Global Interrupts

AN583. Implementation of the Data Encryption Standard Using PIC17C42 KEY SCHEDULE INTRODUCTION THE DATA ENCRYPTION STANDARD

FIGURE 1 - TABLE READ

How to Implement ICSP Using PIC17CXXX OTP MCUs PIC17CXXX IN-CIRCUIT SERIAL PROGRAMMING USING TABLE WRITE INSTRUCTIONS VPP 13V

ICSP Socket Module User s Guide

AN586. Macros for Page and Bank Switching INTRODUCTION

TC74 Serial Temperature Sensor Demo Board User s Guide

MPLAB. Processor Module and Device Adapter Specification. Host-to-Pod Processor Module. Logic Probe Connector Indicator Lights

TC642DEMO FAN CONTROL MODULE FOR TC642/646 FEATURES GENERAL DESCRIPTION BOARD SCHEMATIC

Improving the Susceptibility of an Application to ESD HIGH VOLTAGE POWER SUPPLY 5V POWER SUPPLY PIN VSS

PIC18C601/801. PIC18C601/801 Rev. C0 Silicon/Data Sheet Errata. 3. Module: Interrupts. 1. Module: WDT. 2. Module: I/O

TB042. Interfacing a KEELOQ Encoder to a PLL Circuit THE RF ENABLE OUTPUT OVERVIEW WHY USE A PLL HCS362 INTERFACE INTERFACING TO PLLS

AN537. Serial EEPROM Endurance. Everything a System Engineer Needs to Know About Serial EEPROM Endurance

TB079. Programming Baseline Flash Devices with PICkit 1 PIC12F508/509 AND PIC16F505 PROGRAMMING INTRODUCTION. PICkit 1 FIRMWARE VERSION 2.0.

TB056. Demonstrating the Set_Report Request With a PS/2 to USB Keyboard Translator Example INTRODUCTION THE SET_REPORT REQUEST DESCRIPTORS

TB004. Automatic Calibration of the WDT Time-out Period CONCLUSION INTRODUCTION IMPLEMENTATION FIGURE 1: PROGRAM FLOWCHART

TB082. Understanding Reset Events On The PIC10F20X INTRODUCTION WATCHDOG TIMER OR WDT POWER-ON RESET (POR)

MPLAB ICE. Processor Module and Device Adapter Specification 2.0 TERMINOLOGY CONTENTS 1.0 INTRODUCTION SYSTEM. 2.1 Host to Pod Cable. 2.

AN915. 1K, 2K and 4K Microwire EEPROM Migration WRITE CYCLE TIME INTRODUCTION INITIATING WRITE CYCLES BUS SPEED COMPATIBILITY DIFFERENCES

How to Implement ICSP Using PIC16CXXX OTP MCUs VDD. MCLR/VPP ICSP Connector. To application circuit Isolation circuits

AN759. Interface Control Document for the MHz Anti-Collision Interrogator EXTERNAL INTERFACES SCOPE. Electrical Interfaces.

TC74. General Description. Features. Applications. Functional Block Diagram. Package Types

MPLAB ICE Processor Module and Device Adapter Specification. Host-to-Pod Processor Module. Logic Probe Connector.

TCN75. 2-Wire Serial Temperature Sensor and Thermal Monitor. Package Type. Features. General Description. Applications SOIC TCN75MOA MSOP TCN75MUA

TC64X/TC64XB Fan Control Demo Board User s Guide

M Floating Point to ASCII Conversion

TB066. Temperature Sensor Backgrounder INTRODUCTION ENTER THE TEMPERATURE SENSOR THEN AND NOW. Microchip Technology Inc.

AN536. Basic Serial EEPROM Operation. Basic Serial EEPROM Operation BASIC SERIAL EEPROM OPERATION CONTENTS SERIAL EEPROM APPLICATIONS

TB011. Using SRAM With A PIC16CXXX IMPLEMENTATION INTRODUCTION BLOCK DIAGRAM OF MULTIPLEXED ADDRESS/DATA BUS ON A PIC16C74

TC670. Tiny Predictive Fan Failure Detector. Features. General Description. Applications. Package Type. Typical Application Circuit

TB033. Using the PIC16F877 To Develop Code For PIC16CXXX Devices INTRODUCTION. Stan D Souza, Rodger Richey Microchip Technology Inc.

MCP2140. MCP2140 Rev. A Silicon/Data Sheet Errata. Clarifications/Corrections to the Data Sheet: INTEGRATED OPTICAL TRANSCEIVER

AN237. Implementing a LIN Slave Node on a PIC16F73 INTRODUCTION APPLICATIONS. Resource Usage. Bit Rate. Summary. Process Time AVAILABLE PROCESS TIME

TB026. Calculating Program Memory Checksums Using a PIC16F87X ACCESSING MEMORY INTRODUCTION. PIC16C7X vs. PIC16F87X. Microchip Technology Inc.

TB004. Automatic Calibration of the WDT Time-out Period CONCLUSION INTRODUCTION IMPLEMENTATION FIGURE 1: PROGRAM FLOWCHART

AN1006. Interfacing SPI Serial EEPROMs to PIC18 Devices INTRODUCTION CIRCUIT FOR PIC18F1220 AND 25 SERIES (SPI) DEVICE

Section 40. Introduction (Part IV)

PIC16F72. PIC16F72 Data Sheet Errata. Clarifications/Corrections to the Data Sheet: 1. Module: Core (Product Identification System)

Electromechanical Switch Replacement Smart Switch for Automotive Applications and More

TC1047A Temperature-to-Voltage Converter PICtail Demo Board User s Guide

EVALUATION KIT FOR TC642/TC646/TC647/TC648/TC649 BDC FAN CONTROLLERS

PIC12F752/HV752 Family Silicon Errata and Data Sheet Clarification. DEV<8:0> (1) REV<4:0> Silicon Revision (2)

Section 35. Glossary

SPI Communication with the AR1020 Controller

AN510. Implementation of an Asynchronous Serial I/O IMPLEMENTATION INTRODUCTION. Microchip Technology Inc.

AN551. Serial EEPROM Solutions vs. Parallel Solutions. Serial EEPROM Solutions vs. Parallel Solutions PARALLEL NON-VOLATILE MEMORIES SERIAL EEPROMS

Simplifying External Memory Connections of PIC17CXXX PICmicro Microcontrollers. FIGURE 1: EXTERNAL MEMORY INTERFACE BLOCK DIAGRAM (x16 DEVICES)

TC72 Digital Temperature Sensor PICtail Demo Board User s Guide

AN713. Controller Area Network (CAN) Basics INTRODUCTION CAN PROTOCOL BASICS CAN OVERVIEW

PIC18F6390/6490/8390/8490

Interfacing SPI Serial EEPROMs to Microchip PICmicro Microcontrollers PIC16F877 VDD VSS. Vcc 25XXXXX HOLD SCK

Using the 8-Bit Parallel Slave Port

PIC18CXX2 Rev. B Silicon/Data Sheet Errata. As with any windowed EPROM device, please cover the window at all times, except when erasing.

Electromechanical Timer Replacement

MCP250XX DEVELOPMENT KIT USER S GUIDE

PIC16F630/676. PIC16F630/676 EEPROM Memory Programming Specification 1.0 PROGRAMMING THE PIC16F630/676

PIC16C5X Disassembler

FLASH Memory Programming Specification

Electromechanical Switch Replacement Smart Switch for Car Windscreen Wiper Control

Using the C18 Compiler to Interface Microwire Serial EEPROMs to PIC18 Devices CIRCUIT FOR PIC18F1220 AND 93 SERIES (MICROWIRE) DEVICE PIC18F1220

Using the MSSP Module to Interface SPI Serial EEPROMs with PIC18 Devices PDIP (600 MIL) PIC18F452 VDD VSS. Vcc 25XXX HOLD SCK. Vss

PIC10F220/222 Rev. B Silicon/Data Sheet Errata. Sym. Characteristic Min. Typ Max. Units Conditions

PIC12CE5XXA. In-Circuit Serial Programming for PIC12C5XX OTP MCUs 1.0 PROGRAMMING THE PIC12C5XX. Pin Diagram. 1.1 Hardware Requirements

PIC32MX. PIC32MX Rev. B2 ES Silicon Errata. PIC32MX (Rev. B2 ES) Silicon Errata. 1. Module: Device Reset. 2. Module: Software Device Reset

MPLAB ICD 2 DESIGN ADVISORY. Operational Issues AC GROUNDING

PIC12LF1552 Silicon Errata and Data Sheet Clarification DEV<8:0>

Recommended Usage of Microchip SPI Serial SRAM Devices RECOMMENDED CONNECTIONS FOR 23XXXX SERIES DEVICES VCC 23XXXXX HOLD SCK

Complete CAN Solutions for Diverse Embedded Applications. CAN Solutions

PICMASTER PICMASTER CE

MPLAB IDE v6.xx Quick Start Guide

PIC12F629/675 Family Silicon Errata and Data Sheet Clarification. (1) Revision ID for Silicon Revision (2)

AN519. Implementing a Simple Serial Mouse Controller INTRODUCTION THEORY OF OPERATION FUNCTIONAL BLOCKS OF A SERIAL MOUSE

PIC10F200/202/204/206

MCP215X. MCP215X Rev. A Silicon/Data Sheet Errata. 4. Module: IR Protocol Handler. 1. Module: IR Protocol Handler. 5. Module: Test Escapes

AN543. Tone Generation INTRODUCTION IMPLEMENTATION THEORY. y(n) = 2*K1*y(n-1)-y(n-2) y(n-2) = y(n-1) y(n-1) = y(n)

PIC18F2480/2580/4480/4580

AN891. Interrupt-based PIC18 Master LIN Driver in C for Enhanced USART FILES. Microchip Technology Inc.

TB3010. Maximize Software Portability for Future PIC32 MCUs CASE 1: WRITING TO SFR INTRODUCTION CASE 2: READING FROM SFR. Microchip Technology Inc.

In-Circuit Serial Programming (ICSP ) for PIC16C715 OTP MCUs

16K (2K x 8) CMOS EEPROM I/O0 I/O1 I/O2. Vcc NC NC A7 A6 A5 A4 A3 A Microchip Technology Inc. DS11125G-page 1

93C66A/B. 4K 5.0V Automotive Temperature Microwire Serial EEPROM FEATURES PACKAGE TYPE BLOCK DIAGRAM DESCRIPTION

AN812. Paralleling the TC1121 to Reduce Output Resistance for Driving Higher Load Currents INTRODUCTION SUMMARY SINGLE TC1121 APPLICATION CIRCUIT

59C11. 1K 5.0V Microwire Serial EEPROM PACKAGE TYPES FEATURES DESCRIPTION BLOCK DIAGRAM. This document was created with FrameMaker 404

MSSP MODULE. MSSP Module Silicon/Data Sheet Errata. 1. Module: I 2 C (Slave Mode)

Transcription:

Serial Port Utilities AN547 Author: INTRODUCTION Amar Palacherla Microchip Technology Inc. The PIC17C42 has an on-chip high speed Universal Synchronous Asynchronous Receiver Transmitter (USART). The serial port can be configured to operate either in full-duplex asynchronous mode or half duplex synchronous mode. The serial port has a dedicated 8-bit baud rate generator. Either 8- or 9-bits can be transmitted/received. This application note provides information on using the serial port, parity generation, serial port expansion, RS-232 interface, I/O port expansion using the serial port in synchronous mode. SERIAL PORT USAGE Asynchronous Mode Setup Asynchronous mode setup requires selection of 8/9-bits of data transfer, baud rate, setting the baud rate generator, and configuring the TXSTA and RCSTA control registers. The baud rate generator is configured by writing the appropriate value to SPBRG register (bank0, file 17h). The value to be written to SPBRG is given by: SPBRG = Input_Clk_Freq 64 Baud_Rate For example, to select a baud rate of 9600 bits/sec with input clock frequency of 16 MHz, SPBRG is computed from the above equation to be 25. Once the Baud Rate Generator is set up, it is necessary to configure the TXSTA and RCSTA control registers as shown in Example 1 (refer to the data sheet): -1 A brief code to setup the serial port, and receive and transmit data is given in Example 1. Small sections of code for both asynchronous and synchronous mode are given. EXAMPLE 1: INITIALIZATION EXAMPLE SPBRG : 25 : 9600 baud @ 16 MHz input clock TXSTA : 00100000 (20h) : 8-bit transmission, async mode RCSTA : 10010000 (90h) : 8-bit reception, enable serial port, enable reception ;************************************************************************ ; Sample Code For Asynchronous Mode Serial Port Setup ;************************************************************************ #define ClkFreq 16000000 ; input clock frequency = 16 MHz #define baud(x) ((10*ClkFreq/(64*X))+5)/10-1 #define TXSTA_INIT 0x90 #define RCSTA_INIT 0x90 #include "17C42.h" ; file containing the Register Definitions Setup_Async_Mode movlb 0 ; SPBRG, TXSTA & RCSTA are in bank 0 movlw baud(9600) ; equals 25 for 9600 baud movwf SPBRG ; baud rate generator is reset & initialized movlw TXSTA_INIT movwf TXSTA ; 8-bit transmission, async mode movlw RCSTA_INIT movwf RCSTA ; 8-bit reception, enable serial port, ; enable reception ;*************************************************************************** 1997 Microchip Technology Inc. DS00547C-page 4-1

Synchronous Mode Setup Synchronous mode setup requires selection of 8/9-bits of data transfer, bit rate, setting the baud rate generator, and configuring the TXSTA and RCSTA control registers. The baud rate generator is configured by writing the appropriate value to SPBRG register (bank0, file 17h). The value to be written to SPBRG is given by: SPBRG = Input_Clk_Freq 4 Baud_Rate -1 For example, to select a bit rate of 1 Mbits/sec with input clock frequency of 16 MHz, SPBRG is computed from the above equation to be 3. Once the Baud Rate Generator is set up, it is necessary to configure the TXSTA and RCSTA control registers as follows (please refer to the data sheet) : EXAMPLE 2: CONFIGURING TXSTA AND RCSTA CONTROL REGISTORS SPBRG : 3 : 1 Mbits/sec @ 16 Mhz input clock TXSTA : 10110000 (B0h) : 8-bit transmission, Sync mode (MASTER) RCSTA : 10010000 (90h) : 8-bit reception, enable serial port, continuous reception ;************************************************************************ ; Sample Code For Synchronous Mode (MASTER) Serial Port Setup ;************************************************************************ #define ClkFreq 16000000 ; input clock frequency = 16 Mhz #define baud(x) ((10*ClkFreq/(4*X))+5)/10-1 #define TXSTA_INIT 0xB0 #define RCTSA_INIT 0x90 #include "17C42.h" ; file containing the Register Definitions Setup_Sync_Master_Mode movlb 0 ; SPBRG, TXSTA & RCSTA are in bank 0 movlw baud(1000000) ; equals 3 for 1 Mbits/sec movwf SPBRG ; baud rate generator is reset & initialized movlw TXSTA_INIT movwf TXSTA ; 8-bit transmission, async mode movlw RCSTA_INIT movwf RCSTA ; 8-bit reception, enable serial port, ; enable reception ;*************************************************************************** DS00547C-page 4-2 1997 Microchip Technology Inc.

Receiving Data (Software Polling) The sample code in Example 3 provides a way to read the received serial data by software polling (with no serial port interrupts). This applies to both asynchronous and synchronous mode. Software polling is done by checking the RCIF bit (PIR<0>). If this bit is set it means that a word has been received (8 bits are in RCREG and the 9th bit in RCSTA<0>). EXAMPLE 3: POLLING FOR RECEIVE DATA ;******************************************************************* ; Return The 8-bit received Data By Software Polling ; The received data is ed in location SerInData ;******************************************************************* Get_Serial_Data_Poll movlb 1 ; PIR is in bank 1 PollRcv btfss PIR,0 ; check the RBIF bit goto PollRcv ; loop until char received, assume WDT is off movlb 0 ; RCREG is in bank 0 movpf RCREG,SerInData ; Received 8-bits are in SerInData ;********************************************************************* Transmitting Data (Software Polling) The sample code in Example 4 provides a way to transmit serial data by software polling (no serial port interrupts). Software polling is done by checking the bit (PIR<1> in bank 1) to be one, indicating the transfer of TXIF to the serial shift register. EXAMPLE 4: POLLING FOR TRANSMIT DATA ;******************************************************************* ; Transmit 8-bit Data By Software Polling ; The data to be transmitted is in location SerOutData ;******************************************************************* Send_Serial_Data_Poll movlb 1 ; PIR is in bank 1 PollTXIFbtfss PIR,1 ; check the TXIF bit of PIR register in bank1 goto PollTXIF ; loop until char received, assume WDT is off movlb 0 ; RCREG is in bank 0 movfp SerOutData,TXREG ; Received 8-bits are in SerInData ;********************************************************************* 1997 Microchip Technology Inc. DS00547C-page 4-3

Transmitting & Receiving A Block Of Data (Interrupt Driven) A general purpose routine which is interrupt driven, that transmits and receives a block of data is provided in Example 5. The reception or transmission of the block is ended when an end of block character is detected. As an example, the end of block is identified by a 0. The block of data to be transmitted is stored in the program memory and the TABLRD instruction is used to transfer this example data to the file registers and serial port. The user may modify this code to a more general purpose routine that suits the application. Please check the Microchip BBS for the latest version of the source code. Microchip s Worldwide Web Address: www.microchip.com; Bulletin Board Support: MCHIPBBS using CompuServe (CompuServe membership not required). EXAMPLE 5: INTERRUPT DRIVEN TRANSMIT/RECEIVE MPASM 01.40 Released SERINT.ASM 1-22-1997 10:20:49 PAGE 1 LOC OBJECT CODE VALUE LINE SOURCE TEXT 00001 ; TITLE Serial Interface Routines 00002 ; PROCESSOR 42 00003 00004 ;This is a short program to demonstrate how to transmit and receive 00005 ;serial data using the PIC17C42. 00006 ; 00007 ;A message will be transmitted and routed right back to the processor 00008 ;and read. The read information will be saved in an internal buffer. 00009 ; 00010 ; Program: SERINT.ASM 00011 ; Revision Date: 00012 ; 1-22-97 Compatibility with MPASMWIN 1.40 00013 ; 00014 ; 00015 LIST P = 17C42 00016 00017 #include <p17c42.inc> 00001 LIST 00002 ;P17C42.INC Standard Header File, Version 1.03 Microchip Technology, Inc. 00264 LIST 00018 00000080 00019 TX_BUFFER equ 0x80 000000B0 00020 RX_BUFFER equ 0xB0 00000020 00021 RXPTR equ 0x20 00000021 00022 TXPTR equ 0x21 00000022 00023 SERFLAG equ 0x22 00000023 00024 RTINUM equ 0x23 00000001 00025 RXDONE equ 1 00000000 00026 TXDONE equ 0 00000002 00027 HILOB equ 2 00028 ; 00029 ; 0000 00030 ORG 0 0000 C072 00031 goto start 00032 ; 0010 00033 ORG 0x0010 ;vector for tmr0 interrupt 00034 ;tmr0_int ;not used here 00035 ; 0020 00036 ORG 0x0020 ;vector for peripheral interrupt 0020 00037 perf_int 0020 C04D 00038 goto service_perf ;service the interrupts 00039 ; 0030 00040 ORG 0x0030 00041 ; 00042 ;initialize the serial port baud rate interrupts etc. DS00547C-page 4-4 1997 Microchip Technology Inc.

0030 00043 init_serial 0030 2922 00044 clrf SERFLAG, F ;clear all flags 0031 B800 00045 movlb 0 ;select bank 0 0032 B007 00046 movlw 0x07 ;select 9600 baud 0033 7700 00047 movfp W,SPBRG ; / 0034 B090 00048 movlw 0x90 ;set up serial pins 0035 7300 00049 movfp W,RCSTA ; / 0036 2915 00050 clrf TXSTA, F ;setup transmit status 0037 B801 00051 movlb 1 ;select bank 1 0038 2916 00052 clrf PIR, F ;clear all interrupts 0039 2917 00053 clrf PIE, F ;clear all enables 003A 8017 00054 bsf PIE,RCIE ;enable receive interrupt 003B B0B0 00055 movlw RX_BUFFER ;set pointer to rx buffer 003C 4020 00056 movpf W,RXPTR ; / 003D 2907 00057 clrf INTSTA, F ;clear all interrupts 003E 8307 00058 bsf INTSTA,PEIE ;enable peripheral ints 003F 0005 00059 retfie 00060 ; 00061 ;start transmission of first two bytes 0040 00062 start_xmit 0040 B800 00063 movlb 0 ;select bank 0 0041 8515 00064 bsf TXSTA,TXEN ;enable transmit 0042 AB00 00065 tablrd 1,1,W ;load latch 0043 A216 00066 tlrd 1,TXREG ;load high byte 0044 B801 00067 movlb 1 ;select bank 1 0045 00068 empty_chk 0045 9116 00069 btfss PIR,TXIF ;TXBUF empty? 0046 C045 00070 goto empty_chk ;no then keep checking 0047 B800 00071 movlb 0 ;select bank 0 0048 A916 00072 tablrd 0,1,TXREG ;load lo byte 0049 B801 00073 movlb 1 ;select bank 1 004A 8117 00074 bsf PIE,TXIE ;enable transmit interrupts 004B 8222 00075 bsf SERFLAG,HILOB ;set up next for high byte 004C 0002 00076 00077 ; 00078 ; 00079 PAGE 00080 ; 004D 00081 service_perf 00082 ;check for transmit or receive interrupts only 004D 9816 00083 btfsc PIR,RCIF ;RX buffer full? 004E C062 00084 goto service_recv ;yes then service 004F 9116 00085 btfss PIR,TXIF ;TX buffer empty? 0050 C060 00086 goto exit_perf ;no, ignore other int. 0051 00087 service_xmt 0051 9822 00088 btfsc SERFLAG,TXDONE ;all done? 0052 C060 00089 goto exit_perf ;yes then quit 0053 9A22 00090 btfsc SERFLAG,HILOB ;if clr, do low byte 0054 C057 00091 goto rd_hi ;else read high byte 0055 A900 00092 tablrd 0,1,W ;read lo 0056 C058 00093 goto sx_cont ;continue 0057 00094 rd_hi 0057 A200 00095 tlrd 1,W ;read high byte 0058 00096 sx_cont 0058 3A22 00097 btg SERFLAG,HILOB ;toggle flag 0059 B800 00098 movlb 0 ;bsr=0 005A 4016 00099 movpf W,TXREG ;load tx reg 005B 3300 00100 tstfsz W ;last byte? 005C C060 00101 goto exit_perf ;no then cont 1997 Microchip Technology Inc. DS00547C-page 4-5

005D 00102 end_xmt ;else end transmit 005D B801 00103 movlb 1 ;select bank 1 005E 8917 00104 bcf PIE,TXIE ;disable tx interrupt 005F 8022 00105 bsf SERFLAG,TXDONE ;set done flag 0060 00106 exit_perf 0060 8F07 00107 bcf INTSTA,PEIF ;clear peripheral int 0061 0005 00108 retfie 00109 ; 0062 00110 service_recv 0062 9922 00111 btfsc SERFLAG,RXDONE ;RX complete? 0063 C060 00112 goto exit_perf ;exit int 0064 6120 00113 movfp RXPTR,FSR0 ;get pointer 0065 B800 00114 movlb 0 ;select bank 0 0066 6014 00115 movfp RCREG,INDF0 ;load received value 0067 290A 00116 clrf WREG, F ;clr W 0068 3200 00117 cpfsgt INDF0 ;value = 0? 0069 C06D 00118 goto end_recv ;yes then end 006A 1501 00119 incf FSR0, F ;inc pointer 006B 4120 00120 movpf FSR0,RXPTR ;save pointer 006C C060 00121 goto exit_perf ; from int 006D 00122 end_recv 006D 8122 00123 bsf SERFLAG,RXDONE ;set flag 006E 2907 00124 clrf INTSTA, F ;clear all int 006F B801 00125 movlb 1 ;select bank 1 0070 8817 00126 bcf PIE,RCIE ;disable rx interrupts 0071 C060 00127 goto exit_perf ; 00128 PAGE 00129 00130 ; 0072 00131 start 0072 2909 00132 clrf FSR1, F ;assign FSR1 as S.P. 0073 0709 00133 decf FSR1, F ; / 0074 B020 00134 movlw 0x20 ;clear ram space 0075 6100 00135 movfp W,FSR0 ;do indirect addressing 0076 00136 start1 0076 2900 00137 clrf INDF0, F ;clear ram 0077 1F01 00138 incfsz FSR0, F ;inc and skip if done 0078 C076 00139 goto start1 0079 E030 00140 call init_serial ;initialize serial port 007A B000 00141 movlw LOW MESSAGE ;load table pointer 007B 400D 00142 movpf W,TBLPTRL ; / 007C B001 00143 movlw HIGH MESSAGE ; / 007D 400E 00144 movpf W,TBLPTRH ; / 007E E040 00145 call start_xmit ;start transmission 007F 00146 chk_end 007F 9122 00147 btfss SERFLAG,RXDONE ;receive all? 0080 C07F 00148 goto chk_end ;no then keep checking 00149 ; 0081 C081 00150 loop goto loop ;spin wheel 00151 ; 0100 00152 ORG 0x100 0100 00153 MESSAGE 0100 5468 6520 636F 00154 DATA The code is: Tea for the Tillerman 6465 2069 733A 2054 6561 2066 6F72 2074 6865 2054 696C 6C65 726D 616E 0111 0000 00155 DATA 0 00156 ; 00157 ; 00158 END DS00547C-page 4-6 1997 Microchip Technology Inc.

MEMORY USAGE MAP ( X = Used, - = Unused) 0000 : X--------------- ---------------- X--------------- XXXXXXXXXXXXXXXX 0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0080 : XX-------------- ---------------- ---------------- ---------------- 0100 : XXXXXXXXXXXXXXXX XX-------------- ---------------- ---------------- All other memory blocks unused. Program Memory Words Used: 102 Errors : 0 Warnings : 0 reported, 0 suppressed Messages : 9 reported, 0 suppressed 1997 Microchip Technology Inc. DS00547C-page 4-7

PARITY GENERATION Since the serial port of the PIC17C42 does not have an on-chip parity generator, parity is generated using software. It takes only 10 program memory words and executes in 10 instruction cycles to generate parity. Since the serial port of the PIC17C42 can operate in a 9-bit mode, the parity bit can be generated in software and transmitted as the 9th bit or it can be compared with the received 9th bit. In case of transmission, set TX9 to 1 (TXSTA < 6>) to enable 9-bit transmission and write the computed parity bit to TX9D (TXSTA<0>). The 9th bit (parity bit) must be written prior to writing the 8 data bits to TXREG. In case of a reception, first of all enable 9-bit reception by setting RX9 to 1 (RCSTA<6>). Upon successful reception, the 9th bit is received in RX9D (RCSTA<0>). Parity of the 8 bits of received data is computed using the routine listed below and compared with the 9-bit received. Please check the Microchip BBS for the latest version of the source code. Microchip s Worldwide Web Address: www.microchip.com; Bulletin Board Support: MCHIPBBS using CompuServe (CompuServe membership not required). EXAMPLE 6: PRIORITY GENERATION MPASM 01.40 Released PARITY.ASM 1-22-1997 10:17:25 PAGE 1 LOC OBJECT CODE VALUE LINE SOURCE TEXT 00001 TITLE Generate Parity Bit 00002 ; 00003 ;*********************************************************************** 00004 ; Generate Parity Bit for the 8 bit register txmt 00005 ; The parity bit is stored in Bit 0 of parity 00006 ; 00007 ; 00008 ; Program: PARITY.ASM 00009 ; Revision Date: 00010 ; 1-22-97 Compatibility with MPASMWIN 1.40 00011 ; 00012 ;*********************************************************************** 00013 ; 00014 LIST P = 17C42 00015 00016 #include <p17c42.inc> 00001 LIST 00002 ;P17C42.INC Standard Header File, Version 1.03 Microchip Technology, Inc. 00264 LIST 00017 00018 #define TRUE 1 00019 #define FALSE 0 00020 00021 #define ODD_PARITY FALSE 00022 ; 00023 00024 CBLOCK 0x20 00000020 00025 txmt, parity 00026 ENDC 00027 0000 1C20 00028 swapf txmt,w 0001 0C20 00029 xorwf txmt,w 0002 0121 00030 movwf parity 0003 2121 00031 rrncf parity, F 0004 2121 00032 rrncf parity, F 0005 0C21 00033 xorwf parity,w 0006 B503 00034 andlw 0x03 0007 B101 00035 addlw 0x01 0008 210A 00036 rrncf WREG, F 0009 0121 00037 movwf parity DS00547C-page 4-8 1997 Microchip Technology Inc.

00038 ; 00039 #if ODD_PARITY 00040 btg parity,0 00041 #endif 00042 ; 00043 END MEMORY USAGE MAP ( X = Used, - = Unused) 0000 : XXXXXXXXXX------ ---------------- ---------------- ---------------- All other memory blocks unused. Program Memory Words Used: 10 Errors : 0 Warnings : 0 reported, 0 suppressed Messages : 0 reported, 0 suppressed 1997 Microchip Technology Inc. DS00547C-page 4-9

SERIAL PORT EXPANSION The PIC17C42 has only one serial port. For applications that require the PIC17C42 to communicate with multiple serial ports, a scheme that multiplexes and demultiplexes the RX and TX pins is provided below. This method is suitable only if no more than one USART is needed at any one time. This is the case in many applications where the microcontroller drives several output devices serially. Figure 1, shown below suggests a way to expand the on-chip serial port to four serial ports. To use the scheme shown in Figure 1, The PIC17C42 must select the desired serial port by appropriately setting the two pins of PORTB. The same scheme may be used to further expand the serial ports by using more I/O Ports. FIGURE 1: TX PORTB RX PIC17C42 RS-232 INTERFACE MULTIPLEXING THE ON-CHIP USART 1G 1A 1B A B Y C G 1Y0 1Y1 1Y2 1Y3 74HC139 (DE-MUX) 1Y0 1Y1 1Y2 1Y3 74HC151 (MUX) TX0 RX0 TX1 RX1 TX2 RX2 RX3 TX3 FIGURE 2: PIC17C42 TX RX FIGURE 3: C2 C2 C1, C3 = 10 mf, 6.3V C2, C4 = 10 mf, 16V TX RTS <DTR> * Assert DTR Low RX (RS-232) CTS (RS-232) RS-232 INTERFACE TO MAX232 1 3 4 5 11 12 +5V 16 2 14 13 15 6 C3 RS-232 Output C4 Maxim MAX-232 RS-232 Input LOW-COST TWO-CHIP SOLUTION USING SINGLE POWER SOURCE B1 B2 A +5V V- GND MC14C88 +5V INA INB V+ OutB OutA OutB OutA TX (RS-232) RTS (RS-232) RX CTS Two circuits are provided to interface the CMOS levels of a PIC17C42 to the RS-232 levels. Figure 2 provides an interface to MAX232 (MAXIM's RS-232 Driver/Receiver) with a single +5V power supply. Figure 3 provides a low cost two-chip solution for the RS-232 level translation using a single +5V supply (Note that V- of MC14C88 is connected to DTR of RS-232 Interface. By asserting DTR to low, V- gets the negative voltage from the RS-232 line). An alternative single chip low cost solution is provided in Figure 4. However, 3V sources (+5, +12, -12) are necessary. FIGURE 4: GND MC14C89 LOW-COST SINGLE CHIP SOLUTION USING THREE- POWER SOURCES +12V +5V TX VDD 14 VSS 3 TX (RS-232) RX 15 2 RX (RS-232) VSS GND -12V MC145406 DS00547C-page 4-10 1997 Microchip Technology Inc.

Table 1 provides the summary of RS-232 and V.28 Electrical Specifications. TABLE 1: SUMMARY OF RS-232C AND V.28 ELECTRICAL SPECIFICATIONS Parameter Specification Comments Driver Output Voltage 0 level 1 level Max. output +5V to +15V -5V to -15V ±25V Receiver Input Thresholds (Data and clock signals) 0 level 1 level +3V to +125V -3V to -25V With 3-7 kω load With 3-7 kω load No load Receiver Thresholds RTS, DSR, DTR On level Off level +3V to +25V Open circuit or -3V to -25V Receiver Input resistance 3 kω to 7 kω Driver Output resistance Power off condition Driver slew rate Signalling rate Cable length 300Ω Min. 30V/µs max. Up to 20K bits/sec. 50 /15m. Recommended max. length. Detects power Off condition at driver VOUT < ±2V 3 kω < RL < 7k 0pF < CL < 2500 pf Longer cables permissible, if CLOAD 2500 pf I/O PORT EXPANSION USING SYNCHRONOUS MODE Although the PIC17C42 has 33 I/O pins, most of these are multiplexed with other peripheral functions. In cases where more I/O ports are needed, the scheme below expands the I/O port using the synchronous mode of serial port by serially shifting the data. Figure 5 shows a scheme to expand the output ports to 16 bits using two standard logic chips (74HC595). The PIC17C42's serial port is configured in synchronous mode and set to be the MASTER. Thus serial data is available on DT (pin 22) and the clock is available on CK (pin 21). The following code will transmit 16 bits serially and clock all the 16 bits at the same time. FIGURE 5: RX/DT TX/CK PORTB.0 PIC17C42 OUTPUT PORT EXPANSION USING SYNCHRONOUS MODE SI 74HC595 SCLK LCLK SO SI SCLK LCLK 74HC595 QA QB QG QH QA QB QG QH Out 0 Out 0 EXAMPLE 7: PORT IMPORT EXPANSION InitSerialPortTxmt movlb 0 clrf SPBRG ; set to highest baud rate = CLKOUT = CLKIN/4 movlw 0x80 movwf RCSTA ; enable serial port movlw 0xB0 movwf TXSTA ; 8 bit synchronous master mode bcf DDRB,0 ; set bit 0 of PortB as output, to be used as Latch Clk SendSerialData ; shift out DataLo & DataHi serially movlb 0 movfp DataLo,TXREG nop ; wait for TXREG transfer : if slower baud rate is ; used check for TBMT reset movfp DataHi,TXREG wait btfss TXSTA,TRMT ; wait until all 16 bits shifted out goto wait bcf PortB,0 bsf PortB,0 ; clock in the serial data to parallel output of HC595 1997 Microchip Technology Inc. DS00547C-page 4-11

A similar scheme as shown above may be implemented in a reverse manner to expand the input ports. This requires a Parallel In and Serial Out device. Using a standard logic chip (74HC165), the scheme is shown in Figure 6. In order to read the 16-bit input data, the serial port of the PIC17C42 is configured for Synchronous Mode Reception (MASTER mode). An I/O port (PORTB<0>) is used to parallel load the 16 inputs for reading serially. A sample code to read the 16 inputs is shown below. FIGURE 6: RX/DT TX/CK PORTB.0 INPUT PORT EXPANSION USING SYNCHRONOUS MODE SO 74HC165 SCLK PL SO A B G H D0 D7 PIC17C42 SO SCLK A B CLK INHIBIT D0 PL G H D15 74HC165 CLK INHIBIT EXAMPLE 8: InitSerialPortRcv movlb 0 clrf SPBRG ; set to highest baud rate = CLKOUT = CLKIN/4 movlw 0x80 movwf RCSTA ; enable serial port movlw 0x90 movwf TXSTA ; 8-bit synchronous master mode bcf DDRB,0 ; bit 0 of PortB is output, to be used as Parallel ; Load bsf PortB,0 ; disable parallel Load ReadSerialData ; shift out DataLo & DataHi serially movlb 0 bcf PortB,0 ; Parallel Load The Inputs into 74HC165 bsf PortB,0 ; disable parallel Load bsf RCSTA,SREN ; enable single byte reception and wait for data movlb 1 wait1 btfss PIR,RBIF goto wait1 ; check until 8-bits are received movlb 0 movpf RCREG,DataLo ; 1st byte is read movlb 0 bsf RCSTA,SREN ; enable another byte of reception and wait for data movlb 1 wait2 btfss PIR,RBIF goto wait2 ; check until 8-bits are received movlb 0 movpf RCREG,DataHi ; 2nd byte is read DS00547C-page 4-12 1997 Microchip Technology Inc.

Note the following details of the code protection feature on PICmicro MCUs. The PICmicro family meets the specifications contained in the Microchip Data Sheet. Microchip believes that its family of PICmicro microcontrollers is one of the most secure products of its kind on the market today, when used in the intended manner and under normal conditions. There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the PICmicro microcontroller in a manner outside the operating specifications contained in the data sheet. The person doing so may be engaged in theft of intellectual property. Microchip is willing to work with the customer who is concerned about the integrity of their code. Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as unbreakable. Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our product. If you have any further questions about this matter, please contact the local sales office nearest to you. Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. No representation or warranty is given and no liability is assumed by Microchip Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise. Use of Microchip s products as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights. Trademarks The Microchip name and logo, the Microchip logo, FilterLab, KEELOQ, microid, MPLAB, PIC, PICmicro, PICMASTER, PICSTART, PRO MATE, SEEVAL and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. dspic, ECONOMONITOR, FanSense, FlexROM, fuzzylab, In-Circuit Serial Programming, ICSP, ICEPIC, microport, Migratable Memory, MPASM, MPLIB, MPLINK, MPSIM, MXDEV, PICC, PICDEM, PICDEM.net, rfpic, Select Mode and Total Endurance are trademarks of Microchip Technology Incorporated in the U.S.A. Serialized Quick Turn Programming (SQTP) is a service mark of Microchip Technology Incorporated in the U.S.A. All other trademarks mentioned herein are property of their respective companies. 2002, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper. Microchip received QS-9000 quality system certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona in July 1999. The Company s quality system processes and procedures are QS-9000 compliant for its PICmicro 8-bit MCUs, KEELOQ code hopping devices, Serial EEPROMs and microperipheral products. In addition, Microchip s quality system for the design and manufacture of development systems is ISO 9001 certified. 2002 Microchip Technology Inc.

M WORLDWIDE SALES AND SERVICE AMERICAS Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: 480-792-7627 Web Address: http://www.microchip.com Rocky Mountain 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7966 Fax: 480-792-7456 Atlanta 500 Sugar Mill Road, Suite 200B Atlanta, GA 30350 Tel: 770-640-0034 Fax: 770-640-0307 Boston 2 Lan Drive, Suite 120 Westford, MA 01886 Tel: 978-692-3848 Fax: 978-692-3821 Chicago 333 Pierce Road, Suite 180 Itasca, IL 60143 Tel: 630-285-0071 Fax: 630-285-0075 Dallas 4570 Westgrove Drive, Suite 160 Addison, TX 75001 Tel: 972-818-7423 Fax: 972-818-2924 Detroit Tri-Atria Office Building 32255 Northwestern Highway, Suite 190 Farmington Hills, MI 48334 Tel: 248-538-2250 Fax: 248-538-2260 Kokomo 2767 S. Albright Road Kokomo, Indiana 46902 Tel: 765-864-8360 Fax: 765-864-8387 Los Angeles 18201 Von Karman, Suite 1090 Irvine, CA 92612 Tel: 949-263-1888 Fax: 949-263-1338 New York 150 Motor Parkway, Suite 202 Hauppauge, NY 11788 Tel: 631-273-5305 Fax: 631-273-5335 San Jose Microchip Technology Inc. 2107 North First Street, Suite 590 San Jose, CA 95131 Tel: 408-436-7950 Fax: 408-436-7955 Toronto 6285 Northam Drive, Suite 108 Mississauga, Ontario L4V 1X5, Canada Tel: 905-673-0699 Fax: 905-673-6509 ASIA/PACIFIC Australia Microchip Technology Australia Pty Ltd Suite 22, 41 Rawson Street Epping 2121, NSW Australia Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 China - Beijing Microchip Technology Consulting (Shanghai) Co., Ltd., Beijing Liaison Office Unit 915 Bei Hai Wan Tai Bldg. No. 6 Chaoyangmen Beidajie Beijing, 100027, No. China Tel: 86-10-85282100 Fax: 86-10-85282104 China - Chengdu Microchip Technology Consulting (Shanghai) Co., Ltd., Chengdu Liaison Office Rm. 2401, 24th Floor, Ming Xing Financial Tower No. 88 TIDU Street Chengdu 610016, China Tel: 86-28-6766200 Fax: 86-28-6766599 China - Fuzhou Microchip Technology Consulting (Shanghai) Co., Ltd., Fuzhou Liaison Office Unit 28F, World Trade Plaza No. 71 Wusi Road Fuzhou 350001, China Tel: 86-591-7503506 Fax: 86-591-7503521 China - Shanghai Microchip Technology Consulting (Shanghai) Co., Ltd. Room 701, Bldg. B Far East International Plaza No. 317 Xian Xia Road Shanghai, 200051 Tel: 86-21-6275-5700 Fax: 86-21-6275-5060 China - Shenzhen Microchip Technology Consulting (Shanghai) Co., Ltd., Shenzhen Liaison Office Rm. 1315, 13/F, Shenzhen Kerry Centre, Renminnan Lu Shenzhen 518001, China Tel: 86-755-2350361 Fax: 86-755-2366086 Hong Kong Microchip Technology Hongkong Ltd. Unit 901-6, Tower 2, Metroplaza 223 Hing Fong Road Kwai Fong, N.T., Hong Kong Tel: 852-2401-1200 Fax: 852-2401-3431 India Microchip Technology Inc. India Liaison Office Divyasree Chambers 1 Floor, Wing A (A3/A4) No. 11, O Shaugnessey Road Bangalore, 560 025, India Tel: 91-80-2290061 Fax: 91-80-2290062 Japan Microchip Technology Japan K.K. Benex S-1 6F 3-18-20, Shinyokohama Kohoku-Ku, Yokohama-shi Kanagawa, 222-0033, Japan Tel: 81-45-471-6166 Fax: 81-45-471-6122 Korea Microchip Technology Korea 168-1, Youngbo Bldg. 3 Floor Samsung-Dong, Kangnam-Ku Seoul, Korea 135-882 Tel: 82-2-554-7200 Fax: 82-2-558-5934 Singapore Microchip Technology Singapore Pte Ltd. 200 Middle Road #07-02 Prime Centre Singapore, 188980 Tel: 65-334-8870 Fax: 65-334-8850 Taiwan Microchip Technology Taiwan 11F-3, No. 207 Tung Hua North Road Taipei, 105, Taiwan Tel: 886-2-2717-7175 Fax: 886-2-2545-0139 EUROPE Denmark Microchip Technology Nordic ApS Regus Business Centre Lautrup hoj 1-3 Ballerup DK-2750 Denmark Tel: 45 4420 9895 Fax: 45 4420 9910 France Microchip Technology SARL Parc d Activite du Moulin de Massy 43 Rue du Saule Trapu Batiment A - ler Etage 91300 Massy, France Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany Microchip Technology GmbH Gustav-Heinemann Ring 125 D-81739 Munich, Germany Tel: 49-89-627-144 0 Fax: 49-89-627-144-44 Italy Microchip Technology SRL Centro Direzionale Colleoni Palazzo Taurus 1 V. Le Colleoni 1 20041 Agrate Brianza Milan, Italy Tel: 39-039-65791-1 Fax: 39-039-6899883 United Kingdom Arizona Microchip Technology Ltd. 505 Eskdale Road Winnersh Triangle Wokingham Berkshire, England RG41 5TU Tel: 44 118 921 5869 Fax: 44-118 921-5820 01/18/02 2002 Microchip Technology Inc.