STM8S/A Standard Peripherals Firmware Library: main.c Source File

STM8S/A

STM8S_StdPeriph_Examples/CAN/CAN_Networking/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CAN_Networking\main.c
00004   * @author  MCD Application Team
00005   * @version  V2.2.0
00006   * @date     30-September-2014
00007   * @brief   This file contains the main function for CAN networking example.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; 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   * @addtogroup CAN_Networking
00029   * @{
00030   */
00031 
00032 /* Includes ------------------------------------------------------------------*/
00033 #include "main.h"
00034 /* Private defines -----------------------------------------------------------*/
00035 /* Private variables ---------------------------------------------------------*/
00036 uint8_t Key_Pressed_Number = 0x00;
00037 __IO uint8_t Key_status = 0x00;
00038 /* Private function prototypes -----------------------------------------------*/
00039 /* Private functions ---------------------------------------------------------*/
00040 /**
00041   * @brief  Main program.
00042   * @param  None
00043   * @retval None
00044   */  
00045 
00046 void main(void)
00047 {
00048   CAN_TxStatus_TypeDef status = CAN_TxStatus_Failed;
00049   
00050   /* Transmit Parameters */
00051   CAN_Id_TypeDef Tx_IDE = CAN_Id_Standard;
00052   CAN_RTR_TypeDef Tx_RTR = CAN_RTR_Data;
00053   uint8_t Tx_DLC = 0;
00054   uint8_t Tx_Data[8] = {0};
00055   uint32_t Tx_Id = 0;
00056   
00057   /* Clock configuration --------------------------------------*/
00058   CLK_Config();
00059   
00060   /* GPIO Configuration ---------------------------------------*/
00061   GPIO_Config();
00062     
00063   /* Configure LCD mounted on STM8-128 EVAL board -------------*/
00064   LCD_Config();
00065   
00066   /* CAN configuration ----------------------------------------*/
00067   CAN_Config();
00068   
00069   /* Enable Interrupts*/
00070   enableInterrupts();
00071 
00072   /* Infinite loop*/
00073   while(1)
00074   {
00075     while(Key_status != Key_NoPressed)
00076      {
00077     
00078       if(Key_Pressed_Number == 0x0) 
00079       {
00080           Key_Pressed_Number = 0x03;
00081       }
00082       else
00083       {
00084           Key_Pressed_Number--;
00085       }
00086       
00087       /* Sender Display*/
00088       LED_Display(Key_Pressed_Number);
00089       LCD_Display(Key_Pressed_Number);
00090       Delay(TIME);
00091       
00092       /* Transmit Parameters*/
00093       Tx_Id = 0x321;
00094       Tx_IDE = CAN_Id_Standard;
00095       Tx_RTR = CAN_RTR_Data;
00096       Tx_DLC = 1;
00097       Tx_Data[0] = Key_Pressed_Number;
00098       
00099       /* Sender send Frame */
00100       status = CAN_Transmit(Tx_Id,Tx_IDE,Tx_RTR,Tx_DLC,Tx_Data);
00101       
00102       /* while key is not pressed, loop*/
00103       Key_status= Key_NoPressed;
00104    }
00105   }
00106 }
00107 /**
00108   * @brief  Configure system clock to run at 16Mhz
00109   * @param  None
00110   * @retval None
00111   */
00112 void CLK_Config(void)
00113 {
00114   /* Initialization of the clock */
00115   /* Clock divider to HSI/1 */
00116   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00117 }
00118 
00119 /**
00120   * @brief  Configures the CAN mode and filter
00121   * @param  None
00122   * @retval None
00123   */
00124 void CAN_Config(void)
00125 {
00126   CAN_InitStatus_TypeDef status = CAN_InitStatus_Failed;
00127   
00128   /* Filter Parameters */
00129   CAN_FilterNumber_TypeDef CAN_FilterNumber;
00130   FunctionalState CAN_FilterActivation;
00131   CAN_FilterMode_TypeDef CAN_FilterMode;
00132   CAN_FilterScale_TypeDef CAN_FilterScale;
00133   uint8_t CAN_FilterID1;
00134   uint8_t CAN_FilterID2;
00135   uint8_t CAN_FilterID3;
00136   uint8_t CAN_FilterID4;
00137   uint8_t CAN_FilterIDMask1;
00138   uint8_t CAN_FilterIDMask2;
00139   uint8_t CAN_FilterIDMask3;
00140   uint8_t CAN_FilterIDMask4; 
00141   
00142   /* Init Parameters*/
00143   CAN_MasterCtrl_TypeDef CAN_MasterCtrl;
00144   CAN_Mode_TypeDef CAN_Mode;
00145   CAN_SynJumpWidth_TypeDef CAN_SynJumpWidth;
00146   CAN_BitSeg1_TypeDef CAN_BitSeg1;
00147   CAN_BitSeg2_TypeDef CAN_BitSeg2;
00148   uint8_t CAN_Prescaler;  
00149   
00150   /* CAN register init */
00151   CAN_DeInit();
00152     
00153   /* CAN  init */
00154   CAN_MasterCtrl=CAN_MasterCtrl_AllDisabled;
00155   CAN_Mode = CAN_Mode_Normal;
00156   CAN_SynJumpWidth = CAN_SynJumpWidth_1TimeQuantum;
00157   CAN_BitSeg1 = CAN_BitSeg1_8TimeQuantum;
00158   CAN_BitSeg2 = CAN_BitSeg2_7TimeQuantum;
00159   CAN_Prescaler = 1;
00160   status = CAN_Init(CAN_MasterCtrl, CAN_Mode, CAN_SynJumpWidth, CAN_BitSeg1, \
00161                     CAN_BitSeg2, CAN_Prescaler);
00162 
00163   /* CAN filter init */
00164   CAN_FilterNumber = CAN_FilterNumber_0;
00165   CAN_FilterActivation = ENABLE;
00166   CAN_FilterMode = CAN_FilterMode_IdMask;
00167   CAN_FilterScale = CAN_FilterScale_32Bit;
00168   CAN_FilterID1=0;  
00169   CAN_FilterID2=0;
00170   CAN_FilterID3=0;
00171   CAN_FilterID4=0;
00172   CAN_FilterIDMask1=0;
00173   CAN_FilterIDMask2=0;
00174   CAN_FilterIDMask3=0;
00175   CAN_FilterIDMask4=0;  
00176   CAN_FilterInit(CAN_FilterNumber, CAN_FilterActivation, CAN_FilterMode, 
00177                  CAN_FilterScale,CAN_FilterID1, CAN_FilterID2, CAN_FilterID3,
00178                  CAN_FilterID4,CAN_FilterIDMask1, CAN_FilterIDMask2, 
00179                  CAN_FilterIDMask3, CAN_FilterIDMask4);
00180         
00181   /* Enable Fifo message pending interrupt*/
00182   /* Message reception is done by CAN_RX ISR*/
00183   CAN_ITConfig(CAN_IT_FMP, ENABLE);
00184 }
00185 
00186 /**
00187   * @brief  Configure GPIO for LEDs and buttons available on the evaluation board
00188   * @param  None
00189   * @retval None
00190   */
00191 void GPIO_Config(void)
00192 {
00193   /* Initialize LEDs mounted on STM8-128 EVAL board */
00194     STM_EVAL_LEDInit(LED1);
00195     STM_EVAL_LEDInit(LED2);
00196     STM_EVAL_LEDInit(LED3);
00197     STM_EVAL_LEDInit(LED4);
00198     
00199     /* Initialize KEY push-button mounted on STM8-128 EVAL board */
00200     STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
00201   
00202   /* Switch LEDs Off */
00203     STM_EVAL_LEDOff(LED1);
00204     STM_EVAL_LEDOff(LED2);
00205     STM_EVAL_LEDOff(LED3);
00206     STM_EVAL_LEDOff(LED4);
00207 }
00208 
00209 /**
00210   * @brief  Configure the LCD available on the evaluation board
00211   * @param  None
00212   * @retval None
00213   */
00214 void LCD_Config(void)
00215 {
00216     /* Init the Eval board LCD */
00217     STM8S_EVAL_LCD_Init();
00218 
00219     /* Clear LCD */
00220     LCD_Clear();
00221     
00222     /* Display the welcome text on LCD */
00223     LCD_SetCursorPos(LCD_LINE1, 0);
00224     LCD_Print("   STM8S CAN    ");
00225     /* Print "Press Key" on LCD line2*/
00226     LCD_SetCursorPos(LCD_LINE2, 0);
00227     LCD_Print("   Press Key    ");
00228 }
00229 
00230 /**
00231   * @brief Display led status 
00232   * @param Ledstatus the led to set ON, other leds are set OFF
00233   * @retval None
00234   */
00235 void LED_Display(uint8_t Ledstatus)
00236 {
00237   /* Switch LEDs Off */
00238     STM_EVAL_LEDOff(LED1);
00239     STM_EVAL_LEDOff(LED2);
00240     STM_EVAL_LEDOff(LED3);
00241     STM_EVAL_LEDOff(LED4);
00242 
00243  /* Set the led ON depending on the value of Ledstatus*/
00244  switch((uint8_t)Ledstatus)
00245  {
00246    case(0): STM_EVAL_LEDOn(LED4);
00247             break;
00248    
00249    case(1): STM_EVAL_LEDOn(LED3);
00250             break;
00251  
00252    case(2): STM_EVAL_LEDOn(LED2);
00253             break;
00254 
00255    case(3): STM_EVAL_LEDOn(LED1);
00256             break;
00257    }
00258 }
00259 
00260 /**
00261   * @brief Display led status and communication direction on the LCD 
00262   * @param Ledstatus the led status
00263   * @retval None
00264   */
00265 void LCD_Display(uint8_t Ledstatus)
00266 {
00267   LCD_SetCursorPos(LCD_LINE2, 0);
00268     
00269   switch((uint8_t)Ledstatus)
00270  {
00271    case(0): LCD_Print("Sender   LD4 ON "); 
00272             break;
00273    
00274    case(1): LCD_Print("Sender   LD3 ON ");
00275             break;
00276  
00277    case(2): LCD_Print("Sender   LD2 ON ");
00278             break;
00279 
00280    case(3): LCD_Print("Sender   LD1 ON ");
00281             break;
00282             
00283    case(4): LCD_Print("Receiver LD4 ON "); 
00284             break;
00285    
00286    case(5): LCD_Print("Receiver LD3 ON ");
00287             break;
00288  
00289    case(6): LCD_Print("Receiver LD2 ON ");
00290             break;
00291 
00292    case(7): LCD_Print("Receiver LD1 ON ");
00293             break;
00294   }
00295 }
00296 
00297 /**
00298   * @brief Generates a delay time.
00299   * @param nCount the value for looping.
00300   * @retval None
00301   */
00302 void Delay (uint16_t nCount)
00303 {
00304   /* Decrement nCount value */
00305   for (; nCount != 0; nCount--);
00306 }
00307 
00308 #ifdef USE_FULL_ASSERT
00309 /**
00310   * @brief  Reports the name of the source file and the source line number
00311   *   where the assert_param error has occurred.
00312   * @param file: pointer to the source file name
00313   * @param line: assert_param error line source number
00314   * @retval None
00315   */
00316 void assert_failed(uint8_t* file, uint32_t line)
00317 { 
00318   /* User can add his own implementation to report the file name and line number,
00319      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00320 
00321   /* Infinite loop */
00322   while (1)
00323   {
00324   }
00325 }
00326 #endif
00327 
00328 /**
00329   * @}
00330   */
00331 
00332 
00333 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com