Witam wszystkich i proszę o pomoc :-) Pytałem na avrfreaks, ale nie dostałem żadnej odpowiedzi...
Podłączyłem xmega32a4 do PCta i mogę wysyłać dane. Dodałem ISR do kodu, i przerwanie odpala się przy wysłaniu danych (TXC). To działa bez problemu. Teraz dodaję ISR dla RX i sprawdzam... i niestety, gdy wysyłam coś do uC to teoretycznie przerwanie się wykonuje... mówię teoretycznie, bo odebranie znaku resetuje się mikrokontroler! A gdy mikrokontroler się zresetuje i program wykonuje się od nowa, to wszystkie przerwania są wyłączone (i nawet TX już nie działa).
Ktoś wie co robię źle? :-(
Oto mój kod:
#define UART_GPS USARTE0
int main (void) { cli(); sei(); /* enable interrupts */
OSC.CTRL |= OSC_RC32MEN_bm; while(!(OSC_STATUS & OSC_RC32MRDY_bm)); CCP = CCP_IOREG_gc; CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
UARTinit(&UART_GPS); // init serial port
while(1) { UARTputs(&UART_GPS, "test string\r\n"); _delay_ms(500); }
}ISR(USARTE0_RXC_vect) { //UART receive interrupt PORTA.OUTTGL |= (1 <<PIN0); // PA0 is toggled }
ISR(USARTE0_TXC_vect) { //UART receive interrupt PORTA.OUTTGL |= (1 <<PIN1); // PA1 is toggled }
I moje funkcje:
//not finished yet, need to handle PORTE via arg void UARTinit(USART_t *port) { int bsel = 3325; int bscale = -3; // 4800 @ 32MHz
PORTE.OUTSET = PIN3_bm; PORTE.DIRSET = PIN3_bm; // PE3 (TXD0) as output PORTE.DIRCLR = PIN2_bm; // PE2 (RXD0) as input
PORTE.INTCTRL = 0x01; // set interrupt0 low level PORTE.INT0MASK = 0x04; // set pins 3 and 2 for as a source for interrupt
port->BAUDCTRLA = /*(uint8_t)*/ bsel; port->BAUDCTRLB = (bscale << 4) | (bsel >> 8);
PMIC.CTRL = PMIC_LOLVLEN_bm;
// Enable RX and TX low level interrupts port->CTRLA = USART_RXCINTLVL_LO_gc | USART_TXCINTLVL_LO_gc;
// Enable both RX and TX. port->CTRLB = USART_RXEN_bm | USART_TXEN_bm;
// Set USART to 8bit, no parity, 1 stop bit port->CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;
}//send a string to UART uint8_t UARTputs(USART_t *port, const char *ptr) {
while(*ptr) { while(!(port->STATUS & USART_DREIF_bm)) // wait for output buffer to clear / send complete / ready to send _delay_us(1);
port->DATA = *ptr++; // send character }
return 1; }
Używam tego z MAX232 (wiem, że jest na 5V). Niestety nic innego na chwilę obecną pod ręką nie mam. TX z xmegi jest podłączony bezpośrednio, i to działa. RX jest podłączony przez dzielnik napięcia (Umax = 3V, zasilanie uC 3.3V).