How to configure UART with 16F877A

Thread Starter

waseem1

Joined Mar 26, 2010
7
I have a project wireless circuit having transmitter and receiver by using XBee for wireless. The protocol used for communicate the PIC 16F877A and the XBee is UART. And I wrote my program in assembly languge and I don't know the correct way for the UART configuration so could any one help to run my project.

this is my program for TX
include <p16f877a.inc>
COUNT1 equ H'20'
COUNT2 equ H'21'
COUNT3 equ H'22'
COUNT4 equ H'23'
Init
bsf STATUS,RP0 ;Move to Bank1
movlw b'11111111'
movwf TRISA ;Set port A as input
movlw b'00000000'
movwf TRISB ;Set port B as output
movlw b'00000000'
movwf TRISC ;Set port C as output
movlw b'00000000'
movwf TRISD ;Set port D as output
movlw b'00000000'
movwf TRISE ;Set port E as output

bcf STATUS,RP0 ;Return to Bank0
clrf PORTA
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORTE
bcf TXSTA,TX9 ;Selects 8-bit transmission
bsf TXSTA,TXEN ;Transmit Enable bit
bcf TXSTA,SYNC ;Asynchronous mode select
bsf TXSTA,BRGH ;High Baud Rate Select bit
bcf TXSTA,TRMT ;Transmit Shift Register Status bit
start

btfsc PORTC,7
goto powerON
goto powerfail
powerfail
bsf PORTC,6 ;send data to transmitter
bsf PORTD,1 ;switch ON alarm
goto start

powerON
bcf PORTC,6 ;power resume stop sending data for transsmitter
bcf PORTD,1 ;switch off alarm
goto start

Delay ;a delay subroutine of around 1 sec.
movlw d'239'
movwf COUNT3
end
 

Thread Starter

waseem1

Joined Mar 26, 2010
7
I checked the datasheet I got only the configration but how to do it I don't know. you can see above what i have done so could any one tell me the correct way to do the configuration in the program
 

R!f@@

Joined Apr 2, 2009
10,007
that is not ur whole asm.
where's the config statement.
Post all and then someone will take a good look at it.
Otherwise ur question will get ignored.

If you are asking for help, provide every info.
And also properly quote the coding
 

Thread Starter

waseem1

Joined Mar 26, 2010
7
Xbee by default use 9600 bauds; no parity; 8 bits; 2 stop bits.

For setting the 16F877a UART you need to know the oscillator frequency.
So I assume you are using a 4 MHz oscillator, here the setting:

Rich (BB code):
RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $24 ' Enable transmit, BRGH = 1
SPBRG = $19  ' 9600 Baud @ 4MHz, 0,16%
$ stand for hex.

Alberto
Thanks for your reply Iam using 16MHz oscillator. I read the datasheet for configuration but still I don't know how to make the configuration in the program. So could you please help me.
 

Thread Starter

waseem1

Joined Mar 26, 2010
7
for 16 MHz oscillator you have to set registers in this way:

Rich (BB code):
RCSTA = $90 ; Enable serial port & continuous receive
TXSTA = $20 ; Enable transmit, BRGH = 0
SPBRG = $19 ; 9600 Baud @ 16MHz, 0,16%
Alberto
could you please tell me how can i do that in assembly, what I want only send 1 or 0 from XBee transmitter to XBee receiver.
what i have done for receiver
include <p16f877a.inc>
COUNT1 equ H'20'
COUNT2 equ H'21'
COUNT3 equ H'22'
COUNT4 equ H'23'
Init
bsf STATUS,RP0 ;Move to Bank1
movlw b'11111111'
movwf TRISA ;Set port A as input
movlw b'00000000'
movwf TRISB ;Set port B as output
movlw b'00000000'
movwf TRISC ;Set port C as output
movlw b'00000000'
movwf TRISD ;Set port D as output
movlw b'00000000'
movwf TRISE ;Set port E as output

bcf STATUS,RP0 ;Return to Bank0
clrf PORTA
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORTE
bsf RCSTA,SPEN ;Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins)
bcf RCSTA,RX9 ;Selects 8-bit reception
bsf RCSTA,CREN ;Enables continuous receive
bcf RCSTA,FERR ;No framing error
bcf RCSTA,OERR ;No overrun error

start

btfss PORTC,7
goto powerON
goto powerfail
powerfail
bsf PORTD,0 ;turn on led
bsf PORTD,1 ;alarm switch on
bsf PORTD,3 ;energize relay to start timer
goto start

powerON
bcf PORTD,0 ;turn off led
bcf PORTD,1 ;alarm switch off
bcf PORTD,3 ;de-energize timer relay
goto start

Delay ;a delay subroutine of around 1 sec.
movlw d'239'
movwf COUNT3
end
how can i do the that configuration for the transmitter and receiver if the masseage only 1 or 0?
 

R!f@@

Joined Apr 2, 2009
10,007
list p=16f877a ; list directive to define processor
#include <p16f877a.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

;***** VARIABLE DEFINITIONS (examples)

; example of using Shared Uninitialized Data Section
INT_VAR UDATA_SHR
w_temp RES 1 ; variable used for context saving
status_temp RES 1 ; variable used for context saving
pclath_temp RES 1 ; variable used for context saving

; example of using Uninitialized Data Section
TEMP_VAR UDATA ; explicit address specified is not required
temp_count RES 1 ; temporary variable (example)

; example of using Overlayed Uninitialized Data Section
; in this example both variables are assigned the same GPR location by linker
G_DATA UDATA_OVR ; explicit address can be specified
flag RES 2 ; temporary variable (shared locations - G_DATA)

G_DATA UDATA_OVR
count RES 2 ; temporary variable (shared locations - G_DATA)

;**********************************************************************
RESET_VECTOR CODE 0x0000 ; processor reset vector
nop ; nop for icd
pagesel start
goto start ; go to beginning of program


INT_VECTOR CODE 0x0004 ; interrupt vector location

INTERRUPT

movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,w ; move pclath register into w register
movwf pclath_temp ; save off contents of PCLATH register

; isr code can go here or be located as a call subroutine elsewhere

movf pclath_temp,w ; retrieve copy of PCLATH register
movwf PCLATH ; restore pre-isr PCLATH register contents
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt

MAIN_PROG CODE
Above is statements you will need in your program when you compile the ASM.
 

R!f@@

Joined Apr 2, 2009
10,007
It will be in the MPASM tool suite.
You should follow the Installation as stated in the website.
All the .inc Files will then be installed.
Read the help file to preserve the .inc and tempo files.
These files tend to get over written if you donno what you are doing.
If over written, you might need to reinstall them again.
 
Top