Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm8s.h"
- #include "stm8s_uart1.h"
- #include "stm8s_gpio.h"
- #include "stdio.h"
- #include "stm8s_tim4.h"
- #include <string.h>
- #include <stdlib.h>
- #define PUTCHAR_PROTOTYPE char putchar (char c)
- #define GETCHAR_PROTOTYPE char getchar (void)
- void putchars(const char* chars, size_t len);
- uint32_t us_val = 0;
- void print_u16(uint16_t a){
- char c[5];
- c[4] = '\0';
- c[3] = (a % 10) + '0'; a/=10;
- c[2] = (a % 10) + '0'; a/=10;
- c[1] = (a % 10) + '0'; a/=10;
- c[0] = (a % 10) + '0';
- putchars(c, 5);
- }
- void uDelay(uint32_t us)
- {
- us_val = us;
- while(us_val != 0);
- }
- void decrement_timer(){
- if (us_val != 0)
- {
- --us_val;
- }
- }
- void tim4_conf(){
- TIM4_DeInit();
- TIM4_TimeBaseInit(TIM4_PRESCALER_16, 1);
- TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
- enableInterrupts();
- TIM4_Cmd(ENABLE);
- }
- void main(void) {
- char ans;
- /*High speed internal clock prescaler: 1*/
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
- UART1_DeInit();
- UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
- UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
- tim4_conf();
- while (1)
- {
- putchars("hello", 5);
- putchar('\n');
- uDelay(1000000);
- }
- }
- /**
- * @brief Retargets the C library printf function to the UART.
- * @param c Character to send
- * @retval char Character sent
- */
- PUTCHAR_PROTOTYPE
- {
- /* Write a character to the UART1 */
- UART1_SendData8(c);
- /* Loop until the end of transmission */
- while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
- return (c);
- }
- void putchars(const char* chars, size_t len){
- uint8_t i;
- for(i = 0; i < len; ++i){
- putchar(chars[i]);
- }
- }
- /**
- * @brief Retargets the C library scanf function to the USART.
- * @param None
- * @retval char Character to Read
- */
- GETCHAR_PROTOTYPE
- {
- #ifdef _COSMIC_
- char c = 0;
- #else
- int c = 0;
- #endif
- /* Loop until the Read data register flag is SET */
- while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
- c = UART1_ReceiveData8();
- return (c);
- }
- void assert_failed(u8* file, u32 line) {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment