PIC - Comunicaçao serial entre dois PICs

Este projeto tem como objetivo fazer a comunicação serial entre dois microcontroladores PICs 16F628. Afim de uma melhor visualizaçao dos bits de cada dado transmitido/recebido, usou-se o pino do PIC para cada bit. Sendo assim, a tabela 1 mostra a relaçao com a posiçao do bit e seu pino correspondente do PIC.

TABELA 1 - Relação entre a posiçao do bit e o pino do PIC.

O circuito funciona da seguinte maneira: sempre que uma chave do PIC transmissor for acionada, um LED correspondente a esta chave será ligado no PIC receptor. Ver figura 1.

O código fonte foi escrito em flowcode usando o software Ktechlab-gcb 0.0.1 rodando sobre sistema operacional Linux Ubuntu 10.04. A escolha da versão deste sistema deu-se pela compatibilidade com o Ktechlab-gcb que ainda usa as bibliotecas do Qt3. Para simular o funcionamento dos microcontroladores usou-se o software Proteus 7.7 SP2 rodando sobre Windows XP SP2. Nao fez-se a simulação no Ktechlab-gcb pois o mesmo não conseguiu fazer a simulação.

O software Ktechlab-gcb oferece a opção de programar usando flowcode, que é uma programação muito fácil onde usa-se fluxogramas. Nas figuras 2 e 3, podemos ver a programaçao em flowcode.

Figura 1 - Esquema elétrico para simulaçao no software proteus 7.7 SP2.

Figura 2a - Código em flowcode do transmissor.

Figura 2b - Sub-rotina do flowcode transmissor.

Figura 3a - Flowcode do receptor.

Figura 3b - Sub-rotina do receptor.

Para quem usa programa em Assembly ou GCBasic segue os códigos abaixo:

Transmissor - Assembly

;Program compiled by Great Cow BASIC (0.9 29/11/2009)

;Need help? See the GCBASIC forums at http://sourceforge.net/forum/?group_id=169286,

;check the documentation or email w_cholmondeley@users.sourceforge.net.

;********************************************************************************

;Set up the assembler options (Chip type, clock source, other bits and pieces)

LIST p=16F628, r=DEC

#include <P16F628.inc>

__CONFIG _XT_OSC & _MCLRE_OFF & _LVP_OFF & _WDT_OFF

;********************************************************************************

;Set aside memory locations for variables

SysWaitTempUS EQU 117

SysWaitTempUS_H EQU 118

DADO EQU 32

;********************************************************************************

;Vectors

ORG 0

pagesel BASPROGRAMSTART

goto BASPROGRAMSTART

ORG 4

retfie

;********************************************************************************

;Start of program memory page 0

ORG 5

BASPROGRAMSTART

;Call initialisation routines

call INITSYS

;Automatic pin direction setting

banksel TRISB

bsf TRISB,7

bsf TRISB,6

bsf TRISB,5

bsf TRISB,4

bsf TRISA,3

bsf TRISA,2

bsf TRISA,1

bsf TRISA,0

;Start of the main program

clrf OPTION_REG

banksel RCSTA

clrf RCSTA

banksel SPBRG

clrf SPBRG

banksel TXREG

clrf TXREG

banksel TXSTA

clrf TXSTA

movlw 127

movwf TRISA

banksel PORTA

clrf PORTA

clrf PORTB

movlw 219

banksel OPTION_REG

movwf OPTION_REG

movlw 25

movwf SPBRG

movlw 36

movwf TXSTA

movlw 144

banksel RCSTA

movwf RCSTA

__LABEL_VARCOMPARISON

banksel TXSTA

btfsc TXSTA,TRMT

goto ELSE1_1

goto __LABEL_VARCOMPARISON

goto ENDIF1

ELSE1_1

banksel STATUS

call LE_BIT

movf DADO,W

movwf TXREG

nop

goto __LABEL_VARCOMPARISON

ENDIF1

goto BASPROGRAMEND

BASPROGRAMEND

sleep

goto $

;********************************************************************************

INITSYS

movlw 7

movwf CMCON

clrf PORTA

clrf PORTB

return

;********************************************************************************

LE_BIT

btfss PORTA,0

goto ELSE2_1

movlw 1

iorwf DADO,F

goto ENDIF2

ELSE2_1

movlw 254

andwf DADO,F

ENDIF2

btfss PORTA,1

goto ELSE3_1

movlw 2

iorwf DADO,F

goto ENDIF3

ELSE3_1

movlw 253

andwf DADO,F

ENDIF3

btfss PORTA,2

goto ELSE4_1

movlw 4

iorwf DADO,F

goto ENDIF4

ELSE4_1

movlw 251

andwf DADO,F

ENDIF4

btfss PORTA,3

goto ELSE5_1

movlw 8

iorwf DADO,F

goto ENDIF5

ELSE5_1

movlw 247

andwf DADO,F

ENDIF5

btfss PORTB,4

goto ELSE6_1

movlw 16

iorwf DADO,F

goto ENDIF6

ELSE6_1

movlw 239

andwf DADO,F

ENDIF6

btfss PORTB,5

goto ELSE7_1

movlw 32

iorwf DADO,F

goto ENDIF7

ELSE7_1

movlw 223

andwf DADO,F

ENDIF7

btfss PORTB,6

goto ELSE8_1

movlw 64

iorwf DADO,F

goto ENDIF8

ELSE8_1

movlw 191

andwf DADO,F

ENDIF8

btfss PORTB,7

goto ELSE9_1

movlw 128

iorwf DADO,F

goto ENDIF9

ELSE9_1

movlw 127

andwf DADO,F

ENDIF9

return

;********************************************************************************

END

Receptor - Assembly

;Program compiled by Great Cow BASIC (0.9 29/11/2009)

;Need help? See the GCBASIC forums at http://sourceforge.net/forum/?group_id=169286,

;check the documentation or email w_cholmondeley@users.sourceforge.net.

;********************************************************************************

;Set up the assembler options (Chip type, clock source, other bits and pieces)

LIST p=16F628, r=DEC

#include <P16F628.inc>

__CONFIG _XT_OSC & _MCLRE_OFF & _LVP_OFF & _WDT_OFF

;********************************************************************************

;Set aside memory locations for variables

DELAYTEMP EQU 112

SysWaitTempUS EQU 117

SysWaitTempUS_H EQU 118

DADO EQU 32

PORTD EQU 33

TEMP EQU 34

;********************************************************************************

;Vectors

ORG 0

pagesel BASPROGRAMSTART

goto BASPROGRAMSTART

ORG 4

retfie

;********************************************************************************

;Start of program memory page 0

ORG 5

BASPROGRAMSTART

;Call initialisation routines

call INITSYS

;Automatic pin direction setting

banksel TRISB

bcf TRISB,7

bcf TRISB,6

bcf TRISB,5

bcf TRISB,4

bcf TRISA,3

bcf TRISA,2

bcf TRISA,1

bcf TRISA,0

;Start of the main program

clrf SPBRG

clrf OPTION_REG

banksel PORTB

clrf PORTB

clrf PORTD

clrf RCSTA

banksel TXSTA

clrf TXSTA

banksel DADO

clrf DADO

movlw 112

banksel TRISA

movwf TRISA

movlw 15

movwf TRISB

banksel PORTA

clrf PORTA

clrf PORTB

movlw 219

banksel OPTION_REG

movwf OPTION_REG

movlw 25

movwf SPBRG

movlw 36

movwf TXSTA

movlw 144

banksel RCSTA

movwf RCSTA

movlw 49

movwf DELAYTEMP

DelayUS1

decfsz DELAYTEMP,F

goto DelayUS1

__LABEL_DELAY__5

goto $+1

btfsc PIR1,RCIF

goto ELSE1_1

goto __LABEL_DELAY__5

goto ENDIF1

ELSE1_1

movf RCREG,W

movwf DADO

call ESCREVE

goto __LABEL_DELAY__5

ENDIF1

goto BASPROGRAMEND

BASPROGRAMEND

sleep

goto $

;********************************************************************************

ESCREVE

movlw 1

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE2_1

bsf PORTA,0

goto ENDIF2

ELSE2_1

bcf PORTA,0

ENDIF2

movlw 2

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE3_1

bsf PORTA,1

goto ENDIF3

ELSE3_1

bcf PORTA,1

ENDIF3

movlw 4

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE4_1

bsf PORTA,2

goto ENDIF4

ELSE4_1

bcf PORTA,2

ENDIF4

