Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] UART with atmega128 question

Status
Not open for further replies.

Justin Wenger

Newbie level 5
Joined
Sep 18, 2021
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
88
guys, I want to make a UART code that prints '........' permanently and when I Interrupt(with INT0), it prints 'ppppp' instead, and when I interrupt again(with INT1), it prints '.....'again.

But the interrupt part isn't working.. The code works well printing '....' but even if I press INT0, nothing changes and it keeps printing '...'

if someone knows what the problem with this code is, please share your wisdom


Code:
#define F_CPU 16000000
#include <avr/io.h>
#include <avr/interrupt.h>

void uart0_init(void);
int Putchar(char message);
int x=0;
int main(void)
{
uart0_init();
while(1)
{
if(x==0)
{
Putchar('.');
}
if(x==1)
{
Putchar('p');
}
}
}


int interrupt(void)
{
EICRA = 0x0F; // Rising Edge detection
EIMSK = 0x03; // Set INT0 & INT1
SREG |= 0x80; // Global Interrupt Enable
while(1)
{

}
}

ISR(INT0_vect)
{
x++;
}

ISR(INT1_vect)
{
x--;
}


void uart0_init(void) {
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}


int Putchar(char message) {// Putchar "P" cVde
while (((UCSR0A>>UDRE0)&0x01) == 0) ; // UDRE, data register empty
UDR0 = message;
return 0;
}

 
Last edited by a moderator:

In the code posted, the function interrupt() is never called. It looks like this function defines and enables the interrupts. The function has a while(1) statement that would prevent it from returning if it was called.
 
Hi,

Didn't go through the whole code...

But variables that are modified in the interrupt and used in the main loop need to be declared as "volatile".

What happens when "x < 0" or "x > 1" ?


Klaus
 
In the code posted, the function interrupt() is never called. It looks like this function defines and enables the interrupts. The function has a while(1) statement that would prevent it from returning if it was called.
I do not see where you executed attachInterrupt() ?



Regards, Dana.
Thank you so much guys!! It works!!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top