number transmit over uart and receive

Go To Last Post
26 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#ifndef F_CPU
#define F_CPU 8000000UL // 16Mhz clock speed
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
//#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) -1)

void UART_init(long USART_BAUDRATE)
{
	UCSRB |=(1<<RXEN)|(1<<TXEN);//TURN ON TRANSMISSION AND RECEPTION.
	UCSRC |=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);//USE 8 BIT CHARACTER.
	UBRRL |=(BAUD_PRESCALE);//LOAD LOWER 8 BITS OF THE BAUD RATE VALUE.
	UBRRH |=(BAUD_PRESCALE>>8);//LOAD UPPER 8 BIT.
}
unsigned char UART_Rxchar()
{
	while((UCSRA & (1<<RXC))==0);//wait till data is received.
	return(UDR);//RETURN THE BYTE.
}
void UART_Txchar(char ch)
{
	while (!(UCSRA & (1<<UDRE)));//wait for empty transmit buffer.
	UDR=ch;
	
}
void UART_sendstring(char *str)
{
	unsigned char j=0;
	while(str[j]!=0)     // send string till null.
	{
		UART_Txchar(str[j]);
		j++;
	}
}

int main()
{
	char C;
	UART_init(9600);
	uint8_t a=55;
	char buff[5];
	        itoa(a,buff,10);
	        UART_sendstring(buff);
			UART_sendstring("\n\r");
	while(1)
	{

		
	}
}

transmit code......

#ifndef F_CPU
#define F_CPU 8000000UL // 16Mhz clock speed
#endif
#include <avr/io.h>
#include <string.h>
#include <util/delay.h>
#include <avr/interrupt.h>
//#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) -1)
#define LED_ON  PORTD |= (1<<PIND6);
#define LED_OFF  PORTD &= ~(1<<PIND6);

#define DS4_HIGH	PORTB|=(1<<PB0)
#define DS4_LOW	PORTB &=~(1<<PB0)

#define DS3_HIGH	PORTA|=(1<<PA2)
#define DS3_LOW	PORTA &=~(1<<PA2)

#define DS2_HIGH	PORTA|=(1<<PA3)
#define DS2_LOW	PORTA &=~(1<<PA3)

#define DS1_HIGH	PORTA|=(1<<PA4)
#define DS1_LOW	PORTA &=~(1<<PA4)

#define A_HIGH	PORTB|=(1<<PB1)
#define A_LOW	PORTB &=~(1<<PB1)

#define B_HIGH	PORTB|=(1<<PB2)
#define B_LOW	PORTB &=~(1<<PB2)

#define C_HIGH	PORTC|=(1<<PC6)
#define C_LOW	PORTC &=~(1<<PC6)

#define D_HIGH	PORTC|=(1<<PC5)
#define D_LOW	PORTC &=~(1<<PC5)

#define E_HIGH	PORTA|=(1<<PA5)
#define E_LOW	PORTA &=~(1<<PA5)

#define F_HIGH	PORTB|=(1<<PB4)
#define F_LOW	PORTB &=~(1<<PB4)

#define G_HIGH	PORTB|=(1<<PB3)
#define G_LOW	PORTB &=~(1<<PB3)

#define DOT_HIGH	PORTC|=(1<<PC7)
#define DOT_LOW	PORTC &=~(1<<PC7)

volatile int alpha=0;
volatile uint8_t a=1,K,L,M,N;

void zero(void)
{
	A_LOW;
	B_LOW;
	C_LOW;
	D_LOW;
	E_LOW;
	F_LOW;
	G_HIGH;
}

void one ()
{
	
	A_HIGH;
	B_LOW;
	C_LOW;
	D_HIGH;
	E_HIGH;
	F_HIGH;
	G_HIGH;
}

void two ()
{
	
	A_LOW;
	B_LOW;
	C_HIGH;
	D_LOW;
	E_LOW;
	F_HIGH;
	G_LOW;
}

void three ()
{
	
	A_LOW;
	B_LOW;
	C_LOW;
	D_LOW;
	E_HIGH;
	F_HIGH;
	G_LOW;
}

void four ()
{
	A_HIGH;
	B_LOW;
	C_LOW;
	D_HIGH;
	E_HIGH;
	F_LOW;
	G_LOW;
}

void five(){
	
	A_LOW;
	B_HIGH;
	C_LOW;
	D_LOW;
	E_HIGH;
	F_LOW;
	G_LOW;
	
}

void six(){
	A_LOW;
	B_HIGH;
	C_LOW;
	D_LOW;
	E_LOW;
	F_LOW;
	G_LOW;
}

void seven(){

	A_LOW;
	B_LOW;
	C_LOW;
	D_HIGH;
	E_HIGH;
	F_HIGH;
	G_HIGH;
}

void eight(){
	A_LOW;
	B_LOW;
	C_LOW;
	D_LOW;
	E_LOW;
	F_LOW;
	G_LOW;
}

void nine(){
	A_LOW;
	B_LOW;
	C_LOW;
	D_LOW;
	E_HIGH;
	F_LOW;
	G_LOW;
}

void dash(){
	A_HIGH;
	B_HIGH;
	C_HIGH;
	D_HIGH;
	E_HIGH;
	F_HIGH;
	G_LOW;
}

void UART_init(long USART_BAUDRATE)
{
	UCSRB |=(1<<RXEN)|(1<<TXEN);//TURN ON TRANSMISSION AND RECEPTION.
	UCSRC |=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);//USE 8 BIT CHARACTER.
	UBRRL |=(BAUD_PRESCALE);//LOAD LOWER 8 BITS OF THE BAUD RATE VALUE.
	UBRRH |=(BAUD_PRESCALE>>8);//LOAD UPPER 8 BIT.
}
unsigned char UART_Rxchar()
{
	while((UCSRA & (1<<RXC))==0);//wait till data is received.
	return(UDR);//RETURN THE BYTE.
}
void UART_Txchar(char ch)
{
	while (!(UCSRA & (1<<UDRE)));//wait for empty transmit buffer.
	UDR=ch;
	
}

void T2delay()
{
	while(TIFR&0x01==0);
	TCCR2=0;
}

void T2_init()
{
	TCCR2 = (1<<CS22);//|(1<<CS21);//|(1<<CS20);
	TCNT2 = 0xB2;
}
void print_1(int b)
{
    DS1_HIGH;
	DS2_LOW;
	DS3_LOW;
	DS4_LOW;
	switch (b)
	{
		case 1:
		one();
		break;
		
		case 2:
		two();
		break;
		
		case 3:
		three();
		break;
		
		case 4:
		four();
		break;
		
		case 5:
		five();
		break;
		
		case 6:
		six();
		break;
		
		case 7:
		seven();
		break;
		
		case 8:
		eight();
		break;
		
		case 9:
		nine();
		break;
		
		case 0:
		zero();
		break;
		
		default:
		dash();
		
	}
	
}
void print_2(int c)
{
    DS1_LOW;
	DS2_HIGH;
	DS3_LOW;
	DS4_LOW;
	switch (c)
	{
		case 1:
		one();
		break;
		
		case 2:
		two();
		break;
		
		case 3:
		three();
		break;
		
		case 4:
		four();
		break;
		
		case 5:
		five();
		break;
		
		case 6:
		six();
		break;
		
		case 7:
		seven();
		break;
		
		case 8:
		eight();
		break;
		
		case 9:
		nine();
		break;
		
		case 0:
		zero();
		break;
		
		default:
		dash();
		
	}
	
}
void print_3(int d)
{
	DS1_LOW;
	DS2_LOW;
	DS3_HIGH;
	DS4_LOW;
	switch (d)
	{
		case 1:
		one();
		break;
		
		case 2:
		two();
		break;
		
		case 3:
		three();
		break;
		
		case 4:
		four();
		break;
		
		case 5:
		five();
		break;
		
		case 6:
		six();
		break;
		
		case 7:
		seven();
		break;
		
		case 8:
		eight();
		break;
		
		case 9:
		nine();
		break;
		
		case 0:
		zero();
		break;
		
		default:
		dash();
		
	}
	
}
void print_4(int g)
{
		DS1_LOW;
		DS2_LOW;
		DS3_LOW;
		DS4_HIGH;
	switch (g)
	{
			case 1:
			one();
			break;

			case 2:
			two();
			break;

			case 3:
			three();
			break;

			case 4:
			four();
			break;

			case 5:
			five();
			break;

			case 6:
			six();
			break;

			case 7:
			seven();
			break;

			case 8:
			eight();
			break;

			case 9:
			nine();
			break;

			case 0:
			zero();
			break;

			default:
			dash();
	}
}

void ascii(uint16_t f)
{

	K=f%10;
	f=f/10;

	L=f%10;
	f = f/10;
	
	M=f%10;
	f = f/10;
	
	N=f%10;
	f = f/10;

	
}


void getString(char * buf, int len) {
	int idx = 0;
	while(1)
	{
		char c = UART_Rxchar();
		
		if (c == '\0')
		{
			continue;
		}
		// if it is RET or NL..
		if ((c == '\r') || (c == '\n'))
		{
			break;
		}
		// else store the received character
		buf[idx] = c;
		// increment the index
		idx++;
		// if reached the end (with room for final 0x00) break
		if (idx == (len - 1))
		{
			break;
		}
	}
	// mark the end of the
	buf[idx] = 0x00;
	alpha=1;
}

ISR(TIMER2_OVF_vect)
{
	if (a==1)
	{
		print_1(N);
		a=2;
	}
	else if (a==2)
	{
		print_2(M);
		a=3;
	}
	else if (a==3)
	{
		print_3(L);
        a=4;
	}
	else if (a==4)
	{
		print_4(K);
		a=1;
	}
}


int main()
{
	DDRB=0xFF;
	DDRA=0xFF;
	DDRC=0xff;
	TIMSK=(1<<TOIE2);
	T2_init();
	sei();
	DDRD=(1<<PIND6);
	char C;
	UART_init(9600);
	uint8_t rxString[6];
	uint8_t dat;
	while(1)
	{
		getString(rxString,6);
		if (alpha==1)
		{
			//dat=rxString[0];
			ascii(rxString);
			
		}
		

		
	}
}





this is the receiver code.

hello sir, i am trying to transmit and receive  a number over uart and printing it to 7 segment..actually i wanted to make adc read transmit and receive then print it to 7 segment.but it is just a demo code to see whether it is working or not.The number is transmitting as i want but the receiver side has a problem it is giving me 213 continuously..so kindly help me to find any solution.i am providing here an image showing what i am getting i my Proteus.

 

Attachment(s): 

Rjchoudhary

Last Edited: Sat. Jun 6, 2020 - 10:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rajat choudhary wrote:

			ascii(rxString);

 

What do you think that line if code does?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here i have stored the array value in four different variables.....

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But your ascii function is not expecting an array is it?..

 

 

Rajat choudhary wrote:

void ascii(uint16_t f)

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes,what should i do now.?

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rajat choudhary wrote:

Here i have stored the array value in four different variables.....

And also completely ignored the compiler warning against that line of code:

rajat.c:511:10: warning: passing argument 1 of 'ascii' makes integer from pointer without a cast [-Wint-conversion]
    ascii(rxString);

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
your ascii function is not expecting an array is it?..

Rajat choudhary wrote:
what should i do now.?

make it so that it does expect an array!

 

This is standard 'C' programming stuff - not specific to AVR

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually i can't understand how to deal with this...how can i receive and store a decimal number and then print it on 7 seg... I have done the transmission but i can't figure it out how i do next

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rajat choudhary wrote:
Actually i can't understand how to deal with this

This is really standard 'C' programming stuff - not specific to AVR.

 

It is a lot easier to learn the 'C' programming language on a PC or similar - away from all the added complications & restrictions of small microcontrollers!

 

Once you have a good grasp of the language, then is the time to move on to apply it on the AVR

 

Here are some 'C' learning & reference materials for you:

 

http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

 

See also the Beginner's Getting Started tips at #6 in my signature, below.

 

 i receive and store a decimal number and then print it on 7 seg...

That's two entirely separate things - don't try to do them both at once!

 

In his Tutorial, 'Help! It doesn't work!', Kartman wrote:
3. Ask yourself “how many problems do I want to solve at once”? The answer should be one. Trying to solve more than one problem at a time is a recipe for failure

 

https://www.avrfreaks.net/forum/help-it-doesnt-work

 

Separate the 2 problems:

  1. receive stuff on the UART
  2. display stuff on a 7-Segment display

 

There are plenty of examples, tutorials, etc on each of these ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Jun 6, 2020 - 09:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you starting another round about uarts sending/receiving strings?  Wasn't it already covered?
 

Now if you are moving on to a seven seg display, put the uart aside & assume you already have some numbers....

 

Get THAT code with those numbers (say 1234, 957, 806) to work & concentrate on the display..

 

Working a 7 seg display should only take a few lines of code (like maybe 10 lines) & a table of segment decoding.  How are you ending up with a mile of code?

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why don't you simply connect up STDIO and use C standard functions for this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sir, i can't figure it out how i receive and store the decimal value.. And then put it into a variable.. After doing that i can print it on 7 segment...please look my transmission code it is transmitting a decimal number...as my earlier topic string receive and compare over uart i am doing same to get the number in array but it is not working.. I have really problem with doing it i am not starting a new topic,im just asking you a question which i don't understand

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Google scanf().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sir, i can't figure it out how i receive and store the decimal value

If you get it as a string ( 2 ...4 ....3 ....6 ) why not just buffer it and display those digits?  If you need to use the actual numerical value later, it is an easy conversion.   For the display you need separate digits, keep them separate.   The ascii value and digit val are just different by 0x30

so the ascii char 7   is 0x37, you can store either 0x37 or 0x07. 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Jun 7, 2020 - 01:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you honestly believe you are the first person who's wanted to convert binary values into human readable ASCII then receive same and convert it back?this stuff goes to the very core of Cs existence which is why it has things like printf(), sprintf(), scanf(), sscanf(). I suggest you learn about these core features of C first (which is probably more easily done in PC programming) then apply that knowledge to AVR. As I said previously you just need to connect stdin/stdout to UART receive/send character functions then you can do it on an AVR just as you learned to do it on a PC. (BTW, Ignore any idiot who comes along to tell you that those functions have no relevance to micros like AVR)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
As I said previously you just need to connect stdin/stdout to UART receive/send character functions

Thanks for clarifying that - I thought your #11 meant hookup stdout to his 7-segment display. Perhaps a bit of an advanced concept for Rajat but that may be what he really wants.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


