qin552011373 发表于 2013-3-13 21:14
你的什么样的程序?贴出来看看 #include <avr/io.h>
#define F_CPU 7372800UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <util/crc16.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
typedef unsigned char u8;
typedef unsigned int u32;
#define BAUDRATE 9600UL
#define POLY 0X1021
/*****************************************/
#define RX_BUF_SIZE 6 // receive data size
#define TX_BUF_SIZE 5 // send buf
#define START 0X83 //"S"
#define END 0X69 //"E"
#define W_IO 0X87 //"W"
#define R_IO 0X82 //"R"
#define RESET 0X00 //"reset"
#define FAIL 0X70 //"F"
#define E_CMD 0X67 //"C"
#define PA 0X41 //A
#define PB 0X42 //B
#define PC 0X43 //C
#define PD 0x44 //D
/**********************************************/
#define SET(X,Y) (X)|=(1<<(Y))
#define CLI(X,Y) (X)&=~(1<<(Y))
#define enable_interrupt() sei()
#define disable_interrupt() cli()
#define delay_us(US) _delay_us(US)//delay time can't exceed 768us
#define delay_ms(MS) _delay_ms(MS)//max delay time 262ms
#define delay_cycle() asm("nop")
static void sendbyte( u8 dat);
static void send( u8 *ptr,u8 len);
static void reset_mcu(void);
static void set_port(u8 port,u8 bit,u8 value);
static void read_port(u8 port,u8 bit);
/*mcu receive data from PC buffer*/
static volatile u8 rx_buf[RX_BUF_SIZE];
static volatile u8 rx_flag=0;
static volatile u8 rx_num=0;
/*mcu transmit data to PC buffer*/
static volatile u8 tx_buf[TX_BUF_SIZE];
static volatile u8 tx_w_index=0;
static volatile u8 tx_r_index=0;
static volatile u8 tx_num=0;
static volatile u8 dir_value=0xff;
static volatile u8 port_value=0x00;
const static volatile u8 error_p[]={FAIL,FAIL,FAIL};
const static volatile u8 error_c[]={E_CMD,E_CMD,E_CMD};
const static volatile u8 reset[]={RESET,RESET,RESET};
void usart_init(void)
{
UCSRB=0X00;
UCSRA&=~(1<<U2X);// no double baud rate
UCSRB&=~(1<<UCSZ2);// 0 1 1 8 bit data
UCSRC|=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
UCSRC&=~(1<<UMSEL);// asynchronous mode
UCSRC&=~(1<<USBS);// 1 stop bit
UCSRC&=~(1<<UPM1);//no parity check
UCSRC&=~(1<<UPM0);
UCSRC&=~(1<<UCPOL);//no clock polarity
UBRRL=(u8)((F_CPU/16/BAUDRATE)-1);
UBRRH=(u8)(((F_CPU/16/BAUDRATE)-1)>>8); //set baud rate
UCSRB|=(1<<TXCIE)|(1<<RXCIE)|(1<<TXEN)|(1<<RXEN);
DDRD|=(1<<DDD1);//TXD
DDRD&=~(1<<DDD0);//RXD
}
static void reset_mcu(void)
{
WDTCR|=1<<WDTOE|1<<WDE;// disable watchdog;
WDTCR&=~(1<<WDE);
WDTCR=0X40;//17.1ms RESET
}
SIGNAL(USART_TXC_vect)
{
// disable_interrupt();
if(tx_num>0)
{
--tx_num;
UDR=tx_buf[tx_r_index];
++tx_r_index;
if(tx_r_index==TX_BUF_SIZE)
tx_r_index=0;
}
// enable_interrupt();
}
static void sendbyte( u8 dat)
{
while(tx_num==TX_BUF_SIZE);//
disable_interrupt();
if(tx_num!=0||!(UCSRA&(1<<UDRE)))//buffer is not empty!
{
tx_buf[tx_w_index]=dat;
++tx_w_index;
if(tx_w_index==TX_BUF_SIZE)
tx_w_index=0;
++tx_num;
}
else
{
UDR=dat;
// while(!(UCSRA&(1<<TXC)));//
// UCSRA|=(1<<TXC);
}
enable_interrupt();
}
static void send( u8 *ptr,u8 len)
{
u8 i=0;
for(i=0;i<len;i++)
{
sendbyte(*(ptr+i));
}
}
SIGNAL(USART_RXC_vect)
{
u8 dat=0,status=0;
// disable_interrupt();//////
status=UCSRA;
dat=UDR;
if(!rx_flag)
{
if(!(status&(1<<FE|1<<DOR|1<<PE)))
{
rx_buf[rx_num]=dat;
++rx_num;
if(rx_num==RX_BUF_SIZE)
{
rx_num=0;
rx_flag=1;
}
}
}
// enable_interrupt();////
}
void set_port(u8 port,u8 bit,u8 value)
{
switch(port)
{
case PA:
if(bit<8)
{
DDRA|=(1<<bit);
if(value==0x01)
PORTA|=(1<<bit);
else if(value==0x00)
PORTA&=~(1<<bit);
delay_ms(1);
dir_value=DDRA;
port_value=PORTA;
}
break;
case PB:
if(bit<8)
{
DDRB|=(1<<bit);
if(value==0x01)
PORTB|=(1<<bit);
else if(value==0x00)
PORTB&=~(1<<bit);
delay_ms(1);
dir_value=DDRB;
port_value=PORTB;
}
break;
case PC:
if(bit<8)
{
DDRC|=(1<<bit);
if(value==0x01)
PORTC|=(1<<bit);
else if(value==0x00)
PORTC&=~(1<<bit);
delay_ms(1);
dir_value=DDRC;
port_value=PORTC;
}
break;
case PD:
if(bit>1&&bit<8)
{
DDRD|=(1<<bit);
if(value==0x01)
PORTD|=(1<<bit);
else if(value==0x00)
PORTD&=~(1<<bit);
delay_ms(1);
dir_value=DDRD;
port_value=PORTD;
}
break;
default:
break;
}
}
void read_port(u8 port,u8 bit)
{
switch(port)
{
case PA:
if(bit<8)
{
DDRA&=~(1<<bit);
PORTA|=(1<<bit);
delay_ms(1);//output change into input delay
dir_value=DDRA;
port_value=PINA;
}
break;
case PB:
if(bit<8)
{
DDRB&=~(1<<bit);
PORTB|=(1<<bit);
delay_ms(1);//output change into input delay
dir_value=DDRB;
port_value=PINB;
}
break;
case PC:
if(bit<8)
{
DDRC&=~(1<<bit);
PORTC|=(1<<bit);
delay_ms(1);//output change into input delay
dir_value=DDRC;
port_value=PINC;
}
break;
case PD:
if(bit>1&&bit<8)
{
DDRD&=~(1<<bit);
PORTD|=(1<<bit);
delay_ms(1);//output change into input delay
dir_value=DDRD;
port_value=PIND;
}
break;
default:
break;
}
}
void handle_cmd()
{
if(rx_flag)
{
if((rx_buf[0]==START)&&(rx_buf[RX_BUF_SIZE-1]==END))
{
switch(rx_buf[1])
{
case R_IO:
read_port(rx_buf[2],rx_buf[3]);
if((rx_buf[2]==PA||rx_buf[2]==PB||
rx_buf[2]==PC||rx_buf[2]==PD)&&rx_buf[3]<8)
{
sendbyte(START);
sendbyte(R_IO);
sendbyte(dir_value);
sendbyte(port_value);
sendbyte(END);
}
break;
case W_IO:
set_port(rx_buf[2],rx_buf[3],rx_buf[4]);
if((rx_buf[2]==PA||rx_buf[2]==PB||
rx_buf[2]==PC||rx_buf[2]==PD)&&rx_buf[3]<8)
{
sendbyte(START);
sendbyte(W_IO);
sendbyte(dir_value);
sendbyte(port_value);
sendbyte(END);
}
break;
case RESET:
sendbyte(START);
send((u8*)reset,3);
sendbyte(END);
reset_mcu();
delay_ms(50);
break;
default:
sendbyte(START);
send((u8 *)error_c,3);
sendbyte(END);
break;
}
}
else
{
sendbyte(START);
send((u8 *)error_p,3);
sendbyte(END);
}
rx_flag=0;
}
}
void disable_jtag(void)
{
MCUCSR|=(1<<JTD);
MCUCSR|=(1<<JTD);
}
void init_wdt(void)
{
WDTCR|=1<<WDTOE|1<<WDE;// disable watchdog;
WDTCR&=~(1<<WDE);
WDTCR|=1<<WDE|1<<WDP2|1<<WDP1|1<<WDP0;//enable watchdog;2.1S
}
void init_io(void)
{
DDRA=0XFF;
DDRB=0XFF;
DDRC=0XFF;
DDRD=0XFF;
DDRD|=(1<<DDD1);//TXD
DDRD&=~(1<<DDD0);//RXD
PORTA=0X00;
PORTB=0X00;
PORTC=0X00;
PORTD=0X03;
}
int main(void)
{
disable_interrupt();
disable_jtag();
init_io();
usart_init();
init_wdt();
enable_interrupt();
while(1)
{
wdt_reset();
handle_cmd();
}
return 0;
}
这个是代码,设置的时候选的是不优化! |