当前位置:首页 > 单片机 > 单片机
[导读]#include#include#defineRXB81#defineTXB80#defineUPE2#defineOVR3#defineFE4#defineUDRE5#defineRXC7#defineFRAMING_ERROR(1

#include

#include

#defineRXB81

#defineTXB80

#defineUPE2

#defineOVR3

#defineFE4

#defineUDRE5

#defineRXC7

#defineFRAMING_ERROR(1<

#definePARITY_ERROR(1<

#defineDATA_OVERRUN(1<

#defineDATA_REGISTER_EMPTY(1<

#defineRX_COMPLETE(1<

//USARTReceiverbuffer

#defineRX_BUFFER_SIZE8

charrx_buffer[RX_BUFFER_SIZE];

unsignedcharnCount=0;//定义串口接收计数变量

#ifRX_BUFFER_SIZE<256

unsignedcharrx_wr_index,rx_rd_index,rx_counter;

#else

unsignedintrx_wr_index,rx_rd_index,rx_counter;

#endif

//ThisflagissetonUSARTReceiverbufferoverflow

bitrx_buffer_overflow;

//USARTReceiverinterruptserviceroutine

interrupt[USART_RXC]voidusart_rx_isr(void)

{

charstatus,data;

status=UCSRA;

data=UDR;

if((status&(FRAMING_ERROR|PARITY_ERROR|DATA_OVERRUN))==0)

{

//printf("interruptiontriged!rn");//插桩测试,验证中断服务程序处于有效工作状态

nCount++;//接收到数据时,每接收一个,接收计数加1

rx_buffer[rx_wr_index]=data;

if(++rx_wr_index==RX_BUFFER_SIZE)rx_wr_index=0;

if(++rx_counter==RX_BUFFER_SIZE)

{

rx_counter=0;

rx_buffer_overflow=1;

};

};

}

#ifndef_DEBUG_TERMINAL_IO_

//GetacharacterfromtheUSARTReceiverbuffer

#define_ALTERNATE_GETCHAR_

#pragmaused+

chargetchar(void)

{

chardata;

while(rx_counter==0);

data=rx_buffer[rx_rd_index];

if(++rx_rd_index==RX_BUFFER_SIZE)rx_rd_index=0;

#asm("cli")

--rx_counter;

#asm("sei")

returndata;

}

#pragmaused-

#endif

//StandardInput/Outputfunctions

//Declareyourglobalvariableshere

voidmain(void)

{

//Declareyourlocalvariableshere

unsignedchartemp_data=0;

//Input/OutputPortsinitialization

//PortAinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTA=0x00;

DDRA=0x00;

//PortBinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTB=0x00;

DDRB=0x00;

//PortCinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTC=0x00;

DDRC=0x00;

//PortDinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTD=0x00;

DDRD=0x00;

//Timer/Counter0initialization

//Clocksource:SystemClock

//Clockvalue:Timer0Stopped

//Mode:Normaltop=FFh

//OC0output:Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

//Timer/Counter1initialization

//Clocksource:SystemClock

//Clockvalue:Timer1Stopped

//Mode:Normaltop=FFFFh

//OC1Aoutput:Discon.

//OC1Boutput:Discon.

//NoiseCanceler:Off

//InputCaptureonFallingEdge

//Timer1OverflowInterrupt:Off

//InputCaptureInterrupt:Off

//CompareAMatchInterrupt:Off

//CompareBMatchInterrupt:Off

TCCR1A=0x00;

TCCR1B=0x00;

TCNT1H=0x00;

TCNT1L=0x00;

ICR1H=0x00;

ICR1L=0x00;

OCR1AH=0x00;

OCR1AL=0x00;

OCR1BH=0x00;

OCR1BL=0x00;

//Timer/Counter2initialization

//Clocksource:SystemClock

//Clockvalue:Timer2Stopped

//Mode:Normaltop=FFh

//OC2output:Disconnected

ASSR=0x00;

TCCR2=0x00;

TCNT2=0x00;

OCR2=0x00;

//ExternalInterrupt(s)initialization

//INT0:Off

//INT1:Off

//INT2:Off

MCUCR=0x00;

MCUCSR=0x00;

//Timer(s)/Counter(s)Interrupt(s)initialization

TIMSK=0x00;

//USARTinitialization

//CommunicationParameters:8Data,1Stop,NoParity

//USARTReceiver:On

//USARTTransmitter:On

//USARTMode:Asynchronous

//USARTBaudRate:9600

UCSRA=0x00;

UCSRB=0x98;

UCSRC=0x86;

UBRRH=0x00;

UBRRL=0x33;

//AnalogComparatorinitialization

//AnalogComparator:Off

//AnalogComparatorInputCapturebyTimer/Counter1:Off

ACSR=0x80;

SFIOR=0x00;

//Globalenableinterrupts

#asm("sei")

SREG|=0x80;//开全局中断

while(1)

{

//Placeyourcodehere

if(nCount>0)//判断是否有接收数据

{

nCount--;//处理过的数据,减少数据计数

temp_data=getchar();//获取接收字符

if(temp_data<='9'&&temp_data>='0')

{

temp_data=temp_data-38;

printf("%d",temp_data);

}

elseif(temp_data<='Z'&&temp_data>='A')

{temp_data=temp_data+32;

printf("%c",temp_data);

}

elseif(temp_data<='z'&&temp_data>='a')

{temp_data=temp_data-32;

printf("%c",temp_data);}

else

{printf("errorn");}

}

};

}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