Jump to content
ELFORUM - Forumul electronistilor

Folosirea comunicatiei seriale RS232


Guest carst_razvan

Recommended Posts

Guest carst_razvan

Vreau sa trimit litera "A" folosind RS232 la un calculator. Cum s-ar putea face? (Am incarcat valoarea 0x41 intr-un registru, apoi am folosit un exemplu de program de pe internet care nu prea merge. Cred ca pt. a face transmisia seriala tre' sa folosesc un protocol de comunicatie. Banuiesc ca este Non Return to Zero. Programul folosit este:" LIST P=16F84a ; tells which processor is used #INCLUDE "p16f84a.inc" ; defines various registers etc. Look it over. #include "bank.inc" ERRORLEVEL -224 ; supress annoying message because of tris __CONFIG _PWRTE_ON &_LP_OSC &_WDT_OFF ; configuration switches CBLOCK H'0C' txreg ; holds byte to be transmitted rxreg ; holds byte received char ; holds character to be transmitted bitcount ; holds count of bits when transmitting timecnt ; holds counter for timer cont ENDC ORG 0 ; start a program memory location zero goto main ; skip over subroutines;-----------------------------------------------------------------------;; initialization - set up ports and timer options ;;-----------------------------------------------------------------------;init: BANK1 movlw 0x1F movwf TRISA BANK0 movlw 0x01 ; RA0 to input movwf PORTA ; contents of W copied to PORT A ... BANK1 movlw 0x00 movwf TRISB BANK0 movlw 0xFF movwf PORTB ; and PORT B BANK1 movlw 0x01 ; pull-ups active movwf OPTION_REG ; prescalar assigned to TMR0 and set 1:4 rolls over each second return;-----------------------------------------------------------------------;; time delay routines ;;-----------------------------------------------------------------------;onesecond: BANK0 movlw 0x6F ; wait one second movwf timecnt clrf TMR0 bcf INTCON, T0IF ; clear the interrupt flagtime: btfss INTCON, T0IF ; wait on T0IF to be set goto time ; loop till it is decfsz timecnt, 1 ; finished timing? goto time ; not yet return ; yes;-----------------------------------------------------------------------;; the main program ;;-----------------------------------------------------------------------;main: ; this is the main program call init ; set up ports call onesecond ; let things settle for 1 secondloop: ;call rx get a character from keyboard BANK0 ;movwf char save so we can increment it movlw 0x41 movwf char incf char, f ; send back the next 3 characters movf char, W call tx incf char, f movf char, W call tx incf char, f movf char, W call tx incf char, f movf char, W call tx incf char, f movf char, W call tx incf char, f movf char, W call tx incf char, f movf char, W call tx incf char, f movf char, W call tx ;goto loop do it forever;-----------------------------------------------------------------------;; transmit byte in W at 1200 baud out RB7 ;;-----------------------------------------------------------------------;; normal rs232 is -V for a high, +V for a low; this would translate to 0V for high +5 for lowtx: movwf txreg ; set up transmit register comf txreg, f ; flip all bits movlw D'8' ; eight bits to transmit movwf bitcount bsf PORTB, 7 ; start bit (+5V is 'low') nop ; a total of 7 instruction cycles nop ; (one is the rrf at txloop) nop nop noptxloop: rrf txreg, f ; lsb out of txreg into carry rrf PORTB, f ; bit 7 of port B high or low with carry movlw 0x57 movwf conttest1: decfsz cont,1 goto test1 ; make total loop delay 7 instruction counts ; 7 * 122 usec = 854, (should be 833) decfsz bitcount, f ; 4 instruction cycles to here goto txloop ; total 4 + 3 = 7 instructions cycles in loop bcf PORTB, 7 ; 0 Volts is the 'high' state this is nop ; stop bit nop ; delay 7 instruction cycles nop nop return;-----------------------------------------------------------------------;; receive byte at 1200 baud in RA0, put in W ;;-----------------------------------------------------------------------;rx: movlw 0xFF ; eight bits to input movwf bitcountwaitstart: btfss PORTA, 0 ; wait for input to go high, (start bit) goto waitstart nop ; wait 1/2 start bit time 3 including branch nop ; next four instruction are to make nop ; 1 bit time delay before first reading of nop ; input port nopreceive: ; make total loop delay 7 instruction counts ; 7 * 122 usec = 854, (should be 833) movlw 0x57 movwf conttest: decfsz cont,1 goto test rrf PORTA, f ; PORT A bit 0 into carry rrf rxreg, f ; rotate carry into receive register decfsz bitcount, f ; goto receive ; total 4 + 3 = 7 instructions cycles in loop comf rxreg, W ; return compliment of value collected nop ; wait at least one stop bit nop nop nop return end" Am adaptat putin programul pt. rezonatorul meu de 455 KHz)

Link to comment
  • Replies 17
  • Created
  • Last Reply

Top Posters In This Topic

Guest carst_razvan

Am un PIC16F84A. La asta vreau sa-i implementez comunicatia seriala RS232. De pus protocolul de comunicatie e mai greu si de setat la o anumita viteza (9600 baud)

Link to comment

personal nu am facut asta pana acum, dar cred ca poti sa obtii un cod mult mai subtire. ti-as recomanda de asemenea sa folosesti timerul lui. daca nu se ofera nimeni aici sa iti dea o sursa de asemenea program, pe care sa-l fi folosit deja, am sa fac eu unul.mai este un aspect: vrei comunicatie bidirectionala?

Link to comment
Guest carst_razvan

Nu vreau comunicatie bidirectionala. Vreau ca PIC-ul da transmita niste date la calculator. Pentru test as vrea sa trimit la calculator litera "G"

Link to comment

Ai incercat app-note de la Microchip ? Exista exemple de implementare software pt. comunicatii seriale asincrone.

 

Daca te intereseaza doar transmisia, poti folosi urmatoarea rutina:

 

;==============================================Serial_Send  ; Sends the byte in TxData register, byte must be in TxData before subroutine call	movlw	0x08		; initialize counter 0 with 8,	movwf	Counter0	; as we need to send 8 bits		_Bank0	bcf		TX			; send start bit	call 	Delay_1bit	Send_Bit:	rrf		TxData,F	; send data bit	btfss	_C			; test if bit is 0 or 1	goto	Send_0Send_1:					; if it is 1, set TX=1	bsf		TX	goto	NextSend_0:	bcf		TX			; if it is 0, set TX=0Next:	call 	Delay_1bit	; wait necessary delay	decfsz	Counter0,F	; check if there are more bits to send	goto 	Send_Bit	; if there are, send the next one		bsf		TX			; if not, send stop bits (2 stop bits)	call 	Delay_1bit	call    Delay_1bit                  		return;==============================================Delay_1bit   ; Delay for 1-bit period, 9600 baud   movlw 	0xAB		;   movwf	Counter1	; function call takes:Delay_Loop:				; 2(call)+2(initialize counter1)+3*Counter1+1+2 instruction cycles   decfsz	Counter1,F	; = 520 instruction cycles = 104 us (20Mhz crystal)   goto		Delay_Loop	; 104us * 9600 = 0.9984s (-0.16% error)   nop	   return;==============================================

Trebuie ajustat delay-ul in fct. de baud-rate-ul dorit si frecv. oscilatorului. Rutina se apeleaza astfel:

 

movlw 'G'movwf  TxDatacall      Serial_Send

Nu e cea mai buna varianta (procesorul e blocat cat timp se trimit datele), dar merge.

Link to comment

Cu rezonator de 455KHz nu merge protocol prin soft pentru RS232 la 1200bps.Q de 1 MHz care este foarte scump.2 MHz este deja mult mai ieftin si ai rezolvat-o.Dar rezolvarea cea mai buna este cu PIC 16F627 care are si USART hardware si clock intern de 8 MHz si costa si de 3 ori mai putin decit 16F84.Asta este solutia adevarata !Don Mario

Link to comment

Cu rezonator de 455KHz nu merge protocol prin soft pentru RS232 la 1200bps.Q de 1 MHz care este foarte scump.2 MHz este deja mult mai ieftin si ai rezolvat-o.Dar rezolvarea cea mai buna este cu PIC 16F627 care are si USART hardware si clock intern de 8 MHz si costa si de 3 ori mai putin decit 16F84.Asta este solutia adevarata !Don Mario

Si daca asa ne sfatuiesti tu ceva date concrete pentru noi astia mai incepatori nu ne poti oferi? Daca nu deranjam!
Link to comment

salut!am si eu o...intrebare....daca 16F627/8 sunt mai bune decat `84, de ce sunt mai ieftine. adica, de ce sa cumpar un '84 cand pot sa cumpar un '628 mai ieftin si mai bun. plus ca are si osc intern.yo nu l-am folosit pe asta pana acum, da am cumparatu unu si vreau sa il incerc, cand mai ajung pacas!

Link to comment

Ce sfaturi sa-ti mai dau ?Am spus destul de simplu ! 16F627/628 este de 3 ori mai ieftin decit 16F84,are USART hard,deci numai trebuie sa scrii soft pentru seriala,pui octetul in Tx register si il iei pe cel care eventual il receptionezi din Rx register.Are osc intern destul de stabil,mai ales la viteze mici nu sunt nici un fel de probleme.Are si un PWM hard,daca ti-o trebui la ceva.Ram si EEPROM de 2 ori mai mult ca 16F84.Altceva ce sa-ti mai spun.Eu am facut multe aplicatii cu 628 si e super la banii astia.Am facut de exemplu o comanda pentru 2 motoare pas cu pas cu viteza reglabila.Si un comvertor din comenzi paralele (taste ) in seriala si invers.Adica din seriala in comenzi paralele pentru setarile unor chipuri care faceau diferite functii.Don MarioDupa cum vezi,nu mai deranjat !

Link to comment
  • 2 weeks later...

Salut .

 

am facut si eu asa ceva dar cu avr risk-uri si nu pot sa spun ca este simplu dar daca sti ce ai de implementat atunci iese!

 

.PROIECT : Achizitie de date cu atmega8 pe 6 canale

Afisare pe LCD NEC[4x20]

Comunicare RS232 , interfata labview.

 

...www.dezvoltare.avr.go.ro

Link to comment

risk? mai sunt si altii care nu se pricep asa de bine si care vor sa invete, si ii bagi in ceata cu asta. profii tai de la scoala faceau grezeli intentionat, pentru ca erati toti o clasa de genii si trebuia sa va prindeti? si chiar daca erati genii toti, nu cred ca facea...

Link to comment

Salut .am facut si eu asa ceva dar cu avr risk-uri si nu pot sa spun ca este simplu dar daca sti ce ai de implementat atunci iese!.PROIECT : Achizitie de date cu atmega8 pe 6 canale Afisare pe LCD NEC[4x20] Comunicare RS232 , interfata labview....www.dezvoltare.avr.go.ro

nu se scrie cumva risc?
pote si ce are?
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines