avr单片机双通信C程序

来源:本站
导读:目前正在解读《avr单片机双通信C程序》的相关信息,《avr单片机双通信C程序》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《avr单片机双通信C程序》的详细说明。
简介:本文介绍一个关于avr单片机双通信的C程序,感兴趣的朋友可以看看。

将下面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_OVFvoid 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_RXCvoid 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 peripheralsvoid 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_RXCvoid 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_INT0void 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_INT1void int1_isr(void){ //external interupt on INT1     count_temp=3;}//call this routine to initialize all peripheralsvoid 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; }}

提醒:《avr单片机双通信C程序》最后刷新时间 2024-03-14 00:58:07,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《avr单片机双通信C程序》该内容的真实性请自行鉴别。