void USART_puts(unsigned char *s) { while (*s) { USART_putc(*s); // send character pointed to by s s++; // increase pointer location to the next character } }
//***************************************************************************** // void USART_puts_p (const char *s) { #ifdef USART_INT #else while (pgm_read_byte(s)) { USART_putc (pgm_read_byte(s)); s++; } #endif }
//***************************************************************************** // void USART_puts (char *s) { #ifdef USART_INT #else while (*s) { USART_putc (*s); s++; } #endif }
int main(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); Delay_Config(); GPIO_Config(); UART_Config(); ENC_Config(); USART_Cmd(USART1, ENABLE); TIM_Cmd(TIM3, ENABLE); uint16_t count = 0; TIM3->CNT = 0; count = TIM3->CNT; while(1) { //if(count != TIM3->CNT) { USART_putc(USART1, count&0x00FF); USART_putc(USART1, (count&0xFF00)>>8); USART_putc(USART1, '\n'); count = TIM3->CNT; } GPIO_ToggleBits(GPIOC, GPIO_Pin_13|GPIO_Pin_9); delay_ms(50); /* //Delay(0x7FFFFF); GPIO_ResetBits(GPIOC, GPIO_Pin_13|GPIO_Pin_9); //Delay(0x7FFFFF); delay_ms(500); USART_putc(USART1, TIM3->CNT); USART_putc(USART1, '\n'); */ } }
void USART_puts(USART_TypeDef* USARTx,const char *s){ int i; for(i=0;s[i]!='\0';i++) USART_putc(USARTx,s[i]); }
int main(void) { #if PPM_OUTPUT == TRUE isr_channel_pw[0] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[1] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[2] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[3] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[4] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[5] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[6] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[7] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); #if PPMCH == 12 isr_channel_pw[8] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[9] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[10] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); isr_channel_pw[11] = ((((F_CPU/1000) * 1500)/1000)/TIMER1_PRESCALER); #endif TCCR1A = (1<<COM1B1) | (0<<COM1B0) | (1<<WGM11) | (1<<WGM10); TCCR1B = (1<<WGM13)|(1<<WGM12); OCR1A = RC_RESET_PW_TIMER_VAL; OCR1B = RC_PPM_SYNC_PW_VAL; TIMSK1 |= (1<<OCIE1B)|(1<<TOIE1); TCNT1 = 0; isr_channel_number = 1; #endif // timer0 with prescaler 8, and interrupt, Int freq: 9433khz , ~0.0001s TCCR0B |= (1<<CS01); TIMSK0 |= (1<<TOIE0); DDRB |= (1<<PORTB2); //ppm out #ifdef version1 DDRC |= (1<<PORTC5); //inv uart out PCMSK2 |= (1<<PCINT18); PCICR |= (1<<PCIE2); #endif // led #if ENABLE_LED == TRUE DDRC |= (1<<PORTC4); //led out PORTC |= (1<<PORTC4); DDRB |= (1<<PORTB1); //led out PORTB |= (1<<PORTB1); #endif #if RSSI_OUTPUT == TRUE //timer2 fuer RSSI DDRB |= (1<<PORTB3); TCCR2A |= (1<<COM2A1) | (1<<WGM20)| (1<<WGM21); TCCR2B |= (1<<CS21); OCR2A = 0; #endif USART_Init(); sei(); uint8_t data = 0; uint8_t current_bit_in_ch = 0; uint8_t current_bit_in_byte = 0; uint8_t current_byte = 0; uint8_t current_ch = 0; uint8_t i = 0; uint8_t sbus_bytes[25]; int16_t channels[12]; int16_t channels_old[12]; int16_t channels_vold[12]; uint8_t curr_byte = 0; uint8_t ready = 0; #if SPEKTRUM_OUTPUT == TRUE uint8_t spektrum_frame = 1; uint8_t send_sp = 0; #endif #if FORCE_FAILSAFE == FALSE uint8_t first = 0; ReadParameter(); if((MODE != 1)&&(MODE != 2)) MODE = 1; #endif #if FORCE_FAILSAFE == TRUE MODE = 1; #endif while(1) { if(timeout == 0) { curr_byte = 0; timeout = 60; // 6ms } if(uart_getc_nb(&data)) { timeout = 60; //6ms if(curr_byte == 0) { if(data != 0x0F) { curr_byte = 30; } } if( curr_byte == 24) { if(data != 0) { curr_byte = 30; } } if(curr_byte < 25) { sbus_bytes[curr_byte]=data; curr_byte++; } if(curr_byte == 25) { curr_byte = 0; current_bit_in_byte = 0; current_bit_in_ch = 0; current_ch = 0; current_byte = 1; for(i=0;i<12;i++) { channels[i] = 0; } for(i=0;i<132;i++) { if(sbus_bytes[current_byte] & (1<<current_bit_in_byte)) { channels[current_ch] |= (1<<current_bit_in_ch); } current_bit_in_byte++; current_bit_in_ch++; if(current_bit_in_byte == 8) { current_bit_in_byte =0; current_byte++; } if(current_bit_in_ch == 11) { current_bit_in_ch =0; current_ch++; } } #if FORCE_FAILSAFE == FALSE if(first < 110) { first++; } if(first < 100) { if(channels[0] < 800) { MODE = 1; WriteParameter(); } if(channels[0] > 1250) { MODE = 2; WriteParameter(); } } #endif for(i=0;i<12;i++) { if(channels_vold[i] == channels_old[i]) { if(channels_old[i] != channels[i]) { channels_old[i] = channels[i]; if(channels_vold[i] != 0) { channels[i] = channels_vold[i]; } } }; } #if PPM_OUTPUT == TRUE unsigned int temp_isr_channel_pw[(PPMCH +1)]; uint8_t i=0; /* * Floating point math is slow, so perform computations before * disabling interrupts. */ for(i=0;i<PPMCH;i++) { /* * From linear regression of 7 PWM samples from * 1100..1940us. R^2 = 0.999999. * See https://gist.github.com/prattmic/8857047 */ uint16_t pw = 0.624731*channels[i] + 880.561511; temp_isr_channel_pw[i] = ((((F_CPU/1000) * pw)/1000)/TIMER1_PRESCALER); } cli(); for (i = 0; i < PPMCH; i++) { isr_channel_pw[i] = temp_isr_channel_pw[i]; } sei(); #endif //wenn kein failsafe dann output senden if((sbus_bytes[23] & 8) == 0) { if(ready == 0) { ready = 1; #if ENABLE_LED == TRUE ledpause = 0x2FFF; ledcycle = 0; PORTC |= (1<<PORTC4); PORTB |= (1<<PORTB1); if(MODE == 1) ledmode =1; if(MODE == 2) ledmode =2; #endif #if PPM_OUTPUT == TRUE TCCR1B |= TIMER1_PRESCALER_BITS; #endif #if SPEKTRUM_OUTPUT == TRUE send_sp = 1; #endif } } else { #if RSSI_OUTPUT == TRUE OCR2A=0; #endif if(ready == 1) { ready = 0; #if ENABLE_LED == TRUE ledpause = 0x2FFF; ledcycle = 0; PORTC |= (1<<PORTC4); PORTB |= (1<<PORTB1); if(MODE == 1) ledmode =3; #endif if(MODE == 2) { #if SPEKTRUM_OUTPUT == TRUE send_sp = 0; #endif #if ENABLE_LED == TRUE ledmode =4; #endif #if PPM_OUTPUT == TRUE TCCR1B &= (~(1<<CS12)) & (~(1<<CS11)) & (~(1<<CS10)); #endif } } } #if SPEKTRUM_OUTPUT == TRUE if(send_sp == 1) { //115200 uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * 115200) - 1); UBRRH = (uint8_t)(ubrr>>8); UBRRL = (uint8_t)ubrr; UCSRA |= (1 << U2X); //8N1 UCSRC &= ~(1 << UPM1); UCSRC &= ~(1 << UPM0); UCSRC &= ~(1 << USBS); UCSRB &= ~(1 << UCSZ2); UCSRC |= (1 << UCSZ1); UCSRC |= (1 << UCSZ0); _delay_ms(2); if(spektrum_frame == 0) { spektrum_frame=1; USART_putc(0); USART_putc(0); #if SPEKTRUM_TYPE == SPEKTRUM_NORMAL USART_putc( ((uint8_t)((channels[0] >> 9)&0x03)) + (0 << 2) ); USART_putc( ((uint8_t)(channels[0] >> 1)) ); USART_putc( ((uint8_t)((channels[1] >> 9)&0x03)) + (1 << 2) ); USART_putc( ((uint8_t)(channels[1] >> 1)) ); USART_putc( ((uint8_t)((channels[2] >> 9)&0x03)) + (2 << 2) ); USART_putc( ((uint8_t)(channels[2] >> 1)) ); USART_putc( ((uint8_t)((channels[3] >> 9)&0x03)) + (3 << 2) ); USART_putc( ((uint8_t)(channels[3] >> 1)) ); USART_putc( ((uint8_t)((channels[4] >> 9)&0x03)) + (4 << 2) ); USART_putc( ((uint8_t)(channels[4] >> 1)) ); USART_putc( ((uint8_t)((channels[5] >> 9)&0x03)) + (5 << 2) ); USART_putc( ((uint8_t)(channels[5] >> 1)) ); USART_putc( ((uint8_t)((channels[6] >> 9)&0x03)) + (6 << 2) ); USART_putc( ((uint8_t)(channels[6] >> 1)) ); #endif #if SPEKTRUM_TYPE == SPEKTRUM_HIRES USART_putc( ((uint8_t)((channels[0] >> 8)&0x07)) + (0 << 3) ); USART_putc( ((uint8_t)(channels[0])) ); USART_putc( ((uint8_t)((channels[1] >> 8)&0x07)) + (1 << 3) ); USART_putc( ((uint8_t)(channels[1])) ); USART_putc( ((uint8_t)((channels[2] >> 8)&0x07)) + (2 << 3) ); USART_putc( ((uint8_t)(channels[2])) ); USART_putc( ((uint8_t)((channels[3] >> 8)&0x07)) + (3 << 3) ); USART_putc( ((uint8_t)(channels[3])) ); USART_putc( ((uint8_t)((channels[4] >> 8)&0x07)) + (4 << 3) ); USART_putc( ((uint8_t)(channels[4])) ); USART_putc( ((uint8_t)((channels[5] >> 8)&0x07)) + (5 << 3) ); USART_putc( ((uint8_t)(channels[5])) ); USART_putc( ((uint8_t)((channels[6] >> 8)&0x07)) + (6 << 3) ); USART_putc( ((uint8_t)(channels[6])) ); #endif } else {
// обработчик прерываний для всех прерываний связанных с UART1 void USART1_IRQHandler(void){ // если установлен флаг прерывания по поступлению данных if( USART_GetITStatus(USART1, USART_IT_RXNE) ) USART_putc(USART1, USART1->DR); }
void USART_puts(USART_TypeDef* USARTx, const char *s) { for ( int i = 0; s[i] != 0; ++i ) USART_putc(USARTx, s[i]); }
void printByte(char c){ if(usart) USART_putc(usart, c); }