PDA

View Full Version : PIC HSERIN problem



shaiqbashir
- 15th July 2008, 20:01
hi guys!

im using SPI between the two pics. here is my program for SPI master



'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 7/5/2008 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
Include "Modedefs.Bas"

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE HSER_SPBRG 25

define OSC 4

' Set LCD Data port
DEFINE LCD_DREG PORTD
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTC
' Set LCD Register Select bit
DEFINE LCD_RSBIT 0
' Set LCD Enable port
DEFINE LCD_EREG PORTC
' Set LCD Enable bit
DEFINE LCD_EBIT 1
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 8
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50

SSPEN VAR SSPCON1.5 'SSP Enable bit
CKP VAR SSPCON1.4 'Clock Polarity Select
SMP VAR SSPSTAT.7 'Data input sample phase
CKE VAR SSPSTAT.6 'Clock Edge Select bit
SSPIF VAR PIR1.3 'SPI interrupt flag

i VAR BYTE
j var byte
as var byte[5]
com VAR byte[2]
sen var byte[6]
teds1 var byte[10]
teds2 var byte[10]
teds3 var byte[10]
op1 var byte
teds_out var byte[25]
sen0 var byte[10]
sen_all var byte[20]
a var byte[10]

TRISC = %10010000 'set PORTC I/O



SSPEN = 1 'enable SPI pins
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear buffer full status
SMP = 0 'sample in middle of data
SSPCON1.0 = 0
SSPCON1.1 = 0
SSPCON1.2 = 0
SSPCON1.3 = 0

main:
pause 100
j=0
hserout["initialized....",13,10]
pause 1000
hserin [wait("x"),str com\2]
pause 100
hserout["you entered",str com\3,13,10]
pause 100
goto loop


loop:
if j>10 then main
high portB.0
pause 25
low portB.0
pause 25
GoSub getdata 'initiate conversion and receive data
LCDOut $fe, 1, STR a\5, DEC a[5] 'display received string
Pause 100
hserout [dec a[4]," ",dec a[5]," ",dec a[6],13,10]
pause 100
j=j+1
goto loop 'do it forever


getdata:

SSPBUF = "?" 'send ? to start conversion
GoSub letclear 'wait for buffer to clear
IF SSPBUF<>"!" Then getdata 'wait for reply (!)

For i = 0 to 5 'loop for 6 characters
SSPBUF = 0 'write to SSPBUF to start clock
GoSub letclear 'wait for receipt
a[i] = SSPBUF 'store received character in array
Next i 'get next character

Return


letclear:

IF SSPIF = 0 Then letclear 'wait for SPI interupt flag
PauseUs 25 '25uS fudge factor
SSPIF = 0 'reset flag
Return





and here is my SPI slave code



'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 7/5/2008 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
Include "Modedefs.Bas"

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE HSER_SPBRG 25

define osc 4

DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 8
DEFINE ADC_SAMPLEUS 50


' Set LCD Data port
DEFINE LCD_DREG PORTD
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTC
' Set LCD Register Select bit
DEFINE LCD_RSBIT 0
' Set LCD Enable port
DEFINE LCD_EREG PORTC
' Set LCD Enable bit
DEFINE LCD_EBIT 1
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 8
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50


out VAR BYTE[8]
out2 var byte[8]
out3 var byte[8]
teds var byte[20]
teds_out var byte[11]
sen0 var byte[10]
sen_all var byte[20]

i var byte
j var byte
op1 var byte

SSPEN VAR SSPCON1.5
CKP VAR SSPCON1.4
SMP VAR SSPSTAT.7
CKE VAR SSPSTAT.6
SSPIF VAR PIR1.3

SCL Var PortB.6
SDA Var PortB.7


TRISC = %11011111 'set PORTC I/O
SSPCON1 = %00000101 'configure SPI slave, no SS

CKP = 0
CKE = 0
SSPIF = 0
SMP = 0

ADCON1 = $00


INTCON3.3=1
INTCON2.7=0
INTCON2.5=0
INTCON3.0=0
INTCON3.6=1

'***************************************

' PicBasic Pro SPI slave program (see SPImast.bas for connections)


' Allocate RAM
dataout VAR BYTE[8] 'Data out array



'ADCON1 = $00 'PORTA.0 analog, rest PORTA and PORTE pins to digital

dataout[0] = "A" 'Preset output data to "ADC= "
dataout[1] = "D"
dataout[2] = "C"
dataout[3] = "="
dataout[4] = " "

find:
if INTCON3.0=1 THEN loop

HIGH PORTC.1
PAUSE 100
LOW PORTC.1
PAUSE 100
goto find


loop:

INTCON3.3=0
INTCON3.0=0

SSPEN = 0 'disable/enable SSP to reset port
SSPEN = 1
GoSub letclear 'wait for byte received
IF (SSPBUF <> "?") Then loop 'wait for ? to start conversion

ADCIN 1, dataout[5] 'Read ADC channel 0, store in 6th position of string
pause 50
GoSub senddata 'send "!" and string of data
INTCON3.3=1
pause 10
GoTo loop 'do it forever


senddata:
GoSub letclear 'wait until buffer ready
SSPBUF = "!" 'send reply

For i = 0 to 5 'loop for 6 array locations
GoSub letclear 'wait until buffer ready
SSPBUF = dataout[i] 'send array variable
Next i 'next location

Return


letclear:
IF SSPIF = 0 Then letclear 'wait for interrupt flag
SSPIF = 0 'reset flag

Return









The problem is coming with SPI master. It is connected with serial port to Labview. when the circuit is switched on it displayes

Initialized...

(then it ask to enter two string data)

then it displays

You pressed (string data that we entered)

and then it displays sensor reading 11 times.

I have put the condition that when j>10 then goto main and ask for the string data again.

But this doesnt work. It displays sensor data correctly 11 times, and then initialized is displayed and then it halts, i enter the values but nothing happens. Please tell me what is the problem with this,


I shall be thankful to u for this

Archangel
- 16th July 2008, 06:06
I cannot point to it, but I am betting there is an interrupt flag that gets set the first go around that you need to clear in code.

Bruce
- 16th July 2008, 23:42
It may help by adding DEFINE HSER_CLROERR 1 to your USART init section.

shaiqbashir
- 17th July 2008, 03:00
ok guys!

so i have studied a few threads here and made the following modifications in the SPI master program. but again the same problem is occuring. In the first iteration, the program runs very fine, but when it comes to hserin the second time, the program halts and no data is hserin in the pic.


plz help me , this is my final year project, and due to this the whole project is getting affected.




'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 7/5/2008 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
Include "Modedefs.Bas"

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE HSER_SPBRG 25
DEFINE HSER_CLROERR 1


define OSC 4

' Set LCD Data port
DEFINE LCD_DREG PORTD
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTC
' Set LCD Register Select bit
DEFINE LCD_RSBIT 0
' Set LCD Enable port
DEFINE LCD_EREG PORTC
' Set LCD Enable bit
DEFINE LCD_EBIT 1
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 8
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50

SSPEN VAR SSPCON1.5 'SSP Enable bit
CKP VAR SSPCON1.4 'Clock Polarity Select
SMP VAR SSPSTAT.7 'Data input sample phase
CKE VAR SSPSTAT.6 'Clock Edge Select bit
SSPIF VAR PIR1.3 'SPI interrupt flag
OERR var RCSTA.1
CREN var RCSTA.4
RCIF VAR PIR1.5
RCIE var PIE1.5

i VAR BYTE
j var byte
as var byte[5]
com VAR byte[2]
sen var byte[6]
teds1 var byte[10]
teds2 var byte[10]
teds3 var byte[10]
op1 var byte
teds_out var byte[25]
sen0 var byte[10]
sen_all var byte[20]
a var byte[10]
DUM VAR BYTE

TRISC = %10010000 'set PORTC I/O

INTCON=%11000000
PIE1.5=1


SSPEN = 1 'enable SPI pins
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear buffer full status
SMP = 0 'sample in middle of data
SSPCON1.0 = 0
SSPCON1.1 = 0
SSPCON1.2 = 0
SSPCON1.3 = 0

main:
RCIE=1
pause 100
if RCIF=1 then in
hserout["initialized....",13,10]
pause 10
goto main

in:
dum=RCREG
pause 50
hserout["you entered",dec dum,13,10]
pause 100
goto loop


loop:
SSPEN=1
pause 100
for j=0 to 10
high portB.0
pause 25
low portB.0
pause 25
GoSub getdata 'initiate conversion and receive data
LCDOut $fe, 1, STR a\5, DEC a[5] 'display received string
Pause 100
hserout [dec a[4]," ",dec a[5]," ",dec a[6],13,10]
pause 100
next j
pause 100
goto check


getdata:

SSPBUF = "?" 'send ? to start conversion
GoSub letclear 'wait for buffer to clear
IF SSPBUF<>"!" Then getdata 'wait for reply (!)

For i = 0 to 5 'loop for 6 characters
SSPBUF = 0 'write to SSPBUF to start clock
GoSub letclear 'wait for receipt
a[i] = SSPBUF 'store received character in array
Next i 'get next character

Return


letclear:

IF SSPIF = 0 Then letclear 'wait for SPI interupt flag
PauseUs 25 '25uS fudge factor
SSPIF = 0 'reset flag
Return

check:
RCIE=0
WHILE RCIF=1
DUM=RCREG
hserout["find",13,10]
pause 100
CREN=0
pause 25
WEND

goto main

Archangel
- 17th July 2008, 07:13
Seems like you need to follow CREN=0 with CREN=1 to reenable continuous receive

skimask
- 17th July 2008, 14:19
Seems like you need to follow CREN=0 with CREN=1 to reenable continuous receive

DEFINE HSER_CLROERR 1...PBP handles that 'internally.

skimask
- 17th July 2008, 14:20
so i have studied a few threads here and made the following modifications in the SPI master program. but again the same problem is occuring. In the first iteration, the program runs very fine, but when it comes to hserin the second time, the program halts and no data is hserin in the pic.
Which of the 0 HSERIN statements in the above code would that be?


plz help me , this is my final year project, and due to this the whole project is getting affected.

shaiqbashir
- 18th July 2008, 01:25
actually my code is something like this



Include "Modedefs.Bas"

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE HSER_SPBRG 25
DEFINE HSER_CLROERR 1


define OSC 4

' Set LCD Data port
DEFINE LCD_DREG PORTD
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTC
' Set LCD Register Select bit
DEFINE LCD_RSBIT 0
' Set LCD Enable port
DEFINE LCD_EREG PORTC
' Set LCD Enable bit
DEFINE LCD_EBIT 1
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 8
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50

SSPEN VAR SSPCON1.5 'SSP Enable bit
CKP VAR SSPCON1.4 'Clock Polarity Select
SMP VAR SSPSTAT.7 'Data input sample phase
CKE VAR SSPSTAT.6 'Clock Edge Select bit
SSPIF VAR PIR1.3 'SPI interrupt flag
OERR var RCSTA.1
CREN var RCSTA.4
RCIF VAR PIR1.5
RCIE var PIE1.5

i VAR BYTE
j var byte
as var byte[5]
com VAR byte[2]
sen var byte[6]
teds1 var byte[10]
teds2 var byte[10]
teds3 var byte[10]
op1 var byte
teds_out var byte[25]
sen0 var byte[10]
sen_all var byte[20]
a var byte[10]
DUM VAR BYTE

TRISC = %10010000 'set PORTC I/O

INTCON=%11000000
PIE1.5=1


SSPEN = 1 'enable SPI pins
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear buffer full status
SMP = 0 'sample in middle of data
SSPCON1.0 = 0
SSPCON1.1 = 0
SSPCON1.2 = 0
SSPCON1.3 = 0

main:
RCIE=1
pause 100
if RCIF=1 then in
hserout["initialized....",13,10]
pause 10
goto main

in:
hserin[wait("x"),str com\2]
pause 50
hserout["you entered",str com\3,13,10]
pause 100
goto loop


loop:
SSPEN=1
pause 100
for j=0 to 10
high portB.0
pause 25
low portB.0
pause 25
GoSub getdata 'initiate conversion and receive data
LCDOut $fe, 1, STR a\5, DEC a[5] 'display received string
Pause 100
hserout [dec a[4]," ",dec a[5]," ",dec a[6],13,10]
pause 100
next j
pause 100
goto check


getdata:

SSPBUF = "?" 'send ? to start conversion
GoSub letclear 'wait for buffer to clear
IF SSPBUF<>"!" Then getdata 'wait for reply (!)

For i = 0 to 5 'loop for 6 characters
SSPBUF = 0 'write to SSPBUF to start clock
GoSub letclear 'wait for receipt
a[i] = SSPBUF 'store received character in array
Next i 'get next character

Return


letclear:

IF SSPIF = 0 Then letclear 'wait for SPI interupt flag
PauseUs 25 '25uS fudge factor
SSPIF = 0 'reset flag
Return

check:
RCIE=0
WHILE RCIF=1
DUM=RCREG
WEND

CREN=0
pause 25
CREN=1
pause 25
goto main



What basically is a happening that the first HSERIN instrunction which is
HSERIN[wait("x"),str com\2] is executing only once, that is when i turn on the power. the pIC successfuly takes 2 characters as an input after "x" and displays sensors data 10 times which it has acquired from SPI slave through SPI communications. and then when it comes to HSERIN[wait("x"),str com\2] again, it doesnt execute at all, and the PIC seems to be halted.

One observation that i got is this that when i donot use SPI in the above program, im able to use HSERIn again and again. So i think that the problem is somewhere in using SPI. PLease help me dear

Dave
- 18th July 2008, 12:25
shaiqbashir, Where is your interrupt routine? You are setting up the interrupts but only looking at the interrupt input flags. There is no need to setup the global interrupt bits as once again all you are doing is (polling) looking at the input flag bits..

Dave Purola,
N8NTA

shaiqbashir
- 20th July 2008, 02:04
ok i will disable global interrupts and will check it. But interrupt is not a problem here. The first time it executes very perfectly, it is the second time that is creating a huge problem. Second time, on hserin statement, the PIC hangs.
kindly help

shaiqbashir
- 20th July 2008, 11:08
ok guys! the problem is solved by the Grace of Almighty Allah.

I removed the lcdout command, and now the coding works well:)

Thanks a lot for ur kind help