int main()
{
	DDRB=0xFF;
	DDRA=0xFF;
	DDRC=0xff;
	TIMSK=(1<<TOIE2);
	T2_init();
	sei();
	DDRD=(1<<PIND6);
	char C;
	UART_init(9600);
	uint16_t rxString[6];
	uint16_t buff[6];
	uint16_t dat=1023,dat2;
	  char w,t;
	while(1)
	{
		//dat=UART_Rxchar();
		getString(rxString,6);
		if (alpha==1)
		{

			UART_sendstring(rxString);

			 w=rxString[0];
			 t=rxString[1];
			 if (w=='1')
			 {
				 ascii(1023);
			 }

	}
}

hello sir, why my array does'not working this way.i am trying to store the array's data in different variables and here i am storing the 0th position to char w and comparing it works fine but when it comes to store 1st position data in char t variable and comparing it is not working...can you please tell me why this is happening. i am still stuck on this topic to receive a number through usart.

Rjchoudhary

Last Edited: Wed. Jun 10, 2020 - 06:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you Google scanf()?

 

Here - I've done it for you.

https://www.tutorialspoint.com/c...

 

if you write something like:

 

int value;

char msg[20];

sscanf(rxString,"%d",&value);

 

(if (value ==123)

{

sprintf(msg,"The value is: %d\r\n",value); 

}

assuming your string has a valid number string, it will get converted and the result in the variable value.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To use stdio you will just need to make small adjustments to:

unsigned char UART_Rxchar()
{
	while((UCSRA & (1<<RXC))==0);//wait till data is received.
	return(UDR);//RETURN THE BYTE.
}
void UART_Txchar(char ch)
{
	while (!(UCSRA & (1<<UDRE)));//wait for empty transmit buffer.
	UDR=ch;
	
}

so they become:

int UART_Rxchar(FILE * stream)
{
	while((UCSRA & (1 << RXC))==0);//wait till data is received.
	return UDR;//RETURN THE BYTE.
}

int UART_Txchar(char ch, FILE * stream)
{
	while (!(UCSRA & (1<<UDRE)));//wait for empty transmit buffer.
	UDR=ch;

        return 0;	
}

Having done that you can then add to your code:

FILE uart_str = FDEV_SETUP_STREAM(UART_Txchar, UART_Rxchar, _FDEV_SETUP_RW);

and after the call to UART_init() use:

  stdout = stdin = &uart_str;

and now you can simply use getchar(), putchar(), puts(), sprintf(), scanf() and all the other standard C input/output functions. No need to "invent your own". 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Rajat. You must read the compiler warnings and work through each to fix the problem.

 

Form some unknown reason, you changed the definition of rxString from char rxString[6]; to uint16_t rxString[6];

 

This line now is faulty and generates a warning: warning C4133: 'function': incompatible types - from 'uint16_t [6]' to 'char *'

        getString(rxString, 6);

 

Revert the uint16_t rxString[6] change and all should be well.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define F_CPU 8000000
#define BAUD 9600
#define MYUBRR (F_CPU/16/BAUD-1)


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


static int USART_Transmit_IO(char c, FILE *stream);
static unsigned char USART_Receive_IO( FILE *stream );
//static FILE mystdout=FDEV_SETUP_STREAM(USART_Transmit_IO,USART_Receive_IO,_FDEV_SETUP_WRITE);
static FILE uart_str = FDEV_SETUP_STREAM(NULL,USART_Receive_IO, _FDEV_SETUP_RW);

void getstring2(char *store, int len2 )
{
	int idx2=0;
		while(1)
		{
			char c =getchar();
			
			if (c == '\0')
			{
				continue;
			}
			// if it is RET or NL..
			if ((c == '\r') || (c == '\n'))
			{
				break;
			}
			// else store the received character
			store[idx2] = c;
			// increment the index
			idx2++;
			// if reached the end (with room for final 0x00) break
			if (idx2 == (len2 - 1))
			{
				break;
			}
		}
			store[idx2] = 0x00;
			alpha=1;
}

int main(void)
{
		DDRB=0xFF;
		DDRA=0xFF;
		DDRC=0xff;
		TIMSK=(1<<TOIE2);
		T2_init();
		sei();
	USART_Init(MYUBRR);
	
	stdin = &uart_str;
	sei();
	
	char buff[5];
    uint16_t buff2[4];
	char q,w,e,r;
	uint16_t dat;
	while (1)
	{
	
		getstring2(buff,5);
		if (alpha==1)
		{
			
			q=buff[0];
			w=buff[1];
			e=buff[2];
			r=buff[3];
			if (q != '\0')
			{
				if (q=='1')
				{
					buff2[0]=1;
				}
				else if (q=='2')
				{
					buff2[0]=2;
				}
				else if (q=='3')
				{
					buff2[0]=3;
				}
				else if (q=='4')
				{
					buff2[0]=4;
				}
				else if (q=='5')
				{
					buff2[0]=5;
				}
				else if (q=='6')
				{
					buff2[0]=6;
				}
				else if (q=='7')
				{
					buff2[0]=7;
				}
				else if (q=='8')
				{
					buff2[0]=8;
				}
				else if (q=='9')
				{
					buff2[0]=9;
				}
				else if (q=='0')
				{
					buff2[0]=0;
				}
			}
			if (w != '\0')
			{
				if (w=='1')
				{
					buff2[1]=1;
				}
				else if (w=='2')
				{
					buff2[1]=2;
				}
				else if (w=='3')
				{
					buff2[1]=3;
				}
				else if (w=='4')
				{
					buff2[1]=4;
				}
				else if (w=='5')
				{
					buff2[1]=5;
				}
				else if (w=='6')
				{
					buff2[1]=6;
				}
				else if (w=='7')
				{
					buff2[1]=7;
				}
				else if (w=='8')
				{
					buff2[1]=8;
				}
				else if (w=='9')
				{
					buff2[1]=9;
				}
				else if (w=='0')
				{
					buff2[1]=0;
				}
			}
				if (e != '\0')
				{
					if (e=='1')
					{
						buff2[2]=1;
					}
					else if (e=='2')
					{
						buff2[2]=2;
					}
					else if (e=='3')
					{
						buff2[2]=3;
					}
					else if (e=='4')
					{
						buff2[2]=4;
					}
					else if (e=='5')
					{
						buff2[2]=5;
					}
					else if (e=='6')
					{
						buff2[2]=6;
					}
					else if (e=='7')
					{
						buff2[2]=7;
					}
					else if (e=='8')
					{
						buff2[2]=8;
					}
					else if (e=='9')
					{
						buff2[2]=9;
					}
					else if (e=='0')
					{
						buff2[2]=0;
					}
				}
					if (r != '\0')
					{
						if (r=='1')
						{
							buff2[3]=1;
						}
						else if (r=='2')
						{
							buff2[3]=2;
						}
						else if (r=='3')
						{
							buff2[3]=3;
						}
						else if (r=='4')
						{
							buff2[3]=4;
						}
						else if (r=='5')
						{
							buff2[3]=5;
						}
						else if (r=='6')
						{
							buff2[3]=6;
						}
						else if (r=='7')
						{
							buff2[3]=7;
						}
						else if (r=='8')
						{
							buff2[3]=8;
						}
						else if (r=='9')
						{
							buff2[3]=9;
						}
						else if (r=='0')
						{
							buff2[3]=0;
						}
					}

					
				dat=atoi(buff2);

                                ascii(dat);
		}

		
	}
}



sir i have stored integer numbers in my array and now when i want to store it in a variable with the help of atoi function it is not working but my array has the numbers which i transmitted through my transmitter function.can you suggest me to find the solution..

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
			{
				if (q=='1')
				{
					buff2[0]=1;
				}
				else if (q=='2')
				{
					buff2[0]=2;
				}
				else if (q=='3')
				{
					buff2[0]=3;
				}
				else if (q=='4')
				{
					buff2[0]=4;
				}
				else if (q=='5')
				{
					buff2[0]=5;
				}
				else if (q=='6')
				{
					buff2[0]=6;
				}
				else if (q=='7')
				{
					buff2[0]=7;
				}
				else if (q=='8')
				{
					buff2[0]=8;
				}
				else if (q=='9')
				{
					buff2[0]=9;
				}
				else if (q=='0')
				{
					buff2[0]=0;
				}
			}

Seems like an overly complicated way to do:

if ((q >= '0') && ( q <= '9')) {
    buff2[0] = q - '0';
}

also why on earth are you doing:

			q=buff[0];
			w=buff[1];
			e=buff[2];
			r=buff[3];

and then doing that same block of code for each of 'q'. 'w', 'e' and 'r'? Surely you just do:

uint8_t q;
for (int bufidx = 0; bufidx < 4; i++) {
    q = buff[bufidx];
    if ((q >= '0') && ( q <= '9')) {
        buff2[bufidx] = q - '0';
    }
}

Those 7 lines of C completely replace:

		{

			q=buff[0];
			w=buff[1];
			e=buff[2];
			r=buff[3];
			if (q != '\0')
			{
				if (q=='1')
				{
					buff2[0]=1;
				}
				else if (q=='2')
				{
					buff2[0]=2;
				}
				else if (q=='3')
				{
					buff2[0]=3;
				}
				else if (q=='4')
				{
					buff2[0]=4;
				}
				else if (q=='5')
				{
					buff2[0]=5;
				}
				else if (q=='6')
				{
					buff2[0]=6;
				}
				else if (q=='7')
				{
					buff2[0]=7;
				}
				else if (q=='8')
				{
					buff2[0]=8;
				}
				else if (q=='9')
				{
					buff2[0]=9;
				}
				else if (q=='0')
				{
					buff2[0]=0;
				}
			}
			if (w != '\0')
			{
				if (w=='1')
				{
					buff2[1]=1;
				}
				else if (w=='2')
				{
					buff2[1]=2;
				}
				else if (w=='3')
				{
					buff2[1]=3;
				}
				else if (w=='4')
				{
					buff2[1]=4;
				}
				else if (w=='5')
				{
					buff2[1]=5;
				}
				else if (w=='6')
				{
					buff2[1]=6;
				}
				else if (w=='7')
				{
					buff2[1]=7;
				}
				else if (w=='8')
				{
					buff2[1]=8;
				}
				else if (w=='9')
				{
					buff2[1]=9;
				}
				else if (w=='0')
				{
					buff2[1]=0;
				}
			}
				if (e != '\0')
				{
					if (e=='1')
					{
						buff2[2]=1;
					}
					else if (e=='2')
					{
						buff2[2]=2;
					}
					else if (e=='3')
					{
						buff2[2]=3;
					}
					else if (e=='4')
					{
						buff2[2]=4;
					}
					else if (e=='5')
					{
						buff2[2]=5;
					}
					else if (e=='6')
					{
						buff2[2]=6;
					}
					else if (e=='7')
					{
						buff2[2]=7;
					}
					else if (e=='8')
					{
						buff2[2]=8;
					}
					else if (e=='9')
					{
						buff2[2]=9;
					}
					else if (e=='0')
					{
						buff2[2]=0;
					}
				}
					if (r != '\0')
					{
						if (r=='1')
						{
							buff2[3]=1;
						}
						else if (r=='2')
						{
							buff2[3]=2;
						}
						else if (r=='3')
						{
							buff2[3]=3;
						}
						else if (r=='4')
						{
							buff2[3]=4;
						}
						else if (r=='5')
						{
							buff2[3]=5;
						}
						else if (r=='6')
						{
							buff2[3]=6;
						}
						else if (r=='7')
						{
							buff2[3]=7;
						}
						else if (r=='8')
						{
							buff2[3]=8;
						}
						else if (r=='9')
						{
							buff2[3]=9;
						}
						else if (r=='0')
						{
							buff2[3]=0;
						}
					}

Also why are you doing this ASCII to BCD conversion (buff to buff2) anyway? atoi() reads ASCII. So the entire code can be:

int main(void)
{
    DDRB = 0xFF;
    DDRA = 0xFF;
    DDRC = 0xFF;
    TIMSK = (1 << TOIE2);
    T2_init();
    USART_Init(MYUBRR);

    stdin = &uart_str;
    sei();

    char buff[5];
    uint16_t dat;
    while (1)
    {

        getstring2(buff,5);
        dat = atoi(buff);

        ascii(dat);
    }
}

I suggest you do NOT try to do this on an AVR. It is far far easier to learn the basics of C on a PC. (which already has stdin+stdout attached to the "console" anyway).

 

(Oh and you don't need to invent getstring2() either - scanf() would do what you want or even the simpler gets() :  http://www.cplusplus.com/reference/cstdio/gets/ - the only issue with "gets()" is that you have no way to specify a length limit).

Last Edited: Mon. Jun 22, 2020 - 10:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

https://www.tutorialspoint.com/c...

 

atoi() (ascii to integer) expects a string of numbers. It returns the integer value.

 

eg int val = atoi("1234");

So why do you attempt to do the conversion yourself then feed it into atoi()? 

 

You need to stop guessing and spend some time reading.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while(1)
{
    gets(buff);
    dat=atoi(buff);
    ascii(dat);
}

I'm using this and it is working too.. But i have a question, i want to make temperature logger, i have 10 k-type sensor and i want to send the data over uart and receive.The function I'm using is correct for my project or not..can you please tell me. 

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rajat choudhary wrote:
i want to send the data over uart and receive
Is this being sent so a human can read it? If yes then you need something like:

uint16_t reading = logger_get_reading();
char buff[20];
itoa(reading, buff, 10);

UART_send_string(buff);

If that reads 12345 from the logger it will send "12345" to the UART. Another way to achieve the same is:

FILE mystdout=FDEV_SETUP_STREAM(USART_Transmit_IO,USART_Receive_IO,_FDEV_SETUP_WRITE);

...

  stdout = &mystdout;
  printf("%u", logger_get_reading());

but if the transmission is to be from one computer to another (preferably of the same type - ie with same endianness and so on) then you may not want to bother with the conversion to ASCII (which in the case of 12345 would convert 2 bytes into 5 bytes to be sent) then you might just do:

uint16_t reading = logger_get_reading();

UART_send_byte(reading >> 8);
UART_send_byte(reading & 0xFF);

which would simply split  the one 16 bit reading into two separated bytes to pass them over the wires.

 

So start by saying where it is you are sending the data and for what reason.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Given how long it's taken Rajat to get to this stage in his project; I think introducing the concept of binary COMMS may be a bit pre-mature. It does introduce a level of complexity in that you quickly get into the world of a "frame" of data and a protocol for checking & extracting the payload.

 

I don't think Rajat is ready for this yet.

 

Rajat choudhary wrote:
.The function I'm using is correct for my project or not..can you please tell me. 

Fom where we're standing the answer is yes, the function does what you told us it should.

 

You however are best placed to answer that because only you know the project requirements. Plus the fact that once you've written some code doesn't mean it cannot be changed should you find a reason to update it.