movlw 8

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE5_1

bsf PORTA,3

goto ENDIF5

ELSE5_1

bcf PORTA,3

ENDIF5

movlw 16

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE6_1

bsf PORTB,4

goto ENDIF6

ELSE6_1

bcf PORTB,4

ENDIF6

movlw 32

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE7_1

bsf PORTB,5

goto ENDIF7

ELSE7_1

bcf PORTB,5

ENDIF7

movlw 64

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE8_1

bsf PORTB,6

goto ENDIF8

ELSE8_1

bcf PORTB,6

ENDIF8

movlw 128

andwf DADO,W

movwf TEMP

sublw 0

btfsc STATUS, C

goto ELSE9_1

bsf PORTB,7

goto ENDIF9

ELSE9_1

bcf PORTB,7

ENDIF9

return

;********************************************************************************

INITSYS

movlw 7

movwf CMCON

clrf PORTA

clrf PORTB

return

;********************************************************************************

END

Transmissor - GCBasic

'_____________________________________________________

' GcBasic code generated by Ktechlabgcb-Flowcode

'_____________________________________________________

' Chip model and clock speed:

#chip P16F628, 4 'MHz

' Initial values:

OPTION_REG = 0

RCSTA = 0

SPBRG = 0

TXREG = 0

TXSTA = 0

TRISA = 127

PORTA = 0

PORTB = 0

' Start: _______________________________________________

OPTION_REG = 219

SPBRG = 25

TXSTA = 36

RCSTA = 144

__label_varcomparison:

if TRMT = 0 then

goto __label_varcomparison

else

call Le_bit

TXREG = dado

Wait 1 us

goto __label_varcomparison

End if

end

Sub Le_bit


if PORTA.0 on then

dado = dado OR 1

else

dado = dado AND 254

End if



if PORTA.1 on then

dado = dado OR 2

else

dado = dado AND 253

End if



if PORTA.2 on then

dado = dado OR 4

else

dado = dado AND 251

End if



if PORTA.3 on then

dado = dado OR 8

else

dado = dado AND 247

End if



if PORTB.4 on then

dado = dado OR 16

else

dado = dado AND 239

End if



if PORTB.5 on then

dado = dado OR 32

else

dado = dado AND 223

End if



if PORTB.6 on then

dado = dado OR 64

else

dado = dado AND 191

End if



if PORTB.7 on then

dado = dado OR 128

else

dado = dado AND 127

End if


End Sub

Receptor - GCBasic

'_____________________________________________________

' GcBasic code generated by Ktechlabgcb-Flowcode

'_____________________________________________________

' Chip model and clock speed:

#chip P16F628, 4 'MHz

' Initial values:

SPBRG = 0

OPTION_REG = 0

PORTB = 0

PORTD = 0

RCSTA = 0

TXSTA = 0

dado = 0

TRISA = 112

TRISB = 15

PORTA = 0

PORTB = 0

' Start: _______________________________________________

OPTION_REG = 219

SPBRG = 25

TXSTA = 36

RCSTA = 144

Wait 148 us

__label_delay__5:

Wait 2 us

if RCIF = 0 then

goto __label_delay__5

else

dado = RCREG

call Escreve

goto __label_delay__5

End if

end

Sub Escreve

temp = dado AND 1


if temp > 0 then

PORTA.0 = 1

else

PORTA.0 = 0

End if


temp = dado AND 2


if temp > 0 then

PORTA.1 = 1

else

PORTA.1 = 0

End if


temp = dado AND 4


if temp > 0 then

PORTA.2 = 1

else

PORTA.2 = 0

End if


temp = dado AND 8


if temp > 0 then

PORTA.3 = 1

else

PORTA.3 = 0

End if


temp = dado AND 16


if temp > 0 then

PORTB.4 = 1

else

PORTB.4 = 0

End if


temp = dado AND 32


if temp > 0 then

PORTB.5 = 1

else

PORTB.5 = 0

End if


temp = dado AND 64


if temp > 0 then

PORTB.6 = 1

else

PORTB.6 = 0

End if


temp = dado AND 128


if temp > 0 then

PORTB.7 = 1

else

PORTB.7 = 0

End if


End Sub

Video explicando a lógica do programa e sua simulação:

Segue em anexo os arquivos usados neste exemplo: