tyassin
Advanced Member level 1
Hello,
I have this code shown below.
When I press the corresponding button, the program enter the second IF-sentence as it should and displays the data on the LEDS. When I then press another button which should enter the first IF-sentence nothing happens, nothing is transmittet.
I can make it work if a press the button which enters the second IF-sentence and then quickly press the button which enters the first IF-sentence. Then it starts transmitting as long as I hold the button down.
Can anybody see what is wrong?
Thank you
I have this code shown below.
When I press the corresponding button, the program enter the second IF-sentence as it should and displays the data on the LEDS. When I then press another button which should enter the first IF-sentence nothing happens, nothing is transmittet.
I can make it work if a press the button which enters the second IF-sentence and then quickly press the button which enters the first IF-sentence. Then it starts transmitting as long as I hold the button down.
Can anybody see what is wrong?
Thank you
Code:
#include <avr/io.h>
#include <util/delay.h> //Max delay: 262.14 ms / F_CPU in MHz
//#define F_CPU 4000000// Clock Speed
//#define BAUD 9600
#define MYUBRR 24//F_CPU/16/BAUD-1
///////////////////////////////////////////////USART Init/////////////////////////////////////////////////////////
void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UCSR0A=(1<<U2X0); //Divisor in BAUD set to 8. USX0=0 divisor=16, USX0=1 divisor=8
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (0<<USBS0)|(3<<UCSZ00);
}
///////////////////////////////////////////////USART Transmit/////////////////////////////////////////////////////
void USART_Transmit( unsigned char data )
{
PORTD|=(1<<PD6); //RS485==Transmit
_delay_ms(1);
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
//_delay_ms(1);
PORTD&=~(1<<PD6); //PE6: RS485==Receive
}
///////////////////////////////////////////////USART Recieve//////////////////////////////////////////////////////
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) );
/* Get and return received data from buffer */
return UDR0;
}
///////////////////////////////////////////////Main///////////////////////////////////////////////////////////////
int main(void)
{
USART_Init(MYUBRR);
DDRA=0xFF; //PORTA output. LEDs connected to this port
DDRD=0xFF; //PORTF output
DDRC=0x00; //PORTC input
PORTC=0xFF; //Set PORTC to 'High'
PORTA=0xFF; //Turn OFF all LEDs
PORTD&=~(1<<PD6); //PE6: Receive
unsigned char x=0;
while(1)
{
if ((PINC & 0x01)==0)
{
PORTA=0x00;
USART_Transmit(0x54);
}
if ((PINC & 0x02)==0)
{
PORTA=USART_Receive();
}
}
}