Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F877 serial program

Status
Not open for further replies.

Erwin_Macaraig

New Member
I am programming a PIC16F877 serial port. I am trying to send to the serial port the character 'e'. I am using hyperterminal to view the said character. I use 9600 baud rate, one stop bit,no parity and no hardware control settings. But the character I am trying to send to the hyperterminal does not seem to appear, mostly the its just a series of 1's and s.

here is my code. Your comments and suggestiions will be appreciated! Thank you in advance!!!



;source code(firmaware fragment) for the PIC16F877 serial communications



#include "P16f877.inc"



__config _wdt_off & _pwrte_on & _xt_osc & _boden_on & _wrt_enable_on & _lvp_off & _cpd_off





errorlevel -302



#define transmitEnable portc,5





cblock h'20'

temp

char

endc



org h'0000'



init

bcf STATUS,RP1

bsf STATUS,RP0 ;selects bank1

movlw b'10000000'

movwf TRISC ;makes pin7<RX> of portC input



movlw b'1000000' ;Fosc = 12Mhz

movwf SPBRG ;9600 baud rate hispeed



bsf TXSTA,BRGH ;bit2: High speed

bcf TXSTA,SYNC ;bit4: Asynchronous mode

bcf TXSTA,TX9 ;bit6: 8-Bit Transmit Enable bit



bcf PIE1,TXIE ;bit4: USART transmit Interrupt Enable bit

bsf TXSTA,TXEN ;bit5: Transmit enable bit



bcf STATUS,RP0 ;selects bank0

movlw b'10010000'

movwf RCSTA ;bit7: Serial Port Enable bit

bsf transmitEnable



main

call message_one

goto main







message_one

movlw d'101'

call sendit



call delay

call delay

call delay



return




sendit



btfss PIR1,TXIF ;poll bit4: Transmit Interrupt Flag

goto sendit

movwf TXREG


return




delay

movlw d'255'

movwf char

movwf temp



delayloop

decfsz char

goto delayloop

decfsz temp

goto delayloop

return





end
 
Looking at your code, your baud rate setting may be incorrect. Assuming you're using a 12Mhz crystal as comments in your code suggest, the value of b'1000000' or 40h for SPBRG is wrong.

With BRGH=1, the correct value for 9600baud @ 12Mhz is D'77'.
 
You might also try my tutorials, I have an RS232 one which includes the 16F876/7 - but using a 20MHz clock - as motion says, you would have to make changes for your clock speed.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top