problem with uart and interrupts

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I'm a bit new to the avr.

I have a problem with the following code. When I send something to the MCU (e.g. '00110000') the LEDS on port B display something completely different (e.g. '11111100').
Here is the code:

.include "8515def.inc"

.def temp=r16
.def temp2=r17
.def serdata=r18
.def invert=r19
;*****************************************************************

.cseg

.org $000 ; on start jump to reset routine

rjmp reset

.org $009 ; on Receive complete interrupt jump to get subroutine
rjmp get

reset:

cli ;disable interrupts
ldi temp,low(RAMEND)
out SPL,temp
ldi temp,high(RAMEND)
out SPH,temp ;initialize Stack Pointer
ldi temp,12 ;set BAUDrate at 19200
out UBRR,temp
ldi temp,0xf8 ;enable RXC interrupt, RXE, TXE
out UCR,temp
ldi temp,0x20 ;enable idle mode
out MCUCR,temp
sei ;enable global interrupts
sleep: sleep
rjmp sleep
reti

get: ser temp2 ;set portB output
out DDRB,temp2
out PORTB,temp2 ;put out leds
in serdata,UDR ;read UART data
ser invert
eor serdata,invert ;invert data because portb active low
out PORTB,serdata ;
reti

If I don't use interrupts it works perfectly:

.include "8515def.inc"

.def temp=r16
.def temp2=r17
.def serdata=r18
.def invert=r19
;*****************************************************************

ldi temp,low(RAMEND)
out SPL,temp
ldi temp,high(RAMEND)
out SPH,temp ;initialize Stack Pointer
ldi temp,12 ;set BAUDrate at 19200
out UBRR,temp
ldi temp,0x98 ;enable RXC interrupt, RXE, TXE
out UCR,temp
ser temp2
out DDRB,temp2
out PORTB,temp2
in serdata,UDR
ser invert
eor serdata,invert
out PORTB,serdata

Can anyone help me.

Thanks
Ben

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Ben,

You have enabled all the three UART interrupts, but only have an interrupt handler for one of them. Try only setting RXCIE and not the UDRIE and TXCIE. The UDR interrupts will be pouring in on you !

Another thing: do not use labels that are equal to instructions (I am refering to the label sleep). This should have been a reserved word in the Assembler.

A third thing: in general, an interrupt function should preserve the registers (or have them permanently allocated to the interrupt) and also preserve the status register. This does not represent any problem in your small example, but once you build a full blown application you should pay attention to this.

Dr.M

admin's test signature