Commit 246519dd authored by Leandro Lanzieri's avatar Leandro Lanzieri

Se agrega el proyecto del Nodo Temperatura. Comunicacion exitosa entre Central...

Se agrega el proyecto del Nodo Temperatura. Comunicacion exitosa entre Central y Temperatura. Se comienza proyecto de prueba de ADC
parent 1e953219
Old copyright not at start of file, new copyright replaces old in same location
/nodo-central/configuracion.h
/nodo-central/hardware.h
/nodo-central/main.c
/nodo-central/mcu.c
/nodo-central/mcu.h
/prueba-adc/configuracion.h
/prueba-adc/hardware.h
/prueba-adc/main.c
/prueba-adc/mcu.c
/prueba-adc/mcu.h
Failed to create internal source file for proccessing the file
/nodo-central/.cproject
/nodo-central/.project
/nodo-central/.settings/de.innot.avreclipse.core.prefs
/nodo-central/Release/main.d
/nodo-central/Release/main.o
/nodo-central/Release/mcu.d
/nodo-central/Release/mcu.o
/nodo-central/Release/nodo-central.eep
/nodo-central/Release/nodo-central.elf
/nodo-central/Release/nodo-central.hex
/nodo-central/Release/nodo-central.lss
/nodo-central/Release/nodo-central.map
/nodo-central/Release/objects.mk
/nodo-central/Release/sources.mk
/nodo-central/Release/subdir.mk
File has no extension. File UNCHANGED.
/nodo-central/Release/makefile
/prueba-adc/.cproject
/prueba-adc/.project
/prueba-adc/.settings/de.innot.avreclipse.core.prefs
......@@ -30,15 +30,35 @@
*******************************************************************************/
#include "configuracion.h"
#include "comunicacion.h"
#include "hardware.h"
#include "uart.h"
#include "mcu.h"
#include "nrf24l01.h"
int main(void) {
unsigned char datos[5];
inicializarTodo();
Modo_TX(); // Cambia a transmitir
Set_frec(FREC_ON_OFF);
ledBlink();
datos[0] = RELE_TOGGLE;
datos[1] = RELE_TOGGLE;
datos[2] = RELE_TOGGLE;
datos[3] = RELE_TOGGLE;
datos[4] = RELE_TOGGLE;
while(1) {
inicializarTodo();
_delay_ms(1000);
transmit_payload(datos); // Envia los datos
/* NOTA: Este delay y reset hacen la comunicacion estable, si no estan
* se produce una falla permanente o perdida de datos. Se evaluara colocarlos
* dentro de la funcion de envio de paquetes.
*/
_delay_ms(20);
reset();
LED ^= 1; // Led toggle
}
}
......@@ -28,11 +28,13 @@
#include "mcu.h"
#include "hardware.h"
void inicializarTodo() {
inicializarUART();
inicializarGPIO();
inicializarSPI();
inicializarInterrupcionesExternas();
// inicializarInterrupcionesExternas();
inicializarNRF24L01();
sei(); // Habilita interrupciones
}
......@@ -43,6 +45,7 @@ void inicializarGPIO(void) {
LED = 1; // Se enciende con 0
}
void inicializarSPI(void) {
/* Pines de comunicacion con NRF24L01 */
......
/*
* Proyecto Domotica | Nodo Central
* mcu.h
* Contiene las declaraciones de las funciones de mcu.c
* Contiene los prototipos de las funciones de mcu.c
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
......
/*
* Proyecto Domotica | Nodo Central
* uart.h
* Contiene definiciones de las cunfiones de uart.c
* Contiene los prototipos de las funciones de uart.c
*/
#ifndef UART_H_
......
/*
* Proyecto Domotica | Nodo Temperatura
* comunicacion.h
* Contiene los comandos que son interpretables por todos los nodos
* de la red. Tambien las frecuencias predefinidas para cada tipo
* de nodo.
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Proyecto Domotica. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#ifndef COMUNICACION_H_
#define COMUNICACION_H_
/* ======== COMANDOS ========= */
#define RELE_ON 0x0F
#define RELE_OFF 0x01
#define RELE_TOGGLE 0xDA
#define ENTRO_PANTALLA 0x07
#define LUCES_ON 0x1F
#define LUCES_OFF 0x11
#define LUCES_TOGGLE 0x1A
#define LUCES_DUTY 0x1D
#define LUCES_AUMENTAR 0x1B
#define LUCES_DISMINUIR 0x1C
/* ======= FRECUENCIAS ======== */
#define FREC_LUCES 0x66
#define FREC_ON_OFF 0x78
#endif /* COMUNICACION_H_ */
/* Proyecto Domotica | Nodo Temperatura
* configuracion.h
* Importa las librerias propias de AVR. Define constantes de configuracion del funcionamiento
* general del programa, como la frecuencia del cristal utilizado.
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Proyecto Domotica. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#ifndef CONFIGURACION_H_
#define CONFIGURACION_H_
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define FOSC 8000000 // 8MHz Frecuencia de oscilacion
/* UART */
#define BAUD 9600 // Baud Rate
#define UBRRREG ((FOSC/16)/BAUD)-1 // El reultado debe ser el numero del registro UBRR para tener BAUD
/* NRF24L01 */
#define dataLen 5 // Tamao de la carga til (payload) - 1 a 32 bytes
#endif /* CONFIGURACION_H_ */
/*
* Proyecto Domotica | Nodo Temperatura
* hardware.h
* Contiene las definiciones de pines, puertos y registros que son propios de
* cada placa. Tambien una escructura de manejo de a bits.
*
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Proyecto Domotica. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#ifndef HARDWARE_H_
#define HARDWARE_H_
/**************************************Definiciones para Manejo de a bit**********************************/
typedef struct
{
unsigned char B0:1;
unsigned char B1:1;
unsigned char B2:1;
unsigned char B3:1;
unsigned char B4:1;
unsigned char B5:1;
unsigned char B6:1;
unsigned char B7:1;
}BIT;
#define REGBIT(reg,bt) ((volatile BIT*)&reg)->B##bt //MACRO PARA MANEJAR REGISTROS DE A BIT.
/*********************************************************************************************************/
/* Led onboard */
#define LED_PORT PORTD
#define LED_PIN 7
#define LED_DDR DDRD
#define LED REGBIT(PORTD, 7)
/* Chip Enable del NRF24L01 */
#define CE_PORT PORTB
#define CE_PIN 1
#define CE_DDR DDRB
#define CE REGBIT(PORTB, 1)
/* Chip Select Not del NRF24L01 */
#define CSN_PORT PORTB
#define CSN_PIN 2
#define CSN_DDR DDRB
#define CSN REGBIT(PORTB, 2)
/* SPI MOSI */
#define MOSI_PORT PORTB
#define MOSI_PIN 3
#define MOSI_DDR DDRB
#define MOSI REGBIT(PORTB, 3)
/* SPI MISO */
#define MISO_PORT PORTB
#define MISO_PIN 4
#define MISO_DDR DDRB
#define MISO REGBIT(PORTB, 4)
/* SPI SCK */
#define SCK_PORT PORTB
#define SCK_PIN 5
#define SCK_DDR DDRB
#define SCK REGBIT(PORTB, 5)
/* Interrupciones Externas Int0 */
#define INT0_PORT PORTD
#define INT0_PIN 2
#define INT0_DDR DDRD
#endif /* HARDWARE_H_ */
/*
* Proyecto Domotica | Nodo Temperatura
* main.c
* El nodo temperatura debe poder leer los datos a la salida de
* un LM35 haciendo uso del periferico ADC. Tambien debe poder
* enviar estos datos a traves de un modulo NRF24L01+ con una
* conexion SPI.
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Proyecto Domotica. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "configuracion.h"
#include "comunicacion.h"
#include "hardware.h"
#include "nrf24l01.h"
#include "mcu.h"
int main(void) {
inicializarTodo();
Modo_RX(); // Cambia a transmitir
Set_frec(FREC_ON_OFF); // Cambia a frecuencia ON OFF
ledBlink(); // Muestra que termino
while(1) {
/*if (demora_tx >= 20) {
reset();
Modo_RX();
CE = 1;
demora_tx = 0;
_delay_ms(20);
}*/
}
}
ISR(INT0_vect)
{
uint8_t* data;
cli();
CE = 0; // CE bajo
data = WriteToNrf(R, R_RX_PAYLOAD, data, dataLen); //Lee el mensaje
switch(data[1]) //Al buscar el mensaje en data[0] el mismo no contenia los datos correctos. Buscando en el 1 se soluciona este inconveniente.
{
case RELE_ON:
LED = 1;
break;
case RELE_OFF:
LED = 0;
break;
case RELE_TOGGLE:
LED ^= 1;
break;
}
sei();
CE = 1; // CE alto
reset();
}
/*
* Proyecto Domotica | Nodo Temperatura
* mcu.c
* Contiene las funciones generales que se usan en el programa. En este caso de incializacion,
* y encendido del led onboard.
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Proyecto Domotica. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "configuracion.h"
#include "mcu.h"
#include "hardware.h"
void inicializarTodo() {
inicializarGPIO();
inicializarSPI();
inicializarInterrupcionesExternas();
inicializarNRF24L01();
sei(); // Habilita interrupciones
}
void inicializarGPIO(void) {
/* Led onboard */
LED_DDR |= (1 << LED_PIN); // Led como salida
LED = 1; // Se enciende con 0
}
void inicializarSPI(void) {
/* Pines de comunicacion con NRF24L01 */
SCK_DDR |= (1 << SCK_PIN); // SPI SCK como salida
MOSI_DDR |= (1 << MOSI_PIN); // SPI MOSI como salida
CE_DDR |= (1 << CE_PIN); // CE como salida
CSN_DDR |= (1 << CSN_PIN); // CSN como salida
/* Habilita comunicacion SPI y se configura al microcontrolador como master */
SPCR |= (1 << SPE) | (1 << MSTR) | (1 << SPR0);
CE = 1;
CSN =1;
}
void inicializarInterrupcionesExternas(void) {
INT0_DDR &= ~(1 << INT0_PIN); // INT0 como entrada
MCUCR |= (1 << ISC01); // INT0 detecta flanco descendente
GICR |= (1 << INT0); // Habilito la interrupcion de INT0
}
/* Misc */
void ledBlink(void) {
char i;
for(i=0; i<6; i++) {
LED ^= 1; //Toggle Led
_delay_ms(155);
}
}
/*
* Proyecto Domotica | Nodo Temperatura
* mcu.h
* Contiene los prototipos de las funciones de mcu.c
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Proyecto Domotica. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#ifndef MCU_H_
#define MCU_H_
/* Funciones de Inicializacion */
void inicializarTodo(void);
void inicializarGPIO(void);
void inicializarInterrupcionesExternas(void);
void inicializarSPI(void);
void inicializarNRF24L01(void);
/* Misc */
void ledBlink(void);
#endif /* MCU_H_ */
/*
* Proyecto Domotica | Nodo Temperatura
* nrf24l01.c
* Contiene las funciones para comunicarse utilizando el modulo
* NRF24L01+ a traves de SPI. Tambien funciones utilies para su
* uso y configuracion.
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Proyecto Domotica. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "configuracion.h"
#include "comunicacion.h"
#include "nrf24l01.h"
#include "hardware.h"
char WriteByteSPI(unsigned char cData)
{
SPDR = cData;
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
uint8_t GetReg(uint8_t reg) //Funci�n para leer un registro reg
{
_delay_us(10);
CSN = 0;
_delay_us(10);
WriteByteSPI(R_REGISTER + reg);
_delay_us(10);
reg = WriteByteSPI(NOP);
_delay_us(10);
CSN = 1;
return reg;
}
uint8_t *WriteToNrf(uint8_t ReadWrite, uint8_t reg, uint8_t *val, uint8_t antVal) //Funci�n de lectura o escritura
//"W o R" (Escribir o leer), "reg" (el registro), "*val" (valor a escribir) & "antVal" (cantidad de enteros a escribir)
{
cli();
if (ReadWrite == W)
{
reg = W_REGISTER + reg;
}
static uint8_t ret[dataLen];
_delay_us(10);
CSN = 0;
_delay_us(10);
WriteByteSPI(reg);
_delay_us(10);
int i;
for(i=0; i<antVal; i++)
{
if (ReadWrite == R && reg != W_TX_PAYLOAD)
{
ret[i]=WriteByteSPI(NOP);
_delay_us(10);
}
else
{
WriteByteSPI(val[i]);
_delay_us(10);
}
}
CSN = 1;
sei();
return ret;
}
void inicializarNRF24L01(void)
{
//_delay_ms(100);
uint8_t val[5];
val[0]=0x03;
WriteToNrf(W, EN_AA, val, 1); //Habilito auto ACK en P0
val[0]=0x2F;
WriteToNrf(W, SETUP_RETR, val, 1); //Configuro la retransmisi�n con un ARD=705useg y ARC=15
val[0]=0x03;
WriteToNrf(W, EN_RXADDR, val, 1); //Habilito el P0
val[0]=0x03;
WriteToNrf(W, SETUP_AW, val, 1); //Seteo 5 bytes de longitud de direcci�n
val[0]=FREC_ON_OFF;
WriteToNrf(W, RF_CH, val, 1); //Seteo el canal de frecuencia 2,502 GHz
val[0]=0x26;
WriteToNrf(W, RF_SETUP, val, 1); //Seteo vel de transmisi�n en 250Kb/s y potencia en 0dBm
int i;
for(i=0; i<5; i++)
{
val[i]=0x12; //N�mero de direcci�n
}
WriteToNrf(W, RX_ADDR_P0, val, 5); //Cargo la direcci�n del RX
for(i=0; i<5; i++)
{
val[i]=0x13; //N�mero de direcci�n
}
WriteToNrf(W, RX_ADDR_P1, val, 5); //Cargo la direcci�n del RX
for(i=0; i<5; i++)
{
val[i]=0x12; //N�mero de direcci�n
}
WriteToNrf(W, TX_ADDR, val, 5); //Cargo la direcci�n del TX
// Configuraci�n de capacidad de la carga �til (payload) - Est�tica de 1 - 32 bytes
val[0]=dataLen;
WriteToNrf(W, RX_PW_P0, val, 1);
val[0]=dataLen;
WriteToNrf(W, RX_PW_P1, val, 1);
val[0]=0x1F; //Configuraci�n de modo, TX=7E - RX=1F
WriteToNrf(W, CONFIG, val, 1);
//_delay_ms(100);
}
void reset(void)
{
_delay_us(10);
CSN = 0;
_delay_us(10);
WriteByteSPI(W_REGISTER + STATUS);
_delay_us(10);
WriteByteSPI(0b01110000);
_delay_us(10);
CSN = 1;
}
void receive_payload(void)
{
sei();
CE = 1;
_delay_ms(1000);
CE = 0;
cli();
}
void transmit_payload(uint8_t * W_buff)
{
WriteToNrf(R, FLUSH_TX, W_buff, 0);
WriteToNrf(R, W_TX_PAYLOAD, W_buff, dataLen);
sei();
_delay_ms(10);
CE = 1;
_delay_ms(1);
CE = 0;
_delay_ms(10);
}
void Modo_TX(void)
{
uint8_t val[5];
val[0]=0x7E; //Configuraci�n de modo, TX=7E - RX=1F
WriteToNrf(W, CONFIG, val, 1);
CE=0;
}
void Modo_RX(void)
{
uint8_t val[5];
val[0]=0x1F; //Configuraci�n de modo, TX=7E - RX=1F
WriteToNrf(W, CONFIG, val, 1);
}
void Set_frec(uint8_t frecuencia)
{
uint8_t val[5];
val[0]= frecuencia;
WriteToNrf(W, RF_CH, val, 1);
}
void Cambio_Pipe(unsigned char direccion)
{
uint8_t val[5];
int i;
for(i=0; i<5; i++)
{
val[i]=direccion; //N�mero de direcci�n
}
WriteToNrf(W, TX_ADDR, val, 5); //Cargo la direcci�n del TX
}
/*
* Proyecto de Domotica | Nodo Temperatura
* nrf24l01.h
* Contiene los registros del modulo NRF24L01+, los bits
* de configuracion y los prototipos de las funciones de nrf24l01.c
*/
/*******************************************************************************
* Copyright (c) 2015 Martin Enriquez, Leandro Lanzieri Rodriguez
*
* This file is part of Proyecto Domotica.
*
* Proyecto Domotica is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proyecto Domotica is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.