RCREG=RCREG;
RCREG=RCREG;
RCREG=RCREG;
movfw RCREG
movfw RCREG
movfw RCREG
So why is it needed to flush it at the startup? without flushing, it didn't work (I tried it and it didn't)
LIST P=16F628, R=DEC ; Use the PIC16F628 and decimal system
#include "P16F628.INC" ; Include header file
__config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _MCLRE_OFF
errorlevel -302, -207, -203
CBLOCK 0x20 ; Declare variable addresses starting at 0x20
TMP1
ENDC
; --------------------------------
; SET ANALOG/DIGITAL INPUTS PORT A
; --------------------------------
;
movlw 7
movwf CMCON ; CMCON=7 set comperators off
;
; ----------------
; INITIALIZE PORTS
; ----------------
BANKSEL CCP1CON
movlw b'00111100'
movwf CCP1CON
movlw b'00000101' ; TMR2 = on, prescale = 1:4
movwf T2CON
movlw b'01111101' ; set bits 9 - 2
movwf CCPR1L
;
movlw b'00000000' ; set up portA
movwf PORTA
movlw b'00000100' ; RB2(TX)=1 others are 0
movwf PORTB
bsf STATUS,RP0 ; RAM PAGE 1
movlw 0x00
movwf TRISA ; portA all pins input
movlw b'11110010' ; RB7-RB4 and RB1(RX)=input, others output
movwf TRISB
; ------------------------------------
; SET BAUD RATE TO COMMUNICATE WITH PC
; ------------------------------------
; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
;
movlw 0x19 ; 0x19=9600 bps (0x0C=19200 bps)
movwf SPBRG
movlw b'00100100' ; brgh = high (2)
movwf TXSTA ; enable Async Transmission, set brgh
bcf STATUS,RP0 ; RAM PAGE 0
movlw b'10010000' ; enable Async Reception
movwf RCSTA
;
; ------------------------------------
; PROVIDE A SETTLING TIME FOR START UP
; ------------------------------------
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Without the following code, on my PC I just can't control the PWM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
movfw RCREG
movfw RCREG
movfw RCREG ; flush receive buffer
;
; ---------
; MAIN LOOP
; ---------
;
call message ; send "16F628 alive"
loop call receive ; wait for a char
call send ; send the char
goto loop
;
; -------------------------------------------
; RECEIVE CHARACTER FROM RS232 AND STORE IN W
; -------------------------------------------
; This routine does not return until a character is received.
;
receive btfss PIR1,RCIF ; (5) check for received data
goto receive
movfw RCREG ; save received data in W
call EnCheck
return
;
; -------------------------------------------------------------
; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING
; -------------------------------------------------------------
;
send movwf TXREG
bsf STATUS,RP0 ; RAM PAGE 1
WtHere1 btfss TXSTA,TRMT ; (1) transmission is complete if hi
goto WtHere1
bcf STATUS,RP0 ; RAM PAGE 0
return
;
; -------
; MESSAGE
; -------
;
message movlw 'H'
call send
movlw 'I'
call send
movlw ' '
call send
movlw 'F'
call send
movlw 'r'
call send
movlw 'o'
call send
movlw 'm'
call send
movlw ' '
call send
movlw 'J'
call send
movlw 'a'
call send
movlw 'K'
call send
movlw 'o'
call send
movlw 0x0D
call send
return
EnCheck movwf TMP1
sublw 'w'
btfsc STATUS, Z
call Win
movfw TMP1
sublw 's'
btfsc STATUS, Z
call Ukan
movfw TMP1
return
Win incf CCPR1L
incf CCPR1L
incf CCPR1L
incf CCPR1L
incf CCPR1L
movlw 0xFF
movwf PORTA
retlw 0
Ukan decf CCPR1L
decf CCPR1L
decf CCPR1L
decf CCPR1L
decf CCPR1L
clrf PORTA
retlw 0
END [\CODE]
zhaniko93 said:So you mean that those three movfw RCREG-s are useless? they aren't needed? than why doesn't it work on my pic? I think that in reality there is something different for which it's needed to flush.
LIST P=16F628 ;processor is 16F628
include <P16F628.INC> ;Include header file
radix decimal ;all digits are decimal unless otherwise stated
__config _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _MCLRE_OFF & _INTRC_OSC_NOCLKOUT
errorlevel -302 ;suppress all bank switching assembler messages
;-----------------
; EQUATES
;-----------------
TMP1 EQU 0x20 ;RAM address of TMP1 register is hex address 0x20
; ----------------
; INITIALIZE PORTS
; ----------------
; portA all pins output
movlw 0x00
banksel TRISA
movwf TRISA
;RB4 - RB7 input, RB1/RX and RB2/TX assigned to USART, all else outputs
movlw b'11110110'
movwf TRISB
;enable PWM mode
movlw b'00111100'
banksel CCP1CON
movwf CCP1CON
;enable Timer2 with 1:4 prescaler
movlw b'00000101' ; TMR2 = on, prescale = 1:4
movwf T2CON
; set bits 9 - 2
movlw b'01111101'
movwf CCPR1L
;disable on chip comparator
movlw 0x07
movwf CMCON
;initialize Port A
movlw b'00000000'
movwf PORTA
;initialize Port B
bcf PORTB,0
bcf PORTB,3
; ------------------------------------
; SET BAUD RATE TO COMMUNICATE WITH PC
; ------------------------------------
; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
movlw 0x19 ; 0x19=9600 bps (0x0C=19200 bps)
banksel SPBRG
movwf SPBRG
bcf TXSTA,SYNC ;enable async serial
bsf TXSTA,TXEN ;enable serial transmission
banksel RCSTA
bsf RCSTA,SPEN ;enable serial port
bsf RCSTA,CREN ;enable continuous receive
; ------------------------------------
; PROVIDE A SETTLING TIME FOR START UP
; ------------------------------------
; ---------
; MAIN LOOP
; ---------
call message ; send "16F628 alive"
loop call receive ; wait for a char
call send ; send the char
goto loop
; -------------------------------------------
; RECEIVE CHARACTER FROM RS232 AND STORE IN W
; -------------------------------------------
; This routine does not return until a character is received.
receive btfss RCSTA,OERR
btfsc RCSTA,FERR
call SERIAL_ERR_CLR
receive_w8 btfss PIR1,RCIF ; (5) check for received data
goto receive_w8
movfw RCREG ; save received data in W
movwf TMP1
call EnCheck
return
;
; -------------------------------------------------------------
; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING
; -------------------------------------------------------------
;
send movwf TXREG
banksel TXSTA ; RAM PAGE 1
WtHere1 btfss TXSTA,TRMT ; (1) transmission is complete if hi
goto WtHere1
bcf STATUS,RP0 ; RAM PAGE 0
return
; -------
; MESSAGE
; -------
;send message "HI From JaKo" along with hex value 0x0D
message movlw A'H'
call send
movlw A'I'
call send
movlw A' '
call send
movlw A'F'
call send
movlw A'r'
call send
movlw A'o'
call send
movlw A'm'
call send
movlw A' '
call send
movlw A'J'
call send
movlw A'a'
call send
movlw A'K'
call send
movlw A'o'
call send
movlw 0x0D
call send
return
EnCheck movlw A'w'
subwf TMP1,W
btfsc STATUS, Z
call Win
movlw A's'
subwf TMP1,W
btfsc STATUS, Z
call Ukan
movfw TMP1
return
Win movlw 5
addwf CCPR1L,F
movlw 0xFF
movwf PORTA
retlw 0
Ukan movlw 5
subwf CCPR1L,F
clrf PORTA
retlw 0
SERIAL_ERR_CLR bcf RCSTA,CREN
movfw RCREG
movfw RCREG
bsf RCSTA,CREN
return
END
Here...try this code -
Code:; ------------------------------------------------------------- ; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING ; ------------------------------------------------------------- ; send movwf TXREG banksel TXSTA ; RAM PAGE 1 WtHere1 btfss TXSTA,TRMT ; (1) transmission is complete if hi goto WtHere1 bcf STATUS,RP0 ; RAM PAGE 0 return
; -------------------------------------------------------------
; CHECK PREVIOUS TRANSMISSION COMPLETE AND SEND NEW BYTE
; -------------------------------------------------------------------
;
send
banksel TXSTA ; RAM PAGE 1
WtHere1 btfss TXSTA,TRMT ; previous transmission is complete ?
goto WtHere1
banksel 0 ; RAM PAGE 0
movwf TXREG ; load and send new byte
return
Hi,
Just thought you could improve your code -
Code:; ; ------------------------------------------------------------- ; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING ; ------------------------------------------------------------- ; send banksel TXSTA ; RAM PAGE 1 WtHere1 btfss TXSTA,TRMT ; (1) transmission is complete if hi goto WtHere1 banksel 0 ; RAM PAGE 0 movwf TXREG return
;
Get232 btfss PIR1,RCIF ; character available? |B0
goto Get232 ; no, branch, else |B0
movf RCREG,W ; get character |B0
return ; |B0
;
Put232 btfss PIR1,TXIF ; transmit buffer empty? |B0
goto Put232 ; no, branch, else |B0
movwf TXREG ; send character |B0
return ; |B0
;******************************************************************
;* *
;* Filename: 16F88 USART Test.asm *
;* Author: Mike McLaren, K8LH *
;* (C)2010: Micro Application Consultants *
;* Date: 19-Aug-06 *
;* *
;* 16F88 USART Test Program *
;* *
;* *
;* MPLab: 7.40 (tabs=8) *
;* MPAsm: 5.03 *
;* *
;******************************************************************
processor PIC16F88
include P16F88.INC
errorlevel -302
list st=off
__CONFIG _CONFIG1, _LVP_OFF&_PWRTE_ON&_WDT_OFF&_INTRC_IO
__CONFIG _CONFIG2, _IESO_OFF&_FCMEN_OFF
;
; _CP_OFF ; default, no code protect
; _CCP1_RB0 ; default, CCP1 on RB0 pin
; _DEBUG_OFF ; default, debug mode off
; _WRT_PROTECT_OFF ; default, write protect off
; _CPD_OFF ; default
; _LVP_OFF ; -- low voltage programming off
; _BODEN_ON ; default, brown out detect on
; _MCLR_ON ; default,
; _PWRTE_ON ; -- power up timer enabled
; _WDT_OFF ; -- watch dog timer off
; _INTRC_IO or _HS_OSC ; -- INTOSC
;
PtrL equ h'20' ; PutString routine
PtrH equ h'21' ; PutString routine
;
; PutStr macro - print in-line character string
;
PutStr macro str ;
local String, Print
movlw low String ;
movwf PtrL ;
movlw high String ;
movwf PtrH ;
goto Print ;
String dt str,0
Print call PutString ; print string
endm
;******************************************************************
;* *
;******************************************************************
org 0x000
v_reset
clrf STATUS ; force bank 0, IRP 0 |B0
clrf PORTA ; clear PORT A latches |B0
clrf PORTB ; clear PORT B latches |B0
bsf STATUS,RP0 ; bank 1 |B1
clrf ANSEL ; setup PORT A digital I/O |B1
clrf TRISA ; setup PORT A all outputs |B1
movlw b'00100100' ; |B1
movwf TRISB ; set RB5/TX & RB2/RX as inputs |B1
movlw b'01110000' ; |B1
movwf OSCCON ; select 8-MHz INTOSC clock |B1
Stable btfss OSCCON,IOFS ; Int Osc Freq Stable bit set? |B1
goto Stable ; no, branch and wait |B1
movlw d'25' ; 25 (8-MHz) or 64 (20-MHz) |B1
movwf SPBRG ; 19200 baud |B1
movlw b'00100100' ; TXEN=1, SYNC=0, BRGH=1, TX9=0 |B1
movwf TXSTA ; Async, 8, 1, none |B1
bcf STATUS,RP0 ; select Bank 0 |B0
movlw b'10010000' ; SPEN=1, TX9=0, CREN=1, ADDEN=0 |B0
movwf RCSTA ; enable serial port |B0
movf RCREG,W ; flush Rx Buffer |B0
movf RCREG,W ; |B0
PutStr "\x1b[2J" ; home cursor, clear screen |B0
PutStr "K8LH 16F88 UART Test v1.0\r\n\n\n"
Loop call Get232 ; |B0
call Put232 ; |B0
goto Loop ; |B0
;******************************************************************
;
Get232 btfss PIR1,RCIF ; character available? |B0
goto Get232 ; no, branch, else |B0
movf RCREG,W ; get character |B0
return ; |B0
;
Put232 btfss PIR1,TXIF ; transmit buffer empty? |B0
goto Put232 ; no, branch, else |B0
movwf TXREG ; send character |B0
return ; |B0
;
PutString
call GetTable ; get a table character |B0
andlw b'11111111' ; |B0
skpnz ; 00 byte, last character? |B0
return ; yes, return |B0
pagesel Put232 ; |B0
call Put232 ; else,output character |B0
incf PtrL,F ; increment pointer |B0
skpnz ; |B0
incf PtrH,F ; |B0
pagesel PutString ; |B0
goto PutString ; |B0
GetTable
movf PtrH,W ; |B0
movwf PCLATH ; |B0
movf PtrL,W ; |B0
movwf PCL ; |B0
end
Most of us old timers use the TXIF flag which is located in bank 0 on 16F devices (except for the new "enhanced" 16F devices);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?