Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
LevenEigenaar
PostPosted: Nov 23, 2011 - 01:39 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hello!

How can i make a rs232 connection?

Someone a basic script for rs232?

gz

< Moved from Tutorial Forum to here; OP, check the Tutorial forum for a tutorial on RS232 - Plons >
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 23, 2011 - 01:51 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

How can i make a rs232 connection?

Do you mean electrically? You will want to use a MAX232 to do inversion and level conversion. Just searching "MAX232" here should hit loads of threads.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
haker_fox
PostPosted: Nov 23, 2011 - 02:25 PM
Resident


Joined: Oct 15, 2005
Posts: 530
Location: Russia, Far East Siberia, Irkutsk

LevenEigenaar wrote:

Someone a basic script for rs232?

Do you mean software? There are many examples here. But for detail you should to read the datasheet. There is a lot of information about setting up the USART.

For IBM PC you can use Hyper Terminal (in Windows), minicom (Linux) or something else.
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 23, 2011 - 03:16 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


I have too a max232, only a basic script too learn it to use!

Gz
 
 View user's profile Send private message  
Reply with quote Back to top
dksmall
PostPosted: Nov 23, 2011 - 03:47 PM
Raving lunatic


Joined: Apr 16, 2001
Posts: 3522
Location: Phoenix, Arizona

When you say basic, do you mean basic programming language? Or do you want a simple program for serial communications?
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 23, 2011 - 04:48 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


dksmall wrote:
When you say basic, do you mean basic programming language? Or do you want a simple program for serial communications?


I search for a simple program for serial communication!

Please


Thank you
 
 View user's profile Send private message  
Reply with quote Back to top
dksmall
PostPosted: Nov 23, 2011 - 04:53 PM
Raving lunatic


Joined: Apr 16, 2001
Posts: 3522
Location: Phoenix, Arizona

Look through the tutorials forum on this site for help on serial communications.
 
 View user's profile Send private message  
Reply with quote Back to top
tubecut
PostPosted: Nov 23, 2011 - 04:57 PM
Resident


Joined: May 27, 2002
Posts: 737
Location: Alabama USA

What device?
What tool chain?
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
js
PostPosted: Nov 23, 2011 - 11:46 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20353
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Which chip are you using? Have you looked in the datasheet in the USART section? It should have all you need for USART communication both in assembler and C.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Nov 24, 2011 - 12:02 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21257
Location: Orlando Florida

I can post a program that will init the uart in a mega32 and show a little menu. You specify the AVR model, xtal speed. I use the imagecraft compiler. (Similar program is on Ereshop.com under mega32 board examples)

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
pnp
PostPosted: Nov 24, 2011 - 12:07 AM
Hangaround


Joined: Jul 24, 2011
Posts: 358
Location: Greece / Finland

LevenEigenaar,

First read this thread, so you learn how to properly ask for help.

Then, read this thread and this thread to get your answer.

_________________
-Pantelis

Professor of Applied Murphology, University of W.T.F.Justhappened.
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 24, 2011 - 07:20 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


bobgardner wrote:
I can post a program that will init the uart in a mega32 and show a little menu. You specify the AVR model, xtal speed. I use the imagecraft compiler. (Similar program is on Ereshop.com under mega32 board examples)


Hey Bobgardner!

I use the same chip - Atmel Atmega32, i make my programs in studio 5 avr!

Can you send your script?
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 24, 2011 - 07:33 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


I got a ask!

Did i need a Include file to analays the atmega32 ports?
And what is example a code for ByteToSend?

I will use this for a gsm modem!
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Nov 24, 2011 - 08:24 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

Quote:

Did i need a Include file to analays the atmega32 ports?

Select the correct AVR model in the project options, and then in the start of every source file you do
Code:
#include <avr/io.h>


Quote:
And what is example a code for ByteToSend?

There are several good tutorials on serial communication in the Tutorials forum here at AVRfreaks.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 24, 2011 - 08:55 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


How can i chance the F_CPU? in the script>?
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Nov 24, 2011 - 09:38 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

No, that is not recommended.

Do it in the project settings. I'm not at an AS5 machine right now, but it's approximately under Toolchain, Symbols (or is that "Defines"). There you click the Add button and enter something like
Code:
F_CPU=16000000UL

(use your actual CPU clock frequency as the numerical value.

This way all the source source files in the project ill get the definition.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 24, 2011 - 10:56 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


H!!
I got only on my screen 00 00 00 00 00 00 00 ?

I got the atmega 32!

en set the F_CPU to F_CPU=16000000UL

Help please
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 24, 2011 - 11:09 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Just setting the macro F_CPU to 16000000 does not magically make the CPU run at 16MHz of course!

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 24, 2011 - 11:17 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


I don`t understand you,

Is it right the F_CPU settings>?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 24, 2011 - 11:46 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

You didn't read your cross-post did you? Read that then report back.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 24, 2011 - 12:07 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Thank you all its working! Thank you!!!!

But yes yes Razz!

0x3a that is the symbol Z??


On the hex table means z 7a?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 24, 2011 - 12:21 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

http://www.asciitable.com/

0x3A is :
Z is 0x5A
z is 0x7A

(indeed for A..Z the a..z equivalents are all + 0x20)

Of course in a C program it's easier to write (and easier to read!):
Code:
UART_putchar('z'); UART_putchar('Z');
than to try and remember that it is:
Code:
UART_putchar(0x7A); UART_putchar(0x5A);

(note those are single-quotes, used to specify single characters, do not confuse with double-quotes used to specify complete ASCII character strings which are something quite different)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 24, 2011 - 01:56 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Ok, i understand!

But if i send 0x5A it show not a Z???
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 24, 2011 - 02:55 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

But if i send 0x5A it show not a Z???

Then the baud rate is wrong for all the usual reasons.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 11:42 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hey!!

I got a problem,

If i send the code 0x33 that means s,

I got sometimes the code 0x23? that means c?
Example ssssssscsssccsssssssscsssscccsssss


If i chance the cpu speed, i got on my screen strange codes.

I use the atmega32 with this code




#include <avr/io.h>
#include <util/delay.h> // BELANGRIJK, HIERIN DE SYSTEM CLOCK(F_CPU)CONTROLEREN OF HET OVEREENKOMT WAT JE HIER ONDER HEBT INGEVULD!!!!!!
// 8000000UL
#define FOSC 10000000UL //system clock speed - of terwel de snelheid van de cpu van de microprocessor
#define BAUD 9600UL // Baud rate snelheid, die hier staat instellen op het apparaat waar je het naar toe stuurt, of veranderen naar wat het apparaat vraagt
#define MYUBRR FOSC/164/(BAUD-1) // onbekend

void USART_init(unsigned int ubrr); // onbekend
void USART_Transmit(unsigned char data); // onbekend
unsigned char USART_Receive(void); // onbekend


int main(void)
{
USART_init(MYUBRR); // onbekend

while(1) // wanneer
{
//int i; // int maken, kun je gebruik om hier een character via te versturen in het programma

//for(i=0x21; i<0x3A; i++) // loop van a tot z / (1)kun je weg laten en bij USART_TRANSMIT kun je een hexdeximaal neer zetten voor 1 character die word verzonden via de TX






//{
// USART_Transmit(i); // (1)verander de i naar een hexdeximaal en er word een enkele character verzonden
USART_Transmit(0x33); // (1) <<----Zo dus

_delay_ms(500); // Vertranging van 2 seconden van elke cyclus
// }

}
return(0);
}
 
 View user's profile Send private message  
Reply with quote Back to top
leon_heller
PostPosted: Nov 25, 2011 - 01:14 PM
Raving lunatic


Joined: Jul 27, 2001
Posts: 7429
Location: St. Leonards-on-Sea (UK)

Are you using a crystal? That looks like a baud rate problem.

_________________
Leon Heller
G1HSM
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Nov 25, 2011 - 01:16 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

10MHz is fairly unusual. Is it really 10MHz?

BTW whatever made you post this in the Tutorial Forum? It's not like you are totally new here. Surely you know what the respective fora are for?

Moving to AVR Forum.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 01:21 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Crystal? I am a beginner! so i dont now what crystal means.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 25, 2011 - 01:36 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Crystal? I am a beginner! so i dont now what crystal means.

It's a piece of rock formed from quartz. I thought every schoolkid knew this?


_________________
 
 View user's profile Send private message  
Reply with quote Back to top
leon_heller
PostPosted: Nov 25, 2011 - 01:48 PM
Raving lunatic


Joined: Jul 27, 2001
Posts: 7429
Location: St. Leonards-on-Sea (UK)

He obviously isn't using one, if he doesn't know what it is, hence the problem. Smile

_________________
Leon Heller
G1HSM
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Nov 25, 2011 - 02:12 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21257
Location: Orlando Florida

Is the ascii chart different in europe? (escii?) the numbers 0 through 9 are 0x30 thru 0x39. Also, a 'U' is 0x55, which is a good test character because every bit changes (square wave on the scope) and its easy to read the bit width. 9600 bps has 104usec wide bits. Send one character in a loop. Any character. If you cant see that character on the pc, the baud rate and/or the clock setting is boogered. Fix it.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
thygate
PostPosted: Nov 25, 2011 - 02:22 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

they are referring to your clock source. The mcu can be clocked from several different sources. Default it is set to use the internal oscillator (4MHz) and divided by 4 so 1MHz. You can set this with the fuses.

The internal oscillator is sometimes not accurate enough to do reliable serial communication. And the typical symptoms are what you are describing.

You need to use an external crystal oscillator.

Also, please surround your code in your post with code tags so it is readable.
eg:
Code:
void main()
{
}


Also it doesn't matter if a number is represented as decimal/hex/ascii, it is still a number and the way we humans represent it does not matter to a computer.
The following are all the same
USART_Transmit(65);
Code:
USART_Transmit(0x41);
USART_Transmit('A');
USART_Transmit(0b1000001);

Further, F_CPU should not be defined in a file, but should be passed as a command line switch to the compiler. Set it in the project options in AVR Studio or in your MakeFile.
And then calculate your UBBR value from this definition instead of making another definition.
eg:
Code:
#define UART_BPS      9600
#define UART_UBRR      ((F_CPU / (16UL * UART_BPS)) -1)


You cannot arbitrarily change F_CPU to anything and think the cpu is running at a different speed.
This value has to be set to the speed the processor is running at, it does not set the speed it will run at.

Finally,
Code:
_delay_ms(500); // Vertranging van 2 seconden van elke cyclus

No, if F_CPU is set correctly this will generate a delay of 500msec (period), so you will get 2 characters per second (frequency).

Tip: try to always use english for symbol names and preferably also for comments. So when you post your code online everyone can read them.

Don't start a new topic for the same problem as this is, as you noticed, generally not appreciated.


Last edited by thygate on Nov 25, 2011 - 02:40 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 02:40 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Code:

#include <string.h> // standaard include bestand van avr studio
#include <stdlib.h> // standaard include bestand van avr studio
#include "main.h" // extra include bijgeleverd in de map
#include "lcd_drv.h" // extra include bijgeleverd in de map, voor de analyse voor de poorten van de lcd!




#define FOSC 1000000UL //system clock speed - of terwel de snelheid van de cpu van de microprocessor
#define   BAUD 9600UL  // Baud rate snelheid, die hier staat instellen op het apparaat waar je het naar toe stuurt, of veranderen naar wat het apparaat vraagt
#define MYUBRR FOSC/16/(BAUD-1) // onbekend


void USART_init(unsigned int ubrr); // onbekend
void USART_Transmit(unsigned char data); // onbekend
unsigned char USART_Receive(void); // onbekend

int main(void)
{
   USART_init(MYUBRR); // onbekend
   
   while(1) // wanneer
   {
      int i; // int maken, kun je gebruik om hier een character via te versturen in het programma

         for(i=0x21; i<0x3A; i++) // loop van a tot z / (1)kun je weg laten en bij USART_TRANSMIT kun je een hexdeximaal neer zetten voor 1 character die word verzonden via de TX

      {
         USART_Transmit(i); // (1)verander de i naar een hexdeximaal en er word een enkele character verzonden
      //   USART_Transmit(0x3A); // (1) <<----Zo dus
      
      
         
         #ifdef LCD_LINE2 // text voor lijn 1
         lcd_init();
         lcd_xy( 0, 0 ); // (>0<, 0) Betekend de x lijn, deze kiest de plek voor de tekens van links naar rechts // (0, >0<) betekend welke regel er word toegepast
         lcd_putchar(i);//text voor lijn 0 _ms(2000); // Vertranging van 2 seconden van elke cyclus
         
         _delay_ms(1000); 
      }
      #endif
   }
   return(0);
}



here is my code i use
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Nov 25, 2011 - 02:42 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

ok, so far the code tags, now did you read and understand my previous post ? For starters the part about the clock source and F_CPU.

Also put the comments on a seperate line as this is completely unreadable. Or better leave them out altogether as they make no sense and most of them are simply wrong.
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 02:48 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Code:


#include <avr/io.h>
#include <util/delay.h>


#define UART_BPS      9600
#define UART_UBRR      ((F_CPU / (16UL * UART_BPS)) -1)

void USART_init(unsigned int ubrr);
void USART_Transmit(unsigned char data);
unsigned char USART_Receive(void);

void main()
{
   USART_init(UART_UBRR);
   
   while(1)
   {
      int i;

         for(i=0x30; i<0x3A; i++)

      {
         USART_Transmit(i);

         _delay_ms(1000);
      }

   }
   return(0);
}


I understand your previous post!

So this is the right code ?
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Nov 25, 2011 - 02:52 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

provided that you set F_CPU to the actual clock speed the mcu is running at, and USART_init is implemented correctly, this should write characters '0' through '9' to the uart with one second delay in between each character at 9600 bps.

If you are using the internal oscillator this might still give you the same problem, as stated in my previous post. Gathered that you already had it printing characters, but sometimes it would show a different character, your problem is not software related, but related to your clock source. Again, see previous post.

Code:
return(0);

embedded applications should NEVER return from main, so remove that line, it serves no purpose.
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 02:58 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


I set my F-CPU to F_CPU=1000000UL in toolchain.

Oke so USART_init(UART_UBRR); must be delete?

Code:


#include <avr/io.h>
#include <util/delay.h>


#define UART_BPS      9600
#define UART_UBRR      ((F_CPU / (16UL * UART_BPS)) -1)

void USART_init(unsigned int ubrr);
void USART_Transmit(unsigned char data);
unsigned char USART_Receive(void);

void main()
{
   
   
   while(1)
   {
      int i;

         for(i=0x21; i<0x3A; i++)

      {
         USART_Transmit(i);

         _delay_ms(1000);
      }

   }
}   
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Nov 25, 2011 - 03:01 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21257
Location: Orlando Florida

Should I go copy my message from the orphan thread with the same name as this one from 6am today over to here, or should I assume that LE will actually go to his old thread and read the reply?

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
thygate
PostPosted: Nov 25, 2011 - 03:07 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

No. Lets start from the top.

To use the USART peripheral you need to configure it, which you presumably do in USART_init(). It will not work if you do no initialize it.

F_CPU is merely a definition that you (and library functions like _delay_ms) can use to calculate some values in relation to the clock speed the mcu is running at.

It does NOT SET the clockspeed, this is done through the fuse bits.

Since you already had it working as stated in your first post, the problem is not in the software.

If you are using the "internal RC Oscillator" as clock source. Which is the default the chip is shipped with, bit timings on asynchronous serial communications will have some drift as that clock source is not always reliable. To do reliable asynchronous serial communication you need to use an external crystal connected to the XTAL pins of the chip (pin 12, 13 for the ATMega32 PDIP package) and change the fuse bits so the mcu will use this clock source instead of the internal RC Osc.

Careful when changing fuse bits, as you can brick it if you set the wrong option.
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 03:27 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


I will buy the stuff for the xtal pins, and will tested Wednesday , i am free now from work so i going home!

Thank you i will replay you message at Wednesday!

Thank you thygate!! Very Happy

How many M did i need for the crystal ?

Do you speak Dutch, because you are from belgium?
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Nov 25, 2011 - 03:33 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

You're welcome. Yes, Dutch is my native language, but i think it is against forum rules/policy to use anything other than English.

As long as you show dedication and abide by forum rules others will help you too.

Just get a 16MHz crystal and 2 22pF caps and connect them as shown in the datasheet in the chapter "System Clock and options"

Bob Gardner was referring to his post in the other thread :
http://www.avrfreaks.net/index.php?name ... ht=#896137
where he explained the same thing.

Bob, you were probably being sarcastic, but yes ASCII is the same everywhere with the exception of the extended characters (msb set) which can have different representations depending on the region (codepage).
http://en.wikipedia.org/wiki/Extended_ASCII
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Nov 25, 2011 - 03:54 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:
I set my F-CPU to F_CPU=1000000UL in toolchain.
Code:
#include <avr/io.h>
#include <util/delay.h>


#define UART_BPS      9600

9600 is not possible on 1MHz, try 2400.

Also:
Code:
for(i=0x21; i<0x3A; i++)

Assuming this works you will get
Code:
!"#$%&'()*+,-./0123456789:

is that really what you intended to print out?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Nov 25, 2011 - 04:09 PM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13834
Location: Vancouver, BC

If you want to loop from a to z then why not do this:
Code:
for (i = 'a'; i <= 'z'; i++)
As Bob said, 0x21 is NOT 'a'. And why do you have two threads on the very same questions?

[Cliff: I must be half asleep - I saw his other cross-posts but not the fact that there were two identical in this forum - now merged]

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 04:44 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


I have connect my display to the loop! On the display shows

Code:
!"#$%&'()*+,-./0123456789:


And on the reciever laptop, abcdefgade ect?

I have no crystal is this necessary???
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Nov 25, 2011 - 04:52 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


clawson wrote:
Quote:
I set my F-CPU to F_CPU=1000000UL in toolchain.
Code:
#include <avr/io.h>
#include <util/delay.h>


#define UART_BPS      9600

9600 is not possible on 1MHz, try 2400.

Also:
Code:
for(i=0x21; i<0x3A; i++)

Assuming this works you will get
Code:
!"#$%&'()*+,-./0123456789:

is that really what you intended to print out?



Yes, i see that code on my lcd screen.

I use a laptop te recieve the rs232 signal, there i have te code example, abcdeadeace
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Nov 25, 2011 - 05:00 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21257
Location: Orlando Florida

Look at the ascii chart. ! is 0x21, a is 0x61. The uart isnt set up right. You might be in 7 bit mode instead of 8 bit mode. Usually when someone thinks they are running at 8mhz, and the clkdiv fuse is set, they are really running at 1mhz, and everything is off by 8. They try for 9600 and get 1200. You are getting the bottom 6 bits ok, but then here comes the stop bit and the 0x21 gets another most significant bit added to make it 0x41.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
JohanEkdahl
PostPosted: Nov 25, 2011 - 11:11 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

Quote:

I have no crystal is this necessary???

The internal RC oscillator has an accuracy of +/- 10 percent. If you have a very stable power supply, have the circuit at a very stable temperature and have a fair amount of luck, your setup will hit within the +/- 2 percent that UART comms requires. If you are a wise gambler you don't bet on such a lousy hand and get a clock source that is more accurate. A crystal, with the required load caps, is the obvious alternative unless you have another stable and accurate clock source in your setup.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
thygate
PostPosted: Nov 26, 2011 - 02:24 AM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

Quote:
Yes, i see that code on my lcd screen.

I use a laptop te recieve the rs232 signal, there i have te code example, abcdeadeace
? what did you see on what with what code ?
Quote:
I have connect my display to the loop! On the display shows
you're not making any sense.
first you say you see it on a lcd screen, then you say rs232 on a laptop, so which is it ?
Then in your code you first post
Code:
for(i=0x21; i<0x3A; i++)
then
Code:
for(i=0x30; i<0x3A; i++)
then again
Code:
for(i=0x21; i<0x3A; i++)

people are replying to different versions of the posted code and you are unable to relate them back to what you have.

You're causing a lot of confusion and it's only making it harder to understand you.
Be consistent, accurate and complete with your problem description and hardware setup or people simply cannot help you.
We do not have some kind of magical device to tell us what you are doing.
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 19, 2012 - 11:32 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hey

I would receive a string? From rs232,

I have this code for transmit but how can i make one for receive?

Code:
void USART_tx_string( char *data )
{
while ((*data != '\0'))
   {
      while (!(UCSRA & (1 <<UDRE)));
      UDR = *data;
      data++;
   }   
}


grz
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jan 19, 2012 - 11:36 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Code:
char buffer[10];
uint8_t i;
for (i=0; i<10; i++) {
  buffer[i] = UART_get_char();
  if (i == '\n') {
     break;
  }
}

That receives up to 10 characters but breaks out early if '\n' (new line) is received.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 19, 2012 - 11:56 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Code:


#define UBRR_9600 103

r

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#include <string.h> // standaard include bestand van avr studio
#include <stdlib.h> // standaard include bestand van avr studio
#include "main.h" // extra include bijgeleverd in de map
#include "lcd_drv.h" // extra include bijgeleverd in de map, voor de analyse voor de poorten van de lcd!


void InitUART (unsigned char baudrate);
unsigned char ReceiveByte (void);
void TransmitByte (unsigned char data);
void gets1USART(char *buffer, unsigned char len);

void USART_init(unsigned int ubrr)
{
   UBRRH = (unsigned char)(ubrr>>8); //101
   UBRRL = (unsigned char)ubrr;
   UCSRB = (1<<RXEN)|(1<<TXEN);
   //UCSRB = (1<<TXEN);
   //   UCSRC = (3 << UCSZ0);   //asynchronous 8 N 1
   UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}

unsigned char usart_rx(void)
{
   while(!(UCSRA & (1<<RXC)));          //wait until reception complete
   return UDR;                     // receive value from UDR
}

void TransmitByte (unsigned char data)
{

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


  UDR = data;
}

void USART_tx_string( char *data )
{
while ((*data != '\0'))
   {
      while (!(UCSRA & (1 <<UDRE)));
      UDR = *data;
      data++;
   }   
}

/*  MAIN */

int main(void)
{

USART_init(UBRR_9600);
lcd_init();

char buffer[10];
uint8_t i;
 
  while (1)                 // do until finished or broken
   {      
      for (i=0; i<10; i++)
      {
      buffer[i] = usart_rx();
      if (i == '\n')
      {
     break;
   }
      
      
      
      lcd_xy( 0, 0 );
   
   lcd_putchar(buffer[i]);


      }   
}   
}



i have posted the code but it is not working, it send one char.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jan 19, 2012 - 12:04 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

lcd_putchar(buffer[i]);

What did you think this line would do?!?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 19, 2012 - 12:15 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


It will send the I too my display, but i dont now how i get that work.

Code:
      for (i=0; i<10; i++)
      {
      buffer[i] = usart_rx();
      if (i == '\n')
      {
lcd_putchar(buffer[i]);
     break;
   }


on this place?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jan 19, 2012 - 12:20 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

No, try:
Code:
      for (i=0; i<10; i++)
      {
        buffer[i] = usart_rx();
        if (i == '\n')
        {
         break;
        }
        else {
          lcd_putchar(buffer[i]);
        }
      }

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 19, 2012 - 12:28 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Sorry but it isn't working, i see only one char on my lcd screen!



Grz

Code:
int main(void)
{

USART_init(UBRR_9600);
lcd_init();

char buffer[10];
uint8_t i;
 
while(1)
    {
     for (i=0; i<10; i++)
      {
        buffer[i] = usart_rx();
        if (i == '\n')
        {
         break;
        }
        else {
        lcd_xy( 10, 0 );
          lcd_putchar(buffer[i]);
        }
      }
   }
  }

 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 19, 2012 - 12:36 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

No, it IS working - exactly as you wanted it to. Guess why Laughing

_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jan 19, 2012 - 12:40 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Guess why

IOW, which bit did you arbitrarily choose to add that I didn't tell you to use?!? Sheesh!

(if you really want it at (10,1) do that before the for() loop)

_________________


Last edited by clawson on Jan 19, 2012 - 12:41 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 19, 2012 - 12:40 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


I have on my screen one char,

So, it is not working.
Maybe is the code of the lcd not correct for a string?
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 19, 2012 - 12:42 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

No, it's just you not correct to follow the advice given Laughing

_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jan 19, 2012 - 01:27 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

We need a new sub-forum, something like "General programming questions etc". This thread is mostly not in any way about avr-gcc, or even GCC in general, but about understanding algorithms and programming in general.

Leven: You might prosper in the long run stopping AVR development for a while and write C programs on a PC to trim your C skills.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 19, 2012 - 02:14 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Code:
int main(void)
{

USART_init(UBRR_9600);
lcd_init();

char buffer[10];
uint8_t U;

while(1)
{
    while(!(UCSRA &(1<<RXC)));
    U = UDR;
                       
    if (U == 0x41)
   {     
      
      USART_tx_string("2");         
    }
   else
   {
      USART_tx_string("1");                                                                         
    }   
   }
}


this code
Code:
    if (U == 0x41)


how can i chance that example too AA ?
 
 View user's profile Send private message  
Reply with quote Back to top
DO1THL
PostPosted: Jan 19, 2012 - 02:17 PM
Resident


Joined: Aug 29, 2002
Posts: 786
Location: Muenster, Germany

well, you have (assumingly) 10 fingers. Select one of your choice (avoid the thumb, it's going to get ugly with that finger), then type at the appropriate place in the source code editor "AA".

_________________
Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Jan 19, 2012 - 02:47 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

given the current context of the questions and 0x41 = 'A', what he probably means is how to check for 2 received characters to match "AA", not one character equals 0xAA.
 
 View user's profile Send private message  
Reply with quote Back to top
DO1THL
PostPosted: Jan 19, 2012 - 02:54 PM
Resident


Joined: Aug 29, 2002
Posts: 786
Location: Muenster, Germany

Ok, granted. Then it will take an absolute minimum of brain in addition to a finger. Just set a flag on reception of the first 'A', clear it again if the next received char is not an 'A'. Anytime you receive an 'A' you check if that flag is already set, which will mean that you got two consecutive 'A' in a row. Magic, isn't it?

_________________
Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Jan 19, 2012 - 02:59 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

For a more general approach, LevenEigenaar, study this code example.
Code:
#include <avr/io.h>
#include <string.h>
#include "uart.h"

#define BUF_SIZE 10

int main(void)
{
  char buffer[BUF_SIZE];
  int index = 0;
   
  // init uart
  USART_init(UBRR_9600);

  while(1)
  {
    // wait for Rx complete
    while(!(UCSRA &(1<<RXC)));
    
    // store character in buffer
    buffer[index] = UDR;

    // process on newline character
    if (buffer[index] == '\n')
    {
       // zero terminate string on newline character
       buffer[index] = 0;

       // reset index for next string
       index = 0;

       // compare buffer to string
       if (strcmp(buffer, "AA") == 0)
          USART_tx_string("AA recieved\n");
       else
          USART_tx_string("string did nto match\n");   
    }
   
    // increment index, wrap when at end of buffer
    else if (++index == BUF_SIZE)
       index = 0;
  }
 
}

Note: untested.

This uses a simple buffer to store received characters, and performs a check for a match to the string "AA", only after a new line character has been received.

Now try to understand this first, and try to make your questions a lot clearer.
 
 View user's profile Send private message  
Reply with quote Back to top
smileymicros
PostPosted: Jan 19, 2012 - 04:37 PM
Raving lunatic


Joined: Nov 17, 2004
Posts: 6137
Location: Great Smokey Mountains.

clawson wrote:
No, try:
Code:
      for (i=0; i<10; i++)
      {
        buffer[i] = usart_rx();
        if (i == '\n')
        {
         break;
        }
        else {
          lcd_putchar(buffer[i]);
        }
      }
Cliff, I didn't want to read this entire thread to see if you are intentionally jerking the guy around, but if that isn't the case, then what about the line:
Code:
if (i == '\n')
Did you really mean to have that in the code?

[edit]
Missed last page, but question still stands.
[/edit]

Smiley

_________________
FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Jan 19, 2012 - 04:55 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

It's a typo. It should (obviously?) have read:
Code:
        if (buffer[i] == '\n')

Well spotted Embarassed

(sometimes I think I should give up!)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
smileymicros
PostPosted: Jan 19, 2012 - 05:10 PM
Raving lunatic


Joined: Nov 17, 2004
Posts: 6137
Location: Great Smokey Mountains.

Don't give up! If you give up, I'll have to start watching TV again.

Smiley

_________________
FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
DO1THL
PostPosted: Jan 19, 2012 - 08:30 PM
Resident


Joined: Aug 29, 2002
Posts: 786
Location: Muenster, Germany

Smiley,

even without Cliff being around, isn't reading a thread like this one better than any soap you could see on the TV? You could even have chips or popcorn while reading this...

_________________
Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."
 
 View user's profile Send private message  
Reply with quote Back to top
smileymicros
PostPosted: Jan 19, 2012 - 08:47 PM
Raving lunatic


Joined: Nov 17, 2004
Posts: 6137
Location: Great Smokey Mountains.

Well by analogy if Cliff does leave it will go from being a one hour soap to a 1/2 hour soap since he posts about 1/2 the content here.

Okay, I'll be nice. He posts 1/2 the worthy content here.

Smiley

_________________
FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 20, 2012 - 07:44 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Thank you all but my English is not my best. I can scripting c++ but too everything to explain is for me difficult. So SMILEY:p And thank you all for the posted, commends!

This is now the code
Code:
#include <avr/io.h>
#include <string.h>
#define UBRR_9600 103
#include "uart.h"

#define BUF_SIZE 10


void InitUART (unsigned char baudrate);
unsigned char ReceiveByte (void);
void TransmitByte (unsigned char data);
void gets1USART(char *buffer, unsigned char len);


void USART_init(unsigned int ubrr)
{
   UBRRH = (unsigned char)(ubrr>>8); //101
   UBRRL = (unsigned char)ubrr;
   UCSRB = (1<<RXEN)|(1<<TXEN);

   UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}

unsigned char usart_rx(void)
{
   while(!(UCSRA & (1<<RXC)));          //wait until reception complete
   return UDR;                     // receive value from UDR
}

void TransmitByte (unsigned char data)
{

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


  UDR = data;
}

void USART_tx_string( char *data )
{
while ((*data != '\0'))
   {
      while (!(UCSRA & (1 <<UDRE)));
      UDR = *data;
      data++;
   }   
}

int main(void)
{
  char buffer[BUF_SIZE];
  int index = 0;
   
  // init uart
  USART_init(UBRR_9600);

  while(1)
  {
    // wait for Rx complete
   while(!(UCSRA & (1<<RXC)));
     
    // store character in buffer
    buffer[index] = UDR;

    // process on newline character
    if (buffer[index] == '/n')
    {
       // zero terminate string on newline character
       buffer[index] = 0;

       // reset index for next string
       index = 0;

       // compare buffer to string
       if (strcmp(buffer, "AA") == 0)
          USART_tx_string("AA recieved\n");
       else
          USART_tx_string("string did not match\n");   
    }
   
    // increment index, wrap when at end of buffer
    else if (++index == BUF_SIZE)
       index = 0;
  }
 
}


Thank you it works! I will post a video too show you
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jan 20, 2012 - 12:12 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21257
Location: Orlando Florida

No one has asked what clock speed he has specified.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Jan 24, 2012 - 02:17 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


16 mhz i use
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 08, 2012 - 09:05 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hi,

I will try to send a int. With this code

Code:

int test5 = 5;

USART_Transmit(test5);



Example

But isnt work?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 08, 2012 - 09:43 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

What is the parameter to USAT_Transmit()? My guess is it takes either a single character or a pointer to an array of characters. If it's either of these it will not deliver what you want. The output at the PC end that you see as a human is in ASCII so, for example, the letter A is 65 and the digit 5 is 53. So if you used:
Code:
USART_Transmit(53);

you might see '5' at the other end. For numbers 0 to 9 you can convert to their ASCII value by adding 48 to them but a more useful way of specifying that 48 is to use the character '0'. So:
Code:
USART_Transmit(test5 + '0');

may result in '5' appearing at the other end.

When you have several digits to convert it gets more complicated as you have to split a multi-digit number into individual digits by repeated division by 10 and then adding '0' to each of those. For this reason (most versions of) C provide a function called itoa(). So if you use:
Code:
int n = 12345;
char output[8];
itoa(n, output, 10);

then output[] contains the sequence of ASCII characters "12345".

Now it could be that USART_Tranmit() takes a string (that is an array of characters) instead of a single character so this might be directly useable with it.

I guess we'll wait to hear what the interface to that function is.

EDIT: OK I should have re-read the whole thread. I see:
Code:
void USART_Transmit(unsigned char data);

In which case it is the case that:
Code:
USART_Transmit(test5 + '0');

will work as long as test5 is just 0 to 9. For longer numbers then use:
Code:
void USART_String(char * str) {
  while(*str) {
    USART_Transmit(*str++);
  }
}

...

int n = 12345;
char output[8];
itoa(n, output, 10);
USART_String(output);

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 08, 2012 - 10:03 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Thank you! Thank you, you help me alot!


I will try it in my script
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 08, 2012 - 11:12 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hi i got another question,

How can i make from a rs232(uart) string example 12345 a int?


Grz
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 08, 2012 - 11:30 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

How can i make from a rs232(uart) string example 12345 a int?

The opposite of itoa() is (surprise, surprise!) atoi(). Use it as follows;
Code:
char input_string = "12345";
int n;
n = atoi(input_string);

(in reality you would be building up input_string a character at a time by receiving them from the UART). atoi() stops converting when it reaches a character that is not in "-0123456789"

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 08, 2012 - 12:38 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Ehm, i mean.

If i send a variable code example 12345 or 13245 how can i example make on this variable a int function?


Grz
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 08, 2012 - 01:51 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Type your question in Dutch as I don't follow what you are asking in English.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 09:31 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hi i have solved the problem.

I have another queastion.

I have a buffer from example 10, how can I put the buffer[5] to a int?

example 12345678 5 is the number for the int
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 09:48 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

Code:
int i = buffer[4] - '0';
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 09:58 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Thank you!


Why do you - 0 ??

Grz
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 10:11 AM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hi,

It sends none char to my int with your code?!
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 10:27 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

Quote:

Why do you - 0 ??

I don't. I do - '0'.

There is a principal difference between 0 and '0'. The character '0' has ASCII value 48, '1' is 49 and so on.

So by taking any character '0' to '9' and subtract 48 you get the numerical value of the digit character. Using '0' or 48 to subtract does not matter, technically they are identical. But for keeping the code readable I think '0' is neater.

Quote:
It sends none char to my int with your code?!

Show minimal, but complete (as in buids and runs), test program.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 03:56 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Okay.

I understand what you mean. But for me it is not entirely clear.

If I get a string from the rs232 for example 3636. How can I use the number 3 as int? So I, for example, the arithmetic value 3 can do 1000 times

I want to use real timer from a server, the server sends 3450 that means 2 hours evening. That I bear the clock in the Atmega can recover. If the time expires.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 04:07 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

Quote:
If I get a string from the rs232 for example 3636. How can I use the number 3 as int?

I thought I was clear.. Assuming that the string "3636" is in a variable called s, then getting the numerical value of the first digit can be done
Code:
int value = s[0];

and for the third digit
Code:
int value = s[2];


Quote:
So I, for example, the arithmetic value 3 can do 1000 times

I want to use real timer from a server, the server sends 3450 that means 2 hours evening. That I bear the clock in the Atmega can recover. If the time expires.

I am truly sorry, but I can not understand this at all.

What do you mean by "arithmetic value 3 can do 1000 times"? Could you express this in another way?

What makes "3450" map to "2 hours evening" (which I read as "2 o'clock in the afternoon", i.e. "2 PM")?


Last edited by JohanEkdahl on Feb 09, 2012 - 04:13 PM; edited 1 time in total
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Feb 09, 2012 - 04:09 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

I already told you about atoi()?!? What that does is convert each digit it comes to from ASCII to binary (by subtracting 48) then it adds that into a running total and if there are more digits to go it multiplies the total by 10. So for "3636" it would read the '3', subtract 48 to convert '3' into 3 and add it to the total (0 at the start) so the total is 3. There are more digits to go so it multiplies the total by 10 to make it 30 then read the next digit which is '6'. It subtracts 48 to convert '6' into 6 then adds this to the total. As it was 30 it is now 36. There are more digits so it multiplies by 10 to get 360 then reads '3', converts to 3 then adds to the total to get 363. Finally it does the same for 6 so the total is 3636 - which is what you want.

So why do this yourself? Why not use atoi() like I told you?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 04:18 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

Oh so that is what is wanted?
Convert a string consisting of numerical characters to an integer value?

Yes, atoi().
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:11 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Okay I get it a little bit what you mean.

@ JohanEkdahl

I have 2 counters running (server) and (microprocessor) set as the source voltage of the ic is too low, the counter in the ic ward no longer be installed. The server ensures that 2 times a day, a string is sent to the ic to the counter in the ic again correctly set.

What I mean by the time

The numerator is the time, for example,

1day = 14400 minutes (14400/3) = value of counter in ic is 480

So for example, 480 is 00.00 hours
240 is so for example 12.00

7 days = 100800 minutes (100800/3) = value of counter ic is 33600

The same counter is also in the server. Set the accumulator of the ic is empty, then the counter is no longer increase. If the ic is re-inflated, the server will reset the ic equate the numerator of the server.

/ / --------------------------------

So I get off of the server instance, the time 480 (00.00) hours

The ic 480 but does not see (4) (Cool (0) separately

So I want the 4 for example convert an int, and multiplying by 100

Same story as 8 multiplied by 10

And multiplying, for example, with 0 0

Together on beats and you have the same value.

But how can I buffer so for example [1] (This is the value 4) equate to an int?



Thank you for your time, regards
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 09, 2012 - 06:18 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

But how can I buffer so for example [1] (This is the value 4) equate to an int?

Are you listening or not?
Code:
char input = "3450";
int n;
n = atoi(input);

Now n contains the numeric value 3450.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:23 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Yes I understand that but you designate a default value of 3450, that variable must be!

That I from the server each time a different value from sending messages.

So, for example 3450 or 3451 or 3452

Or did I do wrong now?
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:27 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


, Or 3450 is the maximum value?

char input = "3450", / / char up to 3450 so it can also be 33600
int n, / / ​​int value can be so 3
n = atoi (input) / / input buffer [1] For example, this would be the value 3?!

Am I doing it right?
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:50 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Code:
/* convert a string to an integer */
   #include <stdio.h>
   #include <stdlib.h>

   main()
   {
      int sum;
      sum = atoi(buffer[1]);

   }
/

For example, buffer [4] is number 3

Then the int also 3?
 
 View user's profile Send private message  
Reply with quote Back to top
sternst
PostPosted: Feb 09, 2012 - 06:59 PM
Raving lunatic


Joined: Jul 23, 2001
Posts: 2438
Location: Osnabrueck, Germany

LevenEigenaar wrote:
Code:
/* convert a string to an integer */
   #include <stdio.h>
   #include <stdlib.h>

   main()
   {
      int sum;
      sum = atoi(buffer[1]);

   }
/

For example, buffer [4] is number 3

Then the int also 3?
No.
The solution for a single digit was already handed to you by Johan.
Code:
sum = buffer[4] - '0';
And if you are actually getting numbers over the serial line instead of characters, then the solution is
Code:
sum = buffer[4];

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 07:13 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Okay, but why is that piece of script to apply?

So I must do the following

int test;

test = buffer [4];

test = test * 1000;

This must just work?

Buffer [4] can be different for each buffering
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 07:24 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Hey guys

I do not this piece?! Can you explain this to me. Why there - '0 '; is used?

sum = buffer [4] - '0 ';
 
 View user's profile Send private message  
Reply with quote Back to top
sternst
PostPosted: Feb 09, 2012 - 07:24 PM
Raving lunatic


Joined: Jul 23, 2001
Posts: 2438
Location: Osnabrueck, Germany

LevenEigenaar wrote:
This must just work?
It depends on what actually is in buffer[4]. Is it a binary number, or is it a single character representing a single digit of a number?

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 07:35 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Yes you understand what I mean! sternst


It is NOT a binary but a sign! That's the problem. And thus would like to convert data to binary
 
 View user's profile Send private message  
Reply with quote Back to top
DO1THL
PostPosted: Feb 09, 2012 - 08:20 PM
Resident


Joined: Aug 29, 2002
Posts: 786
Location: Muenster, Germany

Boy (LevenEigenaar),

before you do a single keystorke again here, please think (!!!) twice about the difference of a numeric value and a printable character. There's a huge difference between a 3 and a '3', which you simply don't seem to recognize. And that huge difference is that the number 3 (a number is something a processor can do calculations with) has the value 3, whereas the character '3' (something you will see at your terminal as a single printed character) is something that visibly represents the number 3 in a way humans can read.

Once you got that, then you might understand that the difference between a numerical value 0, 1, 2, 3... and the printed character '0', '1', '2', '3'... is simply a fixed offset of 48 (decimal). Since any electronic device needs to find a way to represent printable characters, some coding rules were invented. The coding rule applicable here is called ASCII, a large table that for each printable (and some non-printable ones) character shows the numeric representation when being used in a digital system. So you will find that a numeric value 3 has the numeric value of 3 (who wonders), and the printable character '3' according to the ASCII coding scheme has the value of 51. To get back and forth between numeric values and their printable representation, just ad or subtract this magic 48!

Once you got that, re-read the last dozen replies to your questions, and a sudden enlightenment will shine upon you!

_________________
Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 08:58 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Dankje voor je uitleg,

Het is nu voor mij een heel stuk duidelijker!!

De buffer[4] is een afgedrukte waarde
En een int is een numerieke waarde

Dat begrijp ik!

Bijvoorbeeld de buffer[4] = 4, dat is een afgedrukte character
Dit is dan toch ook voor '0'? een numerieke character?

Waarom gebruiken jullie een - ???
En wat is de werking van het onderstaande code


sum = buffer[4] - '0';

Google Translate wrote:
Thank you for explaining,

It is now a lot clearer for me!

The buffer [4] is a face value
And an int is a numerical value

I understand!

For example, the buffer [4] = 4, which is a printed character
This is then, too, for the '0 '? a numeric character?

Why do you have a -??
And what is the operation of the code below

sum = buffer[4] - '0';
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 09:12 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18545
Location: Lund, Sweden

Quote:

Can you explain this to me. Why there - '0 '; is used?


Again: By taking any character '0' to '9' and subtract 48 you get the numerical value of the digit character. Using '0' or 48 to subtract does not matter, technically they are identical. But for keeping the code readable I think '0' is neater.

Do you understand it better if we just did -48?

Do you at all follow us if we say that the ASCII value of e.g. 'A' is 65 and (here it comes again...) the ASCII value of '0' is 48? Or is this just mumbo-jumbo for you? (If so then you might be helped by going over to Wikipedia and read the article on "ASCII"). I ask these question so that we will know where to start explaining.

Anyhow, why are you pursuing the -'0' thing still? We've already established that what you need instead is to use the atoi() function, which you do not need to write yourself at all. It is a part of avrlibc.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 09:28 PM
Hangaround


Joined: Oct 13, 2011
Posts: 164


Thank you guys for your efforts in my project, it is now completely clear. Tomorrow I'm testing on my work.


Thank you for your time, and miss the next post!

What this forum is great!

Greetings
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits