/*------------------------------------------------ 发送信息和数字 ------------------------------------------------*/ void UART_SendInfor(U8 *pInfo, U16 dDat) { U8 *pDat = (U8 *)&dDat; UART_SendStr(pInfo); UART_SendASCII(*pDat--); }
//* FC06 This command writes the contents of analog output holding register. void PresetSingleRegister(void) { char OutputFrame[OFS]; char temp4[4]; char temp2[2]; uint16_t Register = 0; uint16_t ValueToWrite = 0; uint8_t k = 0; //Clears table for(k = 0; k<OFS; k++){OutputFrame[k] = '\0';} // rewrites slave's address & number of function RewritingChars(OutputFrame,0,12); //* Gets data addres of the register. temp4[0] = word[5]; temp4[1] = word[6]; temp4[2] = word[7]; temp4[3] = word[8]; HexToByte_4(temp4, &Register); if(Check_DataAddr_F06(Register) & Check_DataVal()) { //* The value to write( FF00 = ON, 0000 = OFF ). temp4[0] = word[9]; temp4[1] = word[10]; temp4[2] = word[11]; temp4[3] = word[12]; HexToByte_4(temp4, &ValueToWrite); //* Sets the value. Output_Registers[Register] = ValueToWrite; //* Writes LRC. k = 13; ByteToHex(temp2,GetLRC(OutputFrame)); OutputFrame[k] = temp2[0]; k++; OutputFrame[k] = temp2[1]; k++; OutputFrame[k] = 0x0D; k++; OutputFrame[k] = 0x0A; k++; OutputFrame[k] = 0x0A; k++; //sends frame UART_SendStr(OutputFrame); } }
///FC05 *This command writes the contents of discrete coil /// void ForceSingleCoil(void) { char OutputFrame[OFS]; // output frame char temp4[4]; char temp2[2]; uint16_t Coil = 0; uint16_t StatusToWrite = 0; uint8_t k = 0; //Clears table for(k = 0; k<OFS; k++){OutputFrame[k] = '\0';} // rewrites slave's address & number of function RewritingChars(OutputFrame,0,12); //* Gets data addres of the coil. temp4[0] = word[5]; temp4[1] = word[6]; temp4[2] = word[7]; temp4[3] = word[8]; HexToByte_4(temp4, &Coil); if(Check_DataAddr_F05(Coil)) { //* The status to write( FF00 = ON, 0000 = OFF ). temp4[0] = word[9]; temp4[1] = word[10]; temp4[2] = word[11]; temp4[3] = word[12]; HexToByte_4(temp4, &StatusToWrite); //* Sets the status. SetSingleCoil( &Coil, &StatusToWrite, Output_Registers); //* Writes LRC. k = 13; ByteToHex(temp2,GetLRC(OutputFrame)); OutputFrame[k] = temp2[0]; k++; OutputFrame[k] = temp2[1]; k++; OutputFrame[k] = 0x0D; k++; OutputFrame[k] = 0x0A; k++; OutputFrame[k] = 0x0A; k++; //sending frame UART_SendStr(OutputFrame); } }
int main(void) { UART_Init(); UART_SendStr("\nSTM32F103RET6 is online.\n"); UART_SendStr("I2C init ... "); if (!LM75_Init(100000)) UART_SendStr("ready.\n"); else { UART_SendStr("fail.\n"); UART_SendStr("MCU halted now.\n"); while(1); } uint16_t value; value = LM75_ReadReg(0x00); UART_SendHex16(value); UART_SendChar('\n'); value = LM75_ReadConf(); UART_SendHex8(value); UART_SendChar('\n'); value = LM75_ReadReg(0x02); UART_SendHex16(value); UART_SendChar('\n'); value = LM75_ReadReg(0x03); UART_SendHex16(value); UART_SendChar('\n'); LM75_Shutdown(DISABLE); int16_t temp = LM75_Temperature(); UART_SendInt(temp / 10); UART_SendChar('.'); temp %= 10; if (temp < 0) temp *= -1; UART_SendInt(temp % 10); UART_SendStr("C\n"); while(1); }
//Illegal value in querry frame void ErrorMessage(uint8_t ErrorCode) { char OutputFrame[OFS]; // output frame char temp[2]; uint8_t FunC; uint8_t ErrFuncCode; uint8_t k; //Clears table for(k = 0; k<OFS; k++){OutputFrame[k] = '\0';} //Rewrite first chars RewritingChars(OutputFrame,0,2); //Read Function Code from message temp[0] = word[3]; temp[1] = word[4]; HexToByte(temp, &FunC); //Calculate Function Code for Message with Error ErrFuncCode = FunC + 80; ByteToHex(temp,ErrFuncCode); OutputFrame[3] = temp[0]; OutputFrame[4] = temp[1]; //Fill Data field with ILLEGAL FUNCTION CODE ByteToHex(temp,ErrorCode); OutputFrame[5] = temp[0]; OutputFrame[6] = temp[1]; //* Writes LRC. ByteToHex(temp,GetLRC(OutputFrame)); OutputFrame[7] = temp[0]; OutputFrame[8] = temp[1]; OutputFrame[9] = 0x0D; OutputFrame[10] = 0x0A; OutputFrame[11] = 0x0A; //sending frame UART_SendStr(OutputFrame); }
void EXTI9_5_IRQHandler(void) { if (EXTI->PR & INT1_EXTI_LINE) { i8 = BMC050_ACC_GetTSIRQ(); // Get IRQ source BMC050_ACC_GetXYZ(&X,&Y,&Z); // Get last accelerometer readings BMC050_ACC_SetIRQMode(ACC_IM_RESET); // Reset all latched interrupts UART_SendStr(USART2,"Slope="); UART_SendHex8(USART2,i8); UART_SendChar(USART2,' '); if (i8 & ACC_TS_SLOPEZ) UART_SendStr(USART2,"SLOPEZ "); if (i8 & ACC_TS_SLOPEY) UART_SendStr(USART2,"SLOPEY "); if (i8 & ACC_TS_SLOPEX) UART_SendStr(USART2,"SLOPEX "); UART_SendStr(USART2," X="); UART_SendInt(USART2,X); UART_SendStr(USART2," Y="); UART_SendInt(USART2,Y); UART_SendStr(USART2," Z="); UART_SendInt(USART2,Z); UART_SendChar(USART2,'\n'); EXTI->PR = INT1_EXTI_LINE; // Clear IT bit for EXTI line } }
//* FC16 This command writes the contents of analog output holding registers void ForceMultipleRegisters() { char OutputFrame[OFS]; uint8_t counter = 0; uint16_t Value=0; uint16_t FirstRegister; uint16_t NumberOfRegs; uint16_t Coil; uint8_t NumberOfDataBytes; char temp[2]; char temp4[4]; uint16_t n = 0; //Clears table for(n = 0; n<OFS; n++){OutputFrame[n] = '\0';} // rewrites slave's address & number of function RewritingChars(OutputFrame,0,12); //gets number of first coil temp4[0] = word[5]; temp4[1] = word[6]; temp4[2] = word[7]; temp4[3] = word[8]; HexToByte_4(temp4, &FirstRegister); //gets number of coils to written temp4[0] = word[9]; temp4[1] = word[10]; temp4[2] = word[11]; temp4[3] = word[12]; HexToByte_4(temp4, &NumberOfRegs); //gets the number of data bytes to follow temp[0] = word[13]; temp[1] = word[14]; HexToByte(temp, &NumberOfDataBytes); if(Check_DataAddr_F03_F04(FirstRegister, NumberOfRegs) & Check_DataVal_F16(NumberOfDataBytes)) { counter=15; for(n=0; n<(NumberOfDataBytes/2); n++) { temp4[0] = word[counter]; counter++; temp4[1] = word[counter]; counter++; temp4[2] = word[counter]; counter++; temp4[3] = word[counter]; counter++; HexToByte_4(temp4, &Value); Output_Registers[FirstRegister+n] = Value; } //* Writes LRC. counter = 13; ByteToHex(temp,GetLRC(OutputFrame)); OutputFrame[counter] = temp[0]; counter++; OutputFrame[counter] = temp[1]; counter++; OutputFrame[counter] = 0x0D; counter++; OutputFrame[counter] = 0x0A; counter++; OutputFrame[counter] = 0x0A; counter++; //sending frame UART_SendStr(OutputFrame); } }
///FC03 *This command is requests the content of analog output holding registers/// /// *registers => output or input void ReadHoldingRegisters(uint16_t *registers) { char OutputFrame[OFS]; char temp[4]; char temp2[2]; uint16_t FirstReg = 0; uint16_t NumberOfRegs = 0; uint8_t NumberOfBytes = 0 ; uint8_t ct = 0; uint8_t k = 0; uint8_t counter = 0; uint16_t Content_dec = 0; //Clears table for(k = 0; k<OFS; k++){OutputFrame[k] = '\0';} // rewrites slave's address & number of function RewritingChars(OutputFrame,0,4); //gets number of first register temp[0] = word[5]; temp[1] = word[6]; temp[2] = word[7]; temp[3] = word[8]; HexToByte_4(temp, &FirstReg); //gets quantity of registers temp[0] = word[9]; temp[1] = word[10]; temp[2] = word[11]; temp[3] = word[12]; HexToByte_4(temp, &NumberOfRegs); if(Check_DataAddr_F03_F04(FirstReg, NumberOfRegs) & Check_DataVal()) { // calculates the number of data bytes to follow ( n registers * 2 bytes each) NumberOfBytes = NumberOfRegs*2; //Writes the number of data bytes ByteToHex(temp,NumberOfBytes); OutputFrame[5] = temp[0]; OutputFrame[6] = temp[1]; counter = 7; k = FirstReg; //Reads the contents from Output_Registers for(ct=0;ct<NumberOfRegs;ct++) { Content_dec = registers[k]; ByteToHex_4(temp,Content_dec); OutputFrame[counter] = temp[0]; counter++; OutputFrame[counter] = temp[1]; counter++; OutputFrame[counter] = temp[2]; counter++; OutputFrame[counter] = temp[3]; counter++; k++; } // LRC ByteToHex(temp2,GetLRC(OutputFrame)); OutputFrame[counter] = temp2[0]; counter++; OutputFrame[counter] = temp2[1]; counter++; OutputFrame[counter] = 0x0D; counter++; OutputFrame[counter] = 0x0A; counter++; OutputFrame[counter] = 0x0A; counter++; //sends frame UART_SendStr(OutputFrame); } }
//* FC01 This command requests the ON/OFF status of discrete output coils. void ReadCoilStatus(uint16_t *registers) { char OutputFrame[OFS]; uint8_t counter = 0; uint8_t TempSum=0; uint16_t FirstCoil; uint16_t NumberOfCoils; uint16_t Coil; uint8_t NumberOfDataBytes; char temp[2]; char temp4[4]; uint16_t n = 0; //Clears table for(n = 0; n<OFS; n++){OutputFrame[n] = '\0';} // rewrites slave's address & number of function RewritingChars(OutputFrame,0,4); //gets number of first coil temp4[0] = word[5]; temp4[1] = word[6]; temp4[2] = word[7]; temp4[3] = word[8]; HexToByte_4(temp4, &FirstCoil); //gets quantity of coils temp4[0] = word[9]; temp4[1] = word[10]; temp4[2] = word[11]; temp4[3] = word[12]; HexToByte_4(temp4, &NumberOfCoils); if(Check_DataAddr_F01_F02(FirstCoil, NumberOfCoils) & Check_DataVal()) { // calculates the number of data bytes if((NumberOfCoils%8)!=0) { NumberOfDataBytes = ( NumberOfCoils/8)+1; } else { NumberOfDataBytes = NumberOfCoils/8; } //Writes the number of data bytes ByteToHex(temp,NumberOfDataBytes); OutputFrame[5] = temp[0]; OutputFrame[6] = temp[1]; // calculates data bits to HEX and writing to frame counter = 7; Coil = FirstCoil; n = NumberOfCoils; while(n>0) { if(n>=8) // changes 8bits to hex { uint8_t pwr = 0; for(pwr =0; pwr<8; pwr++,Coil++) { TempSum += (1<<pwr)*StateOfCoil(Coil, registers); /////// (9 dec) 1001 => 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 n--; } ByteToHex(temp,TempSum); TempSum = 0; OutputFrame[counter] = temp[0]; counter++; OutputFrame[counter] = temp[1]; counter++; } else // changing incomplete octet to hex { uint8_t pwr = 0; uint8_t zm = n; for(pwr =0; pwr<zm; pwr++,Coil++) { TempSum += (1<<pwr)*StateOfCoil(Coil, registers); /////// (9 dec) 1001 => 1*2^3 + 0*2^2 + 0*2^1 + 1*2^( n--; } ByteToHex(temp,TempSum); TempSum = 0; OutputFrame[counter] = temp[0]; counter++; OutputFrame[counter] = temp[1]; counter++; } } // writes LRC ByteToHex(temp,GetLRC(OutputFrame)); OutputFrame[counter] = temp[0]; counter++; OutputFrame[counter] = temp[1]; counter++; OutputFrame[counter] = 0x0D; counter++; OutputFrame[counter] = 0x0A; counter++; OutputFrame[counter] = 0x0A; counter++; // sends frame UART_SendStr(OutputFrame); } }
int main(void) { UART_Init(); UART_SendStr("\nSTM32F103RET6 is online.\n"); SD_Init(); uint8_t b = 0; UART_SendStr("SD_CardInit: "); b = SD_CardInit(); UART_SendHex8(b); UART_SendStr("\nCard version: "); UART_SendHex8(SD_CardType); uint8_t response = 0; UART_SendStr("\nCSD: "); response = SD_Read_CSD(); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else UART_SendBufHex((char*)&SD_CSD[0],16); UART_SendStr("\nCID: "); response = SD_Read_CID(); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else UART_SendBufHex((char*)&SD_CID[0],16); UART_SendStr("\nMax bus clk.: "); UART_SendHex32(SD_MaxBusClkFreq); UART_SendStr("\nDevice size: "); UART_SendInt(SD_CardCapacity >> 10); UART_SendStr("Mb\n"); response = SD_Read_Block(0); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else { UART_SendBufHexFancy((char*)&SD_sector[0],512,32,'.'); UART_SendStr("CRC16: "); UART_SendHex16(SD_CRC16_rcv); UART_SendStr(" == "); UART_SendHex16(SD_CRC16_cmp); } UART_SendChar('\n'); response = SD_Read_Block(1); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else { UART_SendBufHexFancy((char*)&SD_sector[0],512,32,'.'); UART_SendStr("CRC16: "); UART_SendHex16(SD_CRC16_rcv); UART_SendStr(" == "); UART_SendHex16(SD_CRC16_cmp); } UART_SendChar('\n'); while(1); }
int main(void) { Delay_Init((void *)0); UARTx_Init(USART2,1382400); UART_SendStr(USART2,"--------------------------------------\n"); if (I2Cx_Init(BMC050_I2C_PORT,400000) != I2C_SUCCESS) { UART_SendStr(USART2,"I2C2 init fail\n"); while(1); } UART_SendStr(USART2,"I2C2 init at 400kHz\n"); // Enable PORTA peripheral RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Configure PA6 as external interrupt GPIOA->MODER &= ~GPIO_MODER_MODER6; // Input mode (reset state) GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR6; // No pull-up, pull-down GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1; // Pull-down // Configure priority group: 4 bits for preemption priority, 0 bits for subpriority. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); // Enable the SYSCFG module clock RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // PA6 -> EXTI line 6 (INT1 from BMC050) EXTI->PR = INT1_EXTI_LINE; // Clear IT pending bit for EXTI line EXTI->IMR |= INT1_EXTI_LINE; // Enable interrupt request from EXTI line EXTI->EMR &= ~INT1_EXTI_LINE; // Disable event on EXTI line EXTI->RTSR |= INT1_EXTI_LINE; // Trigger rising edge enabled EXTI->FTSR &= ~INT1_EXTI_LINE; // Trigger falling edge disabled // Enable the USB interrupt NVICInit.NVIC_IRQChannel = EXTI9_5_IRQn; NVICInit.NVIC_IRQChannelPreemptionPriority = 2; NVICInit.NVIC_IRQChannelSubPriority = 0; NVICInit.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVICInit); ///* uint8_t buf[2]; uint8_t reg; uint8_t val; uint32_t i; //*/ ///* reg = 0x00; val = 0; UART_SendStr(USART2,"\nAccelerometer\n"); for (reg = 0; reg <= 0x3f; reg++) { I2Cx_Write(BMC050_I2C_PORT,®,1,0x18 << 1,I2C_NOSTOP); I2Cx_Read(BMC050_I2C_PORT,&val,1,0x18 << 1); UART_SendStr(USART2,"R"); UART_SendHex8(USART2,reg); UART_SendStr(USART2," = "); UART_SendHex8(USART2,val); UART_SendStr(USART2,"\t\t"); } UART_SendChar(USART2,'\n'); //*/ /* UART_SendStr(USART2,"\nMagnetometer\n"); // Magnetometer power enable buf[0] = 0x4b; buf[1] = 0x01; // Set power control bit I2Cx_Write(BMC050_I2C_PORT,&buf[0],2,0x10 << 1,I2C_STOP); for (reg = 0x40; reg <= 0x52; reg++) { I2Cx_Write(BMC050_I2C_PORT,®,1,0x10 << 1,I2C_NOSTOP); I2Cx_Read(BMC050_I2C_PORT,&val,1,0x10 << 1); UART_SendStr(USART2,"R"); UART_SendHex8(USART2,reg); UART_SendStr(USART2," = "); UART_SendHex8(USART2,val); UART_SendStr(USART2,"\t\t"); } UART_SendStr(USART2,"\n========================================\n"); */ BMC050_ACC_SoftReset(); Delay_ms(5); // must wait for start-up time of accelerometer (2ms) BMC050_Init(); // Enable I2C watchdog timer with 50ms BMC050_ACC_InterfaceConfig(ACC_IF_WDT_50ms); UART_SendStr(USART2,"BMC050 ACC device ID: "); UART_SendHex8(USART2,BMC050_ACC_GetDeviceID()); UART_SendChar(USART2,'\n'); UART_SendStr(USART2,"BMC050 MAG device ID: "); UART_SendHex8(USART2,BMC050_MAG_GetDeviceID()); UART_SendChar(USART2,'\n'); UART_SendStr(USART2,"BMC050 temperature: "); temp = BMC050_ReadTemp(); UART_SendInt(USART2,temp / 10); UART_SendChar(USART2,'.'); UART_SendInt(USART2,temp % 10); UART_SendStr(USART2,"C\n"); BMC050_ACC_SetBandwidth(ACC_BW8); // Accelerometer readings filtering (lower or higher better?) BMC050_ACC_SetIRQMode(ACC_IM_NOLATCH); // No IRQ latching BMC050_ACC_ConfigSlopeIRQ(0,16); // Motion detection sensitivity BMC050_ACC_IntPinMap(ACC_IM1_SLOPE); // Map slope interrupt to INT1 pin BMC050_ACC_SetIRQ(ACC_IE_SLOPEX | ACC_IE_SLOPEY | ACC_IE_SLOPEZ); // Detect motion by all axes BMC050_ACC_LowPower(ACC_SLEEP_100); // Low power with sleep duration 0.1s // BMC050_ACC_Suspend(); while(1); /* while(1) { while (!BMC050_ACC_GetIRQStatus()); // Wait for new data from accelerometer i8 = BMC050_ACC_GetTSIRQ(); BMC050_ACC_GetXYZ(&X,&Y,&Z); UART_SendStr(USART2,"Slope="); UART_SendHex8(USART2,i8); UART_SendChar(USART2,' '); if (i8 & ACC_TS_SLOPEZ) UART_SendStr(USART2,"SLOPEZ "); if (i8 & ACC_TS_SLOPEY) UART_SendStr(USART2,"SLOPEY "); if (i8 & ACC_TS_SLOPEX) UART_SendStr(USART2,"SLOPEX "); UART_SendStr(USART2," X="); UART_SendInt(USART2,X); UART_SendStr(USART2," Y="); UART_SendInt(USART2,Y); UART_SendStr(USART2," Z="); UART_SendInt(USART2,Z); UART_SendChar(USART2,'\n'); BMC050_ACC_SetIRQMode(ACC_IM_RESET); Delay_ms(100); } */ }