pic18f46k22 rs 232 comunication

Status
Not open for further replies.

Ilia Gildin

Junior Member level 3
Joined
Sep 27, 2014
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
184
Hi I wrote the following code to interface thrugh rs-232
Code:
;******************************************************************************
;                                                                             *
;   This file is a basic code template for code generation on the             *
;   PIC18F46K22. This file contains the basic code building blocks to build   *
;   upon.                                                                     *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on features    *
;   of the assembler.                                                         *
;                                                                             *
;   Refer to the respective data sheet for additional information on the      *
;   instruction set.                                                          *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:         xxx.asm                                                *
;    Date:                                                                    *
;    File Version:                                                            *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Files Required:                                                          *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Notes:                                                                   *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Revision History:                                                        *
;                                                                             *
;******************************************************************************

;------------------------------------------------------------------------------
; PROCESSOR DECLARATION
;------------------------------------------------------------------------------

     LIST      P=PIC18F46K22          ; list directive to define processor
     #INCLUDE <P18F46K22.INC>         ; processor specific variable definitions

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the 
; .asm file. The lables following the directive are located in the respective 
; .inc file.  See the data sheet for additional information on configuration 
; word settings.
;
;------------------------------------------------------------------------------

     ;Setup CONFIG11H
     CONFIG  FOSC = INTIO67, PLLCFG = OFF, PRICLKEN = OFF, FCMEN = OFF, IESO = OFF
     ;Setup CONFIG2L
     CONFIG PWRTEN = OFF, BOREN = OFF, BORV = 190
     ;Setup CONFIG2H
     CONFIG WDTEN = OFF, WDTPS = 1
     ;Setup CONFIG3H
     CONFIG MCLRE = EXTMCLR, CCP2MX = PORTC1, CCP3MX = PORTE0, HFOFST = OFF, T3CMX = PORTB5, P2BMX = PORTC0
     ;Setup CONFIG4L
     CONFIG STVREN = OFF, LVP = OFF, XINST = OFF
     ;Setup CONFIG5L
     CONFIG CP0 = OFF, CP1 = OFF, CP2=OFF, CP3=OFF
     ;Setup CONFIG5H
     CONFIG CPB = OFF, CPD = OFF
     ;Setup CONFIG6L
     CONFIG WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
     ;Setup CONFIG6H
     CONFIG WRTB = OFF, WRTC = OFF, WRTD = OFF
     ;Setup CONFIG7L
     CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
     ;Setup CONFIG7H
     CONFIG EBTRB = OFF

;------------------------------------------------------------------------------
;
; VARIABLE DEFINITIONS
;
; Refer to datasheet for available data memory (RAM) organization
;
;------------------------------------------------------------------------------

    CBLOCK 0x60 ; Sample GPR variable register allocations
        MYVAR1  ; user variable at address 0x60
        MYVAR2  ; user variable at address 0x61
        MYVAR3  ; user variable at address 0x62
    ENDC

W_TEMP         EQU        0x000  ; w register for context saving (ACCESS)
STATUS_TEMP    EQU        0x001  ; status used for context saving 
BSR_TEMP       EQU        0x002  ; bank select used for ISR context saving


;------------------------------------------------------------------------------
; EEPROM INITIALIZATION
;
; The 18F46K22 has 256 bytes of non-volatile EEPROM starting at 0xF00000
; 
;------------------------------------------------------------------------------

DATAEE    ORG  0xF00000 ; Starting address for EEPROM for 18F46K22

    DE    "MCHP"        ; Place 'M' 'C' 'H' 'P' at address 0,1,2,3

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

RES_VECT  ORG     0x0000            ; processor reset vector
          GOTO    START             ; go to beginning of program

;------------------------------------------------------------------------------
; HIGH PRIORITY INTERRUPT VECTOR
;------------------------------------------------------------------------------

ISRH      ORG     0x0008

          ; Run the High Priority Interrupt Service Routine
          GOTO    HIGH_ISR             

;------------------------------------------------------------------------------
; LOW PRIORITY INTERRUPT VECTOR
;------------------------------------------------------------------------------

ISRL      ORG     0x0018
          
          ; Run the High Priority Interrupt Service Routine
          GOTO    LOW_ISR             

;------------------------------------------------------------------------------
; HIGH PRIORITY INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

HIGH_ISR  

          ; Insert High Priority ISR Here
rs232

    btfss   TXSTA1,TRMT ;CHECK IF xmit buffer empty
    bra		rs232   ;no, wait	
	movff	RCREG1,TXREG1 ;move the data in temp2 register to TXREG1 rs-232 transmition starts from this point

         ;RETFIE  FAST

;------------------------------------------------------------------------------
; LOW PRIORITY INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

LOW_ISR
          ; Context Saving for Low ISR
          MOVWF   W_TEMP              ; save W register
          MOVFF   STATUS, STATUS_TEMP ; save status register
          MOVFF   BSR, BSR_TEMP       ; save bankselect register

          ; Insert Low Priority ISR Here

          ; Context Saving for Low ISR
          MOVFF   BSR_TEMP, BSR       ; restore bankselect register
          MOVF    W_TEMP, W           ; restore W register
          MOVFF   STATUS_TEMP, STATUS ; restore status register
          RETFIE

;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------
;for rs 232 boud rate 9600 need 16MHz
START
	clrf	ANSELC ;analog module off
	movlw 	B'11000000' ;load to W the binary number of '11000000'
	movwf 	TRISC ;TX and RX pins as input as required for rs 232
	movlw 	B'01110110' ;load to W the binary number of '01110110'
	movwf	OSCCON ;configure 16 MHz with stable HFIOST 
	bsf		BAUDCON1,BRG16 ;enable 16 bit baud rate
	bsf 	TXSTA1,BRGH ;high speed baud rate as we only will use 9600
;-----------------------------------------------------------------------------
;                                 Fosc
;Baud_Rate=---------------------------------------------------------------- from ERRATA
;                     [4([SPBRGH1:SPBRG1]+1)]
;-----------------------------------------------------------------------------
	movlw 	0X0    ;load 0X1 to W
	movwf	SPBRGH1 ;move the data from Wreg to SPBRGH FOR DESIRED BOUD RATE OF 9600
	movlw 	b'11001111' ;move the binary number of 10011111 to Wreg 
	movwf 	SPBRG1 ;move W TO SPBRG1 FOR DESIRED BOUD RATE OF 19200
	bcf 	TXSTA1,SYNC ;rs-232 is asynchronous communication
	bcf		TXSTA1,TX9
	bsf		RCSTA1,SPEN ;serial port enabled
	bsf  	TXSTA1,TXEN ;WE WRITE DATA ON TXREG1 AND THE TRANSMISION STARTS AUTOMATICLY
	bsf		RCSTA1,CREN ;RECIEVE DATA FROM COMPUTER TO RCREG1
	bcf		RCSTA1,RX9
	bcf		RCON,IPEN	;disable low priority interrupts
	bsf		INTCON,GIE_GIEH ;enable all unmasked interrupts
	bsf 	INTCON,PEIE_GIEL ;enable all unmasked peripheral interrupts
	bsf 	PIE1,RC1IE ;interrupt of rs-232 recieved
	
r
	bra		r
	end
and as you can guess its not working.
I use Visual Studio to transmit and receive the data the Visual Studio works fine as I could link the Tx and Rx legs of rs-232 connector I can not do that with a microchip because the other end of my cable is USB. I use PICDEM2 black board.
sincerely yours
Gildin Ilia
 

hello




Code:
; The 18F46K22 has 256 bytes of non-volatile EEPROM starting at 0xF00000  ->
EPPROM Size is 1Ko ! 1024 bytes..

You don't send any charactere..
and don't have Recive interrupt !

your program doest nothing !!!
What behavior do you expect ?
 

to send the same character it received. I tried it in another configuration when I send 0X30 and I expected to recieve the zero char in the text box in my Visual Studio program but it just stuck the visual studio program by it self works fine
please help
 

Where is youre Receiving interrupt ?
you don't need to treat TX interrupt...
Test (RCIE && RCIF), then send back what you received into RCREG
to TX line.
 

Code:
bsf 	PIE1,RC1IE ;interrupt of rs-232 received
that the line where I suppose to receive the RC interrupt it should go to high priority interrupt due to
Code:
 bcf RCON,IPEN	;disable low priority interrupts
but it doesn't why?
 

Code:
bsf 	PIE1,RC1IE ;interrupt of rs-232 received

Ce n'est pas parcequ'on arme une AUTORISATION d'interrupt, qu'on la traite...
Il faut ecrire le traitement de l'interruption Receive

je ne veux pas me replonge dans l'ASM..
aussi regarde cet exemple, meme si il est en MikroC, devrait t'aiguiller
sur la solution.. en particulier la definition de l'interrupt reception..

View attachment 18F26K22_16Mhz_Uart1_Uart2.c.txt

Un buffer est utilisé pour stocker les caracteres recus
un flag (Dataready1) signale dans le main que un ou des caracteres sont arrives.
ou tester le nb de car recu si on ne fait pas de Carriage Retrun apres l'envoi depuis le PC.
Attention : gerer les erreurs de COM, sinon il y a blocage ..on ne recoit plus rien.



Nota: pas besoin, dans l'immediat de traiter la transmission en interrupt
il faut juste tester si le registre de translmmission est vide , avant d'envoyer un caractere.

tu ferais bien mieux de passer au C ..
MikroC est gratuit , mais limite à 2K programme.
sinon C18 ou X8 MLPLABX
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…