Using RS232 with AUSART on 16F88

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
econnections
Posts: 35
Joined: Fri Dec 26, 2008 4:02 pm
Contact:

Using RS232 with AUSART on 16F88

Post by econnections »

Hello,

Ref: Tutorial 45 in 'Assembly for PICMicro v4', v3 Development Board, 16F88 (19.6608Mhz)

I been working with this tutorial and used the two routines to get the letter 'i' to appear on pin 11, RB5. A loop and delay have been added to the 'i' appears every 500ms. When the MAX232 chip arrives I should be able to connect the PIC to the PC and see the character string appearing in the hyperterminal.

Q1. The tutorial says you should be able to data being output on PORTC, pins RC5 and RC6? I'm using the 16F88 so were these notes written for the 16F877 which I believe does have a PORTC? If this is obvious please note I am a newbie and still learining.

Q2. Is there any drawings for the PORTC and PORTD connectors on the v3 development board? I was trying to find out if they were connected to any of the ports on the PICs or whether I could pickup the RB5 signal from them?

Q3. Can you supply a simple example for receiving RS232 data using the 16F88 from the PC? At this stage I only want to send a number from the PC that will appear on PORTA etc.

Any help would be greatly appreciated


For reference here is the test code to transmit the letter 'i'

Code: Select all

;
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
; Configuration data 
; PICmicro MCU type: 16F88 
; Oscillator: HS mode
; LCD display: off 
; 7-segment display: off
; Version 3 board settings: none
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
;
; The following lines embed configuration data into the PICmicro
	LIST P=16F88
	__CONFIG H'2007', H'3F6A'       ; HS mode
	__CONFIG H'2008', H'3FFC'       ; Clock Fail-Safe disabled
	
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

#DEFINE	BANK0	BCF	STATUS,5
#DEFINE	BANK1	BSF	STATUS,5				
				
OPSHUN	EQU	H'81'		;
PCL	EQU	H'02'		;
STATUS	EQU	H'03'		;
PORTA	EQU	H'05'		;
TRISA	EQU	H'85'		;
PORTB	EQU	H'06'		;
TRISB	EQU	H'86'		;
INTCON	EQU	H'0B'		;

PCLATH          EQU H'0A'               ; Bank 0, 1, 2, 3
INTCON          EQU H'0B'               ; Bank 0, 1, 2, 3
PIR1            EQU H'0C'               ; Bank 0
PIE1            EQU H'8C'               ; Bank 1
EEDATA          EQU H'10C'               ; Bank 2
EECON1          EQU H'18C'               ; Bank 3
PIR2            EQU H'0D'               ; Bank 0
EEADR           EQU H'10D'               ; Bank 2
EECON2          EQU H'18D'               ; Bank 3
TMR1L           EQU H'0E'               ; Bank 0
TMR1H           EQU H'0F'               ; Bank 0
T1CON           EQU H'10'               ; Bank 0
CCP1CON         EQU H'17'               ; Bank 0

RCSTA           EQU H'18'               ; Bank 0
TXSTA           EQU H'98'               ; Bank 1
TXREG           EQU H'19'               ; Bank 0
SPBRG           EQU H'99'               ; Bank 1
ANSEL	      EQU H'9B'		; Bank 1
ADRESH          EQU H'1E'               ; Bank 0
ADRESL          EQU H'9E'               ; Bank 1
ADCON0          EQU H'1F'               ; Bank 0
ADCON1          EQU H'9F'               ; Bank 1
TXIE		EQU	4
TXEN		EQU	5
TXIF		EQU	4



	cblock	h'48'
	d1
	d2
	d3
	endc

W	EQU	0		;
F	EQU	1		;
C	EQU	0		;
DC	EQU	1		;
Z	EQU	2		;


				
;************* START OF PROGRAM ***********************			
	
	ORG	0		; Reset vector
	GOTO	5		; Goto start of program
	ORG	4		; Interrupt vector
	GOTO	5		; Goto start of program
	ORG	5		; Start of program memory
	;CLRF	PORTA		
	;CLRF	PORTB		
	;BANK1			; BANK1
	;CLRF	TRISA		
	;CLRF	TRISB		;Port B0-B7 as output

	CALL 	SETBAUD   
	call	DELAY500MS	

	MOVLW	H'69'		;CHAR 'i'
