Problems adding up of Ascii values

[attachment=0]SerialResultFrom Baby Orangutan328p.Hello everyone ,
I’m working on a project that communicate use serial and I got the code working in arduino IDE. But I’m having trouble transferring some lines of code from my code that written in Arduino 1.0.5 to Avr studio 6. So I use my baby orangutan 328 and programming it with pololu avr usb programmer.

if(Serial.available()>0){ value=Serial.read(); Serial.print(value); total=total+value; Serial.println(total); }
In Avr studio 6 I can only read out Ascii code in serial, but some reason i can’t get any addition to work in the program.
serial.transmit&receive

void USART_Transmit( unsigned int data ) { /* Wait for empty transmit buffer */ while ( !( UCSRnA & (1<<UDREn))) ) ; /* Copy 9th bit to TXB8 */ UCSRnB &= ~(1<<TXB8); if ( data & 0x0100 ) UCSRnB |= (1<<TXB8); /* Put data into buffer, sends the data */ UDRn = data; } unsigned int USART_Receive( void ) { unsigned char status, resh, resl; /* Wait for data to be received */ while ( !(UCSRnA & (1<<RXCn)) ) ; /* Get status and 9th bit, then data */ /* from buffer */ status = UCSRnA; resh = UCSRnB; resl = UDRn; /* If error, return -1 */ if ( status & (1<<FEn)|(1<<DORn)|(1<<UPEn) ) return -1; /* Filter the 9th bit, then return */ resh = (resh >> 1) & 0x01; return ((resh << 8) | resl); }

/*in Avr Studio*/ ....... value=UDR0; Total=Total+value; Serial328.Print(data,""); ..........
received data1 97 data2 97
Total’s output is 97.
can anyone help me fix this?
Total’s is suppose to be 194 not 97…
any help would be appreciated.

Hello.

It looks like you are using code written for the Arduino rather than the Orangutan. Could you try using the Orangutan libraries included in the Pololu AVR Development Bundle instead? Also, I cannot see where the transmit and receive code that you posted is being used; it is difficult for me to debug your code without seeing the entire program. Could you simplify your code to the most basic program that demonstrates the problem and post it?

-Taylor

Thank you for your quick response TaylorKaplan
here the code:

//class
#include <pololu/OrangutanTime/OrangutanTime.h>
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atoi */

#define RX_BUFFER_SIZE 128
#define BAUDRATE 9600
#define F_CPU 20000000UL
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
int SUM;

int LetterNumber=0;
unsigned int Sum=0;
unsigned char Data[100];
int beginning=0;
int ending=0;
char dataArray[RX_BUFFER_SIZE];

struct Buffer{
	unsigned char buffer[RX_BUFFER_SIZE];
	int head;
	int trail;	
	}Ring_buffer;
	Buffer rx_buffer = { { 0 }, 0, 0 };

class Serial_Atmega328p 
{
	
	public:
	Serial_Atmega328p(){
		
	}
	inline void store_char(unsigned char cc)
	{
		int ii = (beginning+ 1) % RX_BUFFER_SIZE;

		// if we should be storing the received character into the location
		// just before the tail (meaning that the head would advance to the
		// current location of the tail), we're about to overflow the buffer
		// and so we don't write the character or advance the head.
		if (ii != ending) {
			dataArray[beginning] = cc;
			beginning= ii;
		}
	}

	
	void USART_init(void){
		int BAUD_PRESCALLER= (((F_CPU / (BAUDRATE * 16UL))) - 1);
		UBRR0H = (unsigned char)(BAUD_PRESCALLER>>8);
		UBRR0L = (unsigned char)(BAUD_PRESCALLER);
		UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
		UCSR0C = (1<<USBS0)|(3<<UCSZ00);//3
	}
	/*\n value %d \n*/
	inline int Print( unsigned int data/*,char* Array*/,const char* word){
		
		
		// 	int count=30;
		
		/*char dataArray[100];*/
		/* Wait for empty transmit buffer */
		//while ( !( UCSR0A & (1<<UDRE0)));
		USART_putstring(dataArray);
		/* Copy 9th bit to TXB8 */
		UCSR0B &= ~(1<<TXB80);//i comment this out
		//USART_putstring(dataArray);
		if ( data & 0x0100 )
		UCSR0B |= (1<<TXB80);
		/* Put data into buffer, sends the data */
		int nn;
		nn=sprintf (dataArray,word, data);
		
		//printf ("[%s] is a string %d chars long\n",dataArray,n);
		
		UDR0 = data;
		
		
		delay_ms(500);
		
		
		
	}
	int available(void)
	{
		return (unsigned int)(RX_BUFFER_SIZE + beginning - ending) % RX_BUFFER_SIZE;
	}
// 	int read(void)//arduino
// 	{
// 		int VALUE_serial;
// 		// if the head isn't ahead of the tail, we don't have any characters
// 		if (Ring_buffer.head == Ring_buffer.trail) {
// 			return -1;
// 		} 
// 		else {
// 			unsigned char cc = Ring_buffer.buffer[Ring_buffer.trail];
// 			Ring_buffer.trail = (Ring_buffer.trail + 1) % RX_BUFFER_SIZE;
// 			return cc;
// 			
// 		}
// 		
// 		
// 	}
// 	
	int Read(void){
			if (beginning==ending)
			{
				return -1;
			}
			else{
				char cc=dataArray[ending];
				ending=(unsigned int)(ending+1) % RX_BUFFER_SIZE;
				return cc;				
			}
	}	
	

	int USART_send( unsigned char data){
		
		while(!(UCSR0A & (1<<UDRE0)));
		UDR0 = data;
		
	}


	void USART_putchar(char cc) {
		UDR0 = cc;
		while(UCSR0A, TXC0); /* Wait until transmission ready. */
	}
	char USART_putstring(char* StringPtr){

		while(*StringPtr != 0x00){
			//for(int count=0;count<1000;count++){
				USART_send(*StringPtr);
				
				StringPtr++;
				
				// 		if (*StringPtr==0x00)
				// 		{
					// 			return;
				// 		}
			// 	}
		}
		
	}//*/
	unsigned int USART_Receive(  )
	{
		
		unsigned char status, resh, resl;
		
		/* Wait for data to be received */
		
		while ( (UCSR0A & (1<<RXC0)));
		
		/* Get status and 9th bit, then data */
		/* from buffer */
		
		status = UCSR0A;
		resh = UCSR0B;
		resl = UDR0;
		
		/* If error, return -1 */
		if ( status & ((1<<FE0)|(1<<DOR0)|(1<<UPE0)) )
		return -1;
		/* Filter the 9th bit, then return */
		resh = (resh >> 1) & 0x01;
		return ((resh << 8) | resl);
		
		
	}
	

	
	};
#include <avr/io.h>
#include <inttypes.h>
#include <string.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <math.h>

#include <stdlib.h>
#include "Serial_Atmeaga328p2.h"
Serial_Atmega328p Serial328;
unsigned char read(void);

char Space[]={" "};
char String[]={"\nwould this work\n"};    
char Count[100];
char NUMCnt[100];
char TOTAL[100];
char VALUE[100];

unsigned char value;

int READEDVALUE;

size_t resultread;
volatile char Total=0;

volatile int Total2=0;
char Send_buffer[32];
int NUM =0;
void update(int& NumCount,unsigned int& Sum,unsigned int & value){
	Sum=Sum+value;
	NumCount++;
}
// ISR(USART_RX_vect)
// 
// {
// 	
// 	unsigned char cc = UDR0;
// 	
// 	Serial328.store_char(cc, &rx_buffer);
// }
struct Buffer;
int main(/*void*/){
	   Serial328.USART_init();  
	  //Call the USART initialization code

	//MICSerial::MICSerial(Data,UBRR0H,UBRR0L,UCSR0A,UCSR0B,UDR0,RXEN0,TXEN0,RXCIE0,UDRE0,U2X0);
	

	Serial328.store_char(ending);
	
	while(1){        //Infinite loop
		value=UDR0;
		while ( (UCSR0A & (1<<RXC0)));
		if(Serial328.available()>0)//with out this if(serial328.available()>0) i get -1 on the out put as   expected
			READEDVALUE=Serial328.Read();//new code
			
			
		
		Serial328.Print(READEDVALUE,"\nRead value: %d\n");
		Serial328.Print(value,"\nvalue: %d \n");					
		//
		  
		
		
		
		 
		reti();
		Serial328.USART_Receive();
		cli();
		
		return NUM;
		
	}

return 0;
}
unsigned char read(void){
 
 while(!(UCSR0A & (1<<RXC0)));
 return UDR0;
 
}

but still i can’t get the right reading from the Read().
type a String “how” and send it to the baby orangutan 328p. Then the Baby orangutan 328p translate to Ascii values. So I can then add the ascii values together to get a total value.

Hello, Hovic.

That program is unreasonably long and complicated for us to debug for you. Can you try writing a simple program (ideally only a few lines long) that just uses our OrangutanSerial library?

- Ben

Hello ben
convert the example Section 3.h
how can I get ascii value read out and get the total of the ascii values?
I didn’t get any value, but i got the letter that i sent to baby orangutan.

[code]#include <avr/io.h>
#include <pololu/OrangutanSerial/OrangutanSerial.h>
#include <pololu/orangutan.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned char readSerial;

char receive_buffer[50];

unsigned char receive_buffer_position = 0;

char send_buffer[50];
int beginning;

void wait_for_sending_to_finish()
{
while(!serial_send_buffer_empty());
}

// process_received_byte: Responds to a byte that has been received on
// PD0/RXD. If you are writing your own serial program, you can
// replace all the code in this function with your own custom behaviors.
void process_received_byte(char byte)
{
switch(byte)
{

		default:

// wait_for_sending_to_finish();
send_buffer[0] = byte ^ 0x20;
serial_send(send_buffer,2);
break;
}
}

void check_for_new_bytes_received()
{
while(serial_get_received_bytes() != receive_buffer_position)
{
// Process the new byte that has just been received.
process_received_byte(receive_buffer[receive_buffer_position]);

	if (receive_buffer_position == sizeof(receive_buffer)-1)
	{
		receive_buffer_position = 0;
	}
	else
	{
		receive_buffer_position++;
		
		
	}
}

}

int main()
{
// Set the baud rate to 9600 bits per second. Each byte takes ten bit
// times, so you can get at most 960 bytes per second at this speed.
serial_set_baud_rate(9600);

// Start receiving bytes in the ring buffer.
serial_receive_ring(receive_buffer, sizeof(receive_buffer));

while(1)
{
	// Deal with any new bytes received.
	while ( !(UCSR0A & (1<<RXC0)));
	check_for_new_bytes_received();
	readSerial=serial_get_received_bytes();
	readSerial=*send_buffer;
	// If the user presses the middle button, send "Hi there!"
	// and wait until the user releases the button.
	
		wait_for_sending_to_finish();
		memcpy(send_buffer, PSTR("Hi there!\r\n"), 11);
		serial_send(send_buffer, 11);
		
		delay_ms(1005);
		// Wait for the user to release the button.  While the processor is
		// waiting, the OrangutanSerial library will take care of receiving
		// bytes using the serial reception interrupt.  But if enough bytes
		// arrive during this period to fill up the receive_buffer, then the
		// older bytes will be lost and we won't know exactly how many bytes
		// have been received.
		
	
}

}[/code]

Can you very specifically say what you are trying to accomplish? (Pretend you want me to do this project for you and tell me everything I would need to know to do so.)

- Ben

I would like copy a library from arduino’s hardware serial.CPP and it’s called HardWareSerial:read(void);. Then make hardwareserial:read() compatible for baby orangutan 328p.
It read data out of received_buffer translate it into ASCII (int)values.
But the problem I don’t exactly know how the HardWareSerial:read(void); works.

int value;
int total;
   Void   Setup(){
    Serial.bring(9600);
}
Void loop(){ 
   value= Serial.read();
   total=total+ value;
  Serial.print(value);

Send “how” received ascii int values print out total value= 334
Make the code above work with the orangutan using the orangutan library.

I haven’t tested the following code, but maybe you can give it a shot and let me know how it works?

#include <pololu/orangutan.h>
#include <stdio.h>

char send_buffer[10];
int size;
char received_byte;
unsigned int sum;


int main()
{
   // Set the baud rate to 9600 bits per second.  Each byte takes ten bit
   // times, so you can get at most 960 bytes per second at this speed.
   serial_set_baud_rate(9600);
   
   while(1)
   {
      // loop until we receive a single byte
      while(serial_receive_blocking(&received_byte, 1, 1000));
	  
      sum += received_byte;  // add byte to running total
	  
      size = sprintf(send_buffer, "%d", sum);  // represent sum as an ASCII string (e.g. 97 -> "97")
      if (size > 0)
         serial_send_blocking(send_buffer, (unsigned char)size);  // transmit ASCII representation of sum
   }
}

- Ben

Success! Thank you Ben and to all answer me.

I’m glad to hear it worked. Why do you need a program to do this? Did I just do your homework assignment for you?

- Ben

Nope. This is for a robot that I’m building for my self.
just wanted to transfer Decipher.cpp and Decipher.h to AVR studios.
Decipher.cpp (11.8 KB)
Decipher.h (1.19 KB)

problem 2
This photo shows the response I want from the Baby Orangutan 328. I’m also using the arduino Serial to read the outputs from the Baby Orangutan 328 to the screen .
can i use serial_receive_ring() to achieve this or no?


It is possible for serial_receive_ring() to be part of your solution.

- Ben