⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 实现了C8051F340单片机依据SMTP协议传输数据
💻 C
字号:
//------------------------------------------------------------------------------
// main.c
//------------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// Date: 05/22/06 16:39:45
// Target: C8051F34x 
// Version: 1.31
//
// Description:
//
//    This file is an example SMTP client.
//    It runs on a C8051F340 connected to a CP2200 via AB4 board.
// 
//    This file contains the main routine, MCU initialization code, and 
//    callback functions used by the TCP/IP Library.
//
// Steps for running this example code:
//
//
//    1. DNS can be used to resolve the name of a mail server to an IP address.
//       To use DNS, set USE_DNS to 1.
//
//    2. DHCP can be used to obtain an IP address and the IP address of a DNS
//       server. To use DHCP, set USE_DHCP to 1.
//
//    3. If DHCP is not used, replace IP_SRC_ADDR in mn_userconst.h with the
//       desired IP address.
//
//    4. If DNS is not used, replace IP_SMTP_ADDR in mn_userconst.h with the
//       address of the SMTP server.
//
//    5. If DNS is used, write the name of the DNS server to mail_server[] in
//       main.c.
//
//    6. Replace the value in to[] with the email address to which the email will
//       be sent.
//
//    7. Connect to the C8051F340.
//
//    8. Compile and build the project code.
//
//    9. Download the code to the C8051F340 and press "Go".
//
//    10. The Ethernet controller will initialize and enter the network with
//       either the IP address specified in IP_SRC_ADDR or the IP address
//       obtained via DHCP.
//
//    11. If USE_DNS is set, the C8051F340 will resolve mail_server[] to an
//       IP address.
//
//    12. The C8051F340 will send two emails to email address to[].
//       One email includes an attachment.
//
//    13. After the emails are sent, mn_server() is called so the user may ping
//       the C8051F340.
//

#include "mn_userconst.h"                      // TCP/IP Library Constants
#include "mn_stackconst.h"                     // TCP/IP Library Constants
#include "mn_errs.h"                           // Library Error Codes
#include "mn_defs.h"                           // Library Type definitions
#include "mn_funcs.h"                          // Library Function Prototypes
#include <c8051F340.h>                         // Device-specific SFR Definitions


//------------------------------------------------------------------------------
// Define Statements
//------------------------------------------------------------------------------
#define  USE_DHCP    1     // If set to 1, the program will use DHCP to acquire
                           // its IP address and the IP address of the
                           // DNS server.

#define  USE_DNS     1       // If set to 1, the program will use DNS to resolve
                           // a named SMTP server to an IP address.


//------------------------------------------------------------------------------
// Function Prototypes
//------------------------------------------------------------------------------

// Initialization Routines
void PORT_Init (void);
void SYSCLK_Init (void);
void EMIF_Init(void);
int establish_network_connection();

// Application Functions
void SMTP(void);

//-----------------------------------------------------------------------------
// Variable Declarations
//-----------------------------------------------------------------------------
byte from[]      = "ETHERNET-DK@domain.com";
byte to[]        = "username@domain.com";
byte subject[]   = "SMTP test";
byte message[]   = "Email sent from C8051F340 using ETHERNET-DK.\r\n";
byte attach[]    = "This is the attachment.\r\n";
byte fname[]     = "ETHERNET-DK.txt";

#if USE_DNS
// This is the name of the mail server. It must be null-terminated.
byte mail_server[] = { 0x03, 'w', 'w', 'w', 0x06, 's', 'i', 'l', 'a', 'b', 's',
                       0x03, 'c', 'o', 'm', 0x00 };

#if !USE_DHCP
// If DNS is not used, the IP address of the DNS server should be placed here.
// It must be null-terminated.
const byte ip_dns_addr_init[] = { 255, 255, 255, 255 };

#endif

#endif

//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)
{
   int retval;

   // Disable watchdog timer
   PCA0MD = 0x00;

   // Initialize the MCU
   PORT_Init();
   SYSCLK_Init();
   EMIF_Init();

   while(1)
   {
      // Initialize the TCP/IP stack.
      if (mn_init() < 0)
      {
         // If code execution enters this while(1) loop, the stack failed to initialize.
         // Verify that all boards are connected and powered properly.
         while(1);
      }

      // Connect to the network
      establish_network_connection();

#if USE_DHCP

      // Use DHCP to obtain an IP address.
      if (mn_dhcp_start(PTR_NULL, dhcp_default_lease_time) <= 0)
      {
         // DHCP Error
         // The DHCP server did not assign a valid IP address.
         while(1);

      }

#else

      // If DHCP is not used, override the DHCP settings to use a static address
      dhcp_lease.dhcp_state = DHCP_OK;    // Set DHCP state to OK
      dhcp_lease.infinite_lease = 1;      // Use infinite lease time -
                                          //   will not attempt to re-lease

#endif

      // Send the email via SMTP
      SMTP();

      // Start the Application Layer Services
      // If this routine exits, check the return value for an error code.
      retval = mn_server();

   }
}

//-----------------------------------------------------------------------------
// establish_network_connection
//-----------------------------------------------------------------------------
//
// This function calls mn_ether_init() to initialize the CP2200 and attach to
// the network.
//
// If there is a network connection, the function returns 1.
//
// In the call to mn_ether_init(), NUM_AUTONEG_ATTEMPTS is set to 0, so the
// function will not return until it successfully auto-negotiates.
//
// mn_ether_init() will not be a blocking call if NUM_AUTONEG_ATTEMPTS is set
// to a value greater than 0.
//
int establish_network_connection()
{
   int retval;

   do
   {
      // mn_ether_init() initializes the Ethernet controller.
      // AUTO_NEG indicates that the controller will auto-negotiate.
      retval = mn_ether_init(AUTO_NEG, 0, 0);

      // If there is no link, poll link_status until it sets or the
      // CP2200 resets and then call mn_ether_init() again.
      if (retval == LINK_FAIL)
      {
         while(!link_status && !ether_reset);
      }

      // If retval is less than zero and is not LINK_FAIL, there is a 
      // hardware error.
      else if (retval < 0)
      {
         // Verify that the Ethernet controller is connected and powered properly.
         // Verity that the EMIF has been configured at a speed compatible with the
         //    Ethernet controller.
         while(1);
      }

   }while(retval < 0);

   return (1);

}

//-----------------------------------------------------------------------------
// Application Functions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// SMTP
//-----------------------------------------------------------------------------
//
// This function transmits an email using the SMTP protocol.
//
void SMTP(void)
{
   SCHAR socket_no;
   int retval;
   SMTP_INFO_T mail_info;

#if USE_DNS

#if !USE_DHCP
   // If DHCP is used, then the IP address of the DNS server was received
   // from the DHCP server.
   // Otherwise, manually write the address of the DNS server to ip_dns_addr.
   ip_dns_addr[0] = ip_dns_addr_init[0];
   ip_dns_addr[1] = ip_dns_addr_init[1];
   ip_dns_addr[2] = ip_dns_addr_init[2];
   ip_dns_addr[3] = ip_dns_addr_init[3];

#endif

   // Use DNS to obtain the IP address of the mail server
   if(mn_dns_get_addr(&mail_server, ip_smtp_addr) < 0)
      return;

#endif

   socket_no = mn_smtp_start_session(DEFAULT_PORT);

   if (socket_no >= 0)
   {
      // send a message with an attachment
      mail_info.from = from;
      mail_info.to = to;
      mail_info.subject = subject;
      mail_info.message = message;
      mail_info.attachment = attach;
      mail_info.filename = fname;

      retval = mn_smtp_send_mail(socket_no, &mail_info);

      if (retval > 0)
      {
         // send a message without an attachment
         mail_info.from = from;
         mail_info.to = to;
         mail_info.subject = subject;
         mail_info.message = message;
         mail_info.attachment = PTR_NULL;
         mail_info.filename = PTR_NULL;

         retval = mn_smtp_send_mail(socket_no, &mail_info);
      }

      mn_smtp_end_session(socket_no);

      if (retval < 0)
      {
         // There was an error sending the message
         // If the email arrived at its destination but mn_smtp_send_mail
         // returned SMTP_ERROR (-103), it is possible that we did not wait
         // for the response from the SMTP server long enough.
         // Increase socket_wait_ticks in mn_userconst.h to wait longer.
         ;
      }

   }
}


//-----------------------------------------------------------------------------
// Initialization Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Interrupts, Crossbar and GPIO ports
//
void PORT_Init (void)
{

   IT01CF = 0x03;                      // Enable Interrupt 0 on P0.3
   TCON &= ~0x01;                      // Make /INT0 level triggered

   XBR0    = 0x01;                     // Enable UART on P0.4(TX) and P0.5(RX)
   XBR1    = 0x40;                     // Enable crossbar and enable
                                       // weak pull-ups

   P0MDOUT |= 0x10;                    // enable UTX as push-pull output
   P1MDOUT |= 0xD8;                    // /WR and /RD are push-pull
                                       // AB4 LEDs are push-pull
   P2MDOUT |= 0xFF;
   P3MDOUT |= 0xFF;
   P4MDOUT |= 0xFF;

}

//-----------------------------------------------------------------------------
// EMIF_Init
//-----------------------------------------------------------------------------
//
// Configure the External Memory Interface for both on and off-chip access.
//
void EMIF_Init (void)
{

   EMI0CF = 0x1B;             // non-muxed mode; split mode
                              // with bank select

   EMI0TC = EMIF_TIMING;      // This constant may be modified
                              // according to SYSCLK to meet the
                              // timing requirements for the CP2200

   EMI0CN = BASE_ADDRESS;     // Page of XRAM accessed by EMIF

}

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock.
//
void SYSCLK_Init (void)
{
   int i;

   OSCICN |= 0x03;                     // Configure internal oscillator for
                                       // its maximum frequency
  
   CLKMUL = 0x00;                      // Reset Clock Multiplier and select
                                       // internal oscillator as input source

   CLKMUL |= 0x80;                     // Enable the Clock Multiplier

   for(i = 0; i < 256; i++);           // Delay at least 5us
   
   CLKMUL |= 0xC0;                     // Initialize the Clock Multiplier
   
   while(!(CLKMUL & 0x20));            // Wait for MULRDY => 1
   
   RSTSRC = 0x06;                      // Enable missing clock detector
                                       // and VDD monitor
   
   FLSCL |= 0x10;                      // Set Flash Scale for 48MHz
   
   CLKSEL |= 0x03;                     // Select output of clock multiplier
                                       // as the system clock.

}

//-----------------------------------------------------------------------------
// ether_reset_low
//-----------------------------------------------------------------------------
//
// This routine drives the reset pin of the ethernet controller low.
//
void ether_reset_low()
{

   P1 &= ~0x01;               // Pull reset low

}

//-----------------------------------------------------------------------------
// ether_reset_high
//-----------------------------------------------------------------------------
//
// This routine places the reset pin in High-Z allowing it to be pulled up 
// using the external pull-up resistor.
//
// Additionally, this routine waits for the reset pin to read high before
// exiting.
//
void ether_reset_high (void)
{

   P1 |= 0x01;               // Allow /RST to rise
   while(!(P1 & 0x01));      // Wait for /RST to go high


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -