Dave,
Good point, it's a lab power supply so i didn't had any doubt about it ... I'm going to check it now !
Thanks !
Wilbert
Dave,
Good point, it's a lab power supply so i didn't had any doubt about it ... I'm going to check it now !
Thanks !
Wilbert
And how is the brown out fuse set?
Dave
Always wear safety glasses while programming.
Checked Power supply -> OK
Added additional caps -> problem remains
Changed Power supply -> problem remains
Changed cables -> problem remains
Even when i touch VDD with a none connected cable (15 cm), i see the clock stopping on multiple processors ...
Configuration bits:
device pic16F886, hs_osc, wdt_on, mclr_on, lvp_off, protect_off, cpd_on
Hello Wilbert
I wonder if you have a firmware issue here. Have you handled the case when the data stream is corrupted? Perhaps your firmware is hanging on data corruption. The symptoms you describe could be due to this.
Jerson
Hello Jerson,
My first impression was also having a firmware issue. So i made a modification of the firmware so the only thing the controller is doing is listening to the RS485 bus and every 250ms reading PORTC of the controller when i put the controller in initialization mode through the RS485 bus. Every time he checks PORTC, a led flashes. This works perfectly, he read and understand commands from the RS485 bus, the led flashes but when i touch the ground with a non connected wire, the led stop flashing. When i connect a fixed wire to the ground, every thing works fine and the clock doesn't stop touching inputs ...
So now i made a second modification, only a flashing led, and guess what, the clock doesn't stop what makes me think again something is wrong in my code ...
This is my code:
'************************************************* ***************
'* Name : Module_O *
'* Author : Wilbert Ingels *
'* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 19/11/2007 *
'* Version : 1.0 *
'* Notes : New Hardware 16F886 *
'* : *
'************************************************* ***************
'include "modedefs.bas"
DEFINE OSC 20
'define I2C_SLOW 1
'TRISC = %11111111 'PORTC is an input
TRISB = %11111111
TRISA = %11111111
CCP1CON = 0 ' DISABLE CAPTURE COMPARE MODULE
ADCON1 = 15
'CMCON = 7
ANSEL=0
ANSELH=0
'OSCCON = %01110111
data @0,"3","8","4","I","E" ' Device address
define DEBUG_REG PORTA
define DEBUG_BIT 1
define DEBUG_BAUD 115200
define DEBUG_MODE 0
define DEBUGIN_REG PORTA
define DEBUGIN_BIT 1
define DEBUGIN_MODE 0
'---------------------------------------------
'Variables and constants
'---------------------------------------------
TxD var PORTA.3 ' Transmit pin RS232
RxD var PORTA.2 ' Receive pin RS232
command var byte ' Storage for command
i var byte ' Storage for loop counter
temp var word ' Storage for temperature
DQ var PORTC.0 ' Alias DS1820 data pin
DQ_DIR var TRISC.0 ' Alias DS1820 data direction pin
RS485_mode var PORTA.0 ' Mode for RS485 chip (send or receive)
RS485_SR var PORTA.1 ' RS485 send/receive pin
led_blue var PORTA.5
onewire var PORTB.4
RS9600 con 84
RS19200 con 16416
Buffer_size2 con 6 'Number of bytes received from RS485 bus
RS485string var byte(buffer_size2)
Module_ID var Byte(4)
x var byte
y var byte
z var byte
u var byte
v var byte
s1 var byte
s2 var byte
w1 var word
mode var byte 'L->Live Mode,I->Initialization mode
mode_ID var byte 'N->New module,E->Existing module
sda var PORTB.0 ' I2C bus
rcl var PORTB.1
'---------------------------------------------
'Initialization
'---------------------------------------------
mode = "L" 'mode="L" -> live mode, mode="I" -> initialization mode
input Rxd 'RxD RS232 is an input
low RS485_mode 'RS485 receive mode
low PORTA.4 'Second pin of the blue led
read 4,mode_id 'Mode_ID
read 3,x
read 2,y
read 1,z
read 0,u
if mode_id<>"E" then 'ID is new and must be generated
gosub test_module_type
mode_id="N"
for i=0 to 2
random w1 'generate random ID
if w1.byte0<>255 and w1.byte0<>0 then 'Random value if ok
module_id[i]=w1.byte0
else 'Random value is not ok
Module_id[i]=171 'Random value (My bithday)
endif
next i
serout2 TxD,RS19200,[13,10,"New Module ID: ",dec module_id[0]," ",dec module_id[1]," ",dec module_id[2]," ",module_id[3]," ",mode_id,13,10]
else 'It's an existing ID
Module_ID[3] = x 'copy ID from Eeprom into RAM
Module_ID[2] = y
Module_ID[1] = z
Module_ID[0] = u
endif
serout2 TxD,RS19200,[13,10,"Startup ",Module_id[3],y,z,u,13,10]
'---------------------------------------------
'Start Main Routine
'---------------------------------------------
loop:
if mode="L" then 'bus is in Live mode
gosub rs485_receive
endif
if mode="I" then 'bus is in Initialization mode
gosub rs485_receive_init 'check if broadcast messages are received
gosub check_inputs 'during initialization, inputs needs to be checked
endif
goto loop
'-----------------------------------------------
'Subroutines
'-----------------------------------------------
'-----------------------------------------------
' Test which type of module it is
'-----------------------------------------------
test_module_type:
input PORTB.0
input PORTB.1
TRISC = %11111111 'PORTC is an input
if PORTC=255 then 'intput module
'serout2 TxD,RS19200,[13,10,"I'm an input module ",13,10]
Module_ID[3] = "I"
else 'output module
if PORTB.0=1 and PORTB.1=1 then 'then i'm an output module
'serout2 TxD,RS19200,[13,10,"I'm an output module ",13,10]
TRISC = %00000000 'PORTC is an output
PORTC=0
i2cwrite sda,rcl,%01000000,0,[0],module_error
'serout2 TxD,RS19200,[" and I've Dimmer functionality ",13,10]
Module_ID[3] = "D"
goto end_module
module_error:
'serout2 TxD,RS19200,[" and I've Relais functionality ",13,10]
Module_ID[3] = "O"
end_module:
else
'serout2 TxD,RS19200,[13,10,"Undefined module (B0 and B1 <>1, C<>255) ",13,10]
x=255 'module is invalid
endif
endif
return
'-----------------------
' RS485
'-----------------------
rs485_receive: 'receive routine when in Live mode
low RS485_mode 'receive mode
debugin [WAIT("ST"),rs485string[5],rs485string[4],rs485string[3],rs485string[2],rs485string[1],RS485string[0]] 'wait untill a message comes in starting with "ST"
'serout2 TxD,RS19200,[DEC rs485string[5]," ",dec rs485string[4]," ",dec rs485string[3]," ",dec rs485string[2]," ",dec rs485string[1],13,10]
if rs485string[5]=Module_id[3] and rs485string[4]=Module_id[2] and rs485string[3]=Module_id[1] and rs485string[2]=Module_id[0] then 'message is for this module
if Module_id[3]="I" then 'input module
if RS485string[1]="I" then 'instruction read inputs is received
high led_blue
y=PORTC
pause 4
s1=y
s2=0
gosub rs485_send
low led_blue
endif
endif
if module_id[3]="D" or module_id[3]="O" then 'output module
PORTC = rs485string[1]
high led_blue
pause 4
s1="K"
s2=0
gosub rs485_send
low led_blue
endif
endif
if rs485string[5]=0 and rs485string[4]=0 and rs485string[3]=0 and rs485string[2]=0 then 'broadcast message is received
'serout2 TxD,RS19200,["Data received L ",RS485string[0],RS485string[1],13,10]
Gosub check_init
endif
low RS485_mode 'receive mode
return
rs485_send:
high RS485_mode 'send mode
debug Module_ID[3],Module_ID[2],Module_ID[1],Module_ID[0],s1,s2
low RS485_mode 'receive mode
return
rs485_receive_init: 'recive routine when in initialization mode
low RS485_mode 'receive mode
debugin 250,rs485_cont,[WAIT("ST"),rs485string[5],rs485string[4],rs485string[3],rs485string[2],rs485string[1],RS485string[0]] 'wait untill a message comes in starting with "ST"
'serout2 TxD,RS19200,["Data received I ",RS485string[0],RS485string[1],13,10]
if rs485string[5]=0 and rs485string[4]=0 and rs485string[3]=0 and rs485string[2]=0 then 'broadcast message is received
'serout2 TxD,RS19200,["Broadcast received ",RS485string[0],RS485string[1],13,10]
gosub check_init
endif
goto init_end
rs485_cont:
'here code when timeout
init_end:
return
check_init:
if rs485string[1]="I" and rs485string[0]="I" then 'initialization mode activated
mode="I" 'main routine will do a different loop
high led_blue
serout2 TxD,RS19200,[13,10,"Init mode ",mode,13,10]
endif
if rs485string[1]="L" and rs485string[0]="L" then 'live mode activated
mode="L" 'main routine will do a different loop
low led_blue
serout2 TxD,RS19200,[13,10,"Live mode ",mode,13,10]
endif
return
check_inputs: 'during initialization, inputs needs to be checked
if module_id[3]="I" then 'Module is an input Module
'TRISC = %11111111 'PORTC is an input
low led_blue
serout2 TxD,RS19200,["Check inputs ",mode,13,10]
x=PORTC
if x<>255 then 'Input button is pressed
s1=x
s2=mode_id 'E->existing,N->New
gosub rs485_send
serout2 TxD,RS19200,["RS485send ",13,10]
endif
endif
if module_id[3]="D" or module_id[3]="O" then 'Output module (Dimmer or digital Output)
x=PORTB.2
if x=0 then 'initialization button is pressed
s1=0
s2=mode_id 'E->existing,N->New
gosub rs485_send
endif
endif
high led_blue
return
end
Dave,
You're right, it is a firmware problem. The clock is not stopping but following is happening:
This is the instruction causing problems:
debugin 50,rs485_cont,[WAIT("ST"),rs485string[5],rs485string[4],rs485string[3],rs485string[2],rs485string[1],RS485string[0]]
When a not connected wire is touched to one of the connection, a small spike is generated on the RS485 bus. This what i think that happens than: The Debugin receives 1 bit and is endless waiting for the other bits to come. I've the impression that the timeout only works if a full byte is received.
Is my statement correct? How can this be resolved?
Many thanks !
Wilbert
Good snooping. The glitch is causing your firmware NOT to see the ST sequence. So, you go on waiting till you catch it .... and guess what -- it doesnt come again!
A better technique would be to
1. Wait for byte1=S
2. Wait for byte2=T
3. Process packet
I hope you can fix this yourself.
Jerson
Bookmarks