Cours:PiPico : Différence entre versions

De troyesGEII
Aller à : navigation, rechercher
(Pwm)
(i2c scanner)
Ligne 103 : Ligne 103 :
 
#include "pico/binary_info.h"
 
#include "pico/binary_info.h"
  
// I2C defines
 
// This example will use I2C0 on GPIO8 (SDA) and GPIO9 (SCL) running at 400KHz.
 
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
 
#define I2C_PORT i2c0
 
#define I2C_SDA 8
 
#define I2C_SCL 9
 
  
 
int64_t alarm_callback(alarm_id_t id, void *user_data) {
 
int64_t alarm_callback(alarm_id_t id, void *user_data) {

Version du 19 mars 2025 à 10:58

Brochage

https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf

utilisation de l'ide arduino

https://github.com/earlephilhower/arduino-pico


Créer/configurer un projet VScode ou QtCreator

Création assistée d'un projet

À l'aide de pico_project.py :

Attention, ne pas oublier de spécifier le chemin vers le SDK :

  • export PICO_SDK_PATH=../../pico-sdk en remplaçant ../../pico-sdk par le chemin vers le SDK.

Ouverture avec VScode

Dans VSCode, ouvrir le dossier contenant le projet (ie le fichier CMakeLists.txt), en sélectionnant GCC for arm-none-eabi commme compilateur dans le menu déroulant de configuration du projet.

Ouverture avec QTCreator

QtCreator est capable de lire et interpréter le fichier CMakeLists.txt comme descripteur de projet, il suffit donc

  • d'ouvrir (comme projet) le fichier CMakeLists.txt

Registres

gpio

#include "pico/stdlib.h"
#include "hardware/regs/addressmap.h"

unsigned int& gpioOUT = *(unsigned int*) (SIO_BASE + 0x10);
unsigned int& gpioOE  = *(unsigned int*) (SIO_BASE + 0x20);

unsigned int& gpioCtrl0 = *(unsigned int *) (IO_BANK0_BASE+0x4);
//facultatif
unsigned int& padsBank0_GPIO0 = *(unsigned int *) (PADS_BANK0_BASE+0x4);


int main() {
    // facultatif
    padsBank0_GPIO0|=(1<<6); // input enable
    padsBank0_GPIO0&=~(1<<7); // output disable
    // piloter la broche avec fonction SIO
    gpioCtrl0=5;
    // sortie enable
    gpioOE=1<<0;
    while (true)
    {
        gpioOUT&=~(1<<0);
        sleep_ms(200);
        gpioOUT|=(1<<0);
        sleep_ms(10);
    }
}

GPIO

https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#hardware_gpio

Syntaxe void gpio_init(uint gpio)
Paramètres
gpio numéro de gpio
  • gpio_put(LED_PIN, 1);
  • gpio_set_dir(LED_PIN, GPIO_OUT);
  • gpio_set_pulls (uint gpio, bool up, bool down)

Pwm

  • gpio_set_function(PICO_DEFAULT_LED_PIN, GPIO_FUNC_PWM);
  • uint slice_num = pwm_gpio_to_slice_num(PICO_DEFAULT_LED_PIN);
  • pwm_config config = pwm_get_default_config();
  • pwm_config_set_clkdiv(&config, 4.f);
  • pwm_init(slice_num, &config, true);
  • pwm_set_gpio_level(PICO_DEFAULT_LED_PIN, fade * fade); // rapport cyclique 16 bits

I2C

i2c scanner

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "hardware/timer.h"
#include "hardware/clocks.h"
#include "pico/binary_info.h"


int64_t alarm_callback(alarm_id_t id, void *user_data) {
    // Put your timeout handler code in here
    return 0;
}



bool reserved_addr(uint8_t addr) {
    return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
int main()
{
    stdio_init_all();

    // I2C Initialisation. Using it at 400Khz.
    i2c_init(I2C_PORT, 100*1000);
    
    gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
    gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
    gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
    gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
    // For more examples of I2C use see https://github.com/raspberrypi/pico-examples/tree/master/i2c
    bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));

    // Timer example code - This example fires off the callback after 2000ms
    add_alarm_in_ms(2000, alarm_callback, NULL, false);
    // For more examples of timer use see https://github.com/raspberrypi/pico-examples/tree/master/timer

    printf("System Clock Frequency is %d Hz\n", clock_get_hz(clk_sys));
    printf("USB Clock Frequency is %d Hz\n", clock_get_hz(clk_usb));
    // For more examples of clocks use see https://github.com/raspberrypi/pico-examples/tree/master/clocks

    while (true) {
        printf("\nI2C Bus Scan\n");
        printf("   0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F\n");
    
        for (int addr = 0; addr < (1 << 7); ++addr) {
            if (addr % 16 == 0) {
                printf("%02x ", addr);
            }
    
            // Perform a 1-byte dummy read from the probe address. If a slave
            // acknowledges this address, the function returns the number of bytes
            // transferred. If the address byte is ignored, the function returns
            // -1.
    
            // Skip over any reserved addresses.
            int ret;
            uint8_t rxdata;
            if (reserved_addr(addr))
                ret = PICO_ERROR_GENERIC;
            else
                ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);
    
            printf(ret < 0 ? "." : "@");
            printf(addr % 16 == 15 ? "\n" : "  ");
        }
        printf("Done.\n");
    
        sleep_ms(1000);
    }
}

liens