找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4601|回复: 3
收起左侧

avr单片机双通信C程序

[复制链接]
ID:1 发表于 2013-6-14 18:20 | 显示全部楼层 |阅读模式
将下面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;
 }
}
回复

使用道具 举报

ID:84472 发表于 2015-7-27 17:20 | 显示全部楼层
大神:这个方案能做吗?
硬件:
二套AVR电路板,每套电路板有一个AVR单片机,四位液晶显示电路,一个蜂鸣报警电路,一个振动电路,二个遥控接收放大电路,一个电源指示电路,一个状态指示灯电路,一个晶振电路,一个高电平复位电路,
编程要求:
二个遥控器交替按下,按下时间在0.3左右(0.25到0.35之间)A遥控器按下0.3左右停顿0.055秒左右B遥控器按下0.3左右停顿0.055秒左右,也就是说A遥控器不发送红线信号到B遥控器发送红外线信号的时间不能大于0.07秒(如0.071秒)蜂鸣器就报一下警,显示器数字就加1。小于0.04秒振动器振动一下。
遥控器按下的时间0.3秒左右单片机不用管,单片机只控制A遥控器抬起到B遥控器按下的时间和B遥控器抬起到A遥控器按下的时间。
回复

使用道具 举报

ID:84472 发表于 2015-7-27 17:24 | 显示全部楼层
476902250QQ.COM
回复

使用道具 举报

ID:84472 发表于 2015-8-11 20:51 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表