STM32 Nucleo and Discovery boards

0 nhận xét
I have used STM32 Nucleo and Discovery boards for some of my projects, and I also have a and F7 Disco, so I went through an evaluation process similar to yours. Simplest free/cheap IDE for STM32F7 discovery board

Here are some pieces of advice, take them as personal, experience based, opinions:

0- If these are your first steps in ARM/STM32 programming, you have chosen a (IMO) very good product, but a steep learning curve: the F7 Disco is a very complex board, with lots of sophisticated peripherals (both on chip and on board), and limited access to I/O pins (just the Arduino connectors). Arduino is a different world.
1- Take your time to learn the platform. The amount of information available is daunting: the Reference manual for the processor is 1655 pages. You must know it by heart, including the errata (half joking, but read the MCU datasheet, and I was bitten by one of the errata).
2- STM32F7 does away with the classical STD Peripheral libraries, so it's either hand written code for register access or HAL. I'd go for HAL for anything more complex than a blinky.
3- Use STM32CubeMX to generate clock and peripheral initialization code. Doing it by hand is possible (and definitely a good learning exercise to be done at least once or twice), but tedious and error prone: life is too short. The generated code can be easily included in a project of your favorite IDE.

Now, to the environments:

0- I use both Windows and Linux, but the better IDEs are, for whatever reasons, Windows only. My STM32 coding is 99% under Windows.

Free:

0- Go Commando. Use you preferred editor, gcc, makefiles, openocd. You'll show how tough you are, and pull a lot of hair (the rest will grow white...). Advisable only if you have lot of time, patience and some experience, but you'll learn a lot...maybe too much.
1- The free version of professional packages for ARM are, in general code size limited (32kB). With a complex board as the F7 Disco, it's easy to hit this limit when you start using on-board peripherals. I don't dislike Keil (and it's unlimited, free for STM32F0 cores!)
2- The greatest part of free IDE are Eclipse based. Eclipse is a powerful piece of SW, but I find it very complex and often not so intuitive (I'm also professionally exposed to it).
3- System Workbench is Eclipse based, with all the advantages and disadvantages, but it was the first (AFAIK) to explicitly support the F7 (no surprise, coming from ST itself). It's also well integrated with CubeMX. For free, I find it a reasonable choice. I did not have any great problem with flashing.
4- CooCOX CoIde is a (very) pared down Eclipse IDE. Easy to use with F0-F4, some glitches and frequent crashes (at least for me). Unfortunately, no F7 support roadmap and glacial updates (at least lately). It can be coaxed to compile for F7 (just use F4, same instruction set) but no flash or debug support (IIRC), so I would not advise to use it (at the moment).
5- Em::blocks (now EmBitz) is lean and liked by many. It has almost complete F7 support (still a bit buggy, check the forum). I'm not so keen on its look and feel, but it might be viable and it's worth a try.
6- EmIde: last version is more than one year old, so no F7 support.
7- Coming from Arduino, you might want to consider mbed, similar in many ways, different in others. Not for me, but YMMV. F7 Disco is supported.


Commercial:

0- Most IDE have a time trial or code size limited version. Try them!
1- Professional IDEs are really out of reach for a hobby. Definitely worth it for work, mostly due to real support. If you're just "having fun" consider what you might get with the same amount of money (>2000€) in terms of test and measurement instruments...
2- Crossworks: 150$ for a non-commercial license, if you intend to ever sell your code/product it's a steep 1500$ for a commercial one! No explicit F7 Disco board support package, but F7 is supported (though not 746). Worth trying, but the fixed editor keybindings (I have got Emacs finger-bindings) and lack of editor automation are a sad omission.
3- Visual Studio+Visualgdb. 74€+taxes for the embedded version. Full F7 Disco support, regular updates to GCC and other tools (openocd), 30-day trial. The product works very well, gives you either "do it for me" or detailed control on the build process and includes all libraries and board support packages (not only ST), very good debug support. This is very much a question of taste: Visual Studio Community Edition 2015 is free (and not crippled in any significant way) and it's a very complete, stable and well supported IDE. It comes from Microsoft, though, and this can turn away someone.

What do I use?

For some time, I have been using System Workbench (it works!) and trying all of the above environments. I recently bit the bullet and bought Visualgdb. I do not regret the money spent.

Links:

Keil: https://www.keil.com/arm/demo/eval/arm.htm, http://www2.keil.com/stmicroelectronics-stm32/mdk
CoIde: http://coocox.org/
Em::blocks: http://www.emblocks.org/web/
Crossworks: http://www.rowley.co.uk/arm/index.htm
Visualgdb: http://visualgdb.com/
mbed: https://www.mbed.org/

Some additional considerations:


- I would not like to give the impression that I'm and expert in the embedded field. I work with SW (a lot) and HW (seldom), but there are real professionals in this forum.

- That said, I've been playing with micros for almost 40 years now (designed and built my own 6502 board in '77, and wrote its monitor) ...so I do have some experience (and yes, you can get off my lawn).

- I've checked again Crossworks after answering, and it's much better than I remembered. Definitely worth a try, if the cost and license do not bother you. I still prefer VS+vgdb, though.

- I might have forgotten other options and possibilities, let's hope someone else chimes in. The MCU and the board are still fairly new, so things are changing day by day.

- HAL and mbed. Long story short, mbed like Arduino platform has its uses. It's easy to move the first steps, and there's a community behind it (less than with Arduino). I'm left wanting for more with both (even forgetting about their poor excuses for an IDE...). mbed is multi-vendor (good) but online only (bad). HAL is used by mbed STM32 target, see e.g. serial_api.c, albeit I fear that trying to use both at the same time would not be a good idea. Also, the openness of mbed is a bit under discussion (not that Arduino is free of problems...).

- I have no experience with TI's or NXP's platforms. Some time ago I was given a free F429 Discovery at a demo event, and with Nucleos 10$ a pop, I got addicted...a nice pusher job, was that. Now I crave the heavy stuff (F7...).

- Simpler libraries based upon HAL exist, ST provides BSPs for all of the Eval/Nucleo/Disco boards, with loads of example code for all the peripherals. Alternatively, you could check something like this: http://stm32f4-discovery.com/

- I can't help you with books. I like reading datasheets and programmer's references...now, don't look at me like that!

- If you're not doing this for work (but even then...), find the way that gives you the most fun. These days we have so many free or cheap options for both SW and HW, that is not difficult to find something for one's tastes. Internet religion wars (Microsoft/Linux/Apple, mbed/HAL/bare metal etc.) are a waste of time and energy. Use what you feel comfortable with, and change with no regrets.

- Yes, that board is really sweet (but for the very few I/Os)!

source: http://www.eevblog.com

Use Stm32cubemx to get data from nunchuck

0 nhận xét
In this topic I will show how to connect nunchuck controller with stm32microcontroller by using software stm32cubemx .
Nunchuck controller is used i2c interface.

To set up i2c interface with stm32cubemx you can view at this topic
Nunchuck address is 0x52.Clock was set up with 100kHz.


.........................................................
#define nunchuck 0x52<<1

I2C_HandleTypeDef hi2c1;
 ......................................................................

int main(void)
{

  ...............................................................................

  char cmd[1];
  uint8_t data[6];
  uint8_t  handShake[2] ;
  handShake[0]=0xf0;
  handShake[1]=0x55;
  HAL_I2C_Master_Transmit(&hi2c1,nunchuck,handShake, 2,0x1000);
  HAL_Delay(100);
  handShake[0]=0xfb;
  handShake[1]=0x00;
  HAL_I2C_Master_Transmit(&hi2c1,nunchuck,handShake, 2,0x1000);
  HAL_Delay(100);
  while (1)
  { 

...............................................................................................
   cmd[0]=0x00;
    HAL_I2C_Master_Transmit(&hi2c1,nunchuck,cmd, 1,0x1000);
    HAL_Delay(1);
   HAL_I2C_Master_Receive(&hi2c1, nunchuck, data,6,0x1000);
   uint16_t   accelX = (data[2] << 2)|((data[5] >> 2) & 0x03) ;
   uint16_t  accelY = (data[3] << 2)|((data[5] >> 4) & 0x03) ;
   uint16_t  accelZ = (data[4] << 2)|((data[5] >> 6) & 0x03) ;
   uint8_t buttonc=(data[5]&0x02)>>1;
   uint8_t buttonz=data[5]&0x01;
   ...........................................................
  }
 

 .........................................................................



How to embed a YouTube video, Googlemap, iframe responsive on blogger, wordpress

0 nhận xét
This article show you how to embed a YouTube video responsive and Google map, i frame responsive very easy and quickly on WordPress, Blogger (blogspot) or any html page
How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress

Responsive with YouTube video
Step 1: 
copy a link  of youtube
Step 2:
Go to http://embedresponsively.com/
Step 3: paste link  , choose the type: YouTube
How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress

Step 4 get  embed code
How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress
Step 5: paste to a html on WordPress of blogspot
Responsive with Google map, iframe:
Step 1: Search your place and click to menu of map

How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress
Step 2: Choose share or embed funtion
How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress

Step 3: Choose embed map
How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress

Step 4: Copy the iframe
Step 5: go to http://embedresponsively.com/ and work as my previous introduction

Finally- this is the result:
How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress
on iphone 4 check

How to embed a YouTube video, Googlemap, iframe  responsive on blogger, wordpress
on destop check

How to responsive image on Blogger- BlogSpot on Moblie, Windows phone

0 nhận xét
When you open any page on Desktop, you easy to see your image on page keep the standard site. On Mobile display, you often using img {max-with :100%} for design responsive. But may it broken and incorrect on Home page or special page..
How to  responsive  size of image on Blogger- BlogSpot on Moblie, Windows phone
I going to help you fix responsive  size of image on Blogger- BlogSpot on Moblie, Windows phone error
1- The first you using:
img{max-width:100%}

2- Using this code before </body> or after  </header>
This code help you identify the size of image only on page. Note the important after.
<b:if cond='data:blog.pageType == &quot;item&quot;'><style>.entry-content span img {  height:auto!important}</style></b:if>
Before

After
Fix the error of image on Windows phone
Please do not insert caption on your page with x- lager size. It will be not responsive on Mobile.

Lidar lite + Stm32

0 nhận xét
In this topic I show how to get data from Lidar sensor from stm32 family microcontroller. In my test , stm32f7 discovery board used.
Use STM32CUBEMX to set up i2c interface.   You can check this topicIn my code I reference from this site.


After generated add in main.c file

#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_sdram.h"
#include "stm32f7xx_hal_ltdc.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32f7xx_ll_fmc.h"
/* USER CODE BEGIN Includes */
#define LIDAR_ADD 0x62<<1
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */


/* Private variables ---------------------------------------------------------*/


I2C_HandleTypeDef hi2c1;


/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */


/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);

void configure(int configuration)

{
  uint8_t cmd[1];
  cmd[0]=0x04;
  HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x00, 1, cmd,1,0x100);
  switch (configuration)
  {
    case 0: // Default mode, balanced performance
      cmd[0]=0x80;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0x1000);
      cmd[0]=0x08;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0x1000);
      cmd[0]=0x00;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0x1000);
    break;

    case 1: // Short range, high speed

      cmd[0]=0x1d;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0x1000);
      cmd[0]=0x08;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0x1000);
      cmd[0]=0x00;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0x1000);
    break;

    case 2: // Default range, higher speed short range

      cmd[0]=0x80;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0x1000);
      cmd[0]=0x00;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0x1000);
      cmd[0]=0x00;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0x1000);
    break;

    case 3: // Maximum range

      cmd[0]=0xff;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0x1000);
      cmd[0]=0x08;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0x1000);
      cmd[0]=0x00;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0x1000);
    break;

    case 4: // High sensitivity detection, high erroneous measurements

      cmd[0]=0x80;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0x1000);
      cmd[0]=0x08;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0x1000);
      cmd[0]=0x80;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0x1000);
    break;

    case 5: // Low sensitivity detection, low erroneous measurements

      cmd[0]=0x80;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0x1000);
      cmd[0]=0x08;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0x1000);
      cmd[0]=0xb0;
      HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0x1000);
    break;
  }


int main(void)

{

  /* USER CODE BEGIN 1 */


  /* USER CODE END 1 */


  /* MCU Configuration----------------------------------------------------------*/


  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();
  MX_I2C1_Init();
  BSP_LCD_Init();
  BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
  BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
  BSP_LCD_DisplayOn();
  BSP_LCD_SelectLayer(0);
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SelectLayer(1);
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  //configure(0,LIDAR_ADD);
 uint8_t data[2]={10};
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  configure(3,LIDAR_ADD);
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  uint8_t cmd[0];
  int distance;
  char buffer[30];
  while (1)
  {
       cmd[0]=0x04;
       HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x00, 1, cmd,1,0x100);
       cmd[0]=0x8f;
       HAL_I2C_Master_Transmit(&hi2c1,LIDAR_ADD,cmd, 1,0x100);  
       HAL_I2C_Master_Receive(&hi2c1, LIDAR_ADD, data,2,0x100);
       distance =(data[0]<<8)|(data[1]);
       sprintf(buffer,"Distance=%d [cm]",distance);
       BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)buffer, CENTER_MODE);
       HAL_Delay(50);
       BSP_LCD_Clear(LCD_COLOR_WHITE);
  }
  /* USER CODE END 3 */


...................................................................................



KRS Series Kondo servo with ROS

