Penulis Topik: Mentransfer Data dari EEPROM PIC16f877 ke komputer melalui RJ45  (Dibaca 2234 kali)

0 Anggota dan 2 Pengunjung sedang melihat topik ini.

Offline starda

  • Newbie
  • *
  • Tulisan: 5
abang2, mas2, bpk2 smuanya. saya mau tanya cara Mentransfer Data dari EEPROM PIC16f877 ke komputer melalui RJ45 itu bagaimana caranya???
kalau programing dari komputer ke mikrokontroler saya biasanya memakai konektor DB9, tetapi adakah cara untuk mentransfer data dari eeprom PIC16f877 ke komputer dengan memakai konektor RJ45 langsung atau memakai DB9 lalu di konvert ke RJ45 kemudian ke komputer???
Catatan: Data yang diTransfer adalah OUTPUT dari mikrokontroller
Mohon Pencerahannya

Offline purwanto-amm

  • Administrator
  • Hero Member
  • ********
  • Tulisan: 1066
  • Nikola Tesla
    • purwanto-apriana
« Edit Terakhir: Juli 29, 2010, 07:33:09 AM oleh isurganteng »
Kontak INBOX ke FB
facebook.com/purwanto.apriana

BLOG
purwanto1987.wordpress.com

Offline starda

  • Newbie
  • *
  • Tulisan: 5
Re:Mentransfer Data dari EEPROM PIC16f877 ke komputer melalui RJ45
« Jawab #2 pada: Juli 31, 2010, 11:23:29 AM »
oya mas bisa minta rangkaian skematiknya g?
lalu komponen2 apa aja yg harus di rangkai?
kalau kita ingin mentransfer data dari PIC melalui rangkaian itu, bisa g kita memakai pin output yang sembarang?

Offline purwanto-amm

  • Administrator
  • Hero Member
  • ********
  • Tulisan: 1066
  • Nikola Tesla
    • purwanto-apriana
Re:Mentransfer Data dari EEPROM PIC16f877 ke komputer melalui RJ45
« Jawab #3 pada: Juli 31, 2010, 11:45:23 AM »
Sy belum mencobanya, soalnya komponennya juga agak susah dicari.... harus pesen ke microchip

Tapi sebagai gambaran saja contoh dari MikroPascal membuat HTML Server pake RJ45

Kutip
Kode: [Pilih]
program http_demo;

uses eth_enc28j60_utils, //this is where you should write implementation for UDP and HTTP
     eth_enc28j60;

{***********************************
 * RAM variables
 *}
var myMacAddr   : array[6] of byte {= (0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f)} ; // my MAC address
    myIpAddr    : array[4] of byte {= (192, 168, 20, 60)} ;                   // my IP address
    gwIpAddr    : array[4] of byte {= 192, 168,   1,  1 } ;                   // gateway (router) IP address
    ipMask      : array[4] of byte {= 255, 255, 255,  0 } ;                   // network mask (for example : 255.255.255.0)
    dnsIpAddr   : array[4] of byte {= 192, 168,   1,  1 } ;                   // DNS server IP address


begin
  ADCON1 := 0x0B ;          // ADC convertors will be used with AN2 and AN3
  CMCON  := 0x07 ;          // turn off comparators

  PORTA  := 0 ;
  TRISA  := 0xff ;          // set PORTA as input for ADC

  PORTB  := 0 ;
  TRISB  := 0xff ;          // set PORTB as input for buttons

  PORTD  := 0 ;
  TRISD  := 0 ;             // set PORTD as output

  httpCounter := 0;

  myMacAddr[0] := 0x00;
  myMacAddr[1] := 0x14;
  myMacAddr[2] := 0xA5;
  myMacAddr[3] := 0x76;
  myMacAddr[4] := 0x19;
  myMacAddr[5] := 0x3F;

  myIpAddr[0]  := 192;
  myIpAddr[1]  := 168;
  myIpAddr[2]  := 1;
  myIpAddr[3]  := 60;
 
  gwIpAddr[0]  := 192;
  gwIpAddr[1]  := 168;
  gwIpAddr[2]  := 1;
  gwIpAddr[3]  := 1;

  dnsIpAddr[0] := 192;
  dnsIpAddr[1] := 168;
  dnsIpAddr[2] := 1;
  dnsIpAddr[3] := 1;

  ipMask[0]    := 255;
  ipMask[1]    := 255;
  ipMask[2]    := 255;
  ipMask[3]    := 0;

  {*
   * starts ENC28J60 with :
   * reset bit on RC0
   * CS bit on RC1
   * my MAC & IP address
   * full duplex
   *}

  Spi_init;
  Spi_Ethernet_Init(PORTC, 0, PORTC, 1, myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;
 
  // dhcp will not be used here, so use preconfigured addresses
  Spi_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr) ;

  while true do                   // do forever
    begin
      {*
       * if necessary, test the return value to get error code
       *}

      Spi_Ethernet_doPacket() ;   // process incoming Ethernet packets

      {*
       * add your stuff here if needed
       * Spi_Ethernet_doPacket() must be called as often as possible
       * otherwise packets could be lost
       *}
    end;
end.

Kutip
Kode: [Pilih]
unit eth_enc28j60_utils;

uses eth_enc28j60_api;

{************************************************************
 * ROM constant strings
 *}

const httpHeader : string[31] = 'HTTP/1.1 200 OK' + #10 + 'Content-type: ' ;  // HTTP header
const httpMimeTypeHTML : string[13]   = 'text/html' + #10 + #10 ;             // HTML MIME type
const httpMimeTypeScript : string[14] = 'text/plain' + #10 + #10 ;            // TEXT MIME type
const httpMethod : string[5] = 'GET /';
{*
 * web page, splited into 2 parts :
 * when coming short of ROM, fragmented data is handled more efficiently by linker
 *
 * this HTML page calls the boards to get its status, and builds itself with javascript
 *}
const indexPage : string[758] =
                    '<meta http-equiv="refresh" content="3;url=http://192.168.1.60">' +
                    '<HTML><HEAD></HEAD><BODY>'+
                    '<h1>PIC + ENC28J60 Mini Web Server</h1>'+
                    '<a href=/>Reload</a>'+
                    '<script src=/s></script>'+
                    '<table><tr><td valign=top><table border=1 style="font-size:20px ;font-family: terminal ;">'+
                    '<tr><th colspan=2>ADC</th></tr>'+
                    '<tr><td>AN2</td><td><script>document.write(AN2)</script></td></tr>'+
                    '<tr><td>AN3</td><td><script>document.write(AN3)</script></td></tr>'+
                    '</table></td><td><table border=1 style="font-size:20px ;font-family: terminal ;">'+
                    '<tr><th colspan=2>PORTB</th></tr>'+
                    '<script>'+
                    'var str,i;'+
                    'str="";'+
                    'for(i=0;i<8;i++)'+
                    '{str+="<tr><td bgcolor=pink>BUTTON #"+i+"</td>";'+
                    'if(PORTB&(1<<i)){str+="<td bgcolor=red>ON";}'+
                    'else {str+="<td bgcolor=#cccccc>OFF";}'+
                    'str+="</td></tr>";}'+
                    'document.write(str) ;'+
                    '</script>';

const indexPage2 : string[470] =
                    '</table></td><td>'+
                    '<table border=1 style="font-size:20px ;font-family: terminal ;">'+
                    '<tr><th colspan=3>PORTD</th></tr>'+
                    '<script>'+
                    'var str,i;'+
                    'str="";'+
                    'for(i=0;i<8;i++)'+
                    '{str+="<tr><td bgcolor=yellow>LED #"+i+"</td>";'+
                    'if(PORTD&(1<<i)){str+="<td bgcolor=red>ON";}'+
                    'else {str+="<td bgcolor=#cccccc>OFF";}'+
                    'str+="</td><td><a href=/t"+i+">Toggle</a></td></tr>";}'+
                    'document.write(str) ;'+
                    '</script>'+
                    '</table></td></tr></table>'+
                    'This is HTTP request #<script>document.write(REQ)</script></BODY></HTML>';

var    getRequest  : array[15] of byte; // HTTP request buffer
       dyna        : array[30] of byte; // buffer for dynamic response
       httpCounter : word ;             // counter of HTTP requests
       txt         : string[10];

implementation
{*******************************************
 * user defined functions
 *}

 //-------------- Returns 1 if the given character is a cipher (digit)
function isdigit(character : byte) : byte;
  begin
    result := (character <= '9') and (character >= '0');
  end;

{*
 * this function is called by the library
 * the user accesses to the HTTP request by successive calls to Spi_Ethernet_getByte()
 * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte()
 * the function must return the length in bytes of the HTTP reply, or 0 if nothing to transmit
 *
 * if you don't need to reply to HTTP requests,
 * just define this function with a return(0) as single statement
 *
 *}
function Spi_Ethernet_UserTCP(var remoteHost : array[4] of byte;
                          remotePort, localPort, reqLength : word) : word;
  var
       i   : word ;                   // general purpose integer
       bitMask : byte ;               // for bit mask

  begin
    result := 0;
    if(localPort <> 80) then          // I listen only to web request on port 80
      begin
        result := 0;
        exit;
      end;

    // get 10 first bytes only of the request, the rest does not matter here
    for i := 0 to 10 do
      begin
        getRequest[i] := Spi_Ethernet_getByte() ;
      end;
    getRequest[i] := 0 ;

    i := 0;
    while (httpMethod[i] <> 0) do
      begin
        txt[i] := httpMethod[i];
        i := i + 1;
      end;
   
    if(memcmp(@getRequest, @txt, 5)) then    // only GET method is supported here
      begin
        result := 0 ;
        exit;
      end;

    httpCounter := httpCounter + 1 ;         // one more request done

    if(getRequest[5] = 's') then             // if request path name starts with s, store dynamic data in transmit buffer
      begin
        // the text string replied by this request can be interpreted as javascript statements
        // by browsers

        result := Spi_Ethernet_putConstString(@httpHeader) ;                  // HTTP header
        result := result + Spi_Ethernet_putConstString(@httpMimeTypeScript) ; // with text MIME type

        // add AN2 value to reply
        WordToStr(ADC_Read(2), dyna) ;
        txt := 'var AN2=';
        result := result + Spi_Ethernet_putString(@txt) ;
        result := result + Spi_Ethernet_putString(@dyna) ;
        txt := ';';
        result := result + Spi_Ethernet_putString(@txt) ;

        // add AN3 value to reply
        WordToStr(ADC_Read(3), dyna) ;
        txt := 'var AN3=';
        result := result + Spi_Ethernet_putString(@txt) ;
        result := result + Spi_Ethernet_putString(@dyna) ;
        txt := ';';
        result := result + Spi_Ethernet_putString(@txt) ;

        // add PORTB value (buttons) to reply
        txt := 'var PORTB=';
        result := result + Spi_Ethernet_putString(@txt) ;
        WordToStr(PORTB, dyna) ;
        result := result + Spi_Ethernet_putString(@dyna) ;
        txt := ';';
        result := result + Spi_Ethernet_putString(@txt) ;

        // add PORTD value (LEDs) to reply
        txt := 'var PORTD=';
        result := result + Spi_Ethernet_putString(@txt) ;
        WordToStr(PORTD, dyna) ;
        result := result + Spi_Ethernet_putString(@dyna) ;
        txt := ';';
        result := result + Spi_Ethernet_putString(@txt) ;

        // add HTTP requests counter to reply
        WordToStr(httpCounter, dyna) ;
        txt := 'var REQ=';
        result := result + Spi_Ethernet_putString(@txt) ;
        result := result + Spi_Ethernet_putString(@dyna) ;
        txt := ';';
        result := result + Spi_Ethernet_putString(@txt) ;
      end
    else
      if(getRequest[5] = 't') then                      // if request path name starts with t, toggle PORTD (LED) bit number that comes after
        begin
          bitMask := 0;
          if(isdigit(getRequest[6]) <> 0) then          // if 0 <= bit number <= 9, bits 8 & 9 does not exist but does not matter
            begin
              bitMask := getRequest[6] - '0' ;          // convert ASCII to integer
              bitMask := 1 shl bitMask ;                // create bit mask
              PORTD   := PORTD xor bitMask ;            // toggle PORTD with xor operator
            end;
        end;

    if(result = 0) then                                 // what do to by default
      begin
        result := Spi_Ethernet_putConstString(@httpHeader) ;                // HTTP header
        result := result + Spi_Ethernet_putConstString(@httpMimeTypeHTML) ; // with HTML MIME type
        result := result + Spi_Ethernet_putConstString(@indexPage) ;        // HTML page first part
        result := result + Spi_Ethernet_putConstString(@indexPage2) ;
      end;

  end;

{*
 * this function is called by the library
 * the user accesses to the UDP request by successive calls to Spi_Ethernet_getByte()
 * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte()
 * the function must return the length in bytes of the UDP reply, or 0 if nothing to transmit
 *
 * if you don't need to reply to UDP requests,
 * just define this function with a return(0) as single statement
 *
 *}
function Spi_Ethernet_UserUDP(var remoteHost : array[4] of byte;
                          remotePort, destPort, reqLength : word) : word;
  begin
    result := 0;
    // reply is made of the remote host IP address in human readable format
    byteToStr(remoteHost[0], dyna) ;           // first IP address byte
    dyna[3] := '.' ;
    byteToStr(remoteHost[1], txt) ;            // second
    dyna[4] := txt[0];
    dyna[5] := txt[1];
    dyna[6] := txt[2];
    dyna[7] := '.' ;
    byteToStr(remoteHost[2], txt) ;            // second
    dyna[8] := txt[0];
    dyna[9] := txt[1];
    dyna[10] := txt[2];
    dyna[11] := '.' ;
    byteToStr(remoteHost[3], txt) ;            // second
    dyna[12] := txt[0];
    dyna[13] := txt[1];
    dyna[14] := txt[2];

    dyna[15] := ':' ;                          // add separator

    // then remote host port number
    WordToStr(remotePort, txt) ;
    dyna[16] := txt[0];
    dyna[17] := txt[1];
    dyna[18] := txt[2];
    dyna[19] := txt[3];
    dyna[20] := txt[4];
    dyna[21] := '[' ;
    WordToStr(destPort, txt) ;
    dyna[22] := txt[0];
    dyna[23] := txt[1];
    dyna[24] := txt[2];
    dyna[25] := txt[3];
    dyna[26] := txt[4];
    dyna[27] := ']' ;
    dyna[28] := 0 ;

    // the total length of the request is the length of the dynamic string plus the text of the request
    result := 28 + reqLength ;

    // puts the dynamic string into the transmit buffer
    Spi_Ethernet_putBytes(@dyna, 28) ;


    // then puts the request string converted into upper char into the transmit buffer
    while(reqLength <> 0) do
      begin
        Spi_Ethernet_putByte(Spi_Ethernet_getByte()) ;
        reqLength := reqLength - 1;
      end;

  end;

end.

Kutip



« Edit Terakhir: Juli 31, 2010, 11:51:36 AM oleh purwanto-amm »
Kontak INBOX ke FB
facebook.com/purwanto.apriana

BLOG
purwanto1987.wordpress.com