Guest User

Untitled

a guest
Dec 15th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include "stm8s.h"
  2. #include "stm8s_uart1.h"
  3. #include "stm8s_gpio.h"
  4. #include "stdio.h"
  5. #include "stm8s_tim4.h"
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. #define PUTCHAR_PROTOTYPE char putchar (char c)
  10. #define GETCHAR_PROTOTYPE char getchar (void)
  11.  
  12. void putchars(const char* chars, size_t len);
  13.  
  14. uint32_t us_val = 0;
  15.  
  16. void print_u16(uint16_t a){
  17.     char c[5];
  18.     c[4] = '\0';
  19.     c[3] = (a % 10) + '0'; a/=10;
  20.     c[2] = (a % 10) + '0'; a/=10;
  21.     c[1] = (a % 10) + '0'; a/=10;
  22.     c[0] = (a % 10) + '0';
  23.     putchars(c, 5);
  24. }
  25.  
  26. void uDelay(uint32_t us)
  27. {
  28.     us_val = us;
  29.     while(us_val != 0);
  30. }
  31.  
  32. void decrement_timer(){
  33.     if (us_val != 0)
  34.   {
  35.     --us_val;
  36.   }
  37. }
  38.  
  39. void tim4_conf(){
  40.   TIM4_DeInit();
  41.     TIM4_TimeBaseInit(TIM4_PRESCALER_16, 1);
  42.     TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  43.     enableInterrupts();
  44.     TIM4_Cmd(ENABLE);
  45. }
  46.  
  47. void main(void) {
  48.  
  49.     char ans;
  50.     /*High speed internal clock prescaler: 1*/
  51.     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  52.        
  53.     UART1_DeInit();
  54.  
  55.     UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
  56.                             UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
  57.                            
  58.     tim4_conf();
  59.                            
  60.        
  61.     while (1)
  62.     {
  63.        
  64.         putchars("hello", 5);
  65.         putchar('\n');
  66.         uDelay(1000000);
  67.     }
  68.    
  69. }
  70.  
  71. /**
  72.   * @brief Retargets the C library printf function to the UART.
  73.   * @param c Character to send
  74.   * @retval char Character sent
  75.   */
  76. PUTCHAR_PROTOTYPE
  77. {
  78.   /* Write a character to the UART1 */
  79.   UART1_SendData8(c);
  80.   /* Loop until the end of transmission */
  81.   while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
  82.  
  83.   return (c);
  84. }
  85.  
  86. void putchars(const char* chars, size_t len){
  87.     uint8_t i;
  88.     for(i = 0; i < len; ++i){
  89.         putchar(chars[i]);
  90.     }
  91. }
  92.  
  93. /**
  94.   * @brief Retargets the C library scanf function to the USART.
  95.   * @param None
  96.   * @retval char Character to Read
  97.   */
  98. GETCHAR_PROTOTYPE
  99. {
  100. #ifdef _COSMIC_
  101.   char c = 0;
  102. #else
  103.   int c = 0;
  104. #endif
  105.   /* Loop until the Read data register flag is SET */
  106.   while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
  107.     c = UART1_ReceiveData8();
  108.   return (c);
  109. }
  110.  
  111. void assert_failed(u8* file, u32 line) {
  112.     /* User can add his own implementation to report the file name and line number,
  113.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  114.     /* Infinite loop */
  115.     while (1) {
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment