add pcf8591 lib + example
This commit is contained in:
		
							parent
							
								
									b982a132ae
								
							
						
					
					
						commit
						dbcb707c1c
					
				
					 3 changed files with 50 additions and 39 deletions
				
			
		|  | @ -1,43 +1,36 @@ | |||
| /*
 | ||||
|  * Example of using PCA9685 PWM driver | ||||
|  * | ||||
|  * Part of esp-open-rtos | ||||
|  * Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com> | ||||
|  * Public domain | ||||
|  */ | ||||
| #include <esp/uart.h> | ||||
| #include <espressif/esp_common.h> | ||||
| #include <i2c/i2c.h> | ||||
| #include <pca9685/pca9685.h> | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| #define ADDR 0x40 | ||||
| #include "espressif/esp_common.h" | ||||
| #include "esp/uart.h" | ||||
| 
 | ||||
| #include "FreeRTOS.h" | ||||
| #include "task.h" | ||||
| 
 | ||||
| #include "i2c/i2c.h" | ||||
| #include "pcf8591/pcf8591.h" | ||||
| 
 | ||||
| #define SCL_PIN 5 | ||||
| #define SDA_PIN 4 | ||||
| 
 | ||||
| #define PWM_FREQ 500 | ||||
| static void measure(void *pvParameters) | ||||
| { | ||||
|     while (1) | ||||
|     { | ||||
|         vTaskDelay(1000 / portTICK_PERIOD_MS); | ||||
|         printf("Value: %d\n", pcf8591_read(PCF8591_DEFAULT_ADDRESS, 0x03)); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void user_init(void) | ||||
| { | ||||
|     uart_set_baud(0, 115200); | ||||
|     printf("SDK version:%s\n", sdk_system_get_sdk_version()); | ||||
| 
 | ||||
|     // Just some information
 | ||||
|     printf("\n"); | ||||
|     printf("SDK version : %s\n", sdk_system_get_sdk_version()); | ||||
|     printf("GIT version : %s\n", GITSHORTREV); | ||||
| 
 | ||||
|     i2c_init(SCL_PIN, SDA_PIN); | ||||
| 
 | ||||
|     pca9685_init(ADDR); | ||||
| 
 | ||||
|     pca9685_set_pwm_frequency(ADDR, 1000); | ||||
|     printf("Freq 1000Hz, real %d\n", pca9685_get_pwm_frequency(ADDR)); | ||||
| 
 | ||||
|     uint16_t val = 0; | ||||
|     while (true) | ||||
|     { | ||||
|         printf("Set ch0 to %d, ch4 to %d\n", val, 4096 - val); | ||||
|         pca9685_set_pwm_value(ADDR, 0, val); | ||||
|         pca9685_set_pwm_value(ADDR, 4, 4096 - val); | ||||
| 
 | ||||
|         if (val++ == 4096) | ||||
|             val = 0; | ||||
|     } | ||||
|     xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL); | ||||
| } | ||||
|  |  | |||
|  | @ -1,15 +1,22 @@ | |||
| #include <stddef.h> | ||||
| #include <stdint.h> | ||||
| #include <stdbool.h> | ||||
| 
 | ||||
| #include <i2c/i2c.h> | ||||
| #include "pcf8591.h" | ||||
| 
 | ||||
| static uint8_t mAddress = PCF8591_DEFAULT_ADDRESS; | ||||
| static float mVoltage = 3.3f; | ||||
| /**
 | ||||
|  * CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE | ||||
|  */ | ||||
| 
 | ||||
| void | ||||
| pcf8591_set_address(uint8_t addr) | ||||
| #define PCF8591_CTRL_REG_READ 0x03 | ||||
| 
 | ||||
| uint8_t | ||||
| pcf8591_read(uint8_t addr, uint8_t analog_pin) | ||||
| { | ||||
|     //
 | ||||
|     uint8_t res = 0; | ||||
|     uint8_t control_reg = PCF8591_CTRL_REG_READ & analog_pin; | ||||
| 
 | ||||
|     i2c_slave_read(addr, &control_reg, &res, 1); | ||||
| 
 | ||||
|     return res; | ||||
| } | ||||
|  |  | |||
|  | @ -14,14 +14,25 @@ extern "C" | |||
| { | ||||
| #endif | ||||
| 
 | ||||
| #define PCF8591_DEFAULT_ADDRESS 0x90 | ||||
| /**
 | ||||
|  * CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE | ||||
|  */ | ||||
| 
 | ||||
| #define PCF8591_DEFAULT_ADDRESS 0x48 | ||||
| 
 | ||||
| void pcf8591_init(void); | ||||
| 
 | ||||
| /**
 | ||||
|  * Set new sensor address for switching another. | ||||
|  * Read input value of an analog pin. | ||||
|  * @param[in] addr Pointer to device | ||||
|  * @return none | ||||
|  * @param[in] analog_pin pin number: | ||||
|  *            0 - AIN0 | ||||
|  *            1 - AIN1 | ||||
|  *            2 - AIN2 | ||||
|  *            3 - AIN3 | ||||
|  * @return analog value | ||||
|  */ | ||||
| void pcf8591_set_address(uint8_t addr); | ||||
| uint8_t pcf8591_read(uint8_t addr, uint8_t analog_pin); | ||||
| 
 | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue