External interrupt with Timer interrupt in atmega328P not working together

I am trying to send characters(unsigned char) using the USART of the atemga328P to a virtual terminal setup in proteus. I am using the Arduino uno with the atmega328p.The code is below. below it is the link to screen shot of the proteus simulation.

I want the program to work such that when I press the button after starting the simulation, it will print HELLO! and wait. Then if i press the button again then it should display WORLD and toggle between these two words every time I press the button. In between each letter I want to create a 200ms delay.

The problem I am facing is after I start the proteus simulation, only the first letter “H” ( it is inside the transmit1() function) of is displayed. (I think the program keeps looping inside the while loop defined inside the DELAY() function.) Even if I press the button nothing changes.

It works when I call the _delay_ms() using the library, instead of DELAY(); function I made. But in this case even before I press the button and call the START() function, HELLO! is displayed in the terminal with the start of the simulation.

I am trying to use the 16-bit Timer/Counter1 to make a delay function (DELAY();) which can be called in between transmissions.
I am using the external interrupt to trigger the the transmissions by calling the START(); function. I have connected a button to the INT0 pin in the Arduino uno board to trigger the interrupt.

I am newbie to embedded programming . I am using Atmel Studio 7 to write the code and generate the hex file.
I would really appreciate if someone can help me out….!

#define F_CPU 16000000UL
#define BAUD 9600
#define UBRR_Value ((F_CPU/16/BAUD)-1)
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

int volatile toggle = 0;
int volatile toggle2 = 0;


void USART_CONFIG(void){
    
    //baude rate set
    UBRR0H = (unsigned char)(UBRR_Value>>8);
    UBRR0L = (unsigned char)UBRR_Value;
    
    //enable transmitter:UCSR0B
    UCSR0B |= (1<<TXEN0);
    //set frame format: 8bits data, 1 stop bit
    UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
    
}

void TIMER_CONFIG(void){
        TCCR1B |= (1<<WGM12)|(1<<CS12);//ctc mode set,prescaler 256
        OCR1AH = 0x30;
        OCR1AL =0xD4; //set for one second
        TIMSK1 |=(1<<OCIE1A); //enable timer compare interrupt
        TCNT1 = 0;
}

ISR(TIMER1_COMPA_vect){
    toggle2 = 0;
    TCCR1B &= 0B11111011; //stopping the counter
}

void BUTTON_CONFIG(void){screenshot of the proteus simulation
    
    //external interrupt enable on PD2 INT0
    EIMSK |= 0B00000001;
    //interrupt set for rising edge
    EICRA |= 0B00000011;
    
    
}



void DELAY(void){
    TIMER_CONFIG();
    toggle2 = 1;
    while(toggle2 == 1){
        //looping in this until toggle2 will be zero by the ISR
    }
    return;
}

void transmitDATA(unsigned char data){
    while (!(UCSR0A & (1<<UDRE0))){
        //waiting until transmit buffer is empty
    }
    UDR0 = data;
    return;
}

void transmit1(void){
    
    transmitDATA('H');
    DELAY();
    transmitDATA('E');
    DELAY();
    transmitDATA('L');
    DELAY();
    transmitDATA('L');
    DELAY();
    transmitDATA('O');
    DELAY();
    transmitDATA('!');
    return;
}


void transmit2(){
    transmitDATA('W');
    DELAY();
    transmitDATA('O');
    DELAY();
    transmitDATA('R');
    DELAY();
    transmitDATA('L');
    DELAY();
    transmitDATA('D');
    DELAY();
    return;
}



void START(void){
        //if toggle==1 then transmit1
        if(toggle == 1){
            transmit1();
            }else{
        //if toggle ==0 then tranmit2   
            transmit2();
        }
}

ISR(INT0_vect){
    toggle = 1-toggle;
    START();
}

int main(void)
{   
    
    //configure the USART transmitter
    USART_CONFIG();
    //configure the external interrupt
    BUTTON_CONFIG();
    //enable global interrupts
    sei();
    while (1) 
    {   
    }   
    return 0;
}

screenshot of the proteus simulation

It works if there is no external interrupt. That means; If i put don’t include a button feature to toggle between words and let the program to run in infinite while loop. the DELAY() works. I have mentioned below how the START() function and the main function will look like then. in this case the BUTTON_CONFIG() function which configure external interrupt is commented.

void START(void){
    while(1){
        //if toggle==1 then transmit1
        if(toggle == 1){
            transmit1();
            }else{
        //if toggle ==0 then tranmit2   
            transmit2();
        }
    }
}

int main(void)
{   
    
    //configure the USART transmitter
    USART_CONFIG();
    //configure the external interrupt
    //BUTTON_CONFIG();
    //enable global interrupts
    sei();
    START();
    while (1) 
    {   
    }   
    return 0;
}

New contributor

wallie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật