Tuesday, November 22, 2011

Code Dump: Remote Controlled 4 Channel Relay




#include <16f628a.h> 
#fuses INTRC_IO, NOWDT, BROWNOUT,  NOLVP 
#use delay(internal=4MHz)
#define LED  PIN_B4
#define IR  PIN_B3
#define RELAY1  PIN_A2
#define RELAY2  PIN_A3
#define RELAY3  PIN_B1
#define RELAY4  PIN_B2

/* TIMER0 configuration */ 
#define TIMER0_CONFIG   RTCC_INTERNAL | RTCC_DIV_1 

/* Interrupt rate:                     */ 
/* 4/4000000*65536*1 = 0.256 ms       */ 
/*                                     */ 
/*     Start: 3.0 ms (ignored)         */ 
/*     "1":   1.8 ms (225)           */ 
/*     "0":   1.2 ms  (150)           */ 
/*

*/
#define ONE_MIN  190
#define ONE_MAX  400 
#define ZERO_MIN 10 
#define ZERO_MAX 185 

short rly1,rly2,rly3,rly4,state;

/* irframes[0] (start) will be garbage, ignore it...  */ 
int16 irframes[13]; 
int8 ircount = 0; 
int1 irdone = FALSE; 


#int_ccp1 
void ext_ccp1() { 
  if (irdone) return; 
  irframes[ircount++] = get_timer0(); 
  if (ircount >= 13) 
    irdone = TRUE; 
  set_timer0(0); 
  enable_interrupts(INT_TIMER0); 
//#output_bit(LED,1);delay_ms(1);output_bit(LED,0);delay_ms(1);
} 


#int_timer0 
void timer0_isr() { 
  disable_interrupts(INT_TIMER0); 
} 


#separate 
int1 decode_ir(int8 &addr, int8 &cmd) { 
  int8 i; 
  int8 mask; 
  int8 bits[13]; 

  addr = 0; 
  cmd = 0; 

  for (i=1; i<=12; i++) { 
    if ((ONE_MIN <= irframes[i]) && (irframes[i] <= ONE_MAX)) 
      bits[i] = 0x01; 
    else 
      if ((ZERO_MIN <= irframes[i]) && (irframes[i] <= ZERO_MAX)) 
        bits[i] = 0x00; 
      else        // Error 
        return FALSE; 
  } 

  mask = 0x01; 
  for (i=1; i<=7; i++) { 
    if (bits[i]) 
      cmd = cmd | mask; 
    mask <<= 1; 
  } 

  mask = 0x01; 
  for (i=8; i<=12; i++) { 
    if (bits[i]) 
      addr = addr | mask; 
    mask <<= 1; 
  } 

  return TRUE; 
} 


void start_ir() { 
  memset(irframes, 0x00, sizeof(irframes)); 
  ircount = 0; 
  irdone = FALSE; 
} 


void led_blink(int i)
{
int j;
for (j=0;j>

No comments:

Post a Comment