/** * Initialize the I2C */ void InitI2C() { //I2C Pin Config mPORTBSetPinsDigitalOut(I2C_SCL_Pin | I2C_SDA_Pin); I2CEnable(I2C1, FALSE); //Soft reset I2C Bus by pulsing the clock line 10 times mPORTBSetBits(I2C_SCL_Pin | I2C_SDA_Pin); unsigned int i; unsigned int wait; for (i = 0; i < 20; i++) { for (wait = 0; wait < 20; wait++); mPORTBToggleBits(I2C_SCL_Pin); } mPORTBSetBits(I2C_SCL_Pin | I2C_SDA_Pin); // Configure Various I2C Options //!!!!! - Slew rate control off(High speed mode enabled), If enabled, RA0 and RA1 fail to work, see silicon errata (Microchip Hardware Bugs) I2CConfigure(I2C1, I2C_ENABLE_SLAVE_CLOCK_STRETCHING | I2C_ENABLE_HIGH_SPEED); // Set the I2C baud rate int I2C_actualClock = I2CSetFrequency(I2C1, SYS_FREQ, I2C_Clock); UART_SendString("I2C Clock: "); UART_SendInt(I2C_actualClock); UART_SendString(" Hz\n\r"); // Enable the I2C bus I2CEnable(I2C1, TRUE); while (!I2CTransmitterIsReady(I2C1)); // configure the interrupt priority for the I2C peripheral //INTSetVectorPriority(INT_I2C_1_VECTOR,INT_PRIORITY_LEVEL_3); //INTClearFlag(INT_I2C1); //INTEnable(INT_I2C1,INT_ENABLED); }
void UART_Action(uint8 *dat,uint8 len) { uint8 i; UART_SendString(dat,len); UART_SendString("\r\n",2); for(i=0;i<4;i++) { if(*dat!=UART_Header[i])return ; dat++; } if(*dat=='0') { LCD1602_AreaClear(0,0,16); LCD1602_Show(0,0,dat+2,len-6); EEPROM_WriteString(0x40,dat+2,len-6); EEPROM_WriteString(0x40+len-6,spaces,22-len); } else if(*dat=='1') { LCD1602_AreaClear(0,1,16); LCD1602_Show(0,1,dat+2,len-6); EEPROM_WriteString(0x80,dat+2,len-6); EEPROM_WriteString(0x80+len-6,spaces,22-len); } }
//客户端发送数据,带协议 static void ConnectSendCode(u8 *ip,u16 port,u8 cmd,u8 *buff,u16 len) { u8 p[300]; u8 p1[100]; u8 buf; u8 sum=0; //sprintf(p,"ConnectSend(\"%s\",%d,{0xaa,0x%02X,0x%02X,0x%02X,",ip,port,_16T8H(len),_16T8L(len),cmd); sprintf(p,"ConnectSend(\"%s\",%d,{0xaa,%d,%d,%d,",ip,port,_16T8H(len),_16T8L(len),cmd); while(len--) { buf=*buff; //sprintf(p1,"0x%02x,",buf); sprintf(p1,"%d,",buf); strcat(p,p1); sum+=buf; buff++; } //sprintf(p1,"0x%02x,",sum); sprintf(p1,"%d,",sum); strcat(p,p1); //sprintf(p,"%s,%X",p1,sum); //sprintf(p1,"%s,0x55",p); strcat(p,"0x55})\r\n\0"); UART_SendString(p); }
/** * Compare Interrupt */ void TIM4_IRQHandler(void){ if(TIM_GetITStatus(TIM4, TIM_IT_CC1) == SET){ // clear IRQ Status TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); NVIC_ClearPendingIRQ(TIM2_IRQn); // now increment local loop counter and modify local // time on second increment ++loops; if(loops == RATE_MIN){ // reset timing information for next 1 second round loops = 0; totalDuration = newTotalDuration; lastOverflow = 0; currentCycleDuration = 0; immediateCorrection = 0; // only applied till next cycle/second starts, then // newTotalDuration contains corrected value } if(loops % UPDATE_RATE_SEC == 0){ DCF77_incrementTime(&clockTime); UART_SendString("S\n\0"); } // update timer compare register and update duration of current cycle /second in ticks currentCycleDuration += lastCompareDuration; lastCompareDuration = Clock_calcMacrotickDuration(); previousCompareRegisterValue = TIM4->CCR1; TIM4->CCR1 += lastCompareDuration; // update visualization of clock updateVisualization(&clockTime, (loops % UPDATE_RATE_SEC), UPDATE_RATE_SEC); } }
void UART_Action(unsigned char *dat, unsigned char len) { //UART_SendString(dat, len); if(len <= 1) return; if(len >= 5 && strncmp(dat, "D:Inf", 5) == 0) { UART_SendString("M:D.Inf:", 8); if(DoorNotCloseFlag) UART_SendString("Y\n", 2); else UART_SendString("N\n", 2); } else if(len >= 2 && strncmp(dat, "D:", 2) == 0) { //UART_SendString(dat, len); UART_SendString("N:D.SERR\n", 9); } }
int main(void) { volatile float zz = 360.0 * zz; // clear stripe for(uint16_t i = 0; i < LED; i++){ stripe[i].blue = 0; stripe[i].green = 0; stripe[i].red = 0; } // run uart UART_init(); UART_SendString("STM32F103WS2812 says hello\n\0"); //WS2812_Init(); //WS2812_clear(); Delay(5000000L); Animator_Init(); while(1){ for(uint16_t j = 1; j < LED; j++){ // wandering light for(uint16_t i = 0; i < LED; i++){ stripe[i].blue = 0; stripe[i].green = 0; stripe[i].red = 0; } stripe[j].red = 0xAF; stripe[j].green = 0; stripe[j].blue = 0; // first led bugfix? pegel zu klein? // if(j!=0){ // stripe[0].blue = 1; // } //WS2812_Init(); // WS2812_send(stripe, LED); Delay(7500000L); //while(1); } } }
PUBLIC VOID O1N_OAM_DbgMsg(VOID *pFmt, ...) { va_list ap; /* DO NOT use OPEN_STACK_VAR/OPEN_STACK_END macro */ va_start(ap, pFmt); vsnprintf((char *) gaStr, (size_t) sizeof(gaStr), (char *) pFmt, ap); UART_SendString((char *) gaStr); va_end(ap); }
void Display10BitsData(int displayed_data) { unsigned char display_buf[6] = {0}; // 数据转换缓冲区 ConvertDataTo10Bits(display_buf, displayed_data); // 转换数据显示 // 6个10位数据, 最高位为符号位 #if ENABLE_IN_51 UART_SendString(display_buf, 6); #endif // #if ENABLE_IN_51 #if ENABLE_IN_WIN for (int i = 0; i < 6; ++i) { printf("%d ", display_buf[i]); } // for printf("\n"); #endif // #if ENABLE_IN_WIN } // end of Display10BitsData().
//客户端发送数据,不带协议 static void ConnectSend(u8 *ip,u16 port,u8 *dat) { u8 p[100]; sprintf(p,"ConnectSend(\"%s\",%d,\"%s\")\r\n\0",ip,port,dat); UART_SendString(p); }
/** * @brief Clock_Sync * Perform a resynchronisation between DCF77Clock and local clock, * triggered by the minute overflow * @param dcfTime - current time * @param failed - number of consecutive failed syncs (0 if syncs are consecutive, i.e. every minute) */ void Clock_Sync(volatile struct DCF77_Time_t* dcfTime, uint8_t failed){ NVIC_DisableIRQ(TIM4_IRQn); int64_t currentPos = (TIM4->CNT - previousCompareRegisterValue) + currentCycleDuration; immediateCorrection = 0; // store correction time volatile struct DCF77_Time_t* correct = NULL; volatile struct DCF77_Time_t lTime; // perform correction // NOTE: We assume WC, we are near the middle when the external trigger hit, therefore /2 correction rate, // slower convergence but more stable if( currentPos >= totalDuration/2 ){ // if the local clock is to slow, we can use current pos as new length of the cycle, // but we must also consider the remaining ticks until the overflow occures (calling Clock_IncrementSecond) // currentPos is delay summed up over failed+1 minutes currentPos = currentPos / (failed+1); newTotalDuration = (totalDuration + currentPos)/2; // but we start right now to apply our cycle change instead of waiting for the next round to start immediateCorrection = -(totalDuration - currentPos)/2; // store corrected time DFC77_cloneDCF(&lTime, dcfTime); // now decrement time by one second, to get the right time at next increment second call DCF77_decrementTime(&lTime); // choose right correction correct = &lTime; } else { // no we assume the local clock is too fast, and we have to increase the cycle length // currentPos is delay summed up over failed+1 minutes currentPos = currentPos / (failed+1); newTotalDuration = totalDuration + (currentPos/2); // but we start right now to apply our cycle change instead of waiting for the next round to start // so we modify the immediate correction to apply our changes immediateCorrection = (currentPos/2); // choose right correction correct = dcfTime; } // ok we update the local time DFC77_cloneDCF(&clockTime, correct); // reenable timing interrupt NVIC_EnableIRQ(TIM4_IRQn); // print correction value char str[100]; itoa((int) immediateCorrection, str); UART_SendString(str); UART_SendString(":\0"); itoa((int) totalDuration, str); UART_SendString(str); UART_SendString(":\0"); itoa((int) newTotalDuration, str); UART_SendString(str); UART_SendString("\n\0"); }
void uart_terminal_command(_UARTBUF *recive_buf, _SETTINGSOFCHANNEL *settings_channel ) { NVIC_DisableIRQ(UART4_IRQn); /*Disable Interrupt for UART4*/ u8 uart_command[UART_COMMAND_MAS_SIZE][8]= { "af1", "af2", //0,1 "fcut", "swinput",//2,3 "fd", "cfg", //4,5 "reboot", "help", // 6,7 "default",//8 "start","stop",//9,10 "spi", "uart"//11,12 }; u8 i; u8 *str_uart=recive_buf->UART_Recive_Buf; u8 tmp_buf[SIZE_UART_BUF]; u8 lenght_recive_buf=0; u8 error_happened=0; s8 numder_of_command=-1; u8 tmp; u8 print_settings=0; u8 str[100]; while(lenght_recive_buf < recive_buf->UART_Buf_Len){ for(/*Empty*/; *str_uart == ' ' && lenght_recive_buf< recive_buf->UART_Buf_Len ; str_uart++ , lenght_recive_buf++ ); // remove the SPACE from the receive UARt buffer for(i=0 ; *str_uart != ' ' && i < recive_buf->UART_Buf_Len ; i++, str_uart++ , lenght_recive_buf++ ){ // read the command tmp_buf[i]=*str_uart; } tmp_buf[i]='\0'; for(/*Empty*/; *str_uart == ' ' && i < recive_buf->UART_Buf_Len ; str_uart++ , lenght_recive_buf++ ); // remove the SPACE from the recive UARt buffer for(i=0; i != UART_COMMAND_MAS_SIZE; i++){ // Find the number of command if(!strcmp((char const* )tmp_buf,(char const* )uart_command[i])){ numder_of_command = i; break; } } if(numder_of_command == -1){ sprintf((char *)str, " Command \"%s\" not found!", tmp_buf); UART_SendString(UART4, str); }else{ switch(numder_of_command){ case 0: case 1: case 2: case 3: case 4: for(i=0 ; *str_uart != ' ' && i < recive_buf->UART_Buf_Len ; i++, str_uart++ , lenght_recive_buf++ ){ // read the command tmp_buf[i]=*str_uart; } if(i==0){ error_happened=1; } tmp_buf[i]='\0'; for( i=0; tmp_buf[i] != '\0'; i++ ){ // check that we get the number if( '0' > tmp_buf[i] || tmp_buf[i] > '9' ){ error_happened=1; break; } } tmp=(u8)atoi( (char*)tmp_buf ); if(numder_of_command == 0){ // set Af1 if( error_happened == 1 || Set_Settings_DA6( tmp ) == 1 ){ sprintf((char *)str, " For command \"af1\" argument \"%s\" is wrong!", tmp_buf); UART_SendString(UART4, str ); error_happened=0; }else{ settings_channel->Aplification_factor_1=tmp; } }else if(numder_of_command==1){ // set Af2 if( error_happened==1 || Set_Settings_DA2(tmp ) == 1 ){ sprintf((char *)str, " For command \"af2\" argument \"%s\" is wrong!", tmp_buf); UART_SendString(UART4, str ); error_happened=0; }else{ settings_channel->Aplification_factor_2=tmp; } }else if(numder_of_command==2){ // set fcut if( error_happened==1 || Set_Settings_DA8(tmp ) == 1 ){ sprintf((char *)str, " For command \"fcut\" argument \"%s\" is wrong!", tmp_buf); UART_SendString(UART4, str ); error_happened=0; }else{ settings_channel->Frequency_cut_off=tmp; print_settings=1; } }else if(numder_of_command==3){ // set swinput if( error_happened==1 || Set_Settings_DA12(tmp ) == 1 ){ sprintf((char *)str, " For command \"swinput\" argument \"%s\" is wrong!", tmp_buf); UART_SendString(UART4, str ); error_happened=0; }else{ settings_channel->Switching_input=tmp; } }else if(numder_of_command==4){ // set fd if( error_happened==1 || Set_Settings_FD(tmp, settings_channel)== 1 ){ sprintf((char *)str, " For command \"fd\" argument \"%s\" is wrong!", tmp_buf); UART_SendString(UART4, str ); error_happened=0; }else{ settings_channel->Frequency_sampling=tmp; } } print_settings=1; break; case 5: // print current settings print_settings=1; break; case 6: // reboot while(1); break; case 7: // help UART_SendString(UART4, " ***********************************Help menu***********************************"); UART_SendString(UART4, " * *"); UART_SendString(UART4, " * af1 <value> - Amplification factor K1 (DA6), <value> - 0..8 *"); UART_SendString(UART4, " * af2 <value> - Amplification factor K2 (DA2), <value> - 0..12 *"); UART_SendString(UART4, " * fcut <value> - Cutoff frequency of the low pass filter, <value> - 0..19 *"); UART_SendString(UART4, " * swinput <value> - Switching input mode (DA12), <value> - 0..4 *"); UART_SendString(UART4, " * fd <value> - Sampling frequency, <value> - 0..6 *"); UART_SendString(UART4, " * cfg - Inquiry configuration and number of channel *"); UART_SendString(UART4, " * default - Default configuration *"); UART_SendString(UART4, " * reboot - Reboot channel *"); UART_SendString(UART4, " * spi/uart - Send data from ADC to SPI or UART port *"); UART_SendString(UART4, " * start - Start read ADC data *"); UART_SendString(UART4, " * stop - Stop read ADC data *"); UART_SendString(UART4, " * *"); UART_SendString(UART4, " *******************************************************************************"); break; case 8: // Set deffault settings Set_Default_Settings(settings_channel); print_settings=1; break; case 9: // Start settings_channel->Start_stop=1; NVIC_EnableIRQ(EXTI0_IRQn); /*Enable Interrupt for PB0 */ break; case 10: // Stop settings_channel->Start_stop=0; NVIC_DisableIRQ(EXTI0_IRQn); /*Enable Interrupt for PB0 */ print_settings=1; break; case 11: // data send to SPI3 port settings_channel->Port_to_send_data_SPI3_or_UART=0; print_settings=1; break; case 12: // data send to UART port settings_channel->Port_to_send_data_SPI3_or_UART=1; print_settings=1; break; default: numder_of_command = -1; UART_SendString(UART4, "Command not recognized!"); break; } numder_of_command = -1; } } if(print_settings==1){ sprintf((char *)str, "\n\r af1 %d af2 %d fcut %d fd %d swinput %d \n\r Number of channel: %d \n\r", settings_channel->Aplification_factor_1, settings_channel->Aplification_factor_2, settings_channel->Frequency_cut_off, settings_channel->Frequency_sampling, settings_channel->Switching_input, settings_channel->Number_of_Channel); UART_SendString(UART4, str ); if( settings_channel->Port_to_send_data_SPI3_or_UART==0){ UART_SendString(UART4, " Port to send data is: SPI \n\r" ); }else{ UART_SendString(UART4, " Port to send data is: UART \n\r" ); } print_settings=0; } recive_buf->UART_Buf_Len=0; NVIC_EnableIRQ(UART4_IRQn); /*Enable Interrupt for UART4*/ UART_SendString(UART4, ">"); }
void UART_Sendint(uint8 uartNO,int num) { char str[5]; sprintf(str,"%d",num); UART_SendString(uartNO,str); }