Skip to content

Instantly share code, notes, and snippets.

@MarrekNozka
Last active February 5, 2021 08:13
Show Gist options
  • Select an option

  • Save MarrekNozka/7f2860ffe653068faf0a2c07e2728592 to your computer and use it in GitHub Desktop.

Select an option

Save MarrekNozka/7f2860ffe653068faf0a2c07e2728592 to your computer and use it in GitHub Desktop.
#include "stm8s.h"
#include "milis.h"
#include <stdio.h>
#define LED_PORT GPIOC
#define LED_PIN GPIO_PIN_3
#define LED_ON GPIO_WriteHigh(LED_PORT, LED_PIN);
#define LED_OFF GPIO_WriteLow(LED_PORT, LED_PIN);
#define LED_FLIP GPIO_WriteReverse(LED_PORT, LED_PIN);
#define BTN_PORT GPIOE
#define BTN_PIN GPIO_PIN_4
#define BTN_PUSH (GPIO_ReadInputPin(BTN_PORT, BTN_PIN)==RESET)
int8_t speed = 0;
int32_t time = 0;
void setup(void)
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); // taktovat MCU na 16MHz
init_milis();
/*---- GPIO setup ---------*/
GPIO_Init(LED_PORT, LED_PIN, GPIO_MODE_OUT_PP_LOW_SLOW);
/*---- UART1 setup ---------*/
UART1_DeInit(); // smažu starou konfiguraci
UART1_Init((uint32_t) 115200,
UART1_WORDLENGTH_8D,
UART1_STOPBITS_1,
UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE,
UART1_MODE_TXRX_ENABLE);
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE); // povolí přerušení UART1 Rx
enableInterrupts();
UART1_Cmd(DISABLE); // zakáže UART1
UART1_Cmd(ENABLE); // povolí UART1
}
void main(void)
{
setup();
/*------ nekonená smyčka ---*/
while(1) {
if (milis()-time > speed * 100) {
time = milis();
LED_FLIP;
}
}
}
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
char c = UART1_ReceiveData8();
UART1_SendData8(c);
speed++;
}
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#elif defined (_SDCC_) /* SDCC patch: ensure same types as stdio.h */
#if SDCC_VERSION >= 30605 // declaration changed in sdcc 3.6.5 (officially with 3.7.0)
#define PUTCHAR_PROTOTYPE int putchar(int c)
#define GETCHAR_PROTOTYPE int getchar(void)
#else
#define PUTCHAR_PROTOTYPE void putchar(char c)
#define GETCHAR_PROTOTYPE char getchar(void)
#endif
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */
/* *************** Funkce ******************** */
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);
}
/**
* @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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment