| Author |
Message |
|
|
Posted: Nov 24, 2011 - 12:07 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
Thank you all its working! Thank you!!!!
But yes yes !
0x3a that is the symbol Z??
On the hex table means z 7a? |
|
|
| |
|
|
|
|
|
Posted: Nov 24, 2011 - 12:21 PM |
|


Joined: Jul 18, 2005
Posts: 62354
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) |
_________________
|
| |
|
|
|
|
|
Posted: Nov 24, 2011 - 01:56 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
Ok, i understand!
But if i send 0x5A it show not a Z??? |
|
|
| |
|
|
|
|
|
Posted: Nov 24, 2011 - 02:55 PM |
|


Joined: Jul 18, 2005
Posts: 62354
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. |
_________________
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 11:42 AM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
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);
} |
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 01:14 PM |
|


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
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 01:16 PM |
|


Joined: Jul 18, 2005
Posts: 62354
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. |
_________________
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 01:21 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
| Crystal? I am a beginner! so i dont now what crystal means. |
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 01:36 PM |
|


Joined: Jul 18, 2005
Posts: 62354
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?
 |
_________________
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 01:48 PM |
|


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.  |
_________________ Leon Heller
G1HSM
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 02:12 PM |
|


Joined: Sep 04, 2002
Posts: 21272
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
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 02:22 PM |
|


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
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 02:40 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
|
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 |
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 02:42 PM |
|


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. |
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 02:48 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
|
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 ? |
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 02:52 PM |
|


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. |
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 02:58 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
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);
}
}
}
|
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 03:01 PM |
|


Joined: Sep 04, 2002
Posts: 21272
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
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 03:07 PM |
|


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. |
|
|
| |
|
|
|
|
|
Posted: Nov 25, 2011 - 03:27 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
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!!
How many M did i need for the crystal ?
Do you speak Dutch, because you are from belgium? |
|
|
| |
|
|
|
|
|