void OLED_Data(char *DATA) /* Function for sending data to OLED */
{
int len = strlen(DATA);
for (int g=0; g<len; g++)
{
for (int index=0; index<5; index++)
{
Wire.beginTransmission(OLED_Write_Address); /* Begin transmission to slave device */
/* Queue data to be transmitted */
Wire.write(0x40); /* For Data Transmission, C = 0 and D/C = 1 */
Wire.write(ASCII[DATA[g] - 0x20][index]);
Wire.endTransmission(); /* Transmit the queued bytes and end transmission to slave device */
}
}
}
void OLED_Command(char DATA) /* Function for sending command to OLED */
{
Wire.beginTransmission(OLED_Write_Address); /* Begin transmission to slave device */
/* Queue data to be transmitted */
Wire.write(0x00); /* For Data Transmission, C = 0 and D/C = 0 */
Wire.write(DATA);
Wire.endTransmission(); /* Transmit the queued bytes and end transmission to slave device */
}
void OLED_clear() /* Function for clearing OLED */
{
OLED_setXY(0x00, 0x7F, 0x00, 0x07); /* Column Start Address 0,
Column End Address 127, Page Start Address 0, Page End Address 7
*/
for (int k=0; k<=1023; k++)
{
Wire.beginTransmission(OLED_Write_Address); /* Begin transmission to slave device */
/* Queue data to be transmitted */
Wire.write(0x40); /* For Data Transmission, C = 0 and D/C = 1 */
Wire.write(0x00);
Wire.endTransmission(); /* Transmit the queued bytes and end transmission to slave device */
}
}
void OLED_setXY(char col_start, char col_end, char page_start, char
page_end) /* Function for setting cursor for writing data */
{
Wire.beginTransmission(OLED_Write_Address); /* Begin transmission to slave device */
/* Queue data to be transmitted */
Wire.write(0x00); /* For Data Transmission, C = 0 and D/C = 0 */
Wire.write(0x21); /* Set Column Start and End Address */
Wire.write(col_start); /* Column Start Address col_start */
Wire.write(col_end); /* Column End Address col_end */
Wire.write(0x22); /* Set Page Start and End Address */
Wire.write(page_start); /* Page Start Address page_start */
Wire.write(page_end); /* Page End Address page_end */
Wire.endTransmission(); /* Transmit the queued bytes and end transmission to slave device */
}
void OLED_init(void) /* Function for initializing OLED */
{
OLED_Command(0xAE); /* Entire Display OFF */
OLED_Command(0xD5); /* Set Display Clock Divide Ratio and Oscillator Frequency */
OLED_Command(0x80); /* Default Setting for Display Clock Divide Ratio and Oscillator Frequency that is recommended */
OLED_Command(0xA8); /* Set Multiplex Ratio */
OLED_Command(0x3F); /* 64 COM lines */
OLED_Command(0xD3); /* Set display offset */
OLED_Command(0x00); /* 0 offset */
OLED_Command(0x40); /* Set first line as the start line of the display */
OLED_Command(0x8D); /* Charge pump */
OLED_Command(0x14); /* Enable charge dump during display on */
OLED_Command(0x20); /* Set memory addressing mode */
OLED_Command(0x00); /* Horizontal addressing mode */
OLED_Command(0xA1); /* Set segment remap with column address 127 mapped to segment 0 */
OLED_Command(0xC8); /* Set com output scan direction, scan from com63 to com 0 */
OLED_Command(0xDA); /* Set com pins hardware configuration */
OLED_Command(0x12); /* Alternative com pin configuration, disable com left/right remap */
OLED_Command(0x81); /* Set contrast control */
OLED_Command(0x80); /* Set Contrast to 128 */
OLED_Command(0xD9); /* Set pre-charge period */
OLED_Command(0xF1); /* Phase 1 period of 15 DCLK, Phase 2 period of 1 DCLK */
OLED_Command(0xDB); /* Set Vcomh deselect level */
OLED_Command(0x20); /* Vcomh deselect level ~ 0.77 Vcc */
OLED_Command(0xA4); /* Entire display ON, resume to RAM content display */
OLED_Command(0xA6); /* Set Display in Normal Mode, 1 = ON, 0 = OFF */
OLED_Command(0x2E); /* Deactivate scroll */
OLED_Command(0xAF); /* Display on in normal mode */
}
void setup() {
Serial.begin(9600);
Wire.begin(); /* Initiate wire library and join I2C bus as a master */
OLED_init(); /* Initialize OLED */
OLED_clear();
}
void loop() {
while (Serial.available()>0){
delay(10); // give a slight delay to let the serial buffer receive the full byte
char c=Serial.read();
readString+=c;
}
if (readString.length()>0){
int str_len=readString.length()-1; //
start converting the string to a char array. Get rid of the "\0"
terminator in the string.
https://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/
char char_array[str_len];
readString.toCharArray(char_array,str_len);
Serial.println(char_array); // debug purpose
OLED_Data(char_array); // this code can
only handle less than 16 characters once through the HC-05
readString=""; // clear the reciever to be ready for the next data
}
}
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
MPU6050_Init();
char buff[50];
MPU6050_Data mpu6050_data;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
mpu6050_data = MPU6050_Read();
for (unsigned int i = 0; i < sizeof(buff); i++) {buff[i] = '\0';}
sprintf((char*)buff, "Accelerometer X = %.2f g\r\n", mpu6050_data.accelerometer.x);
HAL_UART_Transmit(&huart2, buff, sizeof(buff), HAL_MAX_DELAY);
for (unsigned int i = 0; i < sizeof(buff); i++) {buff[i] = '\0';}
sprintf((char*)buff, "Accelerometer Y = %.2f g\r\n", mpu6050_data.accelerometer.y);
HAL_UART_Transmit(&huart2, buff, sizeof(buff), HAL_MAX_DELAY);
for (unsigned int i = 0; i < sizeof(buff); i++) {buff[i] = '\0';}
sprintf((char*)buff, "Accelerometer Z = %.2f g\r\n", mpu6050_data.accelerometer.z);
HAL_UART_Transmit(&huart2, buff, sizeof(buff), HAL_MAX_DELAY);
for (unsigned int i = 0; i < sizeof(buff); i++) {buff[i] = '\0';}
sprintf((char*)buff, "Gyroscope X = %.2f °/sec\r\n", mpu6050_data.gyroscope.x);
HAL_UART_Transmit(&huart2, buff, sizeof(buff), HAL_MAX_DELAY);
for (unsigned int i = 0; i < sizeof(buff); i++) {buff[i] = '\0';}
sprintf((char*)buff, "Gyroscope Y = %.2f °/sec\r\n", mpu6050_data.gyroscope.y);
HAL_UART_Transmit(&huart2, buff, sizeof(buff), HAL_MAX_DELAY);
for (unsigned int i = 0; i < sizeof(buff); i++) {buff[i] = '\0';}
sprintf((char*)buff, "Gyroscope Z = %.2f °/sec\r\n\r\n", mpu6050_data.gyroscope.z);
HAL_UART_Transmit(&huart2, buff, sizeof(buff), HAL_MAX_DELAY);
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/