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

📄 select_uart_settings.c

📁 reference about wireless design which is helpful to everyone
💻 C
字号:
#include "cc2511_app_ex_lib_headers.h"
#include "RF04EB.h"
#include "lcd.h"
#include "select_uart_settings.h"

const __code char susSelections[2][16] = {{'S', 'e', 'l', 'e', 'c', 't', ' ', 'B', 'a', 'u', 'd', 'r', 'a', 't', 'e', ' ' },
                                          {'S', 'e', 'l', 'e', 'c', 't', ' ', 'S', 't', 'o', 'p', ' ', 'B', 'i', 't', 's' }};

char __code const susBaudRate[5][16] = {{'1', '1', '5', '2', '0', '0', ' ', '(', 'J', 'o', 'y', '/', ' ', 'S', '1', ')'},
                                        {' ', '5', '7', '6', '0', '0', ' ', '(', 'J', 'o', 'y', '/', ' ', 'S', '1', ')'},
                                        {' ', '3', '8', '4', '0', '0', ' ', '(', 'J', 'o', 'y', '/', ' ', 'S', '1', ')'},
                                        {' ', '1', '9', '2', '0', '0', ' ', '(', 'J', 'o', 'y', '/', ' ', 'S', '1', ')'},
                                        {' ', ' ', '9', '6', '0', '0', ' ', '(', 'J', 'o', 'y', '/', ' ', 'S', '1', ')'}};

char __code const susStopBit[2][16] = {{'O', 'n', 'e', ' ', 'S', 't', 'o', 'p', ' ', 'B', 'i', 't', ' ', ' ', ' ', ' '},
                                       {'T', 'w', 'o', ' ', 'S', 't', 'o', 'p', ' ', 'B', 'i', 't', ' ', ' ', ' ', ' '}};


//Menu on the LCD screen let the user select baudrate on the UART
void selectUartSettings(void)
{
   char finalText[16];
   BYTE i;
   JOYSTICK_DIRECTION prevJoyDir;
   JOYSTICK_DIRECTION lastThreeJoyDir[3];
   BYTE baudRate;
   BYTE stopBit;
   BYTE selection;

   INIT_JOYSTICK();
   baudRate = 0;
   stopBit = 0;
   selection = 0;

   lcdUpdate((__xdata char *) &susSelections[0][0], (__xdata char *) &susBaudRate[0][0]);

   //choose baudrate
   while(baudRate == 0)
   {
      lastThreeJoyDir[2] = lastThreeJoyDir[1];//Joystick is analog and may be unstable, want three equal before excepting
      lastThreeJoyDir[1] = lastThreeJoyDir[0];
      lastThreeJoyDir[0] = getJoystickDirection();

      if((lastThreeJoyDir[2] == lastThreeJoyDir[1]) && (lastThreeJoyDir[1] == lastThreeJoyDir[0]))
      {
         switch (lastThreeJoyDir[0])
         {
         case UP :
            if(prevJoyDir == CENTRED)
            {
               if(selection > 0) { selection--; }
               else { selection = 4; }
               lcdUpdateLine(LINE2, (__xdata char *) &susBaudRate[selection][0]);
            }
            prevJoyDir = UP;
            break;

         case DOWN :
            if(prevJoyDir == CENTRED)
            {
               if(selection < 4 ) { selection++; }
               else { selection = 0; }
               lcdUpdateLine(LINE2, (__xdata char *) &susBaudRate[selection][0]);
            }
            prevJoyDir = DOWN;
            break;

         default :
            prevJoyDir = CENTRED;
            break;
         }
      }
      if(buttonPushed())
      {
         for(i = 0; i < 6; i++)
         {
            finalText[i] = susBaudRate[selection][i];
         }
         baudRate = selection + 1;
      }
      halWait(1);
   }


   lcdUpdate((__xdata char *) &susSelections[1][0], (__xdata char *) &susStopBit[0][0]);
   selection = 0;

   //choose one or two stop bits
   while(stopBit == 0)
   {
      lastThreeJoyDir[2] = lastThreeJoyDir[1];//Joystick is analog and may be unstable, want three equal before excepting
      lastThreeJoyDir[1] = lastThreeJoyDir[0];
      lastThreeJoyDir[0] = getJoystickDirection();

      if((lastThreeJoyDir[2] == lastThreeJoyDir[1]) && (lastThreeJoyDir[1] == lastThreeJoyDir[0]))
      {
         switch (lastThreeJoyDir[0])
         {
         case UP :
         case DOWN :
            if(prevJoyDir == CENTRED)
            {
               if(selection == 0) { selection = 1; }
               else { selection = 0; }
               lcdUpdateLine(LINE2, (__xdata char *) &susStopBit[selection][0]);
            }
            prevJoyDir = UP;
            break;

         default :
            prevJoyDir = CENTRED;
            break;
         }
      }
      if(buttonPushed())
      {
         finalText[6] = '-';
         if(selection == 0) { finalText[7] = '1'; }
         else               { finalText[7] = '2'; }
         stopBit = selection + 1;
      }
      halWait(1);
   }

   //write result to LCD
   for(i = 8; i < 16; i++) { finalText[i] = ' '; }
   lcdUpdateLine(LINE1, finalText);
   //setup UART
   switch (baudRate)
   {
   case 1:
      if(stopBit == 1){ UART_SETUP(0,115200 ,ONE_STOP_BITS); }
      else            { UART_SETUP(0,115200 ,TWO_STOP_BITS); }
      break;
   case 2:
      if(stopBit == 1){ UART_SETUP(0,57600 ,ONE_STOP_BITS); }
      else            { UART_SETUP(0,57600 ,TWO_STOP_BITS); }
      break;
   case 3:
      if(stopBit == 1){ UART_SETUP(0,38400 ,ONE_STOP_BITS); }
      else            { UART_SETUP(0,38400 ,TWO_STOP_BITS); }
      break;
   case 4:
      if(stopBit == 1){ UART_SETUP(0,19200 ,ONE_STOP_BITS); }
      else            { UART_SETUP(0,19200 ,TWO_STOP_BITS); }
      break;
   case 5:
      if(stopBit == 1){ UART_SETUP(0,9600 ,ONE_STOP_BITS); }
      else            { UART_SETUP(0,9600 ,TWO_STOP_BITS); }
      break;
   }
}

⌨️ 快捷键说明

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