Sign in

username:

password:



Not a member?

Search avrclub



Search tips

Subscribe to avrclub



avrclub by Keywords

AT90S2313 | AT90S8515 | ATMega | ATmega128 | ECL | FETS | IAR | Keyboard | LCD | STK50 | TMOS | UART

Ads

Discussion Groups

Discussion Groups | AVRclub | [AVR club] Strange USART Problem [It's not the internal oscillator]

Atmel AVR Microcontroller discussion group.

[AVR club] Strange USART Problem [It's not the internal oscillator] - Rakesh Kumar - Jul 1 3:33:01 2007

Hello All,

Atmega32 clocked at 11.0592 MHz.
Using ADC for taking samples at a prescaler of 64.
USART communication at 9600 bps.

Here's the thing, I have written two codes in AVR-GCC, both using the same
initialization settings and routines . One of them is transmitting data
properly through an XBEE RF module chip interfaced to a computer.

Other is just filling out dots. (Loss of sync I guess)

Following are the codes. Give out any insights you might have.

Thanks...

-----------------------
Code Working Properly
-----------------------
InitSerial();
adc_init();
int machine_counter=0;
int dummy;
int j;

while (1)
{
int max_a=0; int b_a_max; int t_max_a;

int min_a=1023; int b_a_min; int t_min_a;

int max_b=0; int a_b_max; int t_max_b;

int min_b=1023; int a_b_min; int t_min_b;

TransmitString("Hello, Here it goes:",20);

for (j=0; j<100; j++)
{
int sensor_a=readadchan(0);
int sensor_b=readadchan(1);

if (sensor_a > max_a)
{
max_a=sensor_a;
b_a_max=sensor_b;
t_max_a = machine_counter;
}

if (sensor_a < min_a)
{
min_a=sensor_a;
b_a_min=sensor_b;
t_min_a = machine_counter;
}

if (sensor_b > max_b)
{
max_b=sensor_b;
a_b_max=sensor_a;
t_max_b = machine_counter;
}

if (sensor_b < min_b)
{
min_b=sensor_b;
a_b_min=sensor_a;
t_min_b = machine_counter;
}

machine_counter++;
}

/*dummy = max_a & 0xFF;
max_a = max_a >> 8;

TransmitString(" MAX A:",7);
TransmitByte(max_a);
TransmitByte(dummy);

dummy = b_a_max & 0xFF;
b_a_max = b_a_max >> 8;

TransmitString(" B A MAX:",9);
TransmitByte(b_a_max);
TransmitByte(dummy);

TransmitString(" Count at MAX A:",16);
TransmitByte(t_max_a);

dummy = min_a & 0xFF;
min_a = min_a >> 8;

TransmitString(" MIN A:",7);
TransmitByte(min_a);
TransmitByte(dummy);

dummy = b_a_min & 0xFF;
b_a_min = b_a_min >> 8;

TransmitString(" B A MIN:",9);
TransmitByte(b_a_min);
TransmitByte(dummy);

TransmitString(" Count at MIN A:",16);
TransmitByte(t_min_a);
dummy = max_a & 0xFF;
max_a = max_a >> 8;

TransmitString(" MAX B:",7);
TransmitByte(max_b);
TransmitByte(dummy);

dummy = a_b_max & 0xFF;
a_b_max = a_b_max >> 8;

TransmitString(" A B MAX:",9);
TransmitByte(a_b_max);
TransmitByte(dummy);

TransmitString(" Count at MAX B:",16);
TransmitByte(t_max_b);

dummy = min_b & 0xFF;
min_b = min_b >> 8;

TransmitString(" MIN B:",7);
TransmitByte(min_b);
TransmitByte(dummy);

dummy = a_b_min & 0xFF;
a_b_min = a_b_min >> 8;

TransmitString(" A B MIN:",9);
TransmitByte(a_b_min);
TransmitByte(dummy);

TransmitString(" Count at MIN B:",16);
TransmitByte(t_min_b);

*/

TransmitString(" MAX A:",7);
TransmitInt(max_a);

TransmitString(" B A MAX:",9);
TransmitInt(b_a_max);

TransmitString(" Count at MAX A:",16);
TransmitInt(t_max_a);

TransmitString(" MIN A:",7);
TransmitInt(min_a);

TransmitString(" B A MIN:",9);
TransmitInt(b_a_min);

TransmitString(" Count at MIN A:",16);
TransmitInt(t_min_a);

TransmitString(" MAX B:",7);
TransmitInt(max_b);

TransmitString(" A B MAX:",9);
TransmitInt(a_b_max);

TransmitString(" Count at MAX B:",16);
TransmitInt(t_max_b);

TransmitString(" MIN B:",7);
TransmitInt(min_b);

TransmitString(" A B MIN:",9);
TransmitInt(a_b_min);

TransmitString(" Count at MIN B:",16);
TransmitInt(t_min_b);

machine_counter=0;

}
return 0;

----------------
Code Not working
----------------

int main (void)
{
InitSerial();
adc_init();
int sensor_a;
int sensor_b;
while (1)
{
sensor_a=readadchan(0);
TransmitInt(sensor_a);

sensor_b=readadchan(1);
TransmitInt(sensor_b);

}
return 0;
}

--
Rakesh.
http://gopchandani.wordpress.com

--
Rakesh.
http://gopchandani.wordpress.com
[Non-text portions of this message have been removed]


(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )


[AVR club] Re: Strange USART Problem [It's not the internal oscillator] - torquemada40223 - Jul 1 8:36:31 2007

Rakesh--

On a cursory examination, I notice that the "working" code uses the
TransmitString and TransmitByte functions, while the "broken" code
uses TransmitInt.

If you really want to get to the bottom of it, you should take your
working code, and change it a step at a time toward your code. First
thing I'd try would be to insert a TransmitInt(55) (or some such)
*immediately before* the line TransmitString("Count at MIN A:",16);

Then you'll be able to see if your TransmitInt is doing the right
thing or not. Then snip here and there and test again. Sooner or
later you'll trip across the "crucial difference". Study the before
and after of that modification and become enlightened.

Most of all ... have some fun!

--torx


(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Re: Strange USART Problem [It's not the internal oscillator] - Rakesh Kumar - Jul 1 10:30:37 2007

Hey Torx,

The working code also uses TransmitInt Function. I'd certainly try to tweak
around with the code here and there. But do look at the code of all those
sub-routines. Just in case. :-)

#include

void adc_init(void)
{
ADMUX = 0b01000000; //0x40
ADCSRA |= 0x80;

DDRA = 0x00;
PORTA = 0x00;

ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS0);
}

int readadchan(char n)
{
//read ch n of internal 10 bit a/d
ADMUX = n;
ADMUX |= 0x40;
ADCSRA |= 0x44;
while((ADCSRA & 0x40) !=0){}; //wait for conv complete
//while((ADCSRA & ADIF) ==0);
return ADC;
}

void InitSerial(void)
{
DDRD = 0xFE;
PORTD = 0xFF;

UCSRB = (1 << TXEN) | (1 << RXEN);
UBRRL = 71;
UBRRH=0;
UCSRA = 0x00;

}

void TransmitByte(unsigned char Tosend)
{
// Do nothing until data have been received and is ready to be read from
UDR

while ( !(UCSRA & (1 << UDRE))) ;

UDR=Tosend;
}

void TransmitInt(int ToSend)
{
unsigned char Send;

int dummy = ToSend /1000;
ToSend = ToSend % 1000;

Send = dummy + 0x30;
TransmitByte(Send);

dummy = ToSend / 100;
ToSend = ToSend %100;

Send=dummy + 0x30;
TransmitByte(Send);

dummy = ToSend / 10;
ToSend = ToSend % 10;

Send=dummy + 0x30;
TransmitByte(Send);

TransmitByte(ToSend+ 0x30);

}
void TransmitString(unsigned char Tosend[], int l)
{
int k;
for (k=0; k {
TransmitByte(Tosend[k]);
}
}

On 7/1/07, torquemada40223 wrote:
>
> Rakesh--
>
> On a cursory examination, I notice that the "working" code uses the
> TransmitString and TransmitByte functions, while the "broken" code
> uses TransmitInt.
>
> If you really want to get to the bottom of it, you should take your
> working code, and change it a step at a time toward your code. First
> thing I'd try would be to insert a TransmitInt(55) (or some such)
> *immediately before* the line TransmitString("Count at MIN A:",16);
>
> Then you'll be able to see if your TransmitInt is doing the right
> thing or not. Then snip here and there and test again. Sooner or
> later you'll trip across the "crucial difference". Study the before
> and after of that modification and become enlightened.
>
> Most of all ... have some fun!
>
> --torx
>
>
>

--
Rakesh.
http://gopchandani.wordpress.com
[Non-text portions of this message have been removed]


(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

[AVR club] Re: Strange USART Problem [It's not the internal oscillator] - torquemada40223 - Jul 2 9:06:56 2007

--- In a...@yahoogroups.com, "Rakesh Kumar" wrote:
>
> Hey Torx,
>
> The working code also uses TransmitInt Function. I'd certainly try
to tweak
> around with the code here and there. But do look at the code of all
those
> sub-routines. Just in case. :-)

<...snip...snip...snip...>

I don't have my AVR board running yet, so I'm not proficient in AVR
yet. However, looking at the code and hearing the symptoms "getting
dots", perhaps the integer value passed in is "-2"? In that case, I'd
expect you to get a '.' for an output (though with some other junk
mixed in as well....) It appears that the TransmitInt function
wouldn't handle negative values very nicely. You might want to
compare the value with zero at the beginning, and if less than that,
do a TransmitByte('-') and negate the value before continuing....

--torx



(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )