找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 5276|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

avr單片機(jī)雙通信C程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1 發(fā)表于 2013-6-14 18:20 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
將下面2個(gè)程序分別燒錄進(jìn)2個(gè)單片機(jī)中.
單片機(jī)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)
//共陽(yáng)極數(shù)碼管
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
}
//發(fā)送數(shù)據(jù)uart0_TX
//函數(shù)功能:把要發(fā)送的數(shù)據(jù)data送到發(fā)送數(shù)據(jù)緩存器UDR中,然后發(fā)送出去
void uart0_TX(unsigned char data)
{
     while(!(UCSRA&(1<<UDRE)));
  UDR=data;
}
//8M晶振頻率下的延時(shí)一毫秒
void delay(unsigned int k)
{
    unsigned int m,n;
 for(m=0;m<k;m++)
 {
     for(n=0;n<1140;n++)
  {;}
 }
}
//數(shù)碼管顯示函數(shù)
void display(void)
{
    count_h=count/10;
 count_l=count%10;
 PORTC=table[count_h];
 LED1_ON;
 delay(1);//這個(gè)延時(shí)很重要,不加的話(huà)CUP運(yùn)行過(guò)快將會(huì)導(dǎo)致LED數(shù)碼管無(wú)法顯示;
 LED1_OFF;
 PORTC=table[count_l];
 LED2_ON;
 delay(1);
 LED2_OFF;
}
//主函數(shù)
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++; }
  }
 }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
單片機(jī)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
}
//發(fā)送數(shù)據(jù)函數(shù)
void USART_TX(unsigned char data)
{
    while(!(UCSRA&(1<<UDRE)));
 UDR=data;
}
//主函數(shù)
void main(void)
{
    init_devices();
 while(1)
 {
     cnt1=count_temp;
  USART_TX(cnt1);
  PORTB=led_dis;
 }
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:84472 發(fā)表于 2015-7-27 17:20 | 只看該作者
大神:這個(gè)方案能做嗎?
硬件:
二套AVR電路板,每套電路板有一個(gè)AVR單片機(jī),四位液晶顯示電路,一個(gè)蜂鳴報(bào)警電路,一個(gè)振動(dòng)電路,二個(gè)遙控接收放大電路,一個(gè)電源指示電路,一個(gè)狀態(tài)指示燈電路,一個(gè)晶振電路,一個(gè)高電平復(fù)位電路,
編程要求:
二個(gè)遙控器交替按下,按下時(shí)間在0.3左右(0.25到0.35之間)A遙控器按下0.3左右停頓0.055秒左右B遙控器按下0.3左右停頓0.055秒左右,也就是說(shuō)A遙控器不發(fā)送紅線(xiàn)信號(hào)到B遙控器發(fā)送紅外線(xiàn)信號(hào)的時(shí)間不能大于0.07秒(如0.071秒)蜂鳴器就報(bào)一下警,顯示器數(shù)字就加1。小于0.04秒振動(dòng)器振動(dòng)一下。
遙控器按下的時(shí)間0.3秒左右單片機(jī)不用管,單片機(jī)只控制A遙控器抬起到B遙控器按下的時(shí)間和B遙控器抬起到A遙控器按下的時(shí)間。
回復(fù)

使用道具 舉報(bào)

板凳
ID:84472 發(fā)表于 2015-7-27 17:24 | 只看該作者
476902250QQ.COM
回復(fù)

使用道具 舉報(bào)

地板
ID:84472 發(fā)表于 2015-8-11 20:51 | 只看該作者
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表