PDA

View Full Version : Rc int on 16f874



hakan
- 14th July 2007, 20:09
Hello!
I'm trying to connect to my PIC via a interupt on receivd rs232. The hardware is connected as attachment "Hardware Setup.jpg" (MicroCodeLoader).
My problem is when I try to connect nothing happends.

Here is my code:
<PBP COD>
DEFINE LOADER_USED 1
' Set X-tal to 4 MHz
DEFINE OSC 4

DEFINE HSER_RCSTA 90h 'Hser receive status init
DEFINE HSER_TXSTA 20h 'Hser transmit status init
DEFINE HSER_BAUD 2400 'Hser baud rate

---

SPBRG = 25'103 'SET TO 2400bps

On Interrupt Goto myint ' Define interrupt handler
INTCON = $C0 ' Enable INTE interrupt
PIE1.5 = 1

---

' Interrupt handler
Disable ' No interrupts past this point
myint:
PIR1.5 = 0
lcdout $FE, LcdLine2, " Connected "
pause 1000

Resume ' Return to main program
Enable

end

</PBP COD>

Whats wrong? I can't get it to work!
Pleas help me.

Regards

Håkan
( I hope you can understand my por english and code)

mister_e
- 14th July 2007, 20:15
we will need to see your whole code first... sure there's few things missing here..

hakan
- 14th July 2007, 20:30
OK, here is my entire code.
I have a 2*16 LCD, a button and a shaft encoder connected.

Regards
Håkan

DEFINE LOADER_USED 1
' Set X-tal to 4 MHz
DEFINE OSC 4

DEFINE HSER_RCSTA 90h 'Hser receive status init
DEFINE HSER_TXSTA 20h 'Hser transmit status init
DEFINE HSER_BAUD 2400 'Hser baud rate

' Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 7
'LCD read/write port
DEFINE LCD_RWREG PORTB
'LCD read/write bit
DEFINE LCD_RWBIT 6
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 5
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' 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

SPBRG = 25'103 'SET TO 2400bps


On Interrupt Goto myint ' Define interrupt handler
INTCON.6 = 1' = $C0 ' Enable INTE interrupt
PIE1.5 = 1

' Shaft Encoder
clock var PORTD.0
stat var PORTD.1
' Button
exec var PORTD.2
' ICD
txPin var PORTC.6
rxPin Var PORTC.7
' LCD background
LED var PORTD.3

T2400 CON 396'1021
Inverted CON $4000
Baud CON T2400 '+ Inverted

LcdCls CON $01 ' clear the LCD
LcdLine1 CON $80 ' DDRAM address of line 1
LcdLine2 CON $C0 ' DDRAM address of line 2

countPulses VAR Byte
print VAR Bit
a VAR Byte
b VAR Byte
c var byte
idx var byte
turnOff VAR Bit
failSafe VAR Byte


'Init EE
read $00,a
if a <> 0 then
' First digit --> Delay when turnig off
' Second digit --> Card
' Therd digit --> Relay

' ALL system
write 2,$A ' --> Start adr
write 3,$D ' --> Stop adr

write $A, 11
write $B, 12
write $C, 114
write $D, 16

' Main Sound --> Mixerboard, eq, Ampl.

' Sub Sound --> Only Mixer and Eq etc.

' Main Displ --> Display Comp., screens and Projector

' Sub Displ --> Only Display Comp. and Screens
write $00, $1
endif

PAUSE 1000 ' Wait for LCD to startup

countpulses = 1
turnOff = 0
print = 0

high led


main:

GOSUB readpuls
GOSUB writeDisp
GOSUB readExec
GOTO main

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

'**************************
writeDisp:
IF (print) THEN
SELECT case countPulses

CASE 1
lcdout $FE, LcdCls, "Start ALL Syst. "
CASE 2
lcdout $FE, LcdCls, "Main Sound Syst."
CASE 3
lcdout $FE, LcdCls, "Sub Sound Syst. "
CASE 4
lcdout $FE, LcdCls, "Main Disp System"
CASE 5
lcdout $FE, LcdCls, "Sub Disp System "

CASE ELSE
lcdout $FE, LcdCls, " Illigal value "
END SELECT

Print = 0
ENDIF
RETURN

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

'**************************
readExec:
IF (exec = 0) THEN

lcdout $FE, LcdLine2, " Starting Up "
gosub set
GOSUB logOff
lcdout $FE, LcdLine2, " Shuting Down "
gosub clr
lcdout $FE, LcdLine2, " "

ENDIF

RETURN

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

' '**************************
readPuls:
IF(clock = 0) THEN
IF (stat) THEN
countpulses = ((countpulses - 1)//5)+1'(countpulses - 1) MIN 1'((countpulses - 1)//5)
ELSE
countpulses = ((countpulses + 1)//5)+1'(countpulses + 1) MAX 5'((countpulses + 1)//5)
ENDIF
print = 1
again: IF clock = 0 THEN GOTO again

high led
ENDIF
RETURN


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

' '**************************
set:
read countpulses*2, a
read countpulses*2+1, b

for idx = a to b
read idx,c
SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
pause 100
SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
pause 1000
next
RETURN

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

' '**************************
clr:
read countpulses*2, a
read countpulses*2+1, b

for idx = b to a step -1
read idx,c

if ((c dig 2)= 1) then
pause 5000
endif

SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
pause 100
SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
pause 1000
next
RETURN

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

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

logOff:
lcdout $FE, LcdLine2, " All systems GO "
failsafe = 0
turnOff = 0
logOffloop:
IF (exec = 0) THEN
PAUSE 10
loop1: IF (exec = 0) THEN loop1
IF turnOff THEN
RETURN
ELSE
lcdout $FE, LcdLine2, "OK, to turn OFF?"
PAUSE 200
failsafe = 0
turnOff = 1
ENDIF
ELSE
failSafe = failSafe + 1
IF failSafe > 10 THEN
turnOff = 0
lcdout $FE, LcdLine2, " All systems GO "
PAUSE 10
failsafe = 0
ENDIF

ENDIF
PAUSE 100
GOTO logOffLoop


' Interrupt handler
Disable ' No interrupts past this point
myint:
PIR1.5 = 0
lcdout $FE, LcdLine2, " Connected "
pause 1000

Resume ' Return to main program
Enable

end

mister_e
- 14th July 2007, 21:48
PIR1,5 is a read only register, so you can't aletr it. To clear the RCIF flag, you need to read RCREG untill it's empty.



' Interrupt handler
Disable ' No interrupts past this point
myint:
While RCREG
Dump=RCREG
WEND

lcdout $FE, LcdLine2, " Connected "
pause 1000

Resume ' Return to main program
Enable

end

and then, as you don't use any HSERIN/HSEROUT, you need to change


DEFINE HSER_RCSTA 90h 'Hser receive status init
DEFINE HSER_TXSTA 20h 'Hser transmit status init
DEFINE HSER_BAUD 2400 'Hser baud rate

to


RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 25 ' 2400 Baud @ 4MHz, 0.17%


and...


INTCON.6 = 1' = $C0 ' Enable INTE interrupt
to

INTCON=%11000000

maybe i forgot few things... try it ;)

hakan
- 15th July 2007, 08:25
Thanks for your respond Mister_e.
I have change the code, but no sucess.
Are I'm thinking wrong. Shoudn't I jump into the interrupt rutine when I send a few letters with "Serial Communicator" (MicroCodeStudio +) to the PIC.
Nothing hapends when I try.

hakan
- 15th July 2007, 18:57
I start over from the begining and now it's working.

Thanks for all help!

/Håkan



' Pic Definition
' ==============
' Using PIC 16F874 @ 4MHZ and bootloader
'
DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 7 ' Set LCD Register Select bit
DEFINE LCD_RWREG PORTB 'LCD read/write port
DEFINE LCD_RWBIT 6 'LCD read/write bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 5 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

' Hardware configuration
' ======================
'
'
TRISC = %10000000 ' PORTC.7 is the RX input
' PORTC.6 is the TX output
On Interrupt Goto myint ' Define interrupt handler
INTCON = %11000000 ' Enable INTE interrupt
PIE1.5 = 1

T2CON = %01111111
PIE1.1 = 1

' LCD background
LED var PORTD.3
' LCD
LcdCls CON $01 ' clear the LCD
LcdLine1 CON $80 ' DDRAM address of line 1
LcdLine2 CON $C0 ' DDRAM address of line 2
' Shaft Encoder
clock var PORTD.0
stat var PORTD.1
' ExecuteButton
exec var PORTD.2

' Serial communication definition
' ===============================
' Using internal USART and MAX232 to interface to PC
'
'RCSTA = $90
DEFINE HSER_RCSTA 90h ' enable serial port, enable continuous receive
'TXSTA = $20
define HSER_TXSTA 20h ' enable transmit, BRGH=1
'SPBRG = 25
define HSER_SPBRG 25 ' set baudrate to 2400
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

T2400 CON 396
Inverted CON $4000
Baud CON T2400 '+ Inverted

txPin var PORTC.6
rxPin Var PORTC.7
' Alias definition
' ================
'
'
RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)
OERR var RCSTA.1 ' Overrun error
CREN var RCSTA.4 ' Continuous receive

dump var byte
countPulses VAR Byte
print VAR Bit
a VAR Byte
b VAR Byte
c var byte
idx var byte
turnOff VAR Bit
failSafe VAR Byte
timer var word

' Hardware initialisation
' =======================
'
'
pause 10 ' safe start-up delay
countpulses = 1
turnOff = 0
print = 0
high led

Main:
GOSUB readpuls
GOSUB writeDisp
GOSUB readExec
pause 10
goto main

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

'**************************
writeDisp:
IF (print) THEN
SELECT case countPulses

CASE 1
lcdout $FE, LcdCls, "Start ALL Syst. "
CASE 2
lcdout $FE, LcdCls, "Main Sound Syst."
CASE 3
lcdout $FE, LcdCls, "Sub Sound Syst. "
CASE 4
lcdout $FE, LcdCls, "Main Disp System"
CASE 5
lcdout $FE, LcdCls, "Sub Disp System "

CASE ELSE
lcdout $FE, LcdCls, " Illigal value "
END SELECT

Print = 0
ENDIF
RETURN

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

'**************************
readExec:
IF (exec = 0) THEN
lcdout $FE, LcdLine2, " Starting Up "
gosub set
GOSUB logOff
lcdout $FE, LcdLine2, " Shuting Down "
gosub clr
lcdout $FE, LcdLine2, " "

ENDIF

RETURN

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

' '**************************
readPuls:
IF(clock = 0) THEN
IF (stat) THEN
countpulses = ((countpulses - 1)//5)+1'(countpulses - 1) MIN 1'((countpulses - 1)//5)
ELSE
countpulses = ((countpulses + 1)//5)+1'(countpulses + 1) MAX 5'((countpulses + 1)//5)
ENDIF
print = 1
high led
PIE1.0 = 1
again: IF clock = 0 THEN GOTO again

high led
ENDIF
RETURN

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

' '**************************
set:
read countpulses*2, a
read countpulses*2+1, b

for idx = a to b
read idx,c
'SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
hserout [13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
pause 100
'SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
hserout [13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
pause 1000
next
RETURN

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

' '**************************
clr:
read countpulses*2, a
read countpulses*2+1, b

for idx = b to a step -1
read idx,c

if ((c dig 2)= 1) then
pause 5000
endif

'SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
hserout [13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
pause 100
hserout [13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
'SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
pause 1000
next
RETURN

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

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

logOff:
lcdout $FE, LcdLine2, " All systems GO "
failsafe = 0
turnOff = 0
logOffloop:
IF (exec = 0) THEN
PAUSE 10
loop1: IF (exec = 0) THEN loop1
IF turnOff THEN
RETURN
ELSE
lcdout $FE, LcdLine2, "OK, to turn OFF?"
PAUSE 200
failsafe = 0
turnOff = 1
ENDIF
ELSE
failSafe = failSafe + 1
IF failSafe > 10 THEN
turnOff = 0
lcdout $FE, LcdLine2, " All systems GO "
PAUSE 10
failsafe = 0
ENDIF

ENDIF
PAUSE 100
GOTO logOffLoop


' Interrupt handler
Disable ' No interrupts past this point
myint: if RCIF = 1 then
hserin[WAIT($01),a]
b = 0
while a = $02
hserin[a]
write b, a
b = b + 1
wend

While RCif ' Code until I write the right rutine
Dump=RCREG
WEND

lcdout $FE, LcdLine2, " Connected "
else
timer = timer+1
if timer > $FFFE then
low led
PIE1.1 = 0
endif

endif
Resume ' Return to main program
Enable