whenever button pressed ( assume it interrupt) timer's check if there is any data received or not
If(!(UCSRA&(1<<RXC)))
{
received_data=UDR;
Func(received_data);
}
While(!(UCSRA&(1<<UDRE)));
UDR=DATA_TO_SEND;
if((UCSRA&(1<<RXC)))
{
received_data=UDR;
decide_func(received_data);
}
ISR (TIMER1_COMPA_vect) // Timer1 ISR
{
if(PINA!=0x1f) //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
{
val=PINA;
button(val); //passing PINA value
}
if((UCSRA&(1<<RXC))) //if recevied anytime
{
received_data=UDR; //store the value received
decide_func(received_data); //Passing received value.
}
}
TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);
TCNT1 = 0;
OCR1A = 2499; //I just set this value because it works fine not calculated(please help me here how to
//determine the value to be set in here.
TIMSK |= (1 << OCIE1A);
if([U]PINA[/U]!=0x1f) //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
{
val=[U]PINA[/U];
[U] val=PINA // single read of port value, [/U]
if(val!=0x1f) //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
{
other code;
Hi,
This can be problematic in some cases, because you read the PINA value two times..Code:if([U]PINA[/U]!=0x1f) //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected { val=[U]PINA[/U];
So there is a chance that the port value changes between those two points of time.
--> I recommend to read the port value a single time per run:
Code:[U] val=PINA // single read of port value, [/U] if(val!=0x1f) //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected { other code;
Klaus
val receive PINA value in every 2499 ticks as because i m using CTC so whenever OCR1A value reaches 2499 it will re check ISR and give val its value.val=PINA // single read of port value, [/U]
if(val!=0x1f) //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
{
other code;
Hi,
I can't imagine why reading the UART is controlled by a key press. Does it make sense? In most cases the UART can receive data all the time. In your case - when no key is pressed - there will be data loss.
Usually the UART receive is jnterrupt driven. But not by a timer_interrupt, but by the UART_interrupt. This interrupt is only raised when the UART receives data. Then within the ISR the received data is read and placed in a software_receive_buffer, a received_flag may be set, and then the ISR is finished.
ISRs in general should be very short in time. Do only the very imortant steps in an ISR. Process the received data (from buffer) in the main loop.
Klaus
unsigned int UBRR_value=25;
UBRRH=(unsigned)(UBRR_value>>8);
UBRRL=(unsigned)(UBRR_value);
UCSRB=1<<RXEN|1<<TXEN;
UCSRC=1<<USBS|3<<UCSZ0;
USART Control and Status
Register B – UCSRB
• Bit 7 – RXCIE: RX Complete Interrupt Enable
Writing this bit to one enables interrupt on the RXC Flag. A USART Receive Complete
Interrupt will be generated only if the RXCIE bit is written to one, the Global Interrupt
Flag in SREG is written to one and the RXC bit in UCSRA is set.
• Bit 6 – TXCIE: TX Complete Interrupt Enable
Writing this bit to one enables interrupt on the TXC Flag. A USART Transmit Complete
Interrupt will be generated only if the TXCIE bit is written to one, the Global Interrupt
Flag in SREG is written to one and the TXC bit in UCSRA is set.
• Bit 5 – UDRIE: USART Data Register Empty Interrupt Enable
Writing this bit to one enables interrupt on the UDRE Flag. A Data Register Empty Interrupt
will be generated only if the UDRIE bit is written to one, the Global Interrupt Flag in
SREG is written to one and the UDRE bit in UCSRA is set.
• Bit 4 – RXEN: Receiver Enable
Writing this bit to one enables the USART Receiver. The Receiver will override normal
port operation for the RxD pin when enabled. Disabling the Receiver will flush the
receive buffer invalidating the FE, DOR, and PE Flags.
• Bit 3 – TXEN: Transmitter Enable
Writing this bit to one enables the USART Transmitter. The Transmitter will override normal
port operation for the TxD pin when enabled. The disabling of the Transmitter
(writing TXEN to zero) will not become effective until ongoing and pending transmissions
are completed, i.e., when the transmit Shift Register and transmit Buffer Register
As far as I can see "val" isn´t used elsewhere, therefore the "safe way" should have no influence. --> not more complicated.val receive PINA value in every 2499 ticks as because...
.. don´t tell stories!Actually I tried searching for stuff like UART interrupt but all is theory nothing related to codes and process
You really need to read basics!One thing I need to know, how to set 10us for ISR
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?