Hi Gilles,
You did it with nothing more than "make sure it is unsigned (uint8_t)"
I used a char as i was 110% sure that was equal to an unsigned 8 bit byte...
I have used char a billion times without problems.
But here it was what gave all the problems..
Thanks for all your help.
You're welcome, glad that I was able to help
Here's a suggestion for an alternate coordinate reading function:
uint32_t ReadCoordinate(void)
{
uint32_t receivedValue = 0L;
for(int i=0; i < 4; i++) {
receivedValue |= (long)GetSerialByte() << i*8;
}
return receivedValue;
}
You could use something like this to send received coordinates (in
readable hex) back to the PC (your VB program or e.g. Hyperterminal):
void EchoCoordinate(uint32_t coordinate)
{
SendSerialString("Received: 0x");
for(int bitStart = 28; bitStart >= 0; bitStart -= 4)
{
TransmitSerialByte("0123456789ABCDEF"[(coordinate >> bitStart) &
0x0F]);
}
SendSerialString("\r\n");
}
This is a test routine that uses the above functions:
void CoordinateTest(void)
{
uint32_t coordinate;
while(1) {
SendSerialString("Waiting for coordinate\r\n");
coordinate = ReadCoordinate();
EchoCoordinate(coordinate);
}
}
I quickly tested that in Hyperterminal - you can e.g. press Ctrl-A,
Ctrl-B, Ctrl-C, Ctrl-D, and should get back "Received: 0x04030201"
I'm appending excerpts from the routines used in the above - take
these with a grain of salt though:
- they may not be complete (pasted, not tested

- they are from an Atmega128 project of mine, so you'll have to adapt
the hardware specific stuff (e.g. register names) for your Atmega8
- I'm an AVR noob although I've been programming in C for some while

- your newsreader (or mine) may have messed up line wraps.
- this is using gcc in gnu99 mode which may not be compatible with
your makefile / remaining code.
Regards,
Gilles.
serialcomm.c:
------------------------------
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "serialcomm.h"
/// Size of ring buffer for reception (RINGBUFFER-1 bytes may be
received without being processed)
#define RINGBUFFER_SIZE 64
static volatile uint8_t ringBuffer[RINGBUFFER_SIZE];
static volatile uint8_t bufferHead; ///< incoming bytes will be stored
here
static volatile uint8_t bufferTail; ///< Retrieved bytes will be
delivered from here
/// Set up serial hardware
/// @param divisor divisor to set up baudrate - use BAUDDIVISOR macro
void InitializeSerialPort(uint16_t divisor)
{
cli(); // don't disturb while we're setting up
// Set baud rate
UBRR1H = (uint8_t)(divisor>>8);
UBRR1L = (uint8_t)divisor;
// Enable receiver, transmitter, and received character interrupt
UCSR1B = _BV(RXEN)|_BV(TXEN)|_BV(RXCIE);
// Set frame format: 8 data, 1 stop bit
UCSR1C = _BV(USBS)|(3<<UCSZ0);
// Initialize ring buffer
bufferHead = bufferTail = 0;
sei(); // allow interrupts
}
void TransmitSerialByte(uint8_t byte)
{
/* Wait for empty transmit buffer */
loop_until_bit_is_set(UCSR1A, UDRE);
/* Put byte into buffer, sends the byte */
UDR1 = byte;
}
uint8_t IsSerialByteAvailable(void)
{
return bufferTail != bufferHead;
}
uint8_t GetSerialByte(void)
{
while(!IsSerialByteAvailable())
{
// Wait
}
cli();
uint8_t byte = ringBuffer[bufferTail];
bufferTail = (bufferTail + 1) % RINGBUFFER_SIZE;
sei();
return byte;
}
void SendSerialString(char *s)
{
uint8_t c;
while((c = (uint8_t)(*s++)))
{
TransmitSerialByte(c);
}
}
SIGNAL(SIG_UART1_RECV)
{
if(UCSR1A & _BV(RXC))
{
uint8_t receivedChar = UDR1;
uint8_t tmpHead = (bufferHead + 1) % RINGBUFFER_SIZE;
if(tmpHead == bufferTail) {
return; // buffer full, ignore additional bytes
}
ringBuffer[bufferHead] = receivedChar;
bufferHead = tmpHead;
}
}
Serialcomm.h:
--------------------
#ifndef _SERIALCOMM_H
#define _SERIALCOMM_H
#define BAUDDIVISOR(baudrate) ((F_CPU/((baudrate)*16UL)) - 1)
void InitializeSerialPort(uint16_t divisor);
void TransmitSerialByte(uint8_t data);
uint8_t IsSerialByteAvailable(void);
uint8_t GetSerialByte(void);
void SendSerialString(char *s);
#endif