*****************************************************
Header Datei
*****************************************************
#ifndef AT90CAN128_CAN_SENDEN_H_
#define AT90CAN128_CAN_SENDEN_H_

#define F_CPU 16000000UL

#define set_bit(byte,bit) (byte)|=(1<<bit)
#define	clear_bit(byte,bit) (byte)&=~(1<<bit)


struct MOb
{
	unsigned long ID;
	unsigned char data [18];
};

void CAN_Init (void);
void CAN_Transmit (struct MOb msg);
void delay(void);


void USART0_Init(unsigned int);
void USART0_Transmit( unsigned char);
unsigned char USART0_Receive(void);

#endif 

********************************************************
can.c
********************************************************
#include <avr/io.h>
#include <avr/interrupt.h>
#include "AT90CAN128_CAN_Senden.h"


/* CAN Initialisierung */
void CAN_Init(void)
{
	unsigned char i,j;
	CANGCON |= (1 << SWRES);	// software Reset 
	
	// reset all mailboxes (MOb)
	for (i=0; i<15; i++)
	{
		CANPAGE  = ( i << 4 );	// select the MOb Nr.
		CANCDMOB = 0;			// reset the configuration of MObs
		CANSTMOB = 0;			// reset the MOb Status Register
		
		CANIDT4 = 0;			// reset the Identifier Tag Register
		CANIDT3 = 0;
		CANIDT2 = 0;
		CANIDT1 = 0;
		CANIDM4 = 0;			// reset the Identifier Mask Register
		CANIDM3 = 0;
		CANIDM2 = 0;
		CANIDM1 = 0;
		
		for (j=0; j<8; j++)	// clear the message
		CANMSG = 0;
	}
		
		// set up the baudrate at 250 kb/s with F_CPU 16M Hz (Formel P 241) 
		CANBT1 = 0x0E;			// Clock of CAN 0,5 us
		CANBT2 = 0x04;			// Propagation Time 2 us
		CANBT3 = 0x13;			// Tphs2 = 1 us, Tphs1 = 1,5 us, 3 sample points
		
		CANGCON = (1 << ENASTB);	// ENA/STB activ, start CAN	
		
		CANPAGE = (0<<4);
		CANSTMOB = 0;
		CANCDMOB = (1<<CONMOB1) | (1<<IDE);
		
	
		// clear CAN interrupt register
		CANGIE = (1<<ENIT) | (1<<ENRX);;			// global interrupts enable
		CANIE2 = (1<<0);				// MOb interrupt disable
		CANIE1 = 0;
		CANSIT2 = 0;			// no interrupt by MOb
		CANSIT1 = 0;

		while (! (CANGSTA & (1<<ENFG)) );	// wait until module ready
	
}

/* CAN transmission via MOb 1 */
void CAN_Transmit(struct MOb msg)
{
	unsigned int i;
	CANPAGE = (1 << 4);			// enable MOb 1, autom increment of the index
	CANCDMOB = (1 << IDE)|(8 << DLC0);	// CAN standard 2.0B, 8 bit code 
	
	// write 29 bits  ID Nr.
	msg.ID <<= 3;
	CANIDT4 = (unsigned char) (msg.ID & 0xF8);;  // get bits (2^0 ~~ 2^7)
	CANIDT3 = (unsigned char) (msg.ID >> 8);;	// get bits (2^8 ~~ 2^15) 
	CANIDT2 = (unsigned char) (msg.ID >> 16);	// get bits (2^16 ~~ 2^23)
	CANIDT1 = (unsigned char) (msg.ID >> 24);	// get bits (2^24 ~~ 2^31)
	
	for (i=0; i<8; i++)
		CANMSG = msg.data[i];			// put data into mailbox
	
	CANCDMOB |= (1 << CONMOB0);	// enable transmission
	
	while ( !(CANSTMOB & (1<<TXOK)));	// wait until transmission complete
	
	CANSTMOB &= ~(1<<TXOK);	// reset TXOK
}

void delay(void)
{
	unsigned int i,j;
	for (i=0; i<2000; i++)
		for(j=0; j<2000; j++)
			;
}

*******************************************************************
usart0.c
*******************************************************************
#include <avr/io.h>
#include "AT90CAN128_CAN_Senden.h"

#define F_CPU 16000000UL


/* USART Initialisierung */
void USART0_Init(unsigned int tt)
{
	unsigned int baud;
	
	set_bit(UCSR0A,U2X0); // asyn. mode
	baud=F_CPU/8/tt-1;
	UBRR0H=(unsigned char) (baud>>8);
	UBRR0L=(unsigned char) baud; // set baud rate register
	UCSR0B=(1<<RXEN0)|(1<<TXEN0); // enable receive and transmitter   
	UCSR0C=(0<<UMSEL0)|(0<<UPM0)|(1<<USBS0)|(3<<UCSZ0);	// 8 bit data, 1 stop bit, no parity mode, asyn. 
}

/* USART TX */
void USART0_Transmit( unsigned char data )	
{
	while ( !( UCSR0A & (1<<UDRE0)) ) // wait for the empty of the data register 
		;
	UDR0 = data; // put the date into t(0<<UMSEL0)|(0<<UPM0)|he data register
}

/* USART RX */
unsigned char USART0_Receive(void)
{
	while ( !(UCSR0A & (1<<RXC0)) ) // wait for data to be received 
		;
	return UDR0;  // get data from buffer
}

***********************************************************************
main.c
***********************************************************************
#include <avr/io.h>
#include <avr/interrupt.h>
#include "AT90CAN128_CAN_Senden.h"

#define USART_BAUD 9600

unsigned char temp[144],msgbuffer[8],sub_temp[18];


struct MOb msg;

int main ()
{	
	unsigned int index=0, i,j;
	USART0_Init(USART_BAUD);
	CAN_Init();

	set_bit(PORTE,PE4);
	set_bit(DDRE,PE4);
	temp[144]=0;
	sub_temp[18]=0;
	
	while (1)
	{
		temp[144] = USART0_Receive(); // RS232 receive
		
		for(j=0;j<18;j++)                // 18 Sub_temp, jedesub_temp hat 8 bits   
		{
			for(i=0; i<8; i++)
			{
				sub_temp[j]=temp[j*8+i];   	// sub_temp[1] entspricht 0. bis 7. bit im 144 bits
			}					// sub_temp[2] entspricht 8. bis 15. bit im 144 bits
		
		
			msgbuffer[index] = sub_temp[j];	// put the data  into CAN message buffer
			index++;					
		
			if (index == 8)
			{	
				clear_bit(PORTE,PE4);
				
				for(i=0; i<8; i++)		// get the data 
				{
					msg.ID = 0x10;
					msg.data[i]= msgbuffer[i];
				}
				CAN_Transmit(msg);		// transmit the message	
			}
		}
	}		
	return 0;
}