NEXT	CALL	SERIALSEND
	CALL	DELAY1S
	GOTO	NEXT


;********** INITIALISATION ROUTINE ****************

SETBAUD   BANK1 
         movlw 129          ; BRG for 9600baud from 20MHz, brgh=1
;         movlw 103          ; BRG for 9600baud from 16MHz, brgh=1
;         movlw 25           ; BRG for 9600baud from 4MHz, brgh=1
;         movlw 23           ; BRG for 9600baud from 3.6864MHz, brgh=1

;          movlw 20           ; BRG for 9600baud from 3.2768MHz, brgh=1
;         movlw 22
          movwf SPBRG
          movlw B'00000100'  ; set sync=0, brgh=1 + ninth bit not set
          movwf TXSTA
                 
          bcf PIE1,TXIE      ; clear interrupt bit (bit TXIE)
          BANK0 
               
          movlw B'10000000'  ; set SPEN Bit of RCSTA reg
          movwf RCSTA

          BANK1 
          bsf TXSTA,TXEN     ; enable transmission (bit TXEN)
          BANK0 

          return
                
;********** ROUTINE THAT CAUSES ACTUAL OUTPUT OF SERIAL DATA BYTE *******
                 
SERIALSEND
          btfss PIR1,TXIF    ; wait for TXIF bit 4 to go high
          goto SERIALSEND    ; (showing TXREG empty)
          movwf TXREG        ; put val (held in W) in TXREG ready for transmission
          return 


;********** DELAY ROUTINES *******

DELAY500MS
; Delay = 0.5 seconds
; Clock frequency = 19.6608 MHz

; Actual delay = 0.5 seconds = 2457600 cycles
; Error = 0 %

			;2457600 cycles
	movlw	0x6D
	movwf	d1
	movlw	0x5C
	movwf	d2
	movlw	0x06
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

	return

DELAY1S
; Delay = 1 seconds
; Clock frequency = 19.6608 MHz

; Actual delay = 1 seconds = 4915200 cycles
; Error = 0 %
			;4915195 cycles
	movlw	0xDA
	movwf	d1
	movlw	0xB7
	movwf	d2
	movlw	0x0B
	movwf	d3
Delay_1
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_1

			;5 cycles
	goto	$+1
	goto	$+1
	nop

	return
		
	END			;final line

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using RS232 with AUSART on 16F88

Post by Benj »

Hello

1) The 16F877A has its UART on pins 6 and 7 of portC. The 16F88 has its UART on pins 2 and 5 of PortB. The PIC device datasheet will have all the info you require.

2) On the development board the connectors PORTC and PORTD are connected to PORTC and PORTD of the chip. If your chip does not have these ports then you cannot use these connectors. Larger chips (eg 16F877A) are available at fairly low cost from us or Rapid etc.

3) If you create a assembler program then I will try and help troubleshoot it. Im not writing it from scratch because a) I loath assembler :cry: and b) there will be hundreds of assembler examples on Google. If you want another Flowcode generated working example then that I can do but the code will be a hard to read. Your code above looks like it should work correctly.

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Using RS232 with AUSART on 16F88

Post by Steve »

Unfortunately, when you reach a certain point with assembly, you have to dig in and try to understand the datasheets. But I can offer some hints that may help...

Your code already sets up the UART for reception:

Code: Select all

          movlw B'10000000'  ; set SPEN Bit of RCSTA reg
          movwf RCSTA
What you need to do when you are ready to receive data is to set the CREN pin of the RCSTA register high. Then, you need to continually monitor the RCIF flag. When data has been received, this flag bit will be set high.

To read the incoming data, read the value of RCREG. This will automatically clear the RCIF flag.

Of course, things get more complicated when you add the functionality to trap errors, etc. The section 11.2.2 in the 16F88 datasheet has more detail.

I hope this helps.

econnections
Posts: 35
Joined: Fri Dec 26, 2008 4:02 pm
Contact:

Re: Using RS232 with AUSART on 16F88

Post by econnections »

Thank you for the lead.

I have spent the last couple of days studying the datasheet on UART reception; there isn't much on Google but I did find one useful lead.

I have written the receiption routine which I am convinced should work but appear not to. During testing I have made the following observations which leads me to think the problem may be the v3 Development Board.

Hyperterminal on PC is used to send an character 'i' to COM1 at 9600, 8-bit, no parity and 1 stop bit.

This is received at the MAX232 which converts the signal to TTL. The signal swings between 0-5v.

The output from the MAX232 is connected to RB2(Rx) on J10 causing the LED LB2 to light.

The connection to RB2 is loading the output from MAX232 causing a reduced signal swing of between 0-3.7v which should be enough to communicate with the 16F88

The internal circuitary on the v3 Development Board is further attenuating the signal producing a voltage swing of only 0-2.7v on pin 8 (RB2) of the 16F88 which is not enough for proper communications. I also note that RB2/J10 on the development board is drawing 3.7mA from the MAX232 probably caused by the LED. I also guess the 150ohm, R73 protection resister is part of the problem.

I am trying to complete tutorial 43 (RS232 Serial Communication) in Assembly for PICMicro V4, so where do I go from here?

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Using RS232 with AUSART on 16F88

Post by Steve »

You could be right about the development board interfering with the reception on that pin. We found this "dirtying" of the electrical signals in our original development boards, and our "E-Blocks" concept was actually born out of this.

My suggestion would be to use a different chip which has the UART on portC (e.g. the 16F877a) - that way, the signal to and from the PICmicro will be clean. Alternatively, you could bend the RB2 pin of the 16F88 up so it is not in the DIL socket and is not connected to the rest of the development board circuitry, and then connect this pin directly to your MAX232 circuitry.

Another thing you could try is to provide additonal buffering from the MAX232 chip so that the signal is less-likely to be dragged below the threshold of a positive input. You might even be able to do this using just a pull-up resistor.

As you are discovering, those final few pages of the ASM4PICs tutorial are intended as discussion topics around some of the more advanced aspects of the PICmicro and as such were never intended as complete programs that can be run "out of the box" on the development board.

econnections
Posts: 35
Joined: Fri Dec 26, 2008 4:02 pm
Contact:

Re: Using RS232 with AUSART on 16F88

Post by econnections »

Very useful information, thank you.

I will try attaching a pull up resister between RB2/J10 and Vdd and monitor the effect and try adding an emitter follower on the output from the MAX232.

I like your suggestion of using a 16F877a. It hadn't occured to me that there was no loading on port C. I will eventually move to this PIC but at present I am trying to get to grips with the simplier 16F88 and understand any limitations the development board imposes so I can accommmodate them in furture projects.

I found this so interesting that if I can find a good but reasonably priced screen capture program for creating demo's I might post some support tutorials to help other users?
Last edited by econnections on Fri Mar 13, 2009 2:08 am, edited 1 time in total.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using RS232 with AUSART on 16F88

Post by Benj »

I occasionally use the Camtasia software to create screen capture videos. It seems ok but the audio can be a bit bad sometimes. The file can then be taken from Camtasia and used with Windows movie maker to edit the video.

econnections
Posts: 35
Joined: Fri Dec 26, 2008 4:02 pm
Contact:

Re: Using RS232 with AUSART on 16F88

Post by econnections »

I used an emitter follow built with BC108 to buffer the output from the MAX232. The voltage at RB2/J10 now swings between 0.2-4.1v which is enough to drive the PIC.

The test cicuit allowed be to type keys on the PC using hyperterminal, send the keycodes through the MAX232 to the UART and display the least five bits on PORTA. This allowed me to see that I was receiving information from the PC. I then added additional code to echo (transmit) the keycode back to the PC. Thus allowed me to complete by basic understanding on how to program the UART. I will add aditional code to test the interrupts and send string from the PC that represent commands that the ISR will distinguish and execute. I will add the code here shortly and hopefully create a small training video.

Thank you Steve and Ben for you assistance.

Post Reply