usart.asm

Download USART.asm

If you can't read please download the document

Upload: omar-khayyam

Post on 03-Feb-2016

2 views

Category:

Documents


0 download

DESCRIPTION

utilisation du module USART du PIC

TRANSCRIPT

LIST P=16F628#include TMP_TX EQU H'20' ;Value to hold received valueORG 0x00 ;Start vectorGOTO mainORG 0x04 ;Interrupt vectorGOTO interruptmain:;---Use PORTA as I/OCLRF PORTAMOVLW D'7' ;Use PORTA as I/OMOVWF CMCON ;BSF STATUS,RP0 ;Switch to BANK1CLRF TRISA ;Set all pins of PORTA as outputBSF TRISB,4 ;Set PORTB.4 as inputBCF OPTION_REG,7;---Configure peripheral interruptsMOVLW B'00100000' ;Disable all peripheral interrupts except receiverMOVWF PIE1 ;Peripheral interrupt enable/disable;---Configure general interruptsMOVLW B'01000000' ;Disable all interrupts except peripheralMOVWF INTCON ;Interrupt control register;---Configure SPBRG for desired baud rateMOVLW D'25' ;We will use 9600 MOVWF SPBRG ;baud at 4MHz;---Configure TXSTAMOVLW B'00100100' ;Configure TXSTA as :MOVWF TXSTA ;;8 bit transmission - 6.bit;Transmit enabled - 5.bit;Asynchronous mode - 4.bit;Enable high speed baud rate - 2.bitBCF STATUS,RP0 ;Switch to BANK0MOVLW B'10000000' ;Enable serial portMOVWF RCSTA ;Receive status regCLRF TMP_TXBSF INTCON,7 ;Enable all unmasked interruptsBSF RCSTA,4 ;Enable USART receiveMAIN_LOOP: ;Continous loopBTFSC PORTB,4 ;Check if the button is pressedGOTO MAIN_LOOP ;If not goto continous loopMOVF TMP_TX,W ;Load TMP_TX MOVWF TXREG ;to TXREG;We load TMP_TX on the interrupt routine,;when an information received from RX.GOTO MAIN_LOOP ;Continous loopinterrupt:BCF INTCON,7 ;Disable all interruptsBTFSS PIR1,5 ;Check if the RCIF flag is setGOTO quit_int ;If not return back to the main loopMOVF RCREG,W ;Move the received byte to WMOVWF PORTA ;Move W to PORTAMOVWF TMP_TXBCF PIR1,5quit_int:RETFIEEND