Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How To Convert ascii to decimal To Send via USART For AT MEGA 32...

Status
Not open for further replies.

ismbn

Full Member level 3
Joined
Feb 11, 2012
Messages
160
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,308
Location
Mumbai. india
Activity points
2,444
Hi all...

I am Using AVR Studio 6 for compiling...
here is my code i want to count the pulses from the external source and transmit it though USART. I have connected that(external pulse) to the External interrupt & incrementing it when interrupt hits, Also able to transmit it though USART but the problem is when i am checking it on hyper terminal the ascii characters are coming... like this "☺☻♥♣♣♠•◘○◙♂...."

What to do if i want to send it in decimal values like if the ISR hits 10 time it should send value "10"
not " ◙ "....

Is it Possible to make it like this.....

my
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include "dbits/uart.h"
#include "dbits/uart.c"
#include <math.h>
#define F_CPU 16000000l
int make;
int a=0,b=0,i=0;
void getpar() 
{
	uart_init(16);
	sei();
	uart_putc('w');
	uart_flush ();
	while(uart_available()==0);
	
	make= uart_getc();

}
void ex_intr()
{
	MCUCR |= (1<<ISC10)|(1<<ISC11)|(1<<ISC00)|(1<<ISC01);
	GICR |= (1<<INT0);
	sei();
}

int main(void)
{
	DDRA= 0xff;
	ex_intr();
	getpar();
	
	uart_putc('s');
    while(1)
    {
	}
}

ISR(INT0_vect)
{
	a++;
	PORTA=i;
	uart_putc(a);
	i++;
}

}

Regards...
Ismail
 

a is an integer , you have to convert it to the ASCII format before sending it.
The ASCII representation for numbers is

Code:
DEC   HEX     ASCII

48 	0x30 	 0
49 	0x31 	 1
50 	0x32 	 2
51 	0x33 	 3
52 	0x34 	 4
53 	0x35 	 5
54 	0x36 	 6
55 	0x37 	 7
56 	0x38 	 8
57 	0x39 	 9

so a simple trick you can use is to add decimal 48 to an integer to convert it to its ASCII equivalent.
For example

integer 2, if you add 2+48=50 , 50 is the ASCII character '2'
integer 5, if you add 5+48=53 , 53 is the ASCII character '5'

This works fine for a single digit number, if you have more then you need to use modulo and division to extract each digit in order to convert it.
Check this https://www.edaboard.com/threads/240635/#post1041669

Alex
 
  • Like
Reactions: ismbn

    ismbn

    Points: 2
    Helpful Answer Positive Rating
What is the baudrate ?
Did you correctly set baud rate equally for both AVR and Hyperterminal ?
 

@DineshSL
Yes sir i have made the right baud rate both the side...

i will try the logic given by alex sir...

Ismail

- - - Updated - - -

i am getting response like this

Code:
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»
³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»
³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»
³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»
³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»
³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»
³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»
³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H
»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³H»³
 

i made
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <dbits/uart.h>
#include <dbits/uart.c>
#include <math.h>
#define F_CPU 16000000l
int data1,data2,final_data,make;
int a=0,b=0,i=0;
void getpar()
{
	uart_init(16);
	sei();
	uart_putc('w');
	uart_flush ();
	while(uart_available()==0);
	
	make= uart_getc();
	

}
void ex_intr_init()
{
	MCUCR |= (1<<ISC10)|(1<<ISC11)|(1<<ISC00)|(1<<ISC01);
	GICR |= (1<<INT0);
	sei();
}

int main(void)
{
	DDRA= 0xff;
	ex_intr_init();
	getpar();
	
	uart_putc('s');
    while(1)
    {
		switch (make)
		{
			case 'a':
			{
				uart_puts(" key a");   /// to print string
				data1=a;
				[B][COLOR="#FF0000"]a=48+a;[/COLOR][/B]
				uart_putc(a);            /// to send data via uart
				uart_flush();            //// to flush  usrat
				break;
				
			}
			
			case 'b':
			{
				uart_puts(" key b ");
				break;
			}
			default:
			{
				uart_puts("in default");
			}
		}
	}
}

ISR(INT0_vect)
{
	a++;
}

ISR(INT1_vect)
{
	b++;
}

ismail

- - - Updated - - -

this updated header file i got from internet which i have used in this program
this is really very helpful in communication via USART... i want sheer it with all, but i don't know how to do it, so all edaboard user can take benefit by this...

ismail
 

Attachments

  • uartlibrary.zip
    17.6 KB · Views: 46

The author of the library has them available https://jump.to/fleury

- - - Updated - - -

First or all the variable a is used in the interrupt and in the main function so it should be declared as a volatile variable.
Secondly do you need it to be an integer , why not an unsigned variable.
Third , do you need it to be 16bit variable, why not an unsigned char?

Don't change the value of a like a=a+48 , use that only to send a value (valid for one digit only)
Code:
uart_putc(a+48);
 

Hello Sir,

i made all the correction you said...
but i think (a+48) will work great for the value of which ascii is 0,1,2,3,4...,9. but after that, it will start its nature that start sending ascii...
Is it possible to send the no. pulses in decimal or hex like,If 382 Pulses has been counted so sending 382 or sending 17E in hex

Ismail
 

I did provide you a link is post #2 that shown how to extract the digits, did you read it?
 

Asking me this question means that you don't understand how the code works

I'm using two operators, the division (/) and modulo (%)
When you divide two integers then the result is also an integer and the decimal digits are truncated so if you divide 100/9=11.11 you get a result of 11.
When you divide 9/10=0.9 you get a result of 0 , when you divide 10/4=2.5 you get a result of 2

The modulo, when you use it you get the remainder of the division.
10%4=2 , because 10/4=2 times and the remainder is 2 ( 10-(2*4)=2)
20%3=2 , because 20/3=6 times and the remainder is 2 (20-(3*6)=2)

Now using all that suppose that we have a decimal number like 1234

1234/1000=1.234 which gets truncated to 1 and you get the first digit

1234%1000=234 and 234/100=2.34 which gets truncated to 2 so (1234%1000)/100=2 gives you the second digit

1234%100=34 and 34/10=3.4 which gets truncated to 3 so (1234%100)/10=3 gives you the third digit

1234%10=4 , this time you don't need to do a division so (1234%10)=4 gives you the fourth digit

Alex

Sir, are you telling about this?

I read it but don't know how to use it...
ismail
 

suppose that a equals 1234

a/1000=1.234 which gets truncated to 1 and you get the first digit

a%1000=234 and 234/100=2.34 which gets truncated to 2 so (1234%1000)/100=2 gives you the second digit

a%100=34 and 34/10=3.4 which gets truncated to 3 so (1234%100)/10=3 gives you the third digit

a%10=4 , this time you don't need to do a division so (1234%10)=4 gives you the fourth digit


Code:
char my_array[4];

my_array[0]=a/1000; // this is the first digit
my_array[1]=(a % 1000)/100; // this is the second digit
my_array[2]=(a % 100)/10; // this is the third digit
my_array[3]=(a % 10); // this is the fourth digit
 
  • Like
Reactions: ismbn

    ismbn

    Points: 2
    Helpful Answer Positive Rating
hello sir i have made the coding like this::
 

Attachments

  • uart ismail.txt
    781 bytes · Views: 65

@ismbn, Alexan_e has already provide a code, but still I post a link which also provide the same thing to you.
The concept is same but method is different, (Just to make you aware with this method)... better is to use code posted by alexan_e.
In this link you can also be aware about how to get ASCII values.

https://www.edaboard.com/blog/1784/
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top