<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR_M8單片機(jī)串口控制應(yīng)用程序

          AVR_M8單片機(jī)串口控制應(yīng)用程序

          作者: 時(shí)間:2016-11-24 來源:網(wǎng)絡(luò) 收藏
          #include
          #include
          #include "Io_define.h"
          unsigned char RX_data[4]={0}; //串口接收的數(shù)據(jù)
          unsigned char keyb_state=0;
          unsigned char keyc_state=0;
          unsigned char RX_counter=0; //串口接收到的字節(jié)數(shù)計(jì)數(shù)器
          void UART_init (void);
          void putchar (unsigned char c);
          void puts (unsigned char *s);
          void UART_rx (void);
          void delay_nms (unsigned int n);
          void Key_control (unsigned char comm);
          void Read_Pin (void);
          void putchar(unsigned char c) //字符輸出函數(shù)
          {
          while (!(UCSRA&(1 << UDRE))); //判斷上次發(fā)送有沒有完成
          UDR = c;
          }
          #pragma interrupt_handler UART_rx: iv_USART_RX
          void UART_rx(void) //串口接收中斷函數(shù)
          {
          RX_data[RX_counter] = UDR;

          if (RX_data[RX_counter]==s)
          {
          RX_data[0]=RX_data[RX_counter];
          RX_counter=0;
          }

          RX_counter++; //接收的字節(jié)數(shù)計(jì)數(shù)
          }
          void puts(unsigned char *s) //字符串發(fā)送函數(shù)
          {
          while (*s)
          {
          putchar(*s);
          s++;
          }
          }

          void UART_init(void)
          {
          UCSRB = BIT(RXCIE)| BIT(RXEN) |BIT(TXEN); //允許串口發(fā)送和接收,并響應(yīng)接收完成中斷
          UBRR = 51; //時(shí)鐘8Mhz,波特率9600
          UCSRC = BIT(URSEL)|BIT(UCSZ1)|BIT(UCSZ0); //8位數(shù)據(jù)+1位stop位
          }

          void main(void)
          {
          //unsigned char keyb_state=0;
          //unsigned char keyc_state=0;
          unsigned char i=0;

          OSCCAL=0xa3;

          DDRB = 0B11000111; //初始化繼電器控制端口
          PORTB = 0B00000000;
          DDRC = 0B00111111;
          PORTC = 0B00000000;
          DDRD = 0B10000000;
          PORTD = 0B00000000;

          DDRB |= 0b00111000; //初始化鍵盤接口,行為低電平
          PORTB&= 0b11000111;

          DDRD &= ~0b01111000; //列為高電平
          PORTD|= 0b01111000;

          DDRD &= ~BIT(2); //設(shè)置INT0為輸入,高電平狀態(tài)
          PORTD |= BIT(2);
          GICR |= BIT(INT0); //允許INT0外部低電平中斷

          UART_init();

          SEI();

          while (1)
          {
          if (RX_counter==4)
          {
          RX_counter=0;
          if (RX_data[3] == t) //接收到PC機(jī)發(fā)的狀態(tài)請(qǐng)求指令,則發(fā)送
          { //控制板的key狀態(tài)數(shù)據(jù)給PC
          putchar(0); //控制板地址碼
          delay_nms(10);
          putchar(keyb_state);
          delay_nms(10);
          putchar(keyc_state);
          delay_nms(10);
          putchar(t); //狀態(tài)數(shù)據(jù)標(biāo)志位
          delay_nms(10);
          }
          else
          if ((RX_data[0] == s)&&(RX_data[3] == p)) //檢驗(yàn)指令格式是否正確
          {
          if ((RX_data[1] == u) &&(RX_data[2]==0))//是否是軟件升級(jí)指令
          {
          //puts("upready");
          asm("jmp 0xe00");
          }
          else
          if (RX_data[1]==0) //地址碼是否正確
          {
          Key_control(RX_data[2]);
          }
          }
          }
          }
          }

          void Key_control(unsigned char comm)
          {
          CLI();
          switch (comm)
          {
          case 0: key0 ^= key0_open; break;
          case 1: key1 ^= key1_open; break;
          case 2: key2 ^= key2_open; break;
          case 3: key3 ^= key3_open; break;
          case 4: key4 ^= key4_open; break;
          case 5: key5 ^= key5_open; break;
          case 6: key6 ^= key6_open; break;
          case 7: key7 ^= key7_open; break;
          case 8: key8 ^= key8_open; break;
          case 9: key9 ^= key9_open; break;
          case :: key10 ^= key10_open; break;
          case ;: key11 ^= key11_open; break;
          case a: {
          PORTB |= 0B11000111;
          PORTC |= 0B00111111;
          PORTD |= 0B10000000;
          } break;
          case b: {
          PORTB &= 0B00111000;
          PORTC &= 0B11000000;
          PORTD &= 0B01111111;
          } break;

          }
          Read_Pin();
          putchar(0); //發(fā)送控制板地址碼和key狀態(tài)數(shù)據(jù)
          delay_nms(10);
          putchar(keyb_state);
          delay_nms(10);
          putchar(keyc_state);
          delay_nms(10);
          putchar(t);
          delay_nms(10);
          SEI();
          }
          void Read_Pin(void)
          {
          asm("nop");
          keyb_state = ((PINB & 0B11000111)|((PINC & 0B00110000)>>1)|
          ((PIND & 0B10000000)>>2));
          keyc_state = (PINC & 0B00001111);
          }


          關(guān)鍵詞: AVRM8單片機(jī)串口控

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();