Join Today
Results 1 to 7 of 7
  1. #1
    Junior Member Holmes's Avatar
    Join Date
    Jul 2011
    Location
    Palembang, Indonesia
    Posts
    55

    Komunikasi serial visual C++ dengan cvavr

    kak @norture kira-kira ini cuplikan codenyo, mohon dikoreksi.

    visual c++

    C++ Code:
     
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                     if (!serialPort->IsOpen)
                {
                    serialPort->PortName = "com1";
                    serialPort->Open();
                    a1->Text = "Connect";
                }
                 }
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
                     if (serialPort->IsOpen)
                    {
                        String ^ text = a2->Text;
                        serialPort->Write(text->ToString());
                     }
                 }


    ini dengan c di codevision
    Code:
    int c;
    char  a[32];
    
    
    while (1)
          {
          // Place your code here
          lcd_clear(); 
          c=getchar();
          a[0] =(char)c;
          lcd_gotoxy(6,0);
          lcd_puts(a); 
          delay_ms(1000);  
          }

  2. #2
    Administrator NortuRE's Avatar
    Join Date
    Jun 2011
    Location
    Palembang, Indonesia
    Posts
    73
    Caknyo ado yang salah di bagian program mikro. Di situ kk lihat programnyo ngambil data serial per satu karakter. Solusinyo konfigurasi mikro harus pake mode interrupt (di RX bae). Kemudian pake algoritma seperti kode berikut:
    C++ Code:
     
    unsigned char mystring[32];
     
    void tampilkan_string()
    {
      unsigned char i=0;
     
      while(rx_counter)                    
      {                                    
        mystring[i] = getchar();
        i++;
      }
      mystring[i]=0;
      lcd_gotoxy(0,0);
      lcd_puts(mystring);
    }


    Penjelasan
    Saat kita memakai mode interrupt, mikrokontroller akan menyediakan fasilitas 'penitipan' data yang masuk melalui serial port. Di Codevision, saat kita mengkonfigurasi mikro untuk memakai mode interupsi, akan ada fungsi tambahan void usart_rx_isr yang akan dipanggil otomatis saat ada data yang masuk melalui pin RX. Data ini disimpan dalam sebuah variabel yang berfungsi sebagai receive buffer. Variabel ini bisa diatur kapasitasnya, dan secara default menampung 8 karakter. Apabila data yang masuk melebihi 8 karakter, akan terjadi buffer overflow dan kelebihannya tidak digubris (discarded).

    Dengan memanfaatkan receive buffer ini, kita dapat mengakses data serial secara efisien tanpa jeda pada proses utama mikrokontroler. Untungnya, codevision telah menyediakan fungsi getchar yang telah dimodifikasi supaya fitur interupsi tersebut bisa dipakai dengan mudah.

    Pada kode di atas, kita memanfaatkan counter rx_counter untuk mendeteksi akhir dari buffer. Setiap kali fungsi getchar dipanggil, maka isi dari buffer akan berkurang satu sampai akhirnya menjadi kosong. Saat ini terjadi, maka rx_counter akan bernilai nol dan ini mengindikasikan bahwa tidak ada data lagi yang bisa diambil dan perulangan bisa dihentikan. Namun bila masih ada data yang didapat, ia akan disimpan di array mystring secara berurutan sesuai indeks i yang akan bertambah nilainya setiap kali ada data yang diambil dari buffer.

  3. #3
    Junior Member Holmes's Avatar
    Join Date
    Jul 2011
    Location
    Palembang, Indonesia
    Posts
    55
    C++ Code:
     
     
    /**************************************************  ***
    This program was produced by the
    CodeWizardAVR V2.05.0 Professional
    Automatic Program Generator
    © Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
    <a href="http://www.hpinfotech.com" target="_blank" rel="nofollow">HP InfoTech - CodeVisionAVR C Compiler</a>
     
    Project : 
    Version : 
    Date    : 08/10/2013
    Author  : NeVaDa
    Company : 
    Comments: 
     
     
    Chip type               : ATmega32A
    Program type            : Application
    AVR Core Clock frequency: 11,059200 MHz
    Memory model            : Small
    External RAM size       : 0
    Data Stack size         : 512
    **************************************************  ***/
     
    #include <mega32a.h>
     
    // Alphanumeric LCD Module functions
    #include <alcd.h>
     
    #ifndef RXB8
    #define RXB8 1
    #endif
     
    #ifndef TXB8
    #define TXB8 0
    #endif
     
    #ifndef UPE
    #define UPE 2
    #endif
     
    #ifndef DOR
    #define DOR 3
    #endif
     
    #ifndef FE
    #define FE 4
    #endif
     
    #ifndef UDRE
    #define UDRE 5
    #endif
     
    #ifndef RXC
    #define RXC 7
    #endif
     
    #define FRAMING_ERROR (1<<FE)
    #define PARITY_ERROR (1<<UPE)
    #define DATA_OVERRUN (1<<DOR)
    #define DATA_REGISTER_EMPTY (1<<UDRE)
    #define RX_COMPLETE (1<<RXC)
     
    // USART Receiver buffer
    #define RX_BUFFER_SIZE 8
    char rx_buffer[RX_BUFFER_SIZE];
     
    #if RX_BUFFER_SIZE <= 256
    unsigned char rx_wr_index,rx_rd_index,rx_counter;
    #else
    unsigned int rx_wr_index,rx_rd_index,rx_counter;
    #endif
     
    // This flag is set on USART Receiver buffer overflow
    bit rx_buffer_overflow;
     
    // USART Receiver interrupt service routine
    interrupt [USART_RXC] void usart_rx_isr(void)
    {
    char status,data;
    status=UCSRA;
    data=UDR;
    if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
       {
       rx_buffer[rx_wr_index++]=data;
    #if RX_BUFFER_SIZE == 256
       // special case for receiver buffer size=256
       if (++rx_counter == 0)
          {
    #else
       if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
       if (++rx_counter == RX_BUFFER_SIZE)
          {
          rx_counter=0;
    #endif
          rx_buffer_overflow=1;
          }
       }
    }
     
    #ifndef _DEBUG_TERMINAL_IO_
    // Get a character from the USART Receiver buffer
    #define _ALTERNATE_GETCHAR_
    #pragma used+
    char getchar(void)
    {
    char data;
    while (rx_counter==0);
    data=rx_buffer[rx_rd_index++];
    #if RX_BUFFER_SIZE != 256
    if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
    #endif
    #asm("cli")
    --rx_counter;
    #asm("sei")
    return data;
    }
    #pragma used-
    #endif
     
    // Standard Input/Output functions
    #include <stdio.h>
     
    // Declare your global variables here
     
    unsigned char mystring[32];
     
    void tampilkan_string()
    {
      unsigned char i=0;
     
      while(rx_counter)                    
      {                                    
        mystring[i] = getchar();
        i++;
      }
      mystring[i]=0;
      lcd_gotoxy(0,0);
      lcd_puts(mystring);
    }
     
    void main(void)
    {
    // Declare your local variables here
     
    // Input/Output Ports initialization
    // Port A 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 
    PORTA=0x00;
    DDRA=0x00;
     
    // 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=0x00;
    DDRB=0x00;
     
    // Port C 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 
    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=0xFF
    // OC0 output: Disconnected
    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;
     
    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: Timer1 Stopped
    // Mode: Normal top=0xFFFF
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer1 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: Timer2 Stopped
    // Mode: Normal top=0xFF
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;
     
    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    // INT2: Off
    MCUCR=0x00;
    MCUCSR=0x00;
     
    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;
     
    // USART initialization
    // Communication Parameters: 8 Data, 1 Stop, No Parity
    // USART Receiver: On
    // USART Transmitter: Off
    // USART Mode: Asynchronous
    // USART Baud Rate: 57600
    UCSRA=0x00;
    UCSRB=0x90;
    UCSRC=0x06;
    UBRRH=0x00;
    UBRRL=0x0B;
     
    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;
     
    // ADC initialization
    // ADC disabled
    ADCSRA=0x00;
     
    // SPI initialization
    // SPI disabled
    SPCR=0x00;
     
    // TWI initialization
    // TWI disabled
    TWCR=0x00;
     
    // Alphanumeric LCD initialization
    // Connections specified in the
    // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
    // RS - PORTC Bit 0
    // RD - PORTC Bit 1
    // EN - PORTC Bit 2
    // D4 - PORTC Bit 4
    // D5 - PORTC Bit 5
    // D6 - PORTC Bit 6
    // D7 - PORTC Bit 7
    // Characters/line: 16
    lcd_init(16);
     
    // Global enable interrupts
    #asm("sei")
     
    while (1)
          {
          // Place your code here
             tampilkan_string();
          }
    }

  4. #4
    Junior Member Holmes's Avatar
    Join Date
    Jul 2011
    Location
    Palembang, Indonesia
    Posts
    55
    biso suhu, ane pindahin yang pembuat nol nyo dibawah penampil lcd, enjok delay dikit, jebreettt

    C++ Code:
     
    void tampilkan_string()
    {
      unsigned char i=0;
      while(rx_counter)                    
      {                                    
        mystring[i] = getchar();
        //r = getchar();
        //a[0] =(char)r;
        i++;
      }
     
      lcd_gotoxy(0,0);
      lcd_puts(mystring); 
      delay_ms(2000);
     
      mystring[i]=0;      
      lcd_clear();
     
    }

  5. #5
    Junior Member Holmes's Avatar
    Join Date
    Jul 2011
    Location
    Palembang, Indonesia
    Posts
    55
    C++ Code:
     
     
    wchar_t buf[256],temp[256],buff[256],str[256];
        wchar_t v1[10],v2[10],v3[10], v4[10], v5[10], v6[10];
        LPCTSTR text1 = TEXT("%4.4f"), text2 = TEXT("%4.4f"), text3 = TEXT("%4.4f"), text4 = TEXT("%4.4f"), text5 = TEXT("%4.4f"), text6 = TEXT("%4.4f");
    	LPCTSTR pszFormat = TEXT("%4.4f"),pszFormat2 = TEXT("%4.4f"),pszFormat3 = TEXT("%4.4f"),pszFormat4 = TEXT("%4.4f");
    	StringCbPrintf(buf, sizeof(buf), pszFormat, skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x);
    	SetDlgItemText(m_hWnd, IDC_EDIT1, buf);
    	StringCbPrintf(buff, sizeof(buff), pszFormat3, skel.SkeletonPositions[NUI_SKELETON_POSITION_ELBOW_RIGHT].x);
    	SetDlgItemText(m_hWnd, IDC_EDIT3, buff);
    	StringCbPrintf(str, sizeof(str), pszFormat2, skel.SkeletonPositions[NUI_SKELETON_POSITION_WRIST_RIGHT].x);
    	SetDlgItemText(m_hWnd, IDC_EDIT2, str);
        StringCbPrintf(temp, sizeof(temp), pszFormat4, skel.SkeletonPositions[NUI_SKELETON_POSITION_SHOULDER_RIGHT].x);
    	SetDlgItemText(m_hWnd, IDC_EDIT4, temp);
        StringCbPrintf(v1, sizeof(v1), text1, skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y);
    	SetDlgItemText(m_hWnd, IDC_EDIT5, v1);
    	StringCbPrintf(v2, sizeof(v2), text2, skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].z);
    	SetDlgItemText(m_hWnd, IDC_EDIT6, v2);
        StringCbPrintf(v3, sizeof(v3), text3, skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].w);
    	SetDlgItemText(m_hWnd, IDC_EDIT7, v3);
        StringCbPrintf(v4, sizeof(v4), text4, skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_LEFT].x);
    	SetDlgItemText(m_hWnd, IDC_EDIT8, v4);
     
        //inverse kinematic 2 dof
        double l1, l2, x1, y1, k1, k2, teta1, teta2, costeta2, sinteta2, degree1, degree2;
        l1=0.32, l2=0.32;
     
        x1=skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x;
        y1=skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y;
     
        costeta2=(((x1*x1)+(y1*y1)-(l1*l1)-(l1*l2))/(2*l1*l2));
        sinteta2=sqrt(1-(costeta2*costeta2));
        k1=l1+(l2*costeta2);
        k2=l2*sinteta2;
     
        teta1 = atan2l(y1,x1)-atan2l(k2,k1);
        teta2 = atan2l(sinteta2,costeta2);
        degree1 = teta1 * (180 / 3.14);
        degree2 = teta2 * (180 / 3.14);
     
        StringCbPrintf(v5, sizeof(v5), text5, degree1);
    	SetDlgItemText(m_hWnd, IDC_EDIT9, v5);
        StringCbPrintf(v6, sizeof(v6), text6, degree2);
    	SetDlgItemText(m_hWnd, IDC_EDIT10, v6);
     
        //serial
        if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.01&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.2&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y<=0.01)
        {
                serialPort.sendChar((char)'a');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.21&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.3&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y<=0.01)
        {
                serialPort.sendChar((char)'b');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.31&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.4&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y<=0.01)
        {
                serialPort.sendChar((char)'c');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.41&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.5&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y<=0.01)
        {
                serialPort.sendChar((char)'d');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.51&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.6&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y<=0.01)
        {
                serialPort.sendChar((char)'e');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.51&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.6&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y>=0.1)
        {
                serialPort.sendChar((char)'f');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.31&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.5&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y>=0.1)
        {
                serialPort.sendChar((char)'g');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.21&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.4&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y>=0.1)
        {
                serialPort.sendChar((char)'h');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.11&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.3&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y>=0.1)
        {
                serialPort.sendChar((char)'i');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.01&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.2&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y>=0.1)
        {
                serialPort.sendChar((char)'j');
        }
        else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_ELBOW_RIGHT].x>=0.31&&skel.SkeletonPositions[NUI_SKELETON_POSITION_ELBOW_RIGHT].x<=0.36&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y>=0.1)
        {
                if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.31&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.4)
                {
                    serialPort.sendChar((char)'k');
                }
                else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.41&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.5)
                {
                    serialPort.sendChar((char)'l');
                }
                else if(skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x>=0.51&&skel.SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x<=0.64)
                {
                    serialPort.sendChar((char)'m');
                }
        }
     
    maaf kak kalo caro penulisannyo jelek

  6. #6
    Administrator NortuRE's Avatar
    Join Date
    Jun 2011
    Location
    Palembang, Indonesia
    Posts
    73
    Cubo jgn cuplikannyo bae jul, sekalian filenyo diattach.

  7. #7
    Junior Member Holmes's Avatar
    Join Date
    Jul 2011
    Location
    Palembang, Indonesia
    Posts
    55
    test ahahah
    Attached Files Attached Files


 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •