/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/i2c.h"
#include "hardware/pwm.h"
#include "pico/binary_info.h"
/* Example code to drive a 16x2 LCD panel via a I2C bridge chip (e.g. PCF8574)
NOTE: The panel must be capable of being driven at 3.3v NOT 5v. The Pico
GPIO (and therefor I2C) cannot be used at 5v.
You will need to use a level shifter on the I2C lines if you want to run the
board at 5v.
aaa
Connections on Raspberry Pi Pico board, other boards may vary.
GPIO 4 (pin 6)-> SDA on LCD bridge board
GPIO 5 (pin 7)-> SCL on LCD bridge board
3.3v (pin 36) -> VCC on LCD bridge board
GND (pin 38) -> GND on LCD bridge board
*/
// commands
const int LCD_CLEARDISPLAY = 0x01;
const int LCD_RETURNHOME = 0x02;
const int LCD_ENTRYMODESET = 0x04;
const int LCD_DISPLAYCONTROL = 0x08;
const int LCD_CURSORSHIFT = 0x10;
const int LCD_FUNCTIONSET = 0x20;
const int LCD_SETCGRAMADDR = 0x40;
const int LCD_SETDDRAMADDR = 0x80;
// flags for display entry mode
const int LCD_ENTRYSHIFTINCREMENT = 0x01;
const int LCD_ENTRYLEFT = 0x02;
// flags for display and cursor control
const int LCD_BLINKON = 0x01;
const int LCD_CURSORON = 0x02;
const int LCD_DISPLAYON = 0x04;
// flags for display and cursor shift
const int LCD_MOVERIGHT = 0x04;
const int LCD_DISPLAYMOVE = 0x08;
// flags for function set
const int LCD_5x10DOTS = 0x04;
const int LCD_2LINE = 0x08;
const int LCD_8BITMODE = 0x10;
// flag for backlight control
const int LCD_BACKLIGHT = 0x08;
const int LCD_ENABLE_BIT = 0x04;
// By default these LCD display drivers are on bus address 0x27
static int addr = 0x27;
// Modes for lcd_send_byte
#define LCD_CHARACTER 1
#define LCD_COMMAND 0
#define MAX_LINES 2
#define MAX_CHARS 16
/* Quick helper function for single byte transfers */
void i2c_write_byte(uint8_t val) {
#ifdef i2c_default
i2c_write_blocking(i2c_default, addr, &val, 1, false);
#endif
}
void lcd_toggle_enable(uint8_t val) {
// Toggle enable pin on LCD display
// We cannot do this too quickly or things don't work
#define DELAY_US 600
sleep_us(DELAY_US);
i2c_write_byte(val | LCD_ENABLE_BIT);
sleep_us(DELAY_US);
i2c_write_byte(val & ~LCD_ENABLE_BIT);
sleep_us(DELAY_US);
}
// The display is sent a byte as two separate nibble transfers
void lcd_send_byte(uint8_t val, int mode) {
uint8_t high = mode | (val & 0xF0) | LCD_BACKLIGHT;
uint8_t low = mode | ((val << 4) & 0xF0) | LCD_BACKLIGHT;
i2c_write_byte(high);
lcd_toggle_enable(high);
i2c_write_byte(low);
lcd_toggle_enable(low);
}
void lcd_clear(void) {
lcd_send_byte(LCD_CLEARDISPLAY, LCD_COMMAND);
}
// go to location on LCD
void lcd_set_cursor(int line, int position) {
int val = (line == 0) ? 0x80 + position : 0xC0 + position;
lcd_send_byte(val, LCD_COMMAND);
}
static void inline lcd_char(char val) {
lcd_send_byte(val, LCD_CHARACTER);
}
void lcd_string(const char *s) {
while (*s) {
lcd_char(*s++);
}
}
void lcd_init() {
lcd_send_byte(0x03, LCD_COMMAND);
lcd_send_byte(0x03, LCD_COMMAND);
lcd_send_byte(0x03, LCD_COMMAND);
lcd_send_byte(0x02, LCD_COMMAND);
lcd_send_byte(LCD_ENTRYMODESET | LCD_ENTRYLEFT, LCD_COMMAND);
lcd_send_byte(LCD_FUNCTIONSET | LCD_2LINE, LCD_COMMAND);
lcd_send_byte(LCD_DISPLAYCONTROL | LCD_DISPLAYON, LCD_COMMAND);
lcd_clear();
}
// Variables goblales, constantes, funciones y interrupciones
#define R1 6
#define R2 7
#define R3 8
#define R4 9
#define C1 10
#define C2 11
#define C3 12
int waiter = 250;
int fila = 0;
int columna = 0;
int frecuencia = 0;
int frecuencia_real = 0;
int volumen = 0;
int unidad = 100;
int vil = 0;
int adc_inc = 0;
bool incrementar_callback(){
if (waiter != 0) waiter--;
adc_inc--;
}
void general_callback(uint gpio, uint32_t event_mask){
if (gpio==6 && waiter == 0) {
fila = 1;
}
if (gpio==7 && waiter == 0) {
fila = 2;
}
if (gpio==8 && waiter == 0) {
fila = 3;
}
if (gpio==9 && waiter == 0) {
fila = 4;
}
waiter = 50;
}
void detect_column(int fila){
for (int i = 0; i < 3; i++){
gpio_clr_mask(0x1C00);
gpio_set_mask(1 << (i + C1));
if (gpio_get(fila+5) == 1) {
columna = i+1;
break;
}
}
gpio_set_mask(0x1C00);
}
void cancelar_freq (){
frecuencia = 0;
unidad = 100;
frecuencia_real = 0;
}
void aceptar_freq(){
frecuencia_real = frecuencia;
}
void unit_freq(int num){
if (unidad < 1) unidad = 0;
frecuencia += unidad * num;
unidad /= 10;
}
void change_freq(int fila, int columna){
if (fila == 1 && columna == 1) unit_freq(1);
if (fila == 1 && columna == 2) unit_freq(2);
if (fila == 1 && columna == 3) unit_freq(3);
if (fila == 2 && columna == 1) unit_freq(4);
if (fila == 2 && columna == 2) unit_freq(5);
if (fila == 2 && columna == 3) unit_freq(6);
if (fila == 3 && columna == 1) unit_freq(7);
if (fila == 3 && columna == 2) unit_freq(8);
if (fila == 3 && columna == 3) unit_freq(9);
if (fila == 4 && columna == 1) cancelar_freq();
if (fila == 4 && columna == 2) unit_freq(0);
if (fila == 4 && columna == 3) aceptar_freq();
}
int main() {
stdio_init_all();
gpio_init_mask(0xffff);
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
i2c_init(i2c_default, 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);
// Make the I2C pins available to picotool
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
// PInes de entrada y salida
gpio_set_dir_out_masked(0xf); //leds de prueba
gpio_set_dir_in_masked(0x3C0); // Entradas
gpio_set_dir_out_masked(0x3C00);
// ADC
adc_init();
adc_gpio_init(26); //adc gpio 26 y adc 0
//PWM
gpio_set_function(16, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(16);
pwm_set_wrap(slice_num, 62500);
// Timer
struct repeating_timer timer;
add_repeating_timer_ms(1,incrementar_callback,NULL,&timer);
// Botones
gpio_set_mask(0x1C00);
gpio_set_irq_enabled_with_callback(6, GPIO_IRQ_EDGE_RISE, true, &general_callback);
gpio_set_irq_enabled_with_callback(7, GPIO_IRQ_EDGE_RISE, true, &general_callback);
gpio_set_irq_enabled_with_callback(8, GPIO_IRQ_EDGE_RISE, true, &general_callback);
gpio_set_irq_enabled_with_callback(9, GPIO_IRQ_EDGE_RISE, true, &general_callback);
// Pantalla
lcd_init();
static char *message[] =
{
"Freq: %03d Hz",
"Vol : %3d %%"
};
char buffer[100];
int y = 14;
while (1) {
// Leer matriz
if (fila != 0) {
detect_column(fila);
gpio_put(columna-1,!gpio_get(columna-1));//led
gpio_set_mask(0x1C00); //prender columnas
change_freq(fila,columna);
fila = 0;
columna = 0;
}
// ADC read
if (adc_inc <= 0) {
const float conversion_factor = 3.3f / (1 << 12);
adc_inc = 200;
adc_select_input(0);
volumen = adc_read() * conversion_factor * 100 * 100 / 329;
}
// PWM values
if (frecuencia_real > 10) {
pwm_set_clkdiv(slice_num, 2000/((float)frecuencia_real));
}
else {
pwm_set_clkdiv_int_frac(slice_num, 255,0);
}
pwm_set_chan_level(slice_num, 0, 62500*volumen/100);
//Imprimir LCD
sprintf(buffer,message[0],frecuencia);
lcd_set_cursor(0,0);
lcd_string(buffer);
sprintf(buffer,message[1],volumen);
lcd_set_cursor(1,0);
lcd_string(buffer);
}
}