本帖最后由 ddllxxrr 于 2014-9-8 11:15 编辑
两个单片机通过串口进行通讯。第一个单片机把拨码开关的状态发到第二个,第二个负责显示到LED小灯上。本仿真的截图:
Atmel Studio6的发程序:
- /*
- * GccApplication15.c
- *
- * Created: 2014-9-8 10:50:02
- * Author: Administrator
- */
- #include <avr/io.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define RXC 7
- #define UDRE 5
- uchar key;
- void uart0_send(uchar i)
- {
- while(!(UCSRA&(1<<UDRE)));
- UDR=i;
- }
- int main(void)
- {
- DDRB=0x00;
- PORTB=0xFF;
- UCSRA=0x00;
- UBRRH=0;
- UBRRL=25; //系统时钟8MHz,波特率为9600bps
- UCSRB=0x18;
- UCSRC=0x86;
- while(1)
- {
- key=PINB;
- uart0_send(key);
- }
- }
Atmel Studio6的收程序:
- /*
- * GccApplication16.c
- *
- * Created: 2014-9-8 10:51:22
- * Author: Administrator
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define UDRE 5
- #define FE 4
- #define PE 2
- #define DOR 3
- uchar LEDdisp;
- //interrupt [USART_RXC] void usart_rx_isr(void)//USART串行接收中断
- ISR(USART_RXC_vect)
- {
- uchar status,data;
- status=UCSRA;
- data=UDR;
- if((status&((1<<FE)|(1<<PE)|(1<<DOR)))==0)
- {
- LEDdisp=data;
- }
- }
- int main(void)
- {
- DDRB=0xFF;
- PORTB=0xFF;
- UCSRA=0x00;
- UBRRH=0;
- UBRRL=25; //系统时钟8MHz,波特率为9600bps
- UCSRB=0x90;
- UCSRC=0x86;
- //#asm("sei")
- sei();
- while(1)
- {
- PORTB=LEDdisp;
- }
- }
|