0 nhận xét
We will going to see the code below my introduction about he code below:
KRS Series Kondo servo with ROS


Please see this code:


#!/usr/bin/env python
# license removed for brevity
import rospy
from geometry_msgs.msg import PointStamped
import serial
import time
import struct
import array
import numpy
com = serial.Serial()
com.port='/dev/ttyUSB0'
com.baudrate=115200
com.bytesize=8
com.parity=serial.PARITY_EVEN
com.stopbits=1
com.timeout=0.1
com.open()
buf=bytearray(3)
temp_=bytearray(2)
target_angle=7500
angle=0.0
a_=270.0/8000.0
b_=-135.0-a_*3500.0
def callback(data):
    global target_angle
    global angle
    angle=data.point.x
    target_angle=(int)((angle-b_)/a_)
    if(target_angle>11500):
        target_angle=11500
    elif(target_angle<3500 ) :
        target_angle=3500
def data_pub():
    pub = rospy.Publisher('servo_angle', PointStamped, queue_size=100)
    rospy.Subscriber("master_angle",  PointStamped, callback)   
    rospy.init_node('servo_node', anonymous=True)
    r = rospy.Rate(100) # 100hz
    global target_angle
    global angle
    data_msg= PointStamped();
    while not rospy.is_shutdown():
        data_msg.header.stamp=rospy.get_rostime()
        buf[0]=0x80
        buf[1]=(target_angle>>7)&0x7f
        buf[2]=target_angle&0x7f
        com.write(buf)
        buf_read=com.read(6)
        if (len(buf_read)==6):
          temp_[0]=ord(buf_read[4])
          temp_[1]=ord(buf_read[5])
          val=temp_[1]|(temp_[0]<<7)
          print "%d" %(val)
          data_msg.point.x=angle
          data_msg.point.y=(float)(val)*a_+b_
          pub.publish(data_msg)
       
        r.sleep()
if __name__ == '__main__':
    try:
        data_pub()
    except rospy.ROSInterruptException: pass

Raspberry pi IoT example (upload 6 dof imu sensor data to web)

0 nhận xét
In my test , I use raspberry pi to get data of  imu sensor and temperature sensor and public it to local web server . You also public it to internet  by openning port of your router. I use ssh to remote control raspberry pi.
Use:
       Raspberry pi 3 model B

      6dof sensor included temperature sensor. :
Accelerometer Adxl345 and gyro scope Itg3200

1. Access raspberry pi
sudo ssh pi@192.168.0.1
Input your password
2.
mkdir python
cd python
mkdir templates
before edit code you may be install smbus , flask ...package like
 sudo apt-get install python-flask
 sudo apt-get install python-smbus

..etc
3.
vim test.py
code
--------------------------------------------------------------------------------------------------------------------
import smbus            # use I2C
import math             # mathmatics
from flask import Flask ,render_template, request, jsonify
from time import sleep  # time module
app = Flask(__name__)
#
# define
#
# slave address
GYRO_ADD = 0x68         # device address
ACC_ADD = 0x53         # device address
# register address

ACC_XOUT = 0x32
ACC_YOUT = 0x34
ACC_ZOUT = 0x36
TEMP_OUT = 0x41
GYRO_XOUT = 0x1D
GYRO_YOUT = 0x1F
GYRO_ZOUT = 0x21
TEMP_OUT = 0x1B

bus = smbus.SMBus(1)

def write_data(ADD,adr,val):
    bus.write_byte_data(ADD,adr,val)
    write_data(GYRO_ADD,0x16,0x1A)
    write_data(GYRO_ADD,0x15,0x09)
    write_data(ACC_ADD,0x2D,0x08)
# Sub function
#
# 1byte read
def read_byte(ADD,adr):
    return bus.read_byte_data(ADD, adr)
# 2byte read high->low
def read_word_hl(ADD,adr):
    high = bus.read_byte_data(ADD, adr)
    low = bus.read_byte_data(ADD, adr+1)
    val = (high << 8) + low
    if (val >= 0x8000):         # minus
        return -((65535 - val) + 1)
    else:                       # plus
        return val
# 2byte read low->high
def read_word_lh(ADD,adr):
    low = bus.read_byte_data(ADD, adr)
    high = bus.read_byte_data(ADD, adr+1)
    val = (high << 8) + low
    if (val >= 0x8000):         # minus
        return -((65535 - val) + 1)
    else:                       # plus
        return val
# Main function
#
@app.route("/",methods=['GET', 'POST'])
def loop():
    while 1:
        gyro_x=float(read_word_hl(GYRO_ADD,GYRO_XOUT))/14.375
        gyro_y=float(read_word_hl(GYRO_ADD,GYRO_YOUT))/14.375
        gyro_z=float(read_word_hl(GYRO_ADD,GYRO_ZOUT))/14.375
        acc_x=float(read_word_lh(ACC_ADD,ACC_XOUT))/255*9.8
        acc_y=float(read_word_lh(ACC_ADD,ACC_YOUT))/255*9.8
        acc_z=float(read_word_lh(ACC_ADD,ACC_ZOUT))/255*9.8
        temp_=(float(read_word_hl(GYRO_ADD,TEMP_OUT))+13200)/280+35
        data_gyro ='vx=%2.2f___ vy=%2.2f___vz=%2.2f' %(gyro_x,  gyro_y,  gyro_z)
        data_acc=  'ax=%2.2f___ay=%2.2f___az=%2.2f' %(acc_x,  acc_y,  acc_z)
        data_temp=  '%2.2f' %temp_
        return render_template('index.html', gyro =data_gyro,acc=data_acc,temp=data_temp)

if __name__ == "__main__":
  app.run(host="0.0.0.0", port=1010,debug=True)


--------------------------------------------------------------------------------------------------------
 save and create new file:
vim template/index.html
 code
--------------------------------------------------------------------------------------------------------
<html>
     <body>
               <h1>Gyro: {{gyro}}</h1>
               </br>
               <h1>Acc : {{acc}}</h1>
               </br>
                <h1>Temperature : {{temp}} &degC</h1>
      </body>
</html>

----------------------------------------------------------------------------------------------------------

run
sudo python test.py
save it and open your browser as firefox.

In my case :
192.168.0.1:1010






Connect your MPU to PC with bluetooth device HC 06

0 nhận xét
This topic is show how to send data from MPU to PC.

Mostly , When we get data from sensor and send it to PC , usb- serial module is used. It's very simple but because data sent by wire that you have set MPU near from PC. Wireless connection between MPU and PC is can do with xbee module , but it's expensive and you have had 2 modules, one for PC and another for MPU.
Xbee module
More simple and cheap method is use HC-06 bluetooh module and if your PC had bluetooth device you have had only 1 moudle.
You can buy it at amazon.
1.Connect MPU to usb serial module to set baudrate and name
usb-serial moudle              Bluetooth Module
Gnd..................................Gnd
Tx.....................................Rx
Rx.....................................Tx
Vcc................................... +5V

2. Setting
Open Arduino ->Serial monitor
Set name:  AT+NAME+name
ex: name is ngoduong ->AT+NAMEngoduong
Set baudrate : AT+BAUDn
ex : baudrate 115200 -> AT+BAUD8
AT+BAUD1   -> 1200
AT+BAUD2   -> 2400
AT+BAUD3   ->4800
AT+BAUD4   ->9600
AT+BAUD5   ->19200
AT+BAUD6   ->38400
AT+BAUD7   ->57600
AT+BAUD8   ->115200


3.Pair with your PC
I using ubuntu, so that i use Blueman applet app.
Open Blueman application ->Search ->Add ->Setup->Use custom passkey
Input 1234 -> Forward

waitting...
If it ok -> chose Serial port ->Forward
4. Now you can connect it to you MPU
MPU              Bluetooth Module
Gnd..................................Gnd
Tx.....................................Rx
Rx.....................................Tx
Vcc................................... +5V
To send serial data with stm32 please click


Stm32cube+ Freertos I2C communication between master and slave

0 nhận xét
In this topic , I use HAL library and stm32cube to create i2c communication between master and slave microcontroller. In my test stm32f7 nucleo used as the master and stm32f7 discovery used as the slave.
Stm32cube+ Freertos I2C communication between master and slave
Stm32cube+ Freertos I2C communication between master and slave 
1. Master configuration.
1.1 GPIO and system clock setting
      RCC clock setting RCC-> High speed clock (hse) -> crystal
      Up to 216MHz
      I2C setting  I2C1  -> PB8 : SCL    PB9 :SDA
      USART setting USART3 ->   asynchronous PD9 : TX    PD8: RX




1.2 I2C and Serial setting
      I2C mode ->standard mode
      Frequency-> 100kHz
      USART baud rate -> 115200
      USART word length 8 Bits


2. Slave configuration
2.1 GPIO and system clock setting
      RCC clock setting RCC-> High speed clock (hse) -> crystal
      Up to 216MHz
      I2C setting  I2C1  -> PB8 : SCL    PB9 :SDA
      FreeRTOS -> Checked



2.2  I2C and FreeRTOS setting
      I2C mode ->standard mode
      Frequency-> 100kHz
      Primary slave address 0x6B.
      RTOS_setting -> Add task and queues

 


3.Generate program and edit Src/main.c file
Master 
#include "stm32f7xx_hal.h"
#define ADD 0x6b

I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;
DMA_HandleTypeDef hdma_i2c1_tx;

UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart3_tx;
DMA_HandleTypeDef hdma_usart3_rx;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART3_UART_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_I2C1_Init();
  MX_USART3_UART_Init();

    int16_t i=100;
    char cmd[2];
    char cmd_1[2];
    char buf[40];

    while (1)
      {
        cmd[0]=i>>8;
        cmd[1]=i&0xff;
        HAL_I2C_Master_Transmit(&hi2c1,ADD<<1,cmd,2,1000);
        HAL_I2C_Master_Receive(&hi2c1,ADD<<1,cmd_1,2,1000);
        int16_t a= cmd_1[0]<<8|cmd_1[1];
        memset(buf,'\0',sizeof(buf));   
        sprintf(buf,"I send%:d..I receive:%d\n",i,a);
        HAL_UART_Transmit( &huart3,buf,sizeof(buf),0x100);
        i+=1;
        HAL_Delay(1);

  }
  /* USER CODE END 3 */

}

........................................................


Slave
Edit Src/main.c

#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_sdram.h"
#include "stm32f7xx_hal_ltdc.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32f7xx_ll_fmc.h"
#include "string.h"
#include "cmsis_os.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;
DMA_HandleTypeDef hdma_i2c1_tx;

