| Author |
Message |
|
|
Posted: Jul 12, 2012 - 10:46 AM |
|

Joined: Dec 06, 2011
Posts: 9
|
|
Hi every body..
i use ATmega32A, 16Mhz external crystal, and the fuses settings are:
h-fuse = 0xC9
l-fuse = 0xff
my programmer is: AVR dragon
and i use AVR studio 6
the code is:
Code:
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
#define BAUDrate 19200
#define BAUD_PRESCALE (((F_CPU / (BAUDrate * 16UL))) - 1)
void UARTinit(uint16_t ubrr_value)
{
UCSRB = (1<<RXEN) | (1<<TXEN);
UCSRC =(1<<URSEL) |(3<<UCSZ0);//|(1<<UCSZ1);//| (1<<USBS);
UBRRL = ubrr_value; // Load lower 8-bits of the baud rate value..
// into the low byte of the UBRR register
UBRRH = (ubrr_value>>8);// Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}
char receive_ch(void)
{
while((UCSRA & (1<<RXC))==0)
return UDR;
}
void send_ch(char letter)
{
while((UCSRA & (1<<UDRE))==0)
UDR = letter;
}
int main(void)
{
DDRA = 0x01;
UARTinit(51);
char a = 'A';
char c ;
while(1)
{
send_ch(a);
c = receive_ch();
if ( c == 'A')
{
PORTA = 0x01;
_delay_ms(2000);
PORTA = 0x00;
_delay_ms(2000);
}
}
return 0;
}
so the problem is when i make a (loop-back) by connecting RX with TX of the ATmega32A MCU then sending letter 'A' = 65 through TX then receiving the character through RX then checking the received character but it's not letter 'A'[/code] |
Last edited by m7ammad7assan on Jul 12, 2012 - 11:02 AM; edited 2 times in total
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 10:50 AM |
|

Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia
|
|
 |
_________________ Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 10:52 AM |
|


Joined: Jan 07, 2012
Posts: 1195
Location: North of France
|
|
|
Code:
if ( c == '65')
should be replaced by
Code:
if ( c == 'A')
or by (but it is unpleasant)
Code:
if ( c == 65) // without quotes
if you want something in PORTA ,0 to show some signs of life... |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 10:59 AM |
|

Joined: Dec 06, 2011
Posts: 9
|
|
|
dbrion0606 wrote:
Code:
if ( c == '65')
should be replaced by
Code:
if ( c == 'A')
or by (but it is unpleasant)
Code:
if ( c == 65) // without quotes
if you want something in PORTA ,0 to show some signs of life...
i used if (c == 'A')
also if(c== 65)
all of them does not not work ,,, |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 11:32 AM |
|


Joined: Jul 18, 2005
Posts: 62220
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
The program starts by sending out 'A' - do you actually see that happen on the PC terminal?
BTW Is AVcc connected to Vcc? ISTR that PORTA on mega32 is the one with ADC inputs and that circuit is powered by AVcc which must be connected even if you just use PORTA for output to LEDs or whatever. |
_________________
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 01:18 PM |
|

Joined: Dec 06, 2011
Posts: 9
|
|
|
clawson wrote:
The program starts by sending out 'A' - do you actually see that happen on the PC terminal?
BTW Is AVcc connected to Vcc? ISTR that PORTA on mega32 is the one with ADC inputs and that circuit is powered by AVcc which must be connected even if you just use PORTA for output to LEDs or whatever.
- when i connect it to PC, the PC didn't receive the correct data (corrupted).
- and AVcc not connected to Vcc.
- i am using PORTA only to power one LED. |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 01:35 PM |
|


Joined: Jul 18, 2005
Posts: 62220
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
when i connect it to PC, the PC didn't receive the correct data (corrupted).
Then I'm willing to bet this is FAQ#3 and the chip isn't really running at 16MHz. |
_________________
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 01:38 PM |
|

Joined: Dec 06, 2011
Posts: 9
|
|
|
clawson wrote:
Quote:
when i connect it to PC, the PC didn't receive the correct data (corrupted).
Then I'm willing to bet this is FAQ#3 and the chip isn't really running at 16MHz.
99.99% i grantee that My MCU operates on 16MHz and u can see the fuses values and the second evidence is, when executing _delay_ms(1000) it takes 1 second exactly. |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 01:52 PM |
|


Joined: Jul 23, 2001
Posts: 2437
Location: Osnabrueck, Germany
|
|
|
Code:
char receive_ch(void)
{
while((UCSRA & (1<<RXC))==0)
return UDR;
}
void send_ch(char letter)
{
while((UCSRA & (1<<UDRE))==0)
UDR = letter;
}
Two ';' (or '{}') are missing. |
_________________ Stefan Ernst
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 01:52 PM |
|


Joined: Jan 07, 2012
Posts: 1195
Location: North of France
|
|
Well, why do you define BAUD_PRESCALE if you never use it?
and maybe you should have a look at http://www.avrfreaks.net/index.php?name ... mp;t=45341
Code:
while((UCSRA & (1<<UDRE))==0)// it jumps to the next instruction
UDR = letter;
might be better (it does not have the same meaning) written like that
Code:
while((UCSRA & (1<<UDRE))==0){} // it waits till everything is OK
UDR = letter;
|
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 03:58 PM |
|

Joined: Dec 06, 2011
Posts: 9
|
|
|
sternst wrote:
Code:
char receive_ch(void)
{
while((UCSRA & (1<<RXC))==0)
return UDR;
}
void send_ch(char letter)
{
while((UCSRA & (1<<UDRE))==0)
UDR = letter;
}
Two ';' (or '{}') are missing.
thanx it works now hahahahaha ,,,
i'm Mohammad Hassan from the country of Saudi Arabia |
|
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 01:55 PM |
|


Joined: Jan 07, 2012
Posts: 1195
Location: North of France
|
|
| Well, seems Monsieur OP got his answers... as "it works now". Perhaps one can also write 'B', or even 'Z' with a serial line... |
Last edited by dbrion0606 on Jul 13, 2012 - 04:46 PM; edited 1 time in total
|
| |
|
|
|
|
|