首页 Microchip PIC系列单片机RS232通讯应用

Microchip PIC系列单片机RS232通讯应用

举报
开通vip

Microchip PIC系列单片机RS232通讯应用 ª 1997 Microchip Technology Inc. DS40160A/3_009-page 1 Microchip Technology Incorporated, has been granted a nonexclusive, worldwide license to reproduce, publish and distribute all submitted materials, in either original or edited form. The author...

Microchip PIC系列单片机RS232通讯应用
ª 1997 Microchip Technology Inc. DS40160A/3_009-page 1 Microchip Technology Incorporated, has been granted a nonexclusive, worldwide license to reproduce, publish and distribute all submitted materials, in either original or edited form. The author has affirmed that this work is an original, unpublished work and that he/she owns all rights to such work. All property rights, such as patents, copyrights and trademarks remain with author. OVERVIEW Digitemp Jr. is a device that is designed to measure and report ambient temperature. When connected to an RS-232 port on any PC, it will periodically measure and report in ASCII form the ambient temperature in degrees Celsius. These temperature readings can be monitored with any terminal program. If the terminal program supports capture to disk, the temperature readings can be saved to disk for further analysis with a spreadsheet program or other data analysis tool. The simple ASCII output format of Digitemp Jr. makes it rel- atively easy to write custom software for receiving, recording, and analyzing ambient temperature data. Best of all, Digitemp Jr. requires no external power sup- plies or batteries. It is powered directly by the RS-232 port. DATA TRANSMISSION SPECIFICATIONS When connected to a PC RS-232 port, Digitemp Jr. will obtain an ambient temperature reading and transmit the reading in degrees Celsius approximately once per second. This reading is transmitted in the following for- mat: (-/H)(T)O.D(CR)(LF) The (-/H) character can either be a negative sign or the hundreds digit of the temperature reading. If the tem- perature reading is between 0 ° C and 99.5 ° C, this char- acter will not be transmitted. Realistically, unless an industrial temperature range PIC12C508 is used, you will not be able to measure ambient temperatures below 0 ° C. Moreover, since an industrial temperature range PIC12C508 can only operate to 85 ° C, you will not be able to measure ambient temperatures above 85 ° C. The T character is tens digit of the temperature reading. If the temperature reading is between -9.5 ° C and 9.5 ° C, this character is not transmitted. The O charac- ter is the ones digit of the temperature reading, and the D digit is the tenths digit of the temperature reading. Since the DS1620 has a resolution of 0.5 ° C, the tenths digit will either be a ‘0’ or a ‘5’. The CR and LF characters are the ASCII carriage return (ASCII code 0x0d) and line feed (ASCII code 0x0a) characters. These characters are used to force the terminal program to print the next temperature reading on the next line on the screen. They also serve as delimiting characters between temperature read- ings, allowing easy importing of a captured data into a spreadsheet program. Examples: 0 ° C will be transmitted as 0.0(CR)(LF) 27.5 ° C will be transmitted as 27.5(CR)(LF) -9.5 ° C will be transmitted as -9.5(CR)(LF) -15.5 ° C will be transmitted as -15.5(CR)(LF) The data is transmitted in asynchronous, NRZ format at 9600 bps with one start bit, one stop bit, eight data bits, and no parity. While neither hardware or software flow control is used, the RTS signal from the PC must be asserted because Digitemp Jr. derives its V+ supply from the PC’s RTS output. Author: Michael Kirkhart GSE. Inc. Farmington Hills MI Email: kirkhart@rust.net Digitemp Junior – An RS-232 Port-Powered Digital Thermometer Sensor Interface Sensor Interface DS40160A/3_009-page 2 ª 1997 Microchip Technology Inc. HARDWARE Digitemp Jr. consists of a Microchip Technology’s PIC12C508 8 pin microcontroller, a Dallas semicon- ductor DS1620 digital thermometer, an RS-232 output driver circuit, and a simple Zener diode voltage regula- tor. The data in/out pin (DQ), the clock pin (CLK), and the device select pin (RST) of the DS1620 are con- nected to the PIC12C508’s GP0, GP1, and GP2 I/O pins, respectively. The RS-232 driver circuit is con- trolled by the PIC12C508’s GP4 I/O pin. The PIC12C508 is configured to use the internal 4MHz RC oscillator as a clock source, and to use the GP3 pin as an MCLR input. The RS-232 driver circuit, which is controlled by the PIC12C508’s GP4 I/O pin, consists of a 2N7000 N- channel MOSFET, a 2N4402 PNP transistor, and sev- eral resistors. The emitter of the 2N4402 is connected to 10V (V+), and the collector is connected to the RS- 232 output. When the PIC12C508’s GP4 I/O pin is low, both transistors are off, and the RS-232 output is pulled to approximately -6V via a 3.01K W resistor connected to V-. This is the RS-232 MARK level, and is interpreted by the PC’s UART as a logic ‘1’. When the PIC12C508’s GP4 I/O pin is high, the 2N7000 MOSFET turns on, which then turns on the 2N4402 PNP transistor. This causes the RS-232 output to go to approximately +6V. This is the RS-232 SPACE level, and is interpreted by the PC’s UART as a logic ‘0’. The V+ source is supplied by the RTS pin on the PC serial port through a 1N5719 Schottky diode, and the V- source is supplied by the TxD pin on the PC serial port through another 1N5719 Schottky diode. These diodes protect the device against reverse voltage. The +5V source is derived from the V+ source using a 2K W resis- tor, a 1N4733A Zener diode (5.1V – 5%), and a 1 m F monolithic capacitor. Two 0.1 m F decoupling capacitors are provided for power supply decoupling for the PIC12C508 and the DS1620. For the purpose of testing, the prototype circuit was constructed with sockets for both a PIC12C508 and a PIC16C54. The software was initially written for a PIC16C54, as a PICMASTER™ emulator with a 16C5X pod was available for debugging. After debug- ging the hardware/software with the PICMASTER, the software was converted for use with a PIC12C508 and re-assembled. This required only very minor changes (adding the oscillator trim instructions and changing the port pin equates), which demonstrates the excellent software scalability of the PIC microcontroller family. SOFTWARE The software consists of an initialization block, a main loop, and subroutines for: • Interfacing with the DS1620 • Asynchronous transmission of one byte of data • Math routines • Delay routines Initialization block Invoked on a CPU reset, this code trims the on-board RC oscillator, sets up the OPTION register, initializes the GPIO register as well as the TRIS register, and sets up the DS1620 configuration register for one-shot, CPU controlled temperature conversion operation. Main loop The main loop, which is executed approximately once per second, starts by sending a START CONVERSION command to the DS1620. It then monitors the DS1620 CONVERSION DONE bit in the configuration register to determine when the conversion is complete. Once the conversion is complete, the results of the tempera- ture conversion are read out of the DS1620. The results of this conversion are converted to ASCII and transmit- ted via the RS-232 output one byte at a time. The value transmitted is in degrees Celsius. After transmission of the temperature data, a 760 millisecond delay is exe- cuted. This delay is used to “pad” the main loop execu- tion time to approximately one second. Finally, the watchdog timer is reset, and the main loop is executed again. Sensor Interface ª 1997 Microchip Technology Inc. DS40160A/3_009-page 3 Subroutines • TSWrInst - this subroutine writes an 8-bit instruc- tion to the DS1620, where the instruction is passed in the w register. • TSWrData - this subroutine writes a 9-bit data value to the DS1620, where the 9-bit value is passed in the temp and temp1 file registers. • TSRdData - this subroutine reads a 9 bit data value from the DS1620. The 9-bit value is returned in the temp and temp1 file registers. • asyncTx - this subroutine, which is a modified version of example code from Microchip AN510, is used to transmit one byte of data in asynchro- nous, NRZ format at 9600 bps. Because of the level inversion that takes place from the PIC’s TxD pin (GP4) to the PC UART’s RxD pin, this routine sets the GP4 output if the bit to be transmitted is a 0, and clears the GP4 output if the bit to be trans- mitted is a 1. • mpy8X8, B2_BCD - these subroutines, which were obtained from Microchip AN526, are used when converting the temperature result from the DS1620 into ASCII. • delay, L_delay - these subroutines are used to generate the various time delays needed. The delay subroutine, which was written by Philip Dou- cet and published in an Electronics Design maga- zine “Software Ideas for Design” section, generates a programmable delay. The delay dura- tion, which is specified in instruction cycles, is passed in the delay_h and delay_l file registers. The L_delay subroutine, is used to generate delays in multiples of 10 milliseconds. For this routine, the length of the delay, in units of 10 milli- seconds, is passed in the w register. FIGURE 1: OVERALL PROGRAM FLOWCHART Reset Trim oscillator Initialize OPTION register Initialize GPIO Initialize DSI620 do Temp Sensor Interface DS40160A/3_009-page 4 ª 1997 Microchip Technology Inc. FIGURE 2: MAIN LOOP (DOTEMP) FLOWCHART Y N Y N Y N Y N Do Temp Clear Negative Flag Send temperature convert command to DS1620 Read DS1620 Status Register Has conversion done bit set? Read temperature conversion result from DS1620 Is MSB of result set? Set negative flag Make temperature reading positive by 2 complimenting lower 8 bits of conversion Multiply conversion result by 5 Convert result to packed BCD Is negative flag set? Transmit ‘_’ out RS-232 output End of Temp Reset the watchdog timer Wait for 760 milliseconds Transmit tenth digit out RS-232 output Transmit CR out RS-232 output Transmit LF out RS-232 output Convert tenth digit to ASCII Transmit on es digit out RS-232 output Transmit ‘_’ out RS-232 output Convert on es digit to ASCII Is Result hundreds digit and tens digit = 0? Is Result hundreds digit = 0? Convert hundreds digit to ASCII Transmit hundreds digit out RS-232 output Transmit tens digit out RS-232 output Convert tens digit to ASCII Sensor Interface ª 1997 Microchip Technology Inc. DS40160A/3_009-page 5 FIGURE 3: SCHEMATIC MICROCHIP TOOLS USED Hardware Development Tools Used: The PICMASTER emulator with a 16C54 pod was used to debug the PIC16C54 test version. Assembler/Compiler version: MPLAB 3.22.00 development software with MPASM version 1.50 +5V +5V VDD+5V +5V V- V+ +5V V+ V- Common (Pin 5 on 9 pin IBM connector) U1IC REF. 8 GND U2 To U1 pin 1 8 To U2 pin 8 1 IC Power and Ground Connections 4 +5V PC RTS pin (+12V) (Pin 7 on 9 pin IBM connector) PC TxD pin (-12V) (Pin 3 on 9 pin IBM connector) PC RxD pin (Pin 2 on 9 pin IBM connector) DQ SClkTxD- Rst- RS232Out U1 PIC12C508 GP5/OSC1 2 GP4/OSC2 3 GP3/MCLR 4 GP0 7 GP2 5GP1 6 R1 10K R5 10K J2 1 U2 DS1620 DQ 1 CLK 2 RST 3 THIGH 7 TCOM 5TLOW 6 C2 .1m F C1 .1m F J3 1 J4 1 D2 1N5819 D1 1N5819 R7 2K D3 1N4733A 5.1V C3 1m F R2 40.2K R4 100K J1 1 R6 10K Q2 2N7000 R3 3.01K Q1 2N4402 Sensor Interface DS40160A/3_009-page 6 ª 1997 Microchip Technology Inc. APPENDIX A: SOURCE CODE ;************************************************ ;* Program for the Digitemp Jr. RS-232* ;* powered PC thermometer * ;* * ;* PIC12C508 Version 1.0 written* ;* 7/20/1997 by Michael Kirkhart* ;* * ;************************************************ list p=12C508 ;specifies 12C508 microcontroller list r=DEC ;specifies decimal radix as default list x=ON ;specifies to expand macros in listing errorlevel 1 ;print warnings and errors only in list file ;************************ ;* General system info * ;************************ ; ;Instruction clock frequency = 4.000 MHz ;Non-branching instruction execution time = 1 microsecond ;Fuse settings: Watchdog timer = ON ; Code Protect = OFF ; Oscillator = INTRC __config 0xff6 ;************************ ;* CPU Register equates* ;************************ IND0 equ 00 ;indirect file register RTCC equ 01 ;real time clock/counter PC equ 02 ;program counter STATUS equ 03 ;status register FSR equ 04 ;file select register (pointer) OSCCAL equ 05 ;internal oscillator fine trim register GPIO equ 06 ;general purpose I/O register ;**************************************** ;* Status register bit definitions * ;**************************************** CARRY equ 0 ;carry/!borrow flag DCARRY equ 1 ;BCD carry/!borrow flag ZERO equ 2 ;zero flag PDOWN equ 3 ;powerdown flag TIMEOUT equ 4 ;watchdog timeout flag ;******************************** ;* GPIO bit definitions * ;* (GPIO port pins are used * ;* to communicate to * ;* DS1620 digital temp * ;* sensor and the external)* ;* PC’s serial port)* ;******************************** TSDQ equ 0 ;DS1620 serial data in/out pin (I/O) TSCLK equ 1 ;DS1620 serial data clock pin (O) TSCS equ 2 ;DS1620 chip select pin (O) PCTXD equ 4 ;RS-232 TxD pin (active low) ;Pin = 1: RxD at PC = 0 ;Pin = 0: RxD at PC = 1 Sensor Interface ª 1997 Microchip Technology Inc. DS40160A/3_009-page 7 ;***************************************** ;* Equates for register files (variables)* ;***************************************** xmtReg equ 0x07 ;asychronous TxD buffer flags equ 0x08 ;register file used as a flag register ;(see equates section for bit defines) delay_l equ 0x09 ;register file for delay value LSB delay_h equ 0x0a ;register file for delay value MSB dly_tmp equ 0x0b ;temp value for delay routine temp equ 0x0c ;temporary register file 0 temp1 equ 0x0d ;temporary register file 1 eye equ 0x0e ;used for loop counting value equ 0x0f ;temperature value value1 equ 0x10 ; storage register files ;register files used by math routines op1_H equ 0x11 ;16 bit operand1 MSB op1_L equ 0x12 ;16 bit operand1 LSB op2 equ 0x13 ;8 bit operand2 res1_H equ 0x14 ;16 bit result1 MSB res1_L equ 0x15 ;16 bit result1 LSB count equ 0x16 ;math routine loop counter mathtmp equ 0x17 ;math routine temp register file R0 equ 0x18 ;file registers R1 equ 0x19 ; used by 16 bit binary to R2 equ 0x1a ; packed BCD routine ;**************************************** ;* Miscelaneous equates (constants) * ;**************************************** ;Port A, B initialization values GPINIT equ 0x01 ;GPIO TRIS initialization value TSDQOUT equ 0x00 ;GPIO TRIS value for writing to DS1620 TSDQIN equ 0x01 ;GPIO TRIS value for reading from DS1620 ;DS1620 command values RDTEMP equ 0xaa ;DS1620 read temperature command WRTH equ 0x01 ;DS1620 write TH register command WRTL equ 0x02 ;DS1620 write TL register command RDTH equ 0xa1 ;DS1620 read TH register command RDTL equ 0xa2 ;DS1620 read TL register command STRTCNV equ 0xee ;DS1620 start temperature conversion command STOPCNV equ 0x22 ;DS1620 stop temperature conversion command WRCONFG equ 0x0c ;DS1620 write config register command RDCONFG equ 0xac ;DS1620 read config register command ;DS1620 config register initialization value TSCFG equ 0x03 ;DS1620 config register initialization value ;(CPU, 1SHOT bits set) ;bit defines for ‘flags’ register file NEGTEMP equ 0 ;flag that indicates temperature is negative NEGSIGN equ 1 ;flag that indicates negative sign is to be ; displayed Sensor Interface DS40160A/3_009-page 8 ª 1997 Microchip Technology Inc. ;delay constants for 1 millisecond delay using delay routine ONEMS_H equ 0x03 ; ONEMS_L equ 0xe8 ; ;delay constants for 10 millisecond delay using delay routine TENMS_H equ 0x27 ; TENMS_L equ 0x10 ; ;delay constants for one asynchronous bit time at 9600 baud ;using delay routine (104 microseconds - 4 cycles to load ; constants in delay registers - 8 cycles to set output = 92 microseconds) B9600_H equ 0x00 ; B9600_L equ 0x5c ; ;************************ ;* Macro definitions * ;************************ CLC macro ;this macro will clear the C flag bcf STATUS,CARRY endm SEC macro ;this macro will set the C flag bsf STATUS,CARRY endm SCC macro ;used after an instruction that affects the C btfsc STATUS,CARRY ; flag, this macro will skip the next endm ; instruction if the C flag is clear SCS macro ;used after an instruction that affects the C btfss STATUS,CARRY ; flag, this macro will skip the next endm ; instruction if the C flag is set SLT macro ;used after a subtract instruction, this macro btfsc STATUS,CARRY ; will skip the next instruction if the result endm ; of the subtraction is < 0 SGE macro ;used after a subtract instruction, this macro btfss STATUS,CARRY ; will skip the next instruction if the result endm ; of the subtraction is >= 0 SEQ macro ;used after an instruction that affects the Z btfss STATUS,ZERO ; flag, this macro will skip the next endm ; instruction if a result is zero SNE macro ;used after an instruction that affects the Z btfsc STATUS,ZERO ; flag, this macro will skip the next endm ; instruction if a result is non-zero ;************************ ;* Start of program* ;************************ ; actual reset vector - instruction at address 0x1ff was movlw XX, where ; XX is the calibration value to be copied into the OSCCAL register org 0 ;start of program memory movwf OSCCAL ;calibrate on-chip oscillator goto start ;jump to start of program ;******************************** ;* Subroutines * ;* These must be located in the* ;* lower 256 bytes of program* Sensor Interface ª 1997 Microchip Technology Inc. DS40160A/3_009-page 9 ;* memory * ;******************************** ;************************ ;* DS1620 Routines* ;************************ ;******************************** ;* Routine to write instruction * ;* (8 bit) to DS1620 * ;* * ;* Calling convention: * ;* * ;* w = instruction to be * ;* written to DS1620 * ;* * ;* Returns: 0 in W register * ;* * ;* Routine modifies W,eye,temp * ;* * ;* NOTE: This routine switches * ;* the DS1620 chip select from * ;* low to high, but leaves it * ;* in a high state so that a * ;* read or write access to the * ;* part can be completed * ;* properly. If a start or * ;* stop conversion instruction * ;* is written (these 2 * ;* instructions require no * ;* additional read or write * ;* access), the calling routine * ;* MUST clear the DS1620 chip * ;* select line by doing a: * ;* bcf GPIO,TSCS * ;******************************** TSWrInst movwf temp ;save instruction in temp register movlw TSDQOUT ;set TSDQ pin tris GPIO ; as output bcf GPIO,TSCLK ;clear TSCLK pin and nop ; wait one instruction cycle bsf GPIO,TSCS ;set TSCS pin movlw 8 ;initialize movwf eye ; loop counter TSWrI1 rrf temp ;rotate LSB into carry bit btfss STATUS,CARRY ;is it set? goto TSWrI2 ; no - goto TSWrI2 bsf GPIO,TSDQ ;set TSDQ pin goto TSWrI3 ; and goto TSWrI3 TSWrI2 bcf GPIO,TSDQ ;clear TSDQ
本文档为【Microchip PIC系列单片机RS232通讯应用】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_730700
暂无简介~
格式:pdf
大小:116KB
软件:PDF阅读器
页数:17
分类:互联网
上传时间:2013-10-23
浏览量:10