osThreadId defaultTaskHandle;
osThreadId myTask02Handle;
osMessageQId myQueue01Handle;

void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_I2C1_Init(void);
void StartDefaultTask(void const * argument);
void StartTask02(void const * argument);


int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_I2C1_Init();

       BSP_LCD_Init();
     BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
    BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
    SPI2->DR=1;
    BSP_LCD_DisplayOn();
  
    BSP_LCD_SelectLayer(0);
     BSP_LCD_Clear(LCD_COLOR_BLACK);
 
    BSP_LCD_SelectLayer(1);
    BSP_LCD_Clear(LCD_COLOR_BLACK);
 
    BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
    BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);



  /* Create the thread(s) */
  /* definition and creation of defaultTask */
    osThreadDef(defaultTask, StartDefaultTask, osPriorityRealtime, 0, 512);
    defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* definition and creation of myTask02 */
    osThreadDef(myTask02, StartTask02, osPriorityIdle, 0, 256);
    myTask02Handle = osThreadCreate(osThread(myTask02), NULL);

    osMessageQDef(myQueue01,16, uint16_t);
    myQueue01Handle = osMessageCreate(osMessageQ(myQueue01), NULL);


      osKernelStart();
  
      while (1)
      {


     }
 

}
.................................................................................................
void StartDefaultTask(void const * argument)
{
char buffer[2];
int16_t data;
 

  for(;;)
  {
    HAL_I2C_Slave_Receive(&hi2c1, buffer,2,0x10000);
    HAL_I2C_Slave_Transmit(&hi2c1, buffer,2,0x1000);
    data=(buffer[0]<<8)|(buffer[1]);
    osMessagePut(myQueue01Handle, (int16_t)data, 2);
        osDelay(1);

  }
  /* USER CODE END 5 */ 
}

/* StartTask02 function */
void StartTask02(void const * argument)
{
osEvent evt;
 
char buf[2]="";
int16_t data_receive;

  for(;;)
  {
    evt = osMessageGet(myQueue01Handle,0x1000);
    if (evt.status == osEventMessage) {
        data_receive=evt.value.p ;
        sprintf(buf,"Data receive =%d",data_receive);
        BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)buf, CENTER_MODE);
     } else {
           BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"Failed", CENTER_MODE);

    }
    osDelay(10);
  }
  /* USER CODE END StartTask02 */
}

Stm32f4 - I2C LCD module

0 nhận xét
Use Stm32cubemx to configure I2C.
Connect
LCD               MCU
Vdd                3V
Gnd                Gnd
SCL                 PB6
SDA                 PB9

1. Pin out setting

RCC-> High Speed clock -> Crystal ceramic resonator
Up system clock to 216 Mhz
I2C1 -> I2C ->I2C
2. I2C configuration
Maximum output speed -> High

3. Code generation and edition code

In Src/main.c add code:

#include "stm32f4xx_hal.h"
#define LCD_ADD     0x3E<<1


/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);

int main(void)
{

    HAL_Init();

  /* Configure the system clock */
    SystemClock_Config();

  /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2C1_Init();

    uint8_t cmd[1];
    cmd[0]=0b00111000;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);
    cmd[0]=0b00111001;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);
    cmd[0]=0b00010100;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);
    cmd[0]=0b01111101;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);
    cmd[0]=0b01010110;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);
    cmd[0]=0b01101100;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);
    cmd[0]=0b00001100;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);
    cmd[0]=0b00000001;
    HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0x100);
    HAL_Delay(10);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
    int i=0;
    int j=0;

  while (1)
  {
     char buffer[40]="Hello, Welcome to www.ngolongtech.net";
     HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, 0b00000010,1,0x100);
         for(i=j;i<=strlen(buffer);i++){
        cmd[0]=buffer[i];
        HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x41, 1, cmd,1,0x100);
        HAL_Delay(1);
         }
         j++;
            if(j==(sizeof(buffer)-10)){
            j=0;
         }
         
     HAL_Delay(500);

  }

}

 ------------------------------------------------------------------------------

Share knowledge

Copyright © 2016 - Share knowledge ®