Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

EUSART not clearing RX flag

Status
Not open for further replies.

ColinE

New Member
Has anybody seen this before:

Basically the PIR1.RC1IF flag is not being cleared by reading RCREG1 and I can't see any way this could happen.

The device is a 18F6622 and the code was ported working from a 16F887.

I've tried every configuration change that I can think of and checked that the assembler code does what I think it should.
However after the first byte is received the flag goes high and a loop of RCREG reads doesn't ever reset it.

There's no further serial data coming in and I check the error flags.

Any ideas?

Thank you.
 
Thanks for the reply.
I looked a the *.asm file generated by the compiler and that didn't have the EUSART numbers, but the object code seems to indicate that if you don't specify it assumes '1'.

Anyway I tried writing my own code,with numbers, instead of using Hserin, but it made no difference.

I'm not using interrupts of a buffer, because it will have to accept MBs of fast data. So if it can't keep up by being polled, a buffer isn't going to help.

The basic wait for a command code is as follows.
As soon as a byte is received RC1IF goes high and this block loops straight through forever (having decided the command is invalid).
Hserin is commented out, but does exactly the same.

Code:
'wait for a command
	If RCSTA1.OERR = 1 Then
		RCSTA1.CREN = 0
		RCSTA1.CREN = 1
	Endif
'Hserin rsin
	While PIR1.RC1IF = 0
	Wend
	rsin = RCREG1



This is the basic configure:
Code:
Define CONFIG1H = %00000010  'Oscillator mode (set just x1 crystal)
Define CONFIG2L = %00000000  'Brown out reset (disabled)
Define CONFIG2H = %00000000  'Watchdog timer (disabled)
Define CONFIG3L = %00000000  'External bus (not implemented)
Define CONFIG3H = %00000000  'External reset & EECCP controls
Define CONFIG4L = %00000000  'ISP controls
Define CONFIG5L = %11111111  'Set code protection blocks (disabled)
Define CONFIG5H = %11000000  'EEPROM protetion (disabled)
Define CONFIG6L = %11111111  'Write protection (disabled)
Define CONFIG6H = %11100000  'EEPROM write protection
Define CONFIG7L = %11111111  'Write from other block protection
Define CONFIG7H = %01000000  'Root protection
Define CLOCK_FREQUENCY = 10
WaitMs 1
WDTCON = %00000000  'Diable watchdog
INTCON = %00000000  'Interrupt control (all disabled)
INTCON2 = %00000000  'Interrupt control
INTCON3 = %00000000  'Interrupt control
PIR1 = %00000000  'Peripheral interrupt select
PIR2 = %00000000  'Peripheral interrupt select
PIR3 = %00000000  'Peripheral interrupt select
PIE1 = %00000000  'Peripheral interrupt select
PIE2 = %00000000  'Peripheral interrupt select
PIE3 = %00000000  'Peripheral interrupt select
IPR1 = %00000000  'Interrupt priority
IPR2 = %00000000  'Interrupt priority
IPR3 = %00000000  'Interrupt priority
'0= Output, 1= Input
'RA7,6 Xtal - RA5,4 Spare ports on 16W - RA3 LCD Backlight on - RA2 3v3 Volts - RA1 Line Current - RA0 Line Volts
'***** RA3 will have to be an AN, but not used **** Backlight enable will have to be linked on ************
'Symbol lcd_light = PORTA.3  'Until the touch screen is turned on, this will work
'RB7,6,5 ISP - LCD Panel, RB4 CS*, RB3 RD*, RB2 WR*, RB1 RS - RB0 Touch Interrupt
TRISB = %11100001
PORTB = %00011100
Symbol lcd_cs = PORTB.4
Symbol lcd_rd = PORTB.3
Symbol lcd_wr = PORTB.2
Symbol lcd_rs = PORTB.1
'RC7 Serial TX - RC6 Serial RX  - RC5 SD Data Out - RC4 SD Data In - RC3 SD Clk - RC2 SD CS - RC1 Loopthrough - RC0 Touched
TRISC = %10110001
PORTC = %00000110
Symbol loop_th = PORTC.1
Symbol touch = PORTC.2
'RD7,6,5,4,3,2,1,0 LCD Data LS Byte
TRISD = %00000000
PORTD = %00000000
Symbol lcd_ls = PORTD
'RE7,6,5,4,3,2,1,0 LCD Data MS Byte
TRISE = %00000000
PORTE = %00000000
Symbol lcd_ms = PORTE
'RF7 NC - RF6 Touch Enable - RF5 Touch Y drive - RF4 X Drive - RF3 Y Top - RF2 X Left - RF1 Y Bottom - RF0 X Right
TRISF = %10001111
PORTF = %00000000
Symbol touch_en = PORTF.6
Symbol drive_y = PORTF.5
Symbol drive_x = PORTF.4
Symbol touch_yt = PORTF.3
Symbol touch_xl = PORTF.2
Symbol touch_yb = PORTF.1
Symbol touch_xr = PORTF.0
'RG 4,3,2 NC - RG1,0 SD Card switches
TRISG = %11111111
T0CON = %00000000  'Timer 0 off
CCP1CON = %00000000  'Disable comparitors
CCP2CON = %00000000  'Disable comparitors
CCP3CON = %00000000  'Disable comparitors
CCP4CON = %00000000  'Disable comparitors
CCP5CON = %00000000  'Disable comparitors
ADCON0 = %00000000  'ADC idle for setup
ADCON1 = %00000110  'ADC channels 0 to 8 enabled
ADCON2 = %00100110  'Set ADC Clock


Const softver = 201  'Software version
Const panelt = 3  'Panel type 800x480 KD20050 - fixed in this version
Const comim1 = 3  'compatible image type
Const comim2 = 255  'compatible image type
Const comim3 = 255  'compatible image type

Dim paneln As Byte  'Panel number in chain
Dim tmp As Byte
Dim rsin As Byte  'Serial in data
Dim colour As Byte
Dim dform As Byte

Dim cx As Byte  'counter bytes
Dim cy As Byte

Dim x As Byte
Dim y As Byte
Dim tc As Word
Dim k As Byte

'enable loop through and start serial port
loop_th = 1

Hseropen 19200
'added to make sure it's UART 1
TXSTA1 = %00100000
RCSTA1 = %10010000
 
So you are saying.. even with the"HSERIN rsin" command it doesn't work.... The OERR if clause.... You really should if else with HSERIN

Code:
'wait for a command
	If RCSTA1.OERR = 1 Then
		RCSTA1.CREN = 0
		RCSTA1.CREN = 1
	Else
                Hserin rsin
        EndIf

This way HSERIN is executed only when there is no error..

Is this problem on Oshonsofts simulator... Or in a real device..
 
Thanks again.

It's on a real device.
The code to detect an error never happens. It's just one of the many things I added to try and get a clue.

I looked at the code Hserin compiles to and it's very simple.
It just tests PIR1.RC1IF and waits for it to be set, then reads RCREG, which should clear RC1IF, but doesn't.

So it just drops through every time and doesn't really wait for an input.
 
The signals are the right way up, the baud rates are correct and there are no errors getting flagged.

I know that this can't be a common fault on the device and there must be something I'm missing, but I can't think of anything else to try.
 
I doubt if this is the issue but... TRISC7 should be 1 and TRISC6 should be 0 for asynchronous transmission...

You can find places on the Internet that that say they should both be '1'. I tried that, but have now gone back to what it says in the data sheet. Like you said.

I talked to a friend in the pub last night. We have both been professionally programming PICs for over twenty years and neither of us have any idea what's going on. (We're both hardware engineers, so not truly code monkeys.)

The only conclusion is that I must be doing something stupid, but we can't think what.

The offending item:
6622.jpg
 
Last edited:
Look at the BAUDCONx bit 6 (RCIDL) for a low to high transition when a byte is received.
 
Solved!

Worked through the first 304 pages of the data sheets and trying anything that might be relevant as I went.

CONFIG4L.7 = 1
Turn off background bebugger

I have no idea why this works, as I'm not using RB6 or 7 for anything.

Now to tidy up the mess I've made inserting test code.

Look at the BAUDCONx bit 6 (RCIDL) for a low to high transition when a byte is received.
Thanks. Yes. On my way through the manual I did look at that and it seemed correct.
 
Last edited:
That's why you don't just set the CONFIG bits to all zeros. ;)
 
That's why you don't just set the CONFIG bits to all zeros. ;)

The manual should really include a note to say 'set to zero to cause strange random failures'.
I'm surprised there isn't a 'halt and catch fire' bit, to liven things up.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top