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.