Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC TXIF flagbit problem

Status
Not open for further replies.

vishy71

Full Member level 2
Full Member level 2
Joined
Dec 16, 2010
Messages
126
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Iran
Activity points
2,296
Hi my friends.
I have an problem in my code!
I wanna send 'INIT' to PC and then read data from it but I can't!because of TXIF flag bit!
when I am config bits and registers suddnely TXIF =1 and my program jump in interrupt routin!
I clear TXIE but when i wanna send 'INIT' to pc I must check TXIF and it is 1 logic so 'N' dosn't send to pc!here is my code!but it is not complete because of my problem.
thanks

processor p16f628a
include <p16f628a.inc>

__config _HS_OSC & _WDT_OFF & _PWRTE_OFF & _BOREN_OFF & _BODEN_OFF _CP_OFF

ORG 0X00
GOTO MAIN ;MAIN CODE
ORG 0X04
CALL INTERRUPT ;INTERRUPT ROUTIN
RETFIE

MAIN:
MOVLW 0XC0
MOVWF INTCON
MOVLW 0X98
MOVWF RCSTA
BSF STATUS,5 ;JUMP TO BANK1
CLRF TRSIA ;PORTA AS AN OUTPUT
CLRF TRSIB ;PORTB AS AN OUTPUT
BSF TRISB,1 ;RX PIN
MOVLW 0X20
MOVWF TXSTA
MOVLW 0X0C
MOVWF SPBRG
MOVLW 0X20
MOVWF PIE1
BCF STATUS,5
;SEND INIT TO PC
MOVLW 'I'
MOVWF TXREG
BTFSS PIR1,4
GOTO $-1
BCF PIR1,4
MOVLW 'N'
MOVLW TXREG
BTFSS PIR1,4
GOTO $-1
BCF PIR1,4
MOVLW 'I'
MOVWF TXREG
BTFSS PIR1,4
GOTO $-1
BCF PIR1,4
MOVLW 'T'
MOVWF TXREG
BTFSC PIR1,4
GOTO $-1
BCF PIR1,4

MOVLW 0X00
GOTO $-1

INTERRUPT:
RETURN

END




Can I send data in other way?Please help me.thanks
 

Here's the fixed code:
Code:
	processor p16f628a
	include <p16f628a.inc>

	__config _HS_OSC & _WDT_OFF & _PWRTE_OFF & _BOREN_OFF & _BODEN_OFF & _CP_OFF

	ORG 0X00
	GOTO MAIN ;MAIN CODE

	ORG 0X04
	GOTO INTERRUPT ;INTERRUPT ROUTINE

MAIN:
	MOVLW 	0XC0
	MOVWF 	INTCON
	MOVLW 	0X98
	MOVWF 	RCSTA
	BSF 	STATUS,5 ;JUMP TO BANK1
	;BANKSEL TRISA
	CLRF	PORTA
	CLRF	PORTB
	CLRF 	TRISA ;PORTA AS AN OUTPUT
	CLRF 	TRISB ;PORTB AS AN OUTPUT
	BSF 	TRISB,1 ;RX PIN
	BSF		TRISB,2 ;TX PIN - according to datasheet
	MOVLW 	0X20
	MOVWF 	TXSTA
	MOVLW 	0X0C
	MOVWF 	SPBRG
	MOVLW 	0X0
	MOVWF 	PIE1
	BCF 	STATUS,5
;SEND INIT TO PC
	;BANKSEL TXREG

	MOVLW 	'I'
	MOVWF 	TXREG
	BTFSS 	PIR1,4
	GOTO 	$-1
	BCF 	PIR1,4

	MOVLW 	'N'
	MOVWF 	TXREG
	BTFSS 	PIR1,4
	GOTO 	$-1
	BCF 	PIR1,4

	MOVLW 	'I'
	MOVWF 	TXREG
	BTFSS 	PIR1,4
	GOTO 	$-1
	BCF 	PIR1,4

	MOVLW 	'T'
	MOVWF 	TXREG
	BTFSS 	PIR1,4
	GOTO 	$-1
	BCF 	PIR1,4

	MOVLW 	0X00
	GOTO 	$-1

INTERRUPT:
	RETFIE

END

Why did you enable TMR2 interrupt? That's why it was going to ISR. Fix this by clearing PIR1. You also had an error here:
Code:
MOVLW 'N'
MOVLW TXREG ; Should be MOVWF TXREG, I fixed it in the code
BTFSS PIR1,4
GOTO $-1

Then here:
Code:
__config _HS_OSC & _WDT_OFF & _PWRTE_OFF & _BOREN_OFF & _BODEN_OFF _CP_OFF

You didn't place a & between the _BODEN_OFF and _CP_OFF. I fixed it in the code.
Then, I changed this:
Code:
ORG 0X04
CALL INTERRUPT ;INTERRUPT ROUTIN
RETFIE

INTERRUPT:
RETURN
to
Code:
	ORG 0X04
	GOTO INTERRUPT ;INTERRUPT ROUTINE
INTERRUPT:
	RETFIE
although interrupt isn't going to trigger.


Hope this helps.
Tahmid.
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
thaaaaaaaaaaaaaaaaanks alot.it works.;-)
 

Hi
here is my new code!it works good but I have some problems!
at first when I wanna set RB3(bsf PORTB,3)it dosn't set and when I wanna set RA0,1,2 they dosn't set.
what is the problem!
RA0,1,2 are High and low in high frequency!!!!!!
but RA3 works good.
thanks

include <p16f628A.inc>
__config _WDT_OFF & _HS_OSC & _PWRTE_OFF & _CP_OFF & _LVP_OFF & _BODEN_OFF & _BOREN_OFF
CBLOCK 0X21
TMP0
TMP1
TMP2
ENDC
org 0x00
goto main
org 0x04
GOTO INTERRUPT

main
bsf STATUS,5
BSF PIE1,5
CLRF TRISB
BSF TRISB,1
BSF TRISB,2
CLRF TRISA
movlw .12
movwf SPBRG
movlw B'00100000'
movwf TXSTA
bcf STATUS,5
clrf PORTB
CLRF PORTA
MOVLW 0XC0
MOVWF INTCON
MOVLW B'10011000'
MOVWF RCSTA
CLRF PIR1

movlw .82 ;put 'R'
movwf TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .101 ;put 'e'
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .97 ;put a
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .100 ;put d
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .121 ;put y
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .33 ;put !
MOVWF TXREG


MOVLW 0X00
GOTO $-1

INTERRUPT:
BTFSC PIR1,5
CALL READ
CLRF PIR1
RETFIE

READ:
MOVF RCREG,0
MOVWF TMP0
BTFSC TMP1,0
GOTO READ2
MOVLW .100
SUBWF TMP0,0
BTFSS STATUS,Z
RETURN
BSF TMP1,0
RETURN
READ2:
BTFSC TMP1,1
GOTO READ3
BTFSC TMP0,0
BSF PORTB,0
BTFSS TMP0,0
BCF PORTB,0
BTFSC TMP0,1
BSF PORTA,4
BTFSS TMP0,1
BCF PORTA,4
BTFSC TMP0,2
BSF PORTB,4
BTFSS TMP0,2
BCF PORTB,4
BTFSC TMP0,3
BSF PORTB,5
BTFSS TMP0,3
BCF PORTB,5
BTFSC TMP0,4
BSF PORTB,6
BTFSS TMP0,4
BCF PORTB,6
BTFSC TMP0,5
BSF PORTB,7
BTFSS TMP0,5
BCF PORTB,7
BTFSC TMP0,6
BSF PORTA,0
BTFSS TMP0,6
BCF PORTA,0
BTFSC TMP0,7
BSF PORTA,1
BTFSS TMP0,7
BCF PORTA,1
BSF TMP1,1
RETURN
READ3:
BTFSC TMP1,2
GOTO CLEAR
BTFSC TMP0,0
BSF PORTA,2
BTFSS TMP0,0
BCF PORTA,2
BTFSC TMP0,1
BSF PORTA,3
BTFSS TMP0,1
BCF PORTA,3
CLRF TMP1
BSF TMP1,0
RETURN

CLEAR:
CLRF TMP1
bsf TMP1,0
RETURN
end

---------- Post added at 12:01 ---------- Previous post was at 11:58 ----------

my problem is in Read2 and Read3 labels!
and at first CLRF trisa and trisb and then bsf trisb,1 and bsf trisb,2
thanks
 

Hi,

When you use the Interrupt routine you must use the ' Context Saving ' as detailed in section 14 of the datasheet.

Basically when you break off your current work to service an interrupt you must save the important system information, otherwise the Interrupt routine will overwrite them and it cannot return to where it left off.
( some of the newer chips will do the context saving automatically )

Also, when in the Interrupt routine it is good practice if you do not Call or Goto outside that that routine- though for your test program it will probably work ok.
Try and keep the code in you ISR as short as possible so other interrupts can be serviced quickly, you can simply set Flags (bytes) in the ISR to be checked and acted upon in your main code.
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
Hi
I thinks ISR saving automatically because my programme return after interrupt routin where interrput has been occured!
my problem is on PORTB and PORTA bits!other fucntions works so good.see when I send 100 decimal to pic it runs to read other data from PC and set them on ports.after 100 frist byte has been shown on RB0,RB3,RB4,RB5,RB6,RB7,RA0,RA1
and 2bits of second byte hase been shown on RA2,RA3
RB0,RB4,RB5,RB6,RB7 and RA3 works good and shows data
but RB3 dosn't work and RA0,RA1,RA2 have frequency.(I mean RA0=1 then RA0=0 and RA0=1 then RA0=0....)thanks
 

Hi,

Just a quick reply - off out.

Have not looked at the rest of your code in detail, the main thing was to get the ISR right.

Yes it will 'return' correctly from the ISR , the location is saved in the Program Stack, which is save if you do not do too many Calls etc

However the Data held in W and the STATUS registers in your main code at the time of your Interrupt will be overwritten by the ISR so when you return to the main code it then has the wrong values in W and STATUS - so context saving /restoring is essential.
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
Hi

I have config INTCON register after RS232 and ports states.so interrupt can't occour during config registers.
 

Hi,

Think your best route will be to run your code without any Interrupts for the moment, while you prove things.

Simply send out your 'READY' to the PC, then just sit there waiting to READ / receive a byte of data, when its returned then test the received byte of data and toggle your Ports as needed.

When you have debugged it properly, then take a look at the interrupts in more detail.
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
Hi

not problem in my code!I thing my PIC16F628A has a problem in some port bits!
any way.
can you help me to intizaling SD card with PIC plz?can you give me an assembly project or assembly library for use SD card?
thanks
 

Hi,


You might have a problem with your code rather than the hardware - do you want to post it so I can test it on a Simulator ?

If you suspect a problem with your 628A chip just write a very simple 5 second led flasher routine to turn on each i/o pin on and off in sequence and check each pin with a led or your meter as the program turns them on.

Have not used any Assembler code for a SD card though expect there is some out there on the web, perhaps other members might have some proven code.
Have use an SD card on a demo project with C and a Pic24 - its quiet a complicated routine and the sd card holders are a pain to wire / solder up.

If you are just wanting to store some data to transmit to a PC later you might find the external EEPROM chips a simpler and cheaper way.
They use either the SPI or I2C ports of the 628A and have good Microchip Assembler tutorials.
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
Hi

I have tested ports.and then RB3 dosn't work good.but other pins work good.but in my RA0,1 dosn't work again!RA2 works because I write 0x07 in CMCON register to disable comprators.

my code is in this thread.just see the frist of this page.thanks;-)

---------- Post added at 20:44 ---------- Previous post was at 20:43 ----------

here is my code:
include <p16f628A.inc>
__config _WDT_OFF & _HS_OSC & _PWRTE_OFF & _CP_OFF & _LVP_OFF & _BODEN_OFF & _BOREN_OFF
CBLOCK 0X21
TMP0
TMP1
TMP2
ENDC
org 0x00
goto main
org 0x04
GOTO INTERRUPT

main
bsf STATUS,5
BSF PIE1,5
CLRF TRISB
BSF TRISB,1
BSF TRISB,2
CLRF TRISA
movlw .12
movwf SPBRG
movlw B'00100000'
movwf TXSTA
bcf STATUS,5
clrf PORTB
CLRF PORTA
MOVLW 0XC0
MOVWF INTCON
MOVLW B'10011000'
MOVWF RCSTA
CLRF PIR1

movlw .82 ;put 'R'
movwf TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .101 ;put 'e'
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .97 ;put a
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .100 ;put d
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .121 ;put y
MOVWF TXREG
BCF PIR1,4
BTFSS PIR1,4
GOTO $-1
movlw .33 ;put !
MOVWF TXREG


MOVLW 0X00
GOTO $-1

INTERRUPT:
BTFSC PIR1,5
CALL READ
CLRF PIR1
RETFIE

READ:
MOVF RCREG,0
MOVWF TMP0
BTFSC TMP1,0
GOTO READ2
MOVLW .100
SUBWF TMP0,0
BTFSS STATUS,Z
RETURN
BSF TMP1,0
RETURN
READ2:
BTFSC TMP1,1
GOTO READ3
BTFSC TMP0,0
BSF PORTB,0
BTFSS TMP0,0
BCF PORTB,0
BTFSC TMP0,1
BSF PORTA,4
BTFSS TMP0,1
BCF PORTA,4
BTFSC TMP0,2
BSF PORTB,4
BTFSS TMP0,2
BCF PORTB,4
BTFSC TMP0,3
BSF PORTB,5
BTFSS TMP0,3
BCF PORTB,5
BTFSC TMP0,4
BSF PORTB,6
BTFSS TMP0,4
BCF PORTB,6
BTFSC TMP0,5
BSF PORTB,7
BTFSS TMP0,5
BCF PORTB,7
BTFSC TMP0,6
BSF PORTA,0
BTFSS TMP0,6
BCF PORTA,0
BTFSC TMP0,7
BSF PORTA,1
BTFSS TMP0,7
BCF PORTA,1
BSF TMP1,1
RETURN
READ3:
BTFSC TMP1,2
GOTO CLEAR
BTFSC TMP0,0
BSF PORTA,2
BTFSS TMP0,0
BCF PORTA,2
BTFSC TMP0,1
BSF PORTA,3
BTFSS TMP0,1
BCF PORTA,3
CLRF TMP1
BSF TMP1,0
RETURN

CLEAR:
CLRF TMP1
bsf TMP1,0
RETURN
end
 

Hi,

Yes, you have not turned off the comparators in you code above.

You have not done the ISR Context saving routine, though for your one only type of interrupt it will probably work ok for now but make you code any more complicated at it will fall down,

What I do need to know, as you have not added any notation to your READ and READ2 , is what info are you sending from the PC and what should the Pic do with it
Seems you are sending out one byte and testing certain bits ??
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
Hi,

In addition to the last post, have simulated your code and it sends out the " Ready " ok, but it cannot receive because you have not got a receive routine .. or have I missed something ..?

Also, how can you set / check the Interrupt if you have not done a Read to SET the flag in the first place ??

Suggest you re-work your code without any ISR for now and prove the rest of the code works ok.

Code:
rx_rs232 	btfss   PIR1,  RCIF 	    ;check for received data
     		goto    rx_rs232
                movf    RCREG,W
                return
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
Hi,

Just had time to look at your code and rework it so you can see the Receive mode does work, without using the ISR.

It sends out the Ready! - then waits for you to send a character from the PC.
It then checks what the character is, if it is 'V' then it turn on PortB,0, if its not that character it continues the loop and waits for another character to come in.

Your config line is set to OSC=HS so assume you are using and external crystal, thats fine but you can also use the 628As internal 4meg oscillator.
I have changed the confing line for the Internal and changed the USART settings for 4 meg at 9600.

As you can see from the picture it works fine.

Hope that is what you were trying to do ..?
 

Attachments

  • VISHYRS232.rar
    53.2 KB · Views: 54

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top