Skip to content

Latest commit

 

History

History
107 lines (77 loc) · 2.3 KB

part-11-float.md

File metadata and controls

107 lines (77 loc) · 2.3 KB

Part 11 - float

Today we are going to handle the float data type. In the Pico there is no co-processor to handle floating-point numbers as this is handled through a series of functionality through software in the API.

Let's work with a simple example. 0x05_float.c as follows.

#include <stdio.h>
#include "pico/stdlib.h"

int main() 
{
  stdio_init_all();

  while(1) 
  {
    float x = 40.5;

    printf("%f\n", x); 

    sleep_ms(1000);
  }

  return 0;
}

Very simply we assign a float of 40.5 into x and print it with the _%f _format modifier and then sleep for 1 second.

Let's make a new dir 0x05_float and add our CMakeLists.txt file in it.

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(test_project C CXX ASM)
set(CMAKE_C_STANDARD 11) 
set(CMAKE_CXX_STANDARD 17) 
pico_sdk_init()

add_executable(0x05_float
  0x05_float.c
)

pico_enable_stdio_usb(0x05_float 1)

pico_add_extra_outputs(0x05_float)

target_link_libraries(0x05_float pico_stdlib)

Next we need to copy the pico_sdk_import.cmake file from the external folder in the pico-sdk installation to the 0x05_float project folder.

cp ../pico-sdk/external/pico_sdk_import.cmake .

Finally we are ready to build.

mkdir build
cd build
export PICO_SDK_PATH=../../pico-sdk
cmake ..
make

Then simply copy the .uf2 file to the drive.

cp 0x05_float.uf2 /Volumes/RPI-RP2

Then we need to locate the USB drive so you can do the following.

ls /dev/tty.

Press tab to find the drive and then in my case I will use screen to connect.

screen /dev/tty.usbmodem0000000000001

You should see a an 40.5 being printed every second.

40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000

In our next lesson we will debug.