1 | #include "stm8s.h"
|
2 | #include "stdio.h"
|
3 |
|
4 | void Delay (uint16_t nCount);
|
5 |
|
6 | // UART
|
7 | #ifdef _RAISONANCE_
|
8 | #define PUTCHAR_PROTOTYPE int putchar (char c)
|
9 | #define GETCHAR_PROTOTYPE int getchar (void)
|
10 | #elif defined (_COSMIC_)
|
11 | #define PUTCHAR_PROTOTYPE char putchar (char c)
|
12 | #define GETCHAR_PROTOTYPE char getchar (void)
|
13 | #else /* _IAR_ */
|
14 | #define PUTCHAR_PROTOTYPE int putchar (int c)
|
15 | #define GETCHAR_PROTOTYPE int getchar (void)
|
16 | #endif /* _RAISONANCE_ */
|
17 |
|
18 | void main(void)
|
19 | {
|
20 | uint16_t CCR3_Val = 5;
|
21 |
|
22 | // UART definition
|
23 | char ans;
|
24 | CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); // High speed internal clock prescaler: 1
|
25 | UART1_DeInit();
|
26 | /* UART1 configuration ------------------------------------------------------*/
|
27 | /* UART1 configured as follow:
|
28 | - BaudRate = 115200 baud
|
29 | - Word Length = 8 Bits
|
30 | - One Stop Bit
|
31 | - No parity
|
32 | - Receive and transmit enabled
|
33 | - UART1 Clock disabled
|
34 | */
|
35 | UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
|
36 | UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
|
37 |
|
38 |
|
39 |
|
40 | // PWM output
|
41 | TIM2_TimeBaseInit(TIM2_PRESCALER_1, 99); //Time base configuration
|
42 | TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH); //PWM1 Mode configuration: Channel3
|
43 |
|
44 | TIM2_OC3PreloadConfig(ENABLE);
|
45 | TIM2_ARRPreloadConfig(ENABLE);
|
46 | TIM2_Cmd(ENABLE); //TIM2 enable counter
|
47 |
|
48 |
|
49 |
|
50 | // init LED GPIO
|
51 | GPIO_Init(GPIOB, (GPIO_Pin_TypeDef)GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);
|
52 |
|
53 | while (1)
|
54 | {
|
55 | GPIO_WriteReverse(GPIOB, (GPIO_Pin_TypeDef)GPIO_PIN_5); // Toggles LED
|
56 | Delay(0xFFFF);
|
57 |
|
58 | ans = getchar();
|
59 | printf("%c", ans);
|
60 |
|
61 | }
|
62 |
|
63 | }
|
64 | void Delay(uint16_t nCount)
|
65 | {
|
66 | /* Decrement nCount value */
|
67 | while (nCount != 0)
|
68 | {
|
69 | nCount--;
|
70 | }
|
71 | }
|
72 |
|
73 | PUTCHAR_PROTOTYPE
|
74 | {
|
75 | /* Write a character to the UART1 */
|
76 | UART1_SendData8(c);
|
77 | /* Loop until the end of transmission */
|
78 | while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
|
79 |
|
80 | return (c);
|
81 | }
|
82 |
|
83 | /**
|
84 | * @brief Retargets the C library scanf function to the USART.
|
85 | * @param None
|
86 | * @retval char Character to Read
|
87 | */
|
88 | GETCHAR_PROTOTYPE
|
89 | {
|
90 | #ifdef _COSMIC_
|
91 | char c = 0;
|
92 | #else
|
93 | int c = 0;
|
94 | #endif
|
95 | /* Loop until the Read data register flag is SET */
|
96 | while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
|
97 | c = UART1_ReceiveData8();
|
98 | return (c);
|
99 | }
|
100 |
|
101 | #ifdef USE_FULL_ASSERT
|
102 |
|
103 | /**
|
104 | * @brief Reports the name of the source file and the source line number
|
105 | * where the assert_param error has occurred.
|
106 | * @param file: pointer to the source file name
|
107 | * @param line: assert_param error line source number
|
108 | * @retval None
|
109 | */
|
110 | void assert_failed(uint8_t* file, uint32_t line)
|
111 | {
|
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 |
|
115 | /* Infinite loop */
|
116 | while (1)
|
117 | {
|
118 | }
|
119 | }
|
120 | #endif
|
121 |
|
122 | /**
|
123 | * @}
|
124 | */
|
125 |
|
126 |
|
127 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|