将下面2个程序分别烧录进2个单片机中.
单片机U1
//ICC-AVR application builder : 2013-6-14 10:17:58
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
#define LED1_ON PORTD|=(1<<PD4)
#define LED1_OFF PORTD&=~(1<<PD4)
#define LED2_ON PORTD|=(1<<PD5)
#define LED2_OFF PORTD&=~(1<<PD5)
//共阳极数码管
unsigned char table[]={0XC0,0XF9,0XA4,0xB0,0x99,0x92,0x82,0xF8,0X80,0X90,0X88,0X83,0XC6,0XA1,0X86,0X8e};
unsigned char count=0,count_h,count_l;
unsigned char a=0,cnt,cnt1;
unsigned char key,count_temp;
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0xFF; //m103 output only
DDRC = 0xFF;
PORTD = 0xFF;
DDRD = 0x32;
}
//TIMER0 initialize - prescale:256
// WGM: Normal
// desired value: 0.005Sec
// actual value: 0.005Sec (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x64; //set count
OCR0 = 0x9C; //set compare
TCCR0 = 0x04; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
a++;
TCNT0 = 0x64; //reload counter value
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x33; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char udr0;
udr0=UDR;
count_temp=udr0;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//发送数据uart0_TX
//函数功能:把要发送的数据data送到发送数据缓存器UDR中,然后发送出去
void uart0_TX(unsigned char data)
{
while(!(UCSRA&(1<<UDRE)));
UDR=data;
}
//8M晶振频率下的延时一毫秒
void delay(unsigned int k)
{
unsigned int m,n;
for(m=0;m<k;m++)
{
for(n=0;n<1140;n++)
{;}
}
}
//数码管显示函数
void display(void)
{
count_h=count/10;
count_l=count%10;
PORTC=table[count_h];
LED1_ON;
delay(1);//这个延时很重要,不加的话CUP运行过快将会导致LED数码管无法显示;
LED1_OFF;
PORTC=table[count_l];
LED2_ON;
delay(1);
LED2_OFF;
}
//主函数
void main(void)
{
init_devices();
while(1)
{
key=PINB;
uart0_TX(key);
display();
switch(count_temp)
{
case 1: TIMSK = 0x01;break;
case 2: TIMSK = 0x00;break;
case 3: count=0X00;TIMSK = 0x01;break;
}
if(a==150)
{
a=0;
if(count==59)
{ count=0; }
else { count++; }
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
单片机U2
//ICC-AVR application builder : 2013-6-14 10:31:18
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
unsigned char led_dis;
unsigned char count_temp,cnt1,cnt=1;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x02;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x33; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char udr0;
udr0=UDR;
led_dis=udr0;
}
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
//external interupt on INT0
cnt++;
if(cnt==1)
{
count_temp=1;
}
else if(cnt==2)
{
count_temp=2;
cnt=0;
}
}
#pragma interrupt_handler int1_isr:iv_INT1
void int1_isr(void)
{
//external interupt on INT1
count_temp=3;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
MCUCR = 0x0A;
GICR = 0xC0;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//发送数据函数
void USART_TX(unsigned char data)
{
while(!(UCSRA&(1<<UDRE)));
UDR=data;
}
//主函数
void main(void)
{
init_devices();
while(1)
{
cnt1=count_temp;
USART_TX(cnt1);
PORTB=led_dis;
}
}