Commit be364e86 authored by Leandro Lanzieri's avatar Leandro Lanzieri

Merge branch 'master' of gitlab.com:leandrolanzieri/proyecto-digitales

parents 66131834 bd9a77ea
......@@ -33,6 +33,10 @@ void inicializarADC(void) {
ADCSRA |= (1 << ADEN); // Habilita el ADC
}
void disableADC(void){
ADCSRA &= ~(1 << ADEN); //Deshabilita el ADC
}
unsigned char convertirADC0(void) {
ADCSRA |= (1 << ADSC); // Inicia la conversion
while(!ADIF){} // Espera a que termine de convertir
......
#!/usr/bin/make
###############################################################################
# Makefile para Proyecto Domotica | Nodo Temperatura
###############################################################################
## General Flags
OBJDIR=Generado
PRJ = nodo-temperatura
MCU = atmega8
FCPU = 8000000UL
TARGET = $(OBJDIR)/$(PRJ).elf
CC = avr-gcc
OPT = s
## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)
## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -gdwarf-2 -std=gnu99 -DF_CPU=$(FCPU) -O$(OPT) -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -MD -MP -MT $(OBJDIR)/$(*F).o -MF $(OBJDIR)/$(@F).d
## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2
## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(PRJ).map
## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom
HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings
## Libraries
LIBS = -lm -lc
## Objects that must be built in order to link
OBJECTS = $(OBJDIR)/mcu.o $(OBJDIR)/nrf24l01.o $(OBJDIR)/main.o
## Objects explicitly added by the user
LINKONLYOBJECTS =
## Build
all: $(OBJDIR) $(TARGET) $(OBJDIR)/$(PRJ).hex $(OBJDIR)/$(PRJ).eep $(OBJDIR)/$(PRJ).lss size
## Compile
$(OBJDIR):
mkdir $@
$(OBJDIR)/%.o: %.c
$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@
##Link
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)
$(OBJDIR)/%.hex: $(TARGET)
avr-objcopy -O ihex $(HEX_FLASH_FLAGS) $< $@
$(OBJDIR)/%.eep: $(TARGET)
-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0
$(OBJDIR)/%.lss: $(TARGET)
avr-objdump -h -S $< > $@
size: ${TARGET}
@echo
@avr-size -C --mcu=${MCU} ${TARGET}
$(OBJDIR)/$(PRJ).asm: ${TARGET}
avr-objdump -S $< > $@
asm: $(OBJDIR)/$(PRJ).asm
## FUSES
low_fuse:
avrdude -p $(MCU) -c usbtiny -U lfuse:w:0xCE:m # // grabación de Low Fuse internal clock
high_fuse:
avrdude -p $(MCU) -c usbtiny -U hfuse:w:0xC9:m # // grabación de High Fuse
not_103c:
avrdude -p $(MCU) -c usbtiny -U efuse:w:0xFF:m # // grabación de Low Fuse internal clock
erase:
avrdude -p $(MCU) -c usbtiny -e # //borrado del micro
load_prg:
avrdude -p $(MCU) -c usbtiny -U flash:w:$(OBJDIR)/$(PRJ).hex:a # // grabación del programa
verify:
avrdude -p $(MCU) -c usbtiny -U flash:v:$(OBJDIR)/$(PRJ).hex:a # // verificación
## Clean target
#.PHONY: clean
clean:
rm -r $(OBJDIR)
## Other dependencies
##-include $(wildcard $(OBJDIR)/*)
#-include $(shell mkdir $(OBJDIR) 2>/dev/null) $(wildcard $(OBJDIR)/*)
/*
* 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 (martin.hernan.enriquez@gmail.com),
* Leandro Lanzieri Rodriguez (lanzierileandro@gmail.com).
*
* 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
#define PING 0x19
#define ACK 0x41
/* ======= 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 (martin.hernan.enriquez@gmail.com),
* Leandro Lanzieri Rodriguez (lanzierileandro@gmail.com).
*
* 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>
#include <avr/sleep.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 // Tama�o 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 (martin.hernan.enriquez@gmail.com),
* Leandro Lanzieri Rodriguez (lanzierileandro@gmail.com).
*
* 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 (martin.hernan.enriquez@gmail.com),
* Leandro Lanzieri Rodriguez (lanzierileandro@gmail.com).
*
* 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"
uint8_t* data;
volatile unsigned int demora_tx=0;
unsigned char transmitido=0;
int main(void) {
inicializarTodo();
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Entre los modos se elige Power Down ya que apaga todos los clk,
//el micro se despertará asincronicamente
Modo_RX(); // Cambia a recibir
Set_frec(FREC_ON_OFF); // Cambia a frecuencia ON OFF
ledBlink(); // Muestra que termino
while(1) {
if (demora_tx >= 20) { //Espera a que termine la transmision y vuelve a dormir.
sleep_enable();
reset();
Modo_RX();
CE = 1;
demora_tx = 0;
_delay_ms(20);
sleep_cpu();
sleep_disable();
}
if (!transmitido){ //Transmite una sola vez.
data[0]=0xAA;
Modo_TX();
transmit_payload(data);
transmitido =1;
}
}
}
ISR(INT0_vect)
{
cli();
CE = 0; // CE bajo
data = WriteToNrf(R, R_RX_PAYLOAD, data, dataLen); //Lee el mensaje
switch(data[0]) //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();
return;
}
ISR (TIMER1_OVF_vect)
{
TCNT1H = 0xFC;
TCNT1L = 0x17; //Tiempo para hacer que el timer cuente durante 1 mSeg
demora_tx++;
return;
}
/*
* 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 (martin.hernan.enriquez@gmail.com),
* Leandro Lanzieri Rodriguez (lanzierileandro@gmail.com).
*
* 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();
inicializarTimer();
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 inicializarTimer(void){
TCCR1A = 0x00;
TCCR1B = 0x02; //ARRANCA EL TIMER 1 CON PRESCALER DE OOOOOCHO
TCNT1H = 0xFC;
TCNT1L = 0x17; //Tiempo para hacer que el timer cuente durante 1 mSeg
}
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 (martin.hernan.enriquez@gmail.com),
* Leandro Lanzieri Rodriguez (lanzierileandro@gmail.com).
*
* 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);
void inicializarTimer(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 (martin.hernan.enriquez@gmail.com),
* Leandro Lanzieri Rodriguez (lanzierileandro@gmail.com).
*
* 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)