PDA

View Full Version : 16F628a to 16F873a



mpardinho
- 3rd March 2007, 11:19
i have this code working 100% in 16F628 with 4mhz - resonator


include "modedefs.bas"

' general purpose variables
I VAR BYTE
J VAR BYTE
K VAR BYTE
Contador var byte

'TRISB = %01001010
' 76543210

CE var PORTB.5 '0
CS var PORTB.4 '0
DR var PORTB.3 '1
Clock var PORTB.2 '0
Data_Entrada var PORTB.1 '1
Data_Saida var PORTB.1 '1

' THIS IS THE ADDRESS FOR THE w/ Laipec TRW-24G
Canal_1 var byte ' Channel 1
Canal_2 var byte ' Channel 2


' pin definitions for debug LEDs and switch
Led_Rx var PORTA.1 ' led goes on during rf module configuration
Led_Tx var PORTA.0 ' led goes on during rf message rx or tx

Transmitir var PORTA.2 ' switch used to transmit rf message
Serial_Saida var PORTA.3 ' serial out to pc

' constants for RF configuration modes
RX var byte
TX var Byte

'indicates state of current RF configuration - TX is 0, RX is 1
Estado_Atual var bit

'hold data for RF message
Dados var byte[40]
Dados_Recebidos var byte[4]
Dados_Enviados var byte[4]

'hold data for RF configuration
Configuracao var Byte[16]

'variables for each received or sent message
Msg_Origem var byte
Msg_Destino var byte
Msg_Data_1 var byte
Msg_Data_2 var byte

'-------------------------------------------------------------------------------
Boot:
SEROUT Serial_Saida, T9600, ["BOOT", 13, 10]
PAUSE 1

Canal_1 = $BB
Canal_2 = $BB

CMCON = 7
VRCON = 0
OPTION_REG.7 = 0

PORTA = %00000000
TRISA = %00000100 ' 0 = Output, 1 = Input

PORTC = %00000000
TRISC = %01001010 ' 0 = Output, 1 = Input

Inicio:

' intialize RF module configuration
' constants to define receive/transmit modes at 2402 Mhz frequency
RX = $05
TX = $04
Estado_Atual = rX

SEROUT Serial_Saida, T9600, ["CONFIG", 13, 10]
PAUSE 1

GOSUB Configurar

LOW Transmitir

'-------------------------------------------------------------------------------
contador = 0

while 1

low CS ' bring CS low before turning CE high - only one high at a time!
HIGH CE ' bring CE high to bring RF into active mode

IF Transmitir = 1 THEN
for J = 1 to 70
pause 5

Msg_Destino = "#"
msg_origem = "<"
Msg_Data_1 = 64+J
Msg_Data_2 = J

gosub Enviar_Dados

Msg_Destino = 0 ' CHANGE THIS TP WHO SHOULD RECIEVE MESSAGE
Msg_Data_1 = 0 ' DATA SENT
Msg_Data_2 = 0

LOW Transmitir

next J
ENDIF

' if there's a message waiting in the RX buffer, read it
if DR = 1 Then
INPUT Data_Entrada
GOSUB Receber_Dados
EndIF
WEND


'-------------------------------------------------------------------------------
Receber_Dados:
Dados_Recebidos[0] = 0
Dados_Recebidos[1] = 0
Dados_Recebidos[2] = 0
Dados_Recebidos[3] = 0

' acende led de transmissao
High Led_Rx

INPUT Data_Entrada

' power down RF front-end for current conservation
LOW CE

' clock in data from RF-24G to PIC
for i=0 to 3
' clock should follow data by at least 50nsec
shiftin Data_Entrada, Clock, MSBPRE, [Dados_Recebidos[i]\8]
NEXT

' power up RF front-end again
high CE

Msg_Destino = Dados_Recebidos[0]
Msg_Origem = Dados_Recebidos[1]
Msg_Data_1 = Dados_Recebidos[2]
Msg_Data_2 = Dados_Recebidos[3]

SEROUT Serial_Saida, T9600, [Dados_Recebidos[0], Dados_Recebidos[1], Dados_Recebidos[2], Dados_Recebidos[3], 13, 10]

' apaga led de transmissao
low Led_Rx
RETURN

'-------------------------------------------------------------------------------
Enviar_Dados:
' acende led de recepcao
high Led_Tx

Dados_Enviados[0] = Msg_Destino
Dados_Enviados[1] = Msg_Origem
Dados_Enviados[2] = Msg_Data_1
Dados_Enviados[3] = Msg_Data_2

' set RF to transmit mode...
Estado_Atual = TX
GOSUB Mudar_Estado_Atual

' for shockburst TX, the sum of RX address, payload,
' and CRC must be less than 256 bits long
' the chip then tacks on a preamble to this message,
' and the total msg is 266 bits

OUTPUT Data_Saida

' get back into active mode
HIGH CE

' Need pause between CE high and DATA clock out
pauseus 100

' clock out 5 byte RF address of recipient
for i = 8 to 12
' clock out this byte
shiftout Data_Saida, Clock, MSBFIRST, [Configuracao[i]\8]
Next

' clock out 4 bytes of data payload
for i = 0 to 3
' clock out this byte
shiftout Data_Saida, Clock, MSBFIRST, [Dados_Enviados[i]\8]
Next

' start shockburst transmission
low CE
pauseus 300

' apaga led de recepcao
low Led_Tx

' afterward, set RF back to its default receive state
Estado_Atual = RX
GOSUB Mudar_Estado_Atual
RETURN

'-------------------------------------------------------------------------------
Mudar_Estado_Atual:

' during configuration of the transmitter, we need DATA as an output
OUTPUT Data_Saida

' set chip to configure mode (CE low, CS high)
low CE
high CS

' pause between CS high and DATA clockout
pauseus 100

' Estado_Atual indicates 2402 Mhz frequency and either RX or TX state
' clock out new configuration data from PIC to RF-24G
shiftout Data_Saida, Clock, MSBFIRST, [Estado_Atual\8]

' configuration set on falling edge of CS
low CS
low CE

' for consistency with the rx, stick this here
input DR

' flash the LED on B6 to indicate that we configured
' high Led_Rx
' pause 250
' low Led_Rx
RETURN

'-------------------------------------------------------------------------------
Configurar:
' acende leds de configuracao
high Led_Rx
high Led_Tx

'configuration is a 144 bit word
'must be clocked in MSB first
'bit map
'143:120 - reserved for testing
'119:112 - length of data payload section RX channel 2
'111-104 - length of data payload section RX channel 1
'103:64 - up to 5 byte address for RX channel 2
'63:24 - up to 5 byte address for RX channel 1
'23:18 - number of address bits (both RX channels)
'17 - 8 or 16 bit CRC
'16 - enable on-chip CRCD generation/checking
'15 - enable two channel receive mode
'14 - communication mode (direct or shockburst)
'13 - RF data rate (1Mbps requires 16Mhz crystal)
'12:10 - crystal frequency
'9:8 - RF output power
'7:1 - frequency channel
'0 - RX or TX operation

'setup Configuracao with config bytes, see datasheet for default configuration
'this array is backwards so we can clock from MSB to LSB by increasing the index
'Data bits 119-112: Max data width on channel 2 (excluding CRC and adrs)
Configuracao[1] = $20 'payloadSize, 4 BYTES
'Data bits 111-104: Max data width on channel 1 (excluding CRC and adrs)
Configuracao[2] = $20 'payloadSize, 4 BYTES
'Data bits 103-64: Channel 2 address, 5 bytes (40 bits)
Configuracao[3] = Canal_2
Configuracao[4] = Canal_2
Configuracao[5] = Canal_2
Configuracao[6] = Canal_2
Configuracao[7] = Canal_2
'Data bits 63-24: Channel 1 address, 5 bytes (40 bits)
Configuracao[8] = Canal_1
Configuracao[9] = Canal_1
Configuracao[10] = Canal_1
Configuracao[11] = Canal_1
Configuracao[12] = Canal_1
'Data bits 23-16: Address width and CRC - 40 bit addr, 16 bit CRC, CRC enabled
Configuracao[13] = $A3
'Data bits 15-8: One Channel Receive, Shockburst Enabled, 250 kilobits, etc
Configuracao[14] = $4F
'Data bits 7-0: 2402 Mhz frequency, RX enabled/disabled (depending on Estado_Atual)
Configuracao[15] = Estado_Atual

' During configuration of the transmitter, we need DATA as an output
INPUT DR
INPUT Data_Entrada
OUTPUT Data_Saida

' set chip to configure mode (CE low, CS high)
low CE
high CS

' pause between CS high and DATA clockout
pauseus 100

' clock out configuration bytes from PIC to RF-24G
for i = 1 to 15
'set config one byte at a time from MSB to LSB
shiftout Data_Saida, Clock, MSBFIRST, [Configuracao[i]\8]
next

' configuration is set on the falling edge of CS, bring it low
low CS
low CE

' apaga leds de configuracao
low Led_Rx
low Led_Tx
RETURN


translate to 16F873A not work.....than i use serout, invalid caracters

change portb 16F628A to portc 16F873A



CE var PORTC.5 '0
CS var PORTC.4 '0
DR var PORTC.3 '1
Clock var PORTC.2 '0
Data_Entrada var PORTC.1 '1
Data_Saida var PORTC.1 '1

PORTC = %00000000
TRISC = %01001010 ' 0 = Output, 1 = Input

Bruce
- 3rd March 2007, 16:40
Have you disabled the A/D module to use PORTA pins as digital?

ADCON1 = 7

mpardinho
- 3rd March 2007, 17:04
yes, ADCON1 = 7

if i test simple code serin, serout work 100%,
with my code i receive wrong caracters,
in 16f628 work perfect

mister_e
- 3rd March 2007, 22:08
mmm, what happen if you do a simple test and use DEBUG instead of SEROUT. As you're using a 4MHZ clock, SEROUT may work or not.

to me the built-in USART is always a better choice so far. and @4mhz, you'll end up with a nice error % -> 0.16%


DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_SPBRG 25 ' 9600 Baud @ 4MHz, 0.16%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

The main problem is that you'll have to change a little bit your actual setup... but...

EDIT:


Data_Entrada var PORTC.1 '1
Data_Saida var PORTC.1 '1

Should they be on a different pin?

mpardinho
- 4th March 2007, 14:53
my code is for TRW-24G
Data_Entrada and Data_Saida is the same pin because TRW-24G use data and clock to send data


shiftout Data_Entrada, Clock, MSBFIRST, [Dados_Enviados[i]\8]
shiftout Data_Saida, Clock, MSBFIRST, [Dados_Enviados[i]\8]