#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/wdt.h>
#define uint8 unsigned char ;
#define uint32 unsigned int ;
//mega8
#define DDR_SPI DDRB
#define PORT_SPI PORTB
#define SS PB2 //ss input
#define MOSI PB3 //mosi input
#define MISO PB4 //miso output
#define SCK PB5 //sck input
#define RX_ON PORTB|= (1<<PB0);
#define TX_ON PORTB&=~(1<<PB0);
//led
volatile unsigned char LED_S = 0;
#define LED_CLR PORTC|= (1<<PC0);
#define LED_SET PORTC&=~(1<<PC0);
volatile unsigned char Timer_Count=0;
volatile unsigned char Second_Count=0;
volatile unsigned char send=0;
volatile unsigned char i,bit;
unsigned char buf[20];
void LED_T(void)
{
if((LED_S%2)==0)
{LED_SET;}
else LED_CLR;
LED_S++;
}
void init_Timer0(void)
{
TCNT0=0x4a;
TCCR0=_BV(CS02)|_BV(CS00); //ck/1024
TIMSK=_BV(TOIE0); //enable Timer0 interrupt
}
ISR (SIG_OVERFLOW0) //20ms
{
TCNT0=0x4a;
Timer_Count++;
if(Timer_Count>=50)
{
Timer_Count=0;
Second_Count++;
send=1;TX_ON;
LED_SET;
}
}
ISR(SIG_INTERRUPT0)
{
if(buf[i]&_BV(bit))
PORTB|= _BV(MISO);
else PORTB&=~_BV(MISO);
if(bit==0)
{
i++;
bit=7;
if(i==18)
{
RX_ON;send=0;i=0;LED_CLR;
}
}
else bit--;
}
int main()
{
//init port
DDRB &=~_BV(PB1); //PB1 input
PORTB|=_BV(PB1);
DDRB|=_BV(PB0); //PB0 output
DDRC|=_BV(PC0); //PC0 output
PORTC|=_BV(PC0);
LED_SET;
_delay_ms(25);
LED_CLR;
while(!(PINB & _BV(PB1)) ); //if SET=0,wait
DDRB &=(~_BV(MOSI)); // input
PORTB|= _BV(MOSI);
DDRB|=_BV(MISO); //output
init_Timer0();
MCUCR |= 0x02;
GICR |= 0x40;
sei();
for(i=0;i<16;i++) buf[i]=0xaa;
buf[16]=0x33;buf[17]=0x33;
i=0;bit=7;
while (1)
{
;
}
return 0;
}