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;
}
wallie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.