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

📄 ledswitch.c

📁 基于lm3s6965的LEDSwitch示例代码,对初学者特别有帮助。
💻 C
字号:
/*----------------------------------------------------------------------------
 *      R T L   T C P N E T   E x a m p l e
 *----------------------------------------------------------------------------
 *      Name:    LEDSWITCH.C
 *      Purpose: Example LED Control Server application
 *      Rev.:    V3.12
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *      Copyright (c) 2004-2007 KEIL - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

#include <RTL.h>
#include <stdio.h>
#include <LM3Sxxxx.H>
#include <string.h>
#include "..\..\..\osram128x64x4.h"

U8 socket_tcp;
U8 socket_udp;

#define PORT_NUM 1001
#define BLINKLED 0x01  // Command for blink the leds on board

/*--------------------------- init ------------------------------------------*/

static void init () {
   /* Add System initialisation code here */

   /* Set the clocking to run from the PLL at 50 MHz */
   SysCtlClockSet (SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |
                   SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ);

   /* Configure the GPIO for the LED. */
   SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOF);
   GPIOPadConfigSet (GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA,
                     GPIO_PIN_TYPE_STD);
   GPIODirModeSet (GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_DIR_MODE_OUT);

   /* Configure UART0 for 115200 baud. */
   SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOA);
   SysCtlPeripheralEnable (SYSCTL_PERIPH_UART0);
   GPIOPinTypeUART (GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
   UARTConfigSet(UART0_BASE, 115200, (UART_CONFIG_WLEN_8 |
                                      UART_CONFIG_STOP_ONE |
                                      UART_CONFIG_PAR_NONE));

   /* Setup and enable the SysTick timer for 100ms. */
   SysTickPeriodSet(SysCtlClockGet() / 10);
   SysTickEnable();
}


/*--------------------------- init_display ----------------------------------*/

static void init_display () {
   /* OLED Module init */

   OSRAM128x64x4Init(1000000);
   OSRAM128x64x4Clear ();
   OSRAM128x64x4StringDraw ("    RL-ARM      ", 20, 20, 11);
   OSRAM128x64x4StringDraw (" LEDsw. example ", 20, 35, 11);
}


/*--------------------------- timer_poll ------------------------------------*/

static void timer_poll () {
   /* System tick timer running in poll mode */

   if ((HWREG (NVIC_ST_CTRL) >> 16) & 1) {
      /* Timer tick every 100 ms */
      timer_tick ();
   }
}


/*--------------------------- fputc -----------------------------------------*/

int fputc (int ch, FILE *f)  {
   /* Debug output to serial port. */

   if (ch == '\n')  {
      UARTCharPut (UART0_BASE, '\r');        /* output CR                    */
   }
   UARTCharPut (UART0_BASE, ch);
   return (ch);
}


/*--------------------------- Process received data  ------------------------*/

void procrec (U8 *buf) {
   switch (buf[0]) {
      case BLINKLED:
         GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_0, buf[1] & 1);
         break;
   }
}
/*--------------------------- UDP socket ------------------------------------*/

U16 udp_callback (U8 soc, U8 *rip, U16 rport, U8 *buf, U16 len) {
   rip  = rip;
   rport= rport;
   len  = len;

   if (soc != socket_udp) {
      /* Check if this is the socket we are connected to */
      return (0);
   }

   procrec(buf);
   return (0);
}

/*--------------------------- TCP socket ------------------------------------*/

U16 tcp_callback (U8 soc, U8 evt, U8 *ptr, U16 par) {
   /* This function is called by the TCP module on TCP event */
   /* Check the 'Net_Config.h' for possible events.          */
   par = par;

   if (soc != socket_tcp) {
      return (0);
   }

   switch (evt) {
      case TCP_EVT_DATA:
         /* TCP data frame has arrived, data is located at *par1, */
         /* data length is par2. Allocate buffer to send reply.   */
         procrec(ptr);
         break;

      case TCP_EVT_CONREQ:
         /* Remote peer requested connect, accept it */
         return (1);

      case TCP_EVT_CONNECT:
         /* The TCP socket is connected */
         return (1);
   }
   return (0);
}


/*--------------------------- main ------------------------------------------*/

int main (void) {
   /* Main Thread of the TcpNet */

   init ();
   init_display ();
   init_TcpNet ();

   /* Initialize UDP Socket and start listening */
   socket_udp = udp_get_socket (0, UDP_OPT_SEND_CS | UDP_OPT_CHK_CS, udp_callback);
   if (socket_udp != 0) {
      udp_open (socket_udp, PORT_NUM);
   }

   /* Initialize TCP Socket and start listening */
   socket_tcp = tcp_get_socket (TCP_TYPE_SERVER, 0, 10, tcp_callback);
   if (socket_tcp != 0) {
      tcp_listen (socket_tcp, PORT_NUM);
   }

   while (1) {
      timer_poll ();
      main_TcpNet ();
   }
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/


⌨️ 快捷键说明

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