Feco
Newbie level 6
- Joined
- May 13, 2013
- Messages
- 12
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,371
Hello.
I wanted to make a led design with controllable brightness. The controlling would work via usart ('0' = darkest --- '9' = brightest, simple) It works good, BUT it doesn't receive every command. If I press a button, sometimes it takes 4-5 presses to receive the data, no idea why. Could you please point out what I did wrong in my code?
Thanks.
I wanted to make a led design with controllable brightness. The controlling would work via usart ('0' = darkest --- '9' = brightest, simple) It works good, BUT it doesn't receive every command. If I press a button, sometimes it takes 4-5 presses to receive the data, no idea why. Could you please point out what I did wrong in my code?
Code:
include <stm32f10x.h>
int indata;
int usart_rec(void) {
while(!(USART1->SR &= USART_SR_RXNE));
return USART1->DR & 0x1FF;
}
void usart_snd(int data) {
while(!(USART1->SR & USART_SR_TXE));
USART1->DR = data;
}
int main(void) {
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
AFIO->MAPR = AFIO_MAPR_TIM3_REMAP;
GPIOA->CRH = GPIO_CRH_CNF10_0 | GPIO_CRH_CNF9_1 | GPIO_CRH_MODE9_0;
GPIOC->CRH = GPIO_CRH_CNF8_1 | GPIO_CRH_MODE8_0;
USART1->BRR = SystemCoreClock/115200;
USART1->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
TIM3->PSC = 239;
TIM3->ARR = 1000;
TIM3->CCR3 = 0;
TIM3->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1;
TIM3->CCER = TIM_CCER_CC3E;
TIM3->CR1 = TIM_CR1_CEN;
while(1) {
indata = usart_rec();
TIM3->CCR3 = ((indata-48)*100);
usart_snd(indata);
}
}
Thanks.