Pic18f252 usart problem

Hello all

I'm probably overlooking the obvious but I can't seem to get the USART on the 18F252 working. I've tested Port RC6 to see if anything is being transmitted but the output remains high. I suspect that I haven't configured the associated registers properly or the Test Routine is wrong. Using other code I know the port works OK. Below are a few details, can anyone advise please?

Thanks, Simon

PIC18F252 Xtal=7.3728 Mhz Desired BAUD rate: 38400 Interrupts not used

; Configure Port C ; CLRF LATC MOVLW B'10010000' ;RC4:RC7=inputs. MOVWF TRISC ;RC0-RC3, RC5-RC6=outputs. ; ; Configure USART ; MOVLW D'2' ;Set BAUD rate. MOVWF SPBRG

;Configure Transmit Status ;and Control. ; MOVLW B'00100000' ;b7: CSRC = don't care here. MOVWF TXSTA ;b6: TX9 = 0 (8-bit mode). ;b5: TXEN = 1, Transmit enabled. ;b4: SYNC = Asynchronous mode. ;b3: unimplemented. ;b2: BRGH = 0, Low Speed mode. ;b1: TRMT = don't care here. ;b0: TX9D = 9th bit for parity. ; ;Configure Receive Status ;and Control. ; MOVLW B'10000000' ;b7: SPEN = 1, Serial port enabled. MOVWF RCSTA ;b6: RX9 = 0, 8-bit reception. ;b5: SREN = 0, don't care for async. ;b4: CREN = 0, Rx disabled. ;b3: ADDEN = 0, 9th bit for parity. ;b2: FERR = 0, don't care here. ;b1: OERR = 0, don't care here. ;b0: RX9D = 0, don't care here.

;Test Routine

LOOP MOVLW A'S' MOVWF TXREG BTFSS PIR1,TXIF GOTO $-2 GOTO LOOP

I've setup a breakpoint at LOOP

Reply to
PigPOg
Loading thread data ...

Simon, I just looked at some interrupt-based code I wrote for the part and I noticed that I set TXEN as the very last step, which enables the RS-232 functionality. Without going back to the datasheet itself right now (it's been some years, now), I'm guessing that this was important to me when writing the code. (I set CREN, of course, just before that point because I wanted continuous reception.)

My sequence looks about like:

  1. Set up mode bits for output, rs232mode.
  2. Set up DTR and DSR directions and set DSR=1.
  3. Ensure that TXSTA and RCSTA start as 0.
  4. Set up the bps rate.
  5. Configure the external RX/TX pins for RS-232.
  6. Set CREN.
  7. Set TXEN.

Is that any help, at all?

Jon

Reply to
Jonathan Kirwan

Many thanks Jon for the reply. I'll give your suggestion a go.

Kind regards, Simon

Reply to
PigPOg

No problem. I skipped the interrupt setup code in that, but figured you didn't care about it. To be clear, I'd be happy to pull out the data sheet again and go through your code more, or otherwise look through some of my own. I did a serial driver both in assembly as well as in c for the 18F252. And they both worked without trouble. I can't remember actually having much trouble, but I also remember reading through the data sheet closely, too, and I think there might have been something like setting TXEN causes other side effects. Not sure, but my bad memory suggests something like that. Anyway, if it turns out you are still struggling and wouldn't mind someone else looking over your shoulder, I'd be happy. My email is on every post.

Jon

Reply to
Jonathan Kirwan

Hi again Jon

I followed your advice but still can't get it to work. For the 252 I can't find anything that relates to configuring RC6 and RC7 for RS232 operation. I know this port is ok as I've used other code to read/write from RC6 and RC7 with no problems. I'm using ICE2000 running MPLAB V8.02.00.00 and have never had problems using other internal functions but this is the first time I've tried using the USART.I really must be overlooking something. All I need to do at this stage is just monitor RC6/TX with a 'scope to look for activity when I transmit an alphanumeric character.

Perhaps I should cut down on the beer!

Kind regards, Simon

Reply to
PigPOg

Jon

I've found the problem. It was a problem with the RS232 driver - a fault that was loading the TX pin. It turns out my code is ok. Many thanks for your help though.

Kind regards, Simon

Reply to
PigPOg

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.