STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/UART1/UART1_IrDA/Transmit/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file UART1_IrDA\Transmit\main.c 00004 * @brief This file contains the main function for UART1 in IrDA transmit mode. 00005 * @author MCD Application Team 00006 * @version V2.2.0 00007 * @date 30-September-2014 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "stm8s.h" 00030 #include "stm8s_eval.h" 00031 00032 /** 00033 * @addtogroup UART1_IrDA_Transmit 00034 * @{ 00035 */ 00036 00037 /* Private typedef -----------------------------------------------------------*/ 00038 /* Private define ------------------------------------------------------------*/ 00039 00040 /* Evalboard I/Os configuration */ 00041 #define Bit_RESET 0 00042 #define Bit_SET 1 00043 /* Private macro -------------------------------------------------------------*/ 00044 /* Private variables ---------------------------------------------------------*/ 00045 /* Private function prototypes -----------------------------------------------*/ 00046 static void GPIO_Config(void); 00047 static void UART1_Config(void); 00048 JOYState_TypeDef ReadJoystick(void); 00049 /* Private functions ---------------------------------------------------------*/ 00050 00051 /** 00052 * @brief Main program. 00053 * @param None 00054 * @retval None 00055 */ 00056 void main(void) 00057 { 00058 00059 JOYState_TypeDef Key = JOY_NONE; 00060 00061 /* GPIO configuration -----------------------------------------*/ 00062 GPIO_Config(); 00063 00064 /* UART1 configuration -----------------------------------------*/ 00065 UART1_Config(); 00066 00067 while (1) 00068 { 00069 /* Read Key */ 00070 while (Key == JOY_NONE) 00071 { 00072 Key = (JOYState_TypeDef)ReadJoystick(); 00073 } 00074 00075 switch (Key) 00076 { 00077 case JOY_UP: 00078 UART1_SendData8(JOY_UP); 00079 while (UART1_GetFlagStatus(UART1_FLAG_TC) == RESET) 00080 {} 00081 Key = JOY_NONE; 00082 break; 00083 case JOY_DOWN: 00084 UART1_SendData8(JOY_DOWN); 00085 while (UART1_GetFlagStatus(UART1_FLAG_TC) == RESET) 00086 {} 00087 Key = JOY_NONE; 00088 break; 00089 case JOY_LEFT: 00090 UART1_SendData8(JOY_LEFT); 00091 while (UART1_GetFlagStatus(UART1_FLAG_TC) == RESET) 00092 {} 00093 Key = JOY_NONE; 00094 break; 00095 case JOY_RIGHT: 00096 UART1_SendData8(JOY_RIGHT); 00097 while (UART1_GetFlagStatus(UART1_FLAG_TC) == RESET) 00098 {} 00099 Key = JOY_NONE; 00100 break; 00101 case JOY_SEL: 00102 UART1_SendData8(JOY_SEL); 00103 while (UART1_GetFlagStatus(UART1_FLAG_TC) == RESET) 00104 {} 00105 Key = JOY_NONE; 00106 break; 00107 default: 00108 break; 00109 } 00110 } 00111 00112 } 00113 00114 /** 00115 * @brief Configures the Multiplexer on the evaluation board to select the IrDA 00116 * @param None 00117 * @retval None 00118 */ 00119 static void GPIO_Config(void) 00120 { 00121 /* Configures the Multiplexer on the evaluation board to select the IrDA */ 00122 /* Initialize I/Os in Output PP Mode */ 00123 GPIO_Init(GPIOF, GPIO_PIN_6 ,GPIO_MODE_OUT_PP_LOW_FAST); 00124 GPIO_Init(GPIOF, GPIO_PIN_5 ,GPIO_MODE_OUT_PP_HIGH_FAST); 00125 } 00126 00127 /** 00128 * @brief Configure UART1 for the communication with IrDA 00129 * @param None 00130 * @retval None 00131 */ 00132 static void UART1_Config(void) 00133 { 00134 UART1_DeInit(); 00135 00136 /* UART1 configured as follow: 00137 - Word Length = 8 Bits 00138 - One Stop Bit 00139 - No parity 00140 - BaudRate = 9600 baud 00141 - Tx and Rx enabled 00142 - UART1 Clock disabled 00143 */ 00144 UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, 00145 UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE); 00146 00147 /* Set Prescaler*/ 00148 UART1_SetPrescaler(0x1); 00149 00150 UART1_IrDAConfig(UART1_IRDAMODE_NORMAL); 00151 00152 UART1_IrDACmd(ENABLE); 00153 } 00154 00155 /** 00156 * @brief Reads joystick position from evalboard. 00157 * @param None. 00158 * @retval Joystick position. 00159 * This parameter can be a value of @ref JOYState_TypeDef enumeration. 00160 * @par Required preconditions: 00161 * None 00162 */ 00163 JOYState_TypeDef ReadJoystick(void) 00164 { 00165 /* "right" key is pressed */ 00166 if (!STM_EVAL_PBGetState(BUTTON_RIGHT)) 00167 { 00168 while (STM_EVAL_PBGetState(BUTTON_RIGHT) == Bit_RESET); 00169 return JOY_RIGHT; 00170 } 00171 /* "left" key is pressed */ 00172 if (!STM_EVAL_PBGetState(BUTTON_LEFT)) 00173 { 00174 while (STM_EVAL_PBGetState(BUTTON_LEFT) == Bit_RESET); 00175 return JOY_LEFT; 00176 } 00177 /* "up" key is pressed */ 00178 if (!STM_EVAL_PBGetState(BUTTON_UP)) 00179 { 00180 while (STM_EVAL_PBGetState(BUTTON_UP) == Bit_RESET); 00181 return JOY_UP; 00182 } 00183 /* "down" key is pressed */ 00184 if (!STM_EVAL_PBGetState(BUTTON_DOWN)) 00185 { 00186 while (STM_EVAL_PBGetState(BUTTON_DOWN) == Bit_RESET); 00187 return JOY_DOWN; 00188 } 00189 /* "sel" key is pressed */ 00190 if (!STM_EVAL_PBGetState(BUTTON_SEL)) 00191 { 00192 while (STM_EVAL_PBGetState(BUTTON_SEL) == Bit_RESET); 00193 return JOY_SEL; 00194 } 00195 /* No key is pressed */ 00196 else 00197 { 00198 return JOY_NONE; 00199 } 00200 } 00201 00202 #ifdef USE_FULL_ASSERT 00203 00204 /** 00205 * @brief Reports the name of the source file and the source line number 00206 * where the assert_param error has occurred. 00207 * @param file: pointer to the source file name 00208 * @param line: assert_param error line source number 00209 * @retval None 00210 */ 00211 void assert_failed(uint8_t* file, uint32_t line) 00212 { 00213 /* User can add his own implementation to report the file name and line number, 00214 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00215 00216 /* Infinite loop */ 00217 while (1) 00218 { 00219 } 00220 } 00221 #endif 00222 /** 00223 * @} 00224 */ 00225 00226 00227 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/