#include <iom8v.h>
#include <macros.h>
#include "Io_define.h"
//#define fosc 8000000
//#define baud 9600
unsigned char RX_data[4]={0};
unsigned char RX_counter=0;
void UART_init (void);
void putchar (unsigned char c);
void puts (unsigned char *s);
void UART_rx (void);
void delay_nms (unsigned int n);
void putchar(unsigned char c) //字符输出函数
{
while (!(UCSRA&(1 << UDRE))); //判断上次发送有没有完成
UDR = c;
}
#pragma interrupt_handler UART_rx: iv_USART_RX
void UART_rx(void)
{
//static unsigned char RX_counter;
/*
if (RX_counter >= 4)
{
RX_counter = 0;
if ((!(RX_data[0] == 's'))||(!(RX_data[3] == 'p')))
{
RX_data[0]=0;
RX_data[1]=0;
RX_data[2]=0;
RX_data[3]=0;
}
}
*/
RX_data[RX_counter] = UDR;
//putchar(RX_data[RX_counter]);
//putchar(RX_counter+0x30);
//RX_counter++;
if (RX_data[RX_counter]=='s')
{
RX_data[0]=RX_data[RX_counter];
RX_counter=0;
}
RX_counter++;
if (!(RX_data[3] == 'p'))
{
RX_data[0]=0;
RX_data[1]=0;
RX_data[2]=0;
RX_data[3]=0;
}
}
void puts(unsigned char *s)
{
while (*s)
{
putchar(*s);
s++;
}
}
void UART_init(void)
{
UCSRB = BIT(RXCIE)| BIT(RXEN) |BIT(TXEN); //允许串口发送和接收,并响应接收完成中断
UBRR = 51;
//UBRRL = (fosc/16/(baud+1))%256;
//UBRRH = (fosc/16/(baud+1))/256;
UCSRC = BIT(URSEL)|BIT(UCSZ1)|BIT(UCSZ0); //8位数据+1位stop位
}
void main(void)
{
DDRB = 0B11111111;
PORTB = 0B00000000;
DDRC = 0B11111111;
PORTB = 0B00000000;
DDRD = 0B11111111;
PORTB = 0B00000000;
UART_init();
SEI();
while (1)
{
if (RX_counter==3)
{
RX_counter=0;
if ((RX_data[0] == 's')&&(RX_data[3] == 'p'))
{
//if ((RX_data[0] == 's')&&(RX_data[1] == 'u')
// &&(RX_data[2]=='0')&&(RX_data[3] == 'p'))
if ((RX_data[1] == 'u') &&(RX_data[2]=='0'))
{
puts("upready");
asm("jmp 0xe00");
}
else
//if ((RX_data[0] == 's')&&(RX_data[3] == 'p')&&(RX_data[1]=='0'))
if (RX_data[1]=='0')
{
if (RX_data[2]=='0')
{
//PORTB ^= 0X01;
key0 ^= key0_open;
puts("00ok");
}
else if (RX_data[2]=='1')
{
//PORTB ^= 0X01;
key1 ^= key1_open;
puts("01ok");
}
/*
switch (RX_data[2])
{
case '0': {
//PORTB ^= 0X01;
key0 ^= key0_open;
puts("00ok");
} break;
case '1': {
key1 ^= key1_open;
//PORTB ^= 0X02;
puts("01ok");
} break;
case 50: {
key2 ^= key2_open;
//PORTB ^= 0X04;
puts("02ok");
} break;
case 51: {
key3 ^= key2_open;
//PORTB ^= 0x07;
puts("03ok");
} break;
case 52: {
key4 ^= key4_open;
puts("04ok");
} break;
case 53: {
key5 ^= key5_open;
puts("05ok");
} break;
case 54: {
key6 ^= key6_open;
//PORTB ^= 0X01;
puts("06ok");
} break;
case 55: {
key7 ^= key7_open;
//PORTB ^= 0X02;
puts("07ok");
} break;
case 56: {
key8 ^= key8_open;
//PORTB ^= 0X04;
puts("08ok");
} break;
case 57: {
key9 ^= key9_open;
//PORTB ^= 0x07;
puts("09ok");
} break;
case 58: {
key10 ^= key10_open;
puts("0:ok");
} break;
case 59: {
key11 ^= key11_open;
puts("0;ok");
} break;
case 60: {
key12 ^= key12_open;
//PORTB ^= 0X01;
puts("0<ok");
} break;
case 61: {
key13 ^= key13_open;
//PORTB ^= 0X02;
puts("0=ok");
} break;
case 62: {
key14 ^= key14_open;
//PORTB ^= 0X04;
puts("0>ok");
} break;
case 63: {
key15 ^= key15_open;
//PORTB ^= 0x07;
puts("0?ok");
} break;
case 64: {
key16 ^= key16_open;
puts("0@ok");
} break;
case 65: {
key17 ^= key17_open;
puts("0Aok");
} break;
case 66: {
key18 ^= key18_open;
//PORTB ^= 0X01;
puts("0Bok");
} break;
case 67: {
key19 ^= key19_open;
//PORTB ^= 0X02;
puts("0Cok");
} break;
case 68: {
PORTB ^= 0B11111111;
PORTC ^= 0B00111111;
PORTD ^= 0B11111100;
//PORTB ^= 0X04;
puts("0Dok");
} break;
case 69: {
PORTB ^= 0x07;
puts("0Eok");
} break;
}*/
RX_data[0]=0;
RX_data[1]=0;
RX_data[2]=0;
RX_data[3]=0;
}
}
}