jueves, 9 de junio de 2011

ESTAMOS EN PRUEBAS

ESTAMOS EN PRUEBAS

El ejemplo publicado no es funcional, aunque puede dar ideas.

miércoles, 1 de junio de 2011

Control de luces de acuario 2011

program Control_acuario_2011

include "MostrarMenuPral"

'-------------------------------------------------------------------------------
'SIMBOLOS
'-------------------------------------------------------------------------------


'-------------------------------------------------------------------------------
'CONSTANTES
'-------------------------------------------------------------------------------


'-------------------------------------------------------------------------------
'VARIABLES
'-------------------------------------------------------------------------------

' Conexiones del modulo LCD
dim LCD_RS as sbit at RB4_bit
    LCD_EN as sbit at RB5_bit
    LCD_D4 as sbit at RB0_bit
    LCD_D5 as sbit at RB1_bit
    LCD_D6 as sbit at RB2_bit
    LCD_D7 as sbit at RB3_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit
' Fin Conexiones del modulo LCD

'Caracter Flecha Arriba
const cFlechaArriba as byte[8] = (0,4,14,31,4,4,4,0)
'Caracter Flecha Abajo
const cFlechaAbajo as byte[8] = (0,4,4,4,31,14,4,0)
'Caracter 1 OFF
const c1Apagado as byte[8] = (0,0,0,12,4,4,4,14)
'Caracter 1 ON
const c1Encendido as byte[8] = (31,31,0,12,4,4,4,14)
'Caracter 2 OFF
const c2Apagado as byte[8] = (0,0,0,14,2,14,8,14)
'Caracter 2 ON
const c2Encendido as byte[8] = (31,31,0,14,2,14,8,14)
'Caracter 3 OFF
const c3Apagado as byte[8] = (0,0,0,14,2,14,2,14)
'Caracter 3 ON
const c3Encendido as byte[8] = (31,31,0,14,2,14,2,14)
'Caracter 4 OFF
const c4Apagado as byte[8] = (0,0,0,10,10,14,2,2)
'Caracter 4 ON
const c4Encendido as byte[8] = (31,31,0,10,10,14,2,2)

Dim ItemMenu as byte                 'Nº de Item actual del bucle Principal
Dim subMenuItem as byte                'Nº de Item actual del menu Principal
Dim subMenuIluminacion as byte        'Nº de Item actual del menu Iluminacion

'Variables de encendido y apagado
Dim Encendido_Hora, Encendido_Minuto, Apagado_Hora, Apagado_Minuto as byte

'Contador de interupcion TMR2
dim cnt as word

'Modos de funcionamiento manual
Dim EncendidoManual as bit
Dim ApagadoManual as bit

'======================================================
' DECLARACIONES
'======================================================

'Cargar caracteres especiales en el LCD
    sub procedure LCD_CargarCaracteres()
       dim i, dchar as byte
       for dchar = 0 to 7
          LCD_Cmd(64 + (dchar * 8))               'Selecciona la pos. en memoria del LCD
          for i = 0 to 7
            select case dchar                     'Selecciona la matriz que contiene el caracter
               case 0
                  LCD_Chr_Cp(c1Apagado[i])
               case 1
                  LCD_Chr_Cp(c1Encendido[i])
               case 2
                  LCD_Chr_Cp(c2Apagado[i])
               case 3
                  LCD_Chr_Cp(c2Encendido[i])
               case 4
                  LCD_Chr_Cp(c3Apagado[i])
               case 5
                  LCD_Chr_Cp(c3Encendido[i])
               case 6
                  LCD_Chr_Cp(c4Apagado[i])
               case 7
                  LCD_Chr_Cp(c4Encendido[i])
            end select
          next i
       next dchar
    end sub



'-------------------------------------------------------------------------------
'INTERRUPCION
'-------------------------------------------------------------------------------
sub procedure interrupt
  if (PIR1.TMR2IF) then                'Si la interupcion la ha causado el TMR2
    Inc(cnt)                           'incrementa el contadorr
    PIR1.TMR2IF = 0                  'Borra la bandera de interupcion del TMR2
    TMR2  =   0                        'Pone el valor del TMR2 a cero
  end if

  if (cnt > 90) then                'Aprox. 30 = 1seg. a 8 MHz
      PORTB.7=0                        'Apaga la iluminacion del display
      ItemMenu =0                'Vuelve al menu del bucle principal
      subMenuItem =0
      subMenuIluminacion =0
  end if
  if PORTA.1=1 then
     cnt = 0                ' reset cnt
     PORTB.7=1
  end if
  if PORTA.0=1 then
     cnt = 0                ' reset cnt
     PORTB.7=1
  end if

end sub

'======================================================
'PROGRAMA PRINCIPAL
'======================================================
main:
'------------------------------------------------------------
'NOTAS SOBRE CONFIGURACION
'------------------------------------------------------------
'00-Hora de encendido
'01-Minuto de encendido
'02-Segundo de encendido
'03-Hora de apagado
'04-Minuto de apagado
'05-Segundo de apagado
'06-Maximo brillo dia
'07-Variacion brillo dia
'08-Maximo brillo noche
'09-Variacion brillo noche


'------------------------------------------------------------
'OPCIONES DE CONFIGURACIÓN
'------------------------------------------------------------
OPTION_REG = 000110
'             ........ 7- RBPU: PORTB Pull-up Enable bit (0 activadas)
'              ....... 6- INTEDG: Interrupt Edge Select bit
'               ...... 5- T0CS: TMR0 Clock Source Select bit (0 reloj)
'                ..... 4- T0SE: TMR0 Source Edge Select bit
'                 .... 3- PSA: Prescaler Assignment(0 -> prescaler a TMR0)
'                  ... 2- PS2: Prescaler Rate Select bits
'                   .. 1- PS1: Prescaler Rate Select bits
'                    . 0- PS0: Prescaler Rate Select bits

     'Bit Value TMR0 Rate WDT Rate
     '000       1:2       1:1
     '001       1:4       1:2
     '010       1:8       1:4
     '011       1:16      1:8
     '100       1:32      1:16
     '101       1:64      1:32
     '110       1:128     1:64
     '111       1:256     1:128


'CONFIGURACIÓN INTERRUPCIONES
INTCON = %10100000
'         ........ 7- GIE: Global Interrupt Enable bit
'          ....... 6- EEIE: EE Write Complete Interrupt Enable bit
'           ...... 5- T0IE: TMR0 Overflow Interrupt Enable bit
'            ..... 4- INTE: RB0/INT External Interrupt Enable bit
'             .... 3- RBIE: RB Port Change Interrupt Enable bit
'              ... 2- T0IF: TMR0 Overflow Interrupt Flag bit
'               .. 1- INTF: RB0/INT External Interrupt Flag bit
'                . 0- RBIF: RB Port Change Interrupt Flag bit

'Deshabilitar conversor A/D
ADCON0 = 000000
'         ........ 7- ADCS1: Clock frequency used for the conversion
'          ....... 6- ADCS2: Clock frequency used for the conversion
'           ...... 5- CHS2: Channel select bits
'            ..... 4- CHS1: Channel select bits
'             .... 3- CHS0: Channel select bits
'              ... 2- GO/DONE: Start conversion/conversion done
'               .. 1- -
'                . 0- ADON: Turns the A2D On or Off
ADCON1 = 000111
'         ........ 7- Result Format Selection Bit
'          ....... 6- -
'           ...... 5- -
'            ..... 4- -
'             .... 3- PCFG3-0: Seleccion segun tabla
'              ... 2- PCFG3-0: Seleccion segun tabla
'               .. 1- PCFG3-0: Seleccion segun tabla
'                . 0- PCFG3-0: Seleccion segun tabla

'Deshabilitar modulo comparador
'CMCON = 000111
'        ........ 7- C2OUT
'         ....... 6- C1OUT
'          ...... 5- C2INV
'           ..... 4- C1INV
'            .... 3- CIS
'             ... 2- CM2
'              .. 1- CM1
'               . 0- CM0
'CUENTA EN TMR0 ANTES DE INTERRUPCIÓN
'TMR0 = 20'Inicia TMR0
            '(255-96=159 va a contar 159 veces antes de interrupcion)

'CONFIGURACIÓN SALIDAS/ENTRADAS
TRISA = %11111111 'PORTA son entradas
PORTA = 000000 'Inicializar PORTA
TRISB = 000000 'PORTB son salidas para el LCD (SOLO PARA PRUEBAS)
PORTB = 000000 'Inicializar PORTB
TRISC = %11111111 'PORTC son salidas PWM (RC6 y RC7 comunicaciones TX y RX)
PORTC = 000000 'Inicializar PORTC
TRISD = 000000 'PORTD son entradas
PORTD = 000000 'Inicializar PORTD
TRISE = 000000 'PORTE son salidas
PORTE = 000000 'Inicializar PORTB
  'INICIAR COMUNICACIONES
'  TRISC =  %10000000'Rx on C7=1, TX on C6=0 for master
'  Usart_init(2400)  'Inicializar el modulo USART

ANSEL  = 0                    'Configurar los pines Analogicos como I/O digitales
ANSELH = 0


ItemMenu =0                    'Inicializar posicion menus
subMenuItem =0

EncendidoManual =0           'Incializar modos manuales    
ApagadoManual =0

'Ajustes del TMR2
T2CON.T2CKPS0=1                    'Prescaler
T2CON.T2CKPS1=1                    'Prescaler
T2CON.TMR2ON=1                    'Habilitar TMR2
T2CON.TOUTPS0=1                    'Postscaler
T2CON.TOUTPS1=1                    'Postscaler
T2CON.TOUTPS2=1                    'Postscaler
T2CON.TOUTPS3=1                    'Postscaler

TMR2  =   0                 'Inicializar el registro del Timer 2
PIE1.TMR2IE = 1             'Habilitar la interupcion por Timer 2
INTCON=0                    'Borramos todas las fuentes de interupcion
INTCON.GIE=1                    'Todas las interupciones habilitadas
INTCON.PEIE=1                    'Interupciones de perifericos habilitadas
INTCON.T0IE=0                    'Interupcion TMR0 deshabilitada
INTCON.INTE=0                    'Interupcion externa deshabilitada
INTCON.RBIE=0                    'Interupcion PORTB deshabilitada
INTCON.T0IF=0                    'Bandera de desbordamiento del TMR0
INTCON.INTF=0                    'Bandera de interupcion externa (RB0/INT)
INTCON.RBIF=0                    'Bandera de interupcion pines RB4:RB7

cnt = 0                        'Inicializar el contador para TMR2

'------------------------------------------------------------
'Mostar pantalla inicial
'------------------------------------------------------------
PORTB.7 =1 'Iluminar el dysplay
Lcd_Init()                      'Inicializar Lcd
Lcd_Cmd(_LCD_CLEAR)             'Borrar display
Lcd_Cmd(_LCD_CURSOR_OFF)        'Cursor apagado
LCD_Out(1,1,"CONTROL ACUARIO")  'Mensaje de inicio
LCD_Out(2,1,"MCN y JCB  2011 ")

Sound_Init(PORTB, 6)            'Sonido de inicio
Sound_Play(7000, 150)
Sound_Play(1500, 100)
Sound_Play(4000, 200)

Lcd_Cmd(_LCD_CLEAR)             'Borrar display
LCD_Out(1,1,"And thanks for")   'Segundo mensaje en arranque
LCD_Out(2,5,"all the fish")
Sound_Play(500, 300)
delay_ms(1000)
Lcd_Cmd(_LCD_CLEAR)                'Borrar display

LCD_CargarCaracteres               'Cargar letras diseñadas



'------------------------------------------------------------
'Bucle principal
'------------------------------------------------------------
  while TRUE                    'Bucle sin fin

    'Indicacion de modos manuales
    if EncendidoManual=1 then
        LCD_Out(2,14," ON MAN.")
    end if
    if ApagadoManual=1 then
        LCD_Out(2,14,"OFF MAN.")
    end if
    if (EncendidoManual=0 AND ApagadoManual=0) then
        LCD_Out(2,14,"    AUTO")
    end if

      Select case ItemMenu      'Casos del Menu Inicial

             Case 0'Pantalla hora y salidas
                  LCD_Out(1,1,"00:00:00") 'Para pruebas (hora actual)
                  LCD_Out(1,12,"00:00") 'Para pruebas (hora on/off)

                  'PARA PRUEBAS (Activar manualmente las salidas)
                  portd=0
                  if button(porta,2,50,1) then portd.0=1 end if
                  if button(porta,3,50,1) then portd.1=1 end if
                  if button(porta,4,50,1) then portd.2=1 end if
                  if button(porta,5,50,1) then portd.3=1 end if


                  'Mostrar el estado de las salidas en el LCD
                  if PORTD.0 =0 then
                     Lcd_Chr(2,1,0)'Salida 1 Apagada
                  else
                     Lcd_Chr(2,1,1)'Salida 1 Encendida
                  end if

                  if PORTD.1 =0 then
                     Lcd_Chr(2,2,2)'Salida 2 Apagada
                  else
                     Lcd_Chr(2,2,3)'Salida 2 Encendida
                  end if

                  if PORTD.2 =0 then
                     Lcd_Chr(2,3,4)'Salida 3 Apagada
                  else
                     Lcd_Chr(2,3,5)'Salida 3 Encendida
                  end if

                  if PORTD.3 =0 then
                     Lcd_Chr(2,4,6)'Salida 4 Apagada
                  else
                     Lcd_Chr(2,4,7)'Salida 4 Encendida
                  end if

                  'Si pulsamos SET vamos al menu Principal (Menu Pral. de ajustes)
                  if button(PORTA,0,10,1) or SubMenuItem > 0 then
                     Sound_Play(8000, 10)
                     VerMenuPral
                  end if

             Case 1

             Case 2

             Case 3

      end select
 wend


end.