| Author |
Message |
|
|
Posted: Jan 19, 2012 - 02:14 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
|
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 ? |
|
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 02:17 PM |
|

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..."
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 02:47 PM |
|


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. |
|
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 02:54 PM |
|

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..."
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 02:59 PM |
|


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. |
|
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 04:37 PM |
|


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
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 04:55 PM |
|


Joined: Jul 18, 2005
Posts: 62354
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
It's a typo. It should (obviously?) have read:
Code:
if (buffer[i] == '\n')
Well spotted
(sometimes I think I should give up!) |
_________________
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 05:10 PM |
|


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
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 08:30 PM |
|

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..."
|
| |
|
|
|
|
|
Posted: Jan 19, 2012 - 08:47 PM |
|


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
|
| |
|
|
|
|
|
Posted: Jan 20, 2012 - 07:44 AM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
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 |
|
|
| |
|
|
|
|
|
Posted: Jan 20, 2012 - 12:12 PM |
|


Joined: Sep 04, 2002
Posts: 21272
Location: Orlando Florida
|
|
| No one has asked what clock speed he has specified. |
_________________ Imagecraft compiler user
|
| |
|
|
|
|
|
Posted: Jan 24, 2012 - 02:17 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
|
|
|
|
|
Posted: Feb 08, 2012 - 09:05 AM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
Hi,
I will try to send a int. With this code
Code:
int test5 = 5;
USART_Transmit(test5);
Example
But isnt work? |
|
|
| |
|
|
|
|
|
Posted: Feb 08, 2012 - 09:43 AM |
|


Joined: Jul 18, 2005
Posts: 62354
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);
|
_________________
|
| |
|
|
|
|
|
Posted: Feb 08, 2012 - 10:03 AM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
Thank you! Thank you, you help me alot!
I will try it in my script |
|
|
| |
|
|
|
|
|
Posted: Feb 08, 2012 - 11:12 AM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
Hi i got another question,
How can i make from a rs232(uart) string example 12345 a int?
Grz |
|
|
| |
|
|
|
|
|
Posted: Feb 08, 2012 - 11:30 AM |
|


Joined: Jul 18, 2005
Posts: 62354
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" |
_________________
|
| |
|
|
|
|
|
Posted: Feb 08, 2012 - 12:38 PM |
|


Joined: Oct 13, 2011
Posts: 165
|
|
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 |
|
|
| |
|
|
|
|
|
Posted: Feb 08, 2012 - 01:51 PM |
|


Joined: Jul 18, 2005
Posts: 62354
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
| Type your question in Dutch as I don't follow what you are asking in English. |
_________________
|
| |
|
|
|
|
|