this should give you a hand!
Code:' Program to echo incoming serial data ' Baudrate : 2400 Bauds ' Method : Polling USART interrupts flags ' Pic Definition ' ============== ' Using PIC 18F2320 @ 4MHZ and bootloader ' DEFINE LOADER_USED 1 DEFINE OSC 4 ' Hardware configuration ' ====================== ' ' TRISC = %10000000 ' PORTC.7 is the RX input ' PORTC.6 is the TX output ' Serial communication definition ' =============================== ' Using internal USART and MAX232 to interface to PC ' RCSTA=$90 ' enable serial port, ' enable continuous receive ' TXSTA=$24 ' enable transmit, ' BRGH=1 ' SPBRG=103 ' set baudrate to 2400 DEFINE HSER_CLOERR 1 ' Alias definition ' ================ ' ' RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty) TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full) CREN var RCSTA.4 ' Receiver enable bit ' Variable definition ' =================== ' ' SerialString var byte[40] Counter var byte Success var bit i var byte ' Hardware initialisation ' ======================= ' ' pause 100 ' safe start-up delay Main: clear if RCIF then ' incomming data? I=RCREG ' take it if i !="!" then discard ' is it the header character? Redo: hserin 500,discard,[i] ' get next character, if it's too long ' get out of here and wait the next header ' character ' if i ="L" then discard ' is it the end? serialstring[counter]=i ' Store into the array counter=counter+1 ' goto Redo Discard: if i="L" then success=1 ' Yeah i got a valid string endif if success then cren=0 ' Disable Receiver hserout ["String received : ",str serialstring\counter,13,10,_ "Character count : ", dec counter,13,10] cren=1 ' Enable receiver endif goto main




Bookmarks