Thông báo

Collapse
No announcement yet.

Mach Nap Avr,psoc,antera...

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Mach Nap Avr,psoc,antera...

    Ban nao can mach nap cho vi xu ly, hoac hoi cac van de ve ky thuat dieu khien nhu:
    -Dieu khien mo to buoc
    -Dieu khien hien thi
    -Dieu khien giao tiep PC
    -Dieu khien so tu xa song vo tuyen radio, hong ngoai, dieu khien trong nha...
    Cac mach nap hay goi dien cho minh. Ho tro sinh vien(gia cuc re chi vai ba chuc ngan)
    Minh dang o Ha Noi
    DT:0974498090
    mail:minhtuan.tm1@gmail.com

  • #2
    giao tiep voi avr dung codevision avr thi phai lam như thế nào nếu nó nhận lệnh từ chương trình điều khiển trên máy tính gửi xuống . ví dụ em lập trình bằng vc giao diện điều khiển 3 động cơ bước thì trong avr ngoài thiết lập port, pin, thì phải thiết lập usart ...hãy xem bài dưới đây

    Comment


    • #3
      giao tiep voi avr dung codevision avr thi phai lam như thế nào nếu nó nhận lệnh từ chương trình điều khiển trên máy tính gửi xuống . ví dụ em lập trình bằng vc giao diện điều khiển 3 động cơ bước thì trong avr ngoài thiết lập port, pin, thì phải thiết lập usart ...hãy xem bài dưới đây
      /************************************************** ***
      This program was produced by the
      CodeWizardAVR V1.24.6 Standard
      Automatic Program Generator
      © Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
      http://www.hpinfotech.com
      e-mail:office@hpinfotech.com

      Project :
      Version :
      Date : 4/17/2007
      Author : F4CG
      Company : F4CG
      Comments:


      Chip type : ATmega88
      Program type : Application
      Clock frequency : 8.000000 MHz
      Memory model : Small
      External SRAM size : 0
      Data Stack size : 256
      ************************************************** ***/

      #include <mega88.h>
      #include <delay.h>
      #include <string.h>
      // === Dinh nghia cac bien ======================================
      bit rs_bCmd=0;
      bit rs_bReset=1;
      unsigned char rs_ID='1',rs_nCmd;
      unsigned char RS232_data,rs_index=0,rs_i;
      //unsigned int RS_i;
      char rs_Cmd[12];
      // ----------- dinh nghia giao tiep RAM ngoai ----
      #define Pin_Step PIND.2
      #define Pin_Dir PIND.3

      #define Limit_A PIND.2
      #define Limit_Z PIND.3

      #define Mode_1 PIND.4
      #define Mode_2 PIND.5
      //---------Dinh nghia Status---------------
      #define Status_1 PINB.5
      #define Status_2 PINB.4
      #define Status_3 PINB.3
      #define Status_4 PINB.2
      //--------- dinh nghia cac dau vao Input---------
      #define Direct_1 PINB.1
      #define Direct_2 PINB.0
      #define Direct_3 PIND.7
      #define Direct_4 PIND.6
      #define Direct_5 PIND.5
      //----------Dinh gnhia dau ra Ouput--------------
      #define Step_1 PORTC.2
      #define Step_2 PORTC.3
      #define Step_3 PORTC.4
      #define Step_4 PORTC.5
      //---
      #define DC1_EN PORTC.0
      #define DC1_DIR PORTC.1
      //--
      #define DC2_EN PORTD.2
      #define DC2_DIR PORTD.3
      //--------------------the End------------------





      //====== Dinh nghia cac bit ===
      bit m_bPWM,m_bDir=1,m_bReset ;
      //====== Dinh nghia cac bien dung chung ======
      unsigned char m_Mode,m_Status,m_Index=1,m_i,m_j,m_n,i;
      unsigned char V_H,V_L;
      unsigned int m_Step,tempNum;
      long int m_CurPos;
      unsigned long int m_cal=125000;
      //====== Dinh nghia bang du lieu ===========
      flash unsigned char step[] = {0xFF,0xEF,0xDF,0xBF,0x7F};
      flash char cmd_w[]= {">PWM set"};
      //flash char cmd_v[] = {">V set"};
      //flash char cmd_p[] = {">Dich buoc"};
      flash char cmd_m[] = {">Di chuyen"};
      flash char cmd_d[] = {">Dao chieu"};
      //flash char v[] = {"Van toc : "};
      //flash char pos[] = {"Vi tri : "};
      flash char info[] = {"STEP DRIVER V1.0"};
      // --- eeprom data - luu gia tri ---
      //eeprom unsigned char e_count=0;

      #define RXB8 1
      #define TXB8 0
      #define UPE 2
      #define OVR 3
      #define FE 4
      #define UDRE 5
      #define RXC 7

      #define FRAMING_ERROR (1<<FE)
      #define PARITY_ERROR (1<<UPE)
      #define DATA_OVERRUN (1<<OVR)
      #define DATA_REGISTER_EMPTY (1<<UDRE)
      #define RX_COMPLETE (1<<RXC)

      // USART Receiver buffer
      #define RX_BUFFER_SIZE0 30
      char rx_buffer0[RX_BUFFER_SIZE0];

      #if RX_BUFFER_SIZE0<256
      unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;
      #else
      unsigned int rx_wr_index0,rx_rd_index0,rx_counter0;
      #endif

      // This flag is set on USART Receiver buffer overflow
      bit rx_buffer_overflow0;

      // USART Receiver interrupt service routine


      #ifndef _DEBUG_TERMINAL_IO_
      // Get a character from the USART Receiver buffer
      #define _ALTERNATE_GETCHAR_
      #pragma used+
      char getchar(void)
      {
      char data;
      while (rx_counter0==0);
      data=rx_buffer0[rx_rd_index0];
      if (++rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
      #asm("cli")
      --rx_counter0;
      #asm("sei")
      return data;
      }
      #pragma used-
      #endif

      // USART Transmitter buffer
      #define TX_BUFFER_SIZE0 30
      char tx_buffer0[TX_BUFFER_SIZE0];

      #if TX_BUFFER_SIZE0<256
      unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;
      #else
      unsigned int tx_wr_index0,tx_rd_index0,tx_counter0;
      #endif

      // USART Transmitter interrupt service routine
      interrupt [USART_TXC] void usart_tx_isr(void)
      {
      if (tx_counter0)
      {
      --tx_counter0;
      UDR0=tx_buffer0[tx_rd_index0];
      if (++tx_rd_index0 == TX_BUFFER_SIZE0) tx_rd_index0=0;
      };
      }

      #ifndef _DEBUG_TERMINAL_IO_
      // Write a character to the USART Transmitter buffer
      #define _ALTERNATE_PUTCHAR_
      #pragma used+
      void putchar(char c)
      {
      while (tx_counter0 == TX_BUFFER_SIZE0);
      #asm("cli")
      if (tx_counter0 || ((UCSR0A & DATA_REGISTER_EMPTY)==0))
      {
      tx_buffer0[tx_wr_index0]=c;
      if (++tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;
      ++tx_counter0;
      }
      else
      UDR0=c;
      #asm("sei")
      }
      #pragma used-
      #endif

      interrupt [USART_RXC] void usart_rx_isr(void)
      {
      /*
      char status,data;
      status=UCSR0A;
      data=UDR0;
      if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
      {
      rx_buffer0[rx_wr_index0]=data;
      if (++rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
      if (++rx_counter0 == RX_BUFFER_SIZE0)
      {
      rx_counter0=0;
      rx_buffer_overflow0=1;
      };
      };
      */
      char status,data;
      status=UCSR0A;
      data=UDR0;
      if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) {
      if(rs_bReset){ // cho nhan byte >
      if(data=='>'){ // dung byte khoi dau
      rs_index = 0;
      rx_buffer0[rs_index] = data; rs_index++;
      rs_bReset = 0;
      putchar(data);
      }else rs_bReset = 1;
      }else{
      if(data == 0x0A){ // CR LR ?
      if(rx_buffer0[rs_index-1]==0x0D){
      rx_buffer0[rs_index] = data; ;// rs_index++;
      if(rx_buffer0[1]==rs_ID){
      for(rs_i=0; rs_i<rs_index; rs_i++) rs_Cmd[rs_i] = rx_buffer0[rs_i];
      rs_bCmd = 1;
      rs_nCmd = rs_index;
      }
      }//else putCmdNote(error);
      rs_bReset = 1;
      rs_index = 0;
      }else{
      rx_buffer0[rs_index] = data;
      rs_index++;
      putchar(data);
      }
      }
      };
      }

      // Standard Input/Output functions
      #include <stdio.h>

      // Declare your global variables here
      // =========== RS232 General function - putstr ====================
      void putCmdNote(flash char *c)
      {
      unsigned char ps_i;
      for(ps_i=0;ps_i<strlenf(c);ps_i++){
      if(ps_i==1){ putchar(rs_ID);putchar(' ');}
      putchar(c[ps_i]);
      }
      putchar(0x0D);
      }
      // =========== RS232 General function - putstr ====================

      void putstrf(flash char *c)
      {

      unsigned char ps_i;
      for(ps_i=0;ps_i<strlenf(c);ps_i++) putchar(c[ps_i]);
      }

      void ExtCmd(){
      switch(rs_Cmd[3]){

      case 'S': // lenh kiem tra -----------------------------------------------
      putchar('>');
      if(m_Step) putchar('1');
      else{
      if(!Limit_A) putchar('A');
      else if(!Limit_Z) putchar('Z');
      else {putchar(m_Status+'0'); putchar('0');}

      }
      // putchar(0x0d);
      break;

      case 'M': // lenh chay buoc voi van toc ----------------------------------------
      tempNum = 0;
      for(m_i=5; rs_Cmd[m_i] != ' '; m_i++) tempNum = 10*tempNum + rs_Cmd[m_i]-'0';
      tempNum = 0xFFFF - m_cal/tempNum;
      V_H = tempNum>>8;
      V_L = tempNum;
      tempNum = 0;
      m_j = m_i+1;
      for(m_i=m_j; rs_Cmd[m_i] !=0x0D; m_i++) tempNum = 10*tempNum + rs_Cmd[m_i]-'0';
      // m_Step = tempNum; TIMSK = 0x82;
      PORTB=0x00;

      break;

      case 'D': // lenh chieu quay cho Test --------------------------------------
      if(rs_Cmd[5] == '0') m_bDir = 0;
      else m_bDir = 1;
      // putCmdNote(cmd_d);
      break;

      case 'H': //---- lenh reset ve goc toa do
      //putchar('h');
      m_bReset=1;
      rs_bReset = 1;
      break;
      case 'Z': //---- Stop tuc thoi ---
      //putchar('z');
      // TIMSK = 0x00;
      rs_bReset = 1;
      break;
      case 'E': //---- Enable dong co dao cat ---
      //putchar('e');
      // Role = 0xFA;
      // PORTC = stepC[indexC] & Role;

      rs_bReset = 1;
      break;

      default : //---- mac dinh thi reset
      rs_bReset = 1;
      break;


      }
      //--- xoa co co lenh moi -- doi lenh tiep theo ---
      rs_bCmd = 0;
      }
      //================================================== =================
      void Out_Step(){
      PORTB = step[m_Index];
      }
      //---------------------------
      void Check_Mode(){
      delay_ms(10);
      m_Mode = !Mode_1 | !Mode_2<<1; // read mode
      m_Status = !Status_1 | !Status_2<<1 | !Status_3<<2 ; // Read Status
      delay_ms(10);
      // putCmdNote(info);
      // putstrf("MODE -"); putchar(m_Mode+'0');
      }

      //--------------------------
      void Init_RS232Mul(){
      // Khoi tao chuong trinh chay o che do giao tiep nhan lenh ----
      putstrf("- : RS232 Multi Mode"); putchar(0x0D);
      putstrf("STATUS : "); putchar(m_Status+'0');putchar(0x0D); delay_ms(10);
      rs_ID = m_Status+'0';
      //------------------------
      // TIMSK = 0x82;
      V_H = 0x00;
      V_L = 0x00;
      //-----------
      m_bDir = 1;
      m_Step = 0;
      m_bPWM = 0;
      while(1) if(rs_bCmd) ExtCmd();
      }

      // Declare your global variables here


      void RoboMove(void)
      {
      DC1_DIR=1;
      while(Direct_1)
      {
      DC1_EN=0;
      }
      Step_1=0;
      delay_ms(1000);
      Step_2=0;
      delay_ms(500);
      Step_3=0;
      delay_ms(200);
      Step_1=1;
      delay_ms(1000);
      Step_3=1;
      delay_ms(500);
      Step_1=0;
      delay_ms(1000);




      }


      void main(void)
      {
      // Declare your local variables here

      // Crystal Oscillator division factor: 1
      #pragma optsize-
      CLKPR=0x80;
      CLKPR=0x00;
      #ifdef _OPTIMIZE_SIZE_
      #pragma optsize+
      #endif

      // Input/Output Ports initialization
      // Port B initialization
      // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
      // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
      PORTB=0xff;
      DDRB=0xff;

      // Port C initialization
      // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
      // State6=T State5=T State4=T State3=T State2=T State1=T State0=T
      PORTC=0x00;
      DDRC=0x00;

      // Port D initialization
      // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
      // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
      PORTD=0x00;
      DDRD=0x00;

      // Timer/Counter 0 initialization
      // Clock source: System Clock
      // Clock value: Timer 0 Stopped
      // Mode: Normal top=FFh
      // OC0A output: Disconnected
      // OC0B output: Disconnected
      TCCR0A=0x00;
      TCCR0B=0x00;
      TCNT0=0x00;
      OCR0A=0x00;
      OCR0B=0x00;

      // Timer/Counter 1 initialization
      // Clock source: System Clock
      // Clock value: Timer 1 Stopped
      // Mode: Normal top=FFFFh
      // OC1A output: Discon.
      // OC1B output: Discon.
      // Noise Canceler: Off
      // Input Capture on Falling Edge
      // Timer 1 Overflow Interrupt: Off
      // Input Capture Interrupt: Off
      // Compare A Match Interrupt: Off
      // Compare B Match Interrupt: Off
      TCCR1A=0x00;
      TCCR1B=0x00;
      TCNT1H=0x00;
      TCNT1L=0x00;
      ICR1H=0x00;
      ICR1L=0x00;
      OCR1AH=0x00;
      OCR1AL=0x00;
      OCR1BH=0x00;
      OCR1BL=0x00;

      // Timer/Counter 2 initialization
      // Clock source: System Clock
      // Clock value: Timer 2 Stopped
      // Mode: Normal top=FFh
      // OC2A output: Disconnected
      // OC2B output: Disconnected
      ASSR=0x00;
      TCCR2A=0x00;
      TCCR2B=0x00;
      TCNT2=0x00;
      OCR2A=0x00;
      OCR2B=0x00;

      // External Interrupt(s) initialization
      // INT0: Off
      // INT1: Off
      // Interrupt on any change on pins PCINT0-7: Off
      // Interrupt on any change on pins PCINT8-14: Off
      // Interrupt on any change on pins PCINT16-23: Off
      EICRA=0x00;
      EIMSK=0x00;
      PCICR=0x00;

      // Timer/Counter 0 Interrupt(s) initialization
      TIMSK0=0x00;
      // Timer/Counter 1 Interrupt(s) initialization
      TIMSK1=0x00;
      // Timer/Counter 2 Interrupt(s) initialization
      TIMSK2=0x00;

      // USART initialization
      // Communication Parameters: 8 Data, 1 Stop, No Parity
      // USART Receiver: On
      // USART Transmitter: On
      // USART0 Mode: Asynchronous
      // USART Baud rate: 9600
      UCSR0A=0x00;
      UCSR0B=0xD8;
      UCSR0C=0x06;
      UBRR0H=0x00;
      UBRR0L=0x33;

      // Analog Comparator initialization
      // Analog Comparator: Off
      // Analog Comparator Input Capture by Timer/Counter 1: Off
      ACSR=0x80;
      ADCSRB=0x00;

      // Global enable interrupts
      #asm("sei")
      // putCmdNote(cmd_d);
      // delay_ms(1000);
      // Check_Mode();
      //---------------------------------


      // Init_RS232Mul();
      /*
      PORTB=0xaa;

      while (1) {
      unsigned char i;
      putchar('f');

      i=getchar();
      PORTB=i;
      // putchar(i);


      };
      */
      while(1)
      {
      RoboMove() ;
      while(1)
      {}

      }

      }

      Comment

      Về tác giả

      Collapse

      minhtuan_tm2 Tìm hiểu thêm về minhtuan_tm2

      Bài viết mới nhất

      Collapse

      Đang tải...
      X