搜索
bottom↓
回复: 0

分亨一PIC12F675程序驱动LED

[复制链接]

出0入0汤圆

发表于 2014-9-30 14:25:26 | 显示全部楼层 |阅读模式

/*
/*
/*-------------------- FUNCTIONAL DESCRIPTION -------------------------------

// GENERAL:
//    DESCRIPTION: (DEMO VERSION [SHORTENED TIMES])
//    THIS PROGRAM GENERATES A 200Hz PWM DIMMING SIGNAL FOR A LDD-1000
//    LED DRIVER. THE BRIGHTNESS (PWM) IS PROGRAMMABLE FROM 0 to 100%.  
//    THE SUNDWN TRIP POINT IS DETERMINED BY A RESISTOR VALUE. THERE IS A
//    LOGIC INPUT FROM AN AC LINE DETECTOR (LOW= AC, HIGH= NO AC). IF THE AC
//    IS OFF AND IT IS SUNDOWN. THE BACKUP LIGHT TURNS ON FOR 1 MINUTE.
//    IF THE AC STAYS OFF FOR MORE THAN 1 MINUTE, THE LIGHT WILL TURN OFF.
//    AT THIS POINT THE CONTROLLER ENTERS A 5 MINUTE WAITING PERIOD. DURING
//    THIS TIME, THE LIGHT WILL BLINK ONCE EVERY 10 SECONDS UNTIL THE END OF
//    THE TIMEOUT PERIOD. AFTER 5 MINUTES THE CONTROLLER RECHECKS THE AC AND
//    SUNDOWN AND RESPONDS NORMALLY. IF THE AC RETURNS OR THE SUN COMES UP
//    WHILE THE LIGHT IS ON OR IN TIMEOUT, THE LIGHT TURNS OFF AND THE
//    CONTROLLER RETURNS TO NORMAL MODE.
//
//    CONNECTIONS:
//    J0 (PROGRAM):
//    P1 (VPP):      12V PROGRAM 5V PULLUP
//    P2 (GND):
//    P3 (PGC):      PROGRAM CLOCK
//    P4 (N.C.):
//    P5 (PGD:       PROGRAM DAATA
//    P6 (VDD):      +5V
//
//    J1 (10VDC TO 30VDC)(NOM=24V):
//    P1 (V+):       POWER IN
//    P2 (GND):      POWER COMMON
//
//    J2 (CDS PHOTOCELL INPUT):
//    P1 (+5V):     CURRENT LIMITED +5V
//    P2 (CDS):     CURRENT FROM CDS CELL
//   
//    J3 (AC DETECT)(90VAC TO 240VAC):
//    P1 (AC IN):    47K PULLUP TO +5V (GND WHEN AC ON)
//    P2 (COMM):     SWITCHED COMMON
//
//    J4 (POWER TO BACKUP LIGHT):
//    P1 (LED+):     DIODE DECOUPLED CONSTANT CURRENT
//    P2 (LED-):     ISOLATED GND
//
//
/*----------------------- REVISION HISTORY ----------------------------------
/*   
/*    File:
/*    Software rev#:    0
/*    PCB rev#:         0
/*    Date:      
/*    By:         
/*    Description:   
/*      
/*
/*------------------- INCLUDES AND PIC SETTINGS -----------------------------
*/

#case

#include <12F675.h>   // PIC 12F675
#device adc=10


#fuses NOWDT        // No Watch Dog Timer
#fuses INTRC_IO     // Internal RC Clock (4.0 Mhz)
#fuses PROTECT      // Code Protect from Reads
#fuses BROWNOUT     // Brownout reset Enabled
#fuses NOMCLR       // Master Clear Disabled
#fuses NOCPD        // No EE Rom Protect
#fuses PUT          // Power On Timer Enabled
#use delay(clock=4000000)   /* 4.0 Mhz Clock selected


/-------- PIC Pin Definitions --------
/*   PIN1 =  VDD      (+5V)                           (POWER)
/*   PIN2 =  A5       (PWM TO LDD-1000)               (PWM)
/*   PIN3 =  A4       (NOT USED)
/*   PIN4 =  VPP      (ICSP PGM VOLTAGE)              (VPP)   
/*   PIN5 =  A2       (NOT USED)
/*   PIN6 =  PGC/A1   (ICSP CLK,CDS SUN UP)           (sun)   (IN) (0= SUN DOWN)         
/*   PIN7 =  PGD/A0   (ICSP DATA, AC ON INPUT)        (ac)    (IN) (0= AC ON)
/*   PIN8 =  VSS      (GND)               
*/


// --------- DEFINE PORTS ----------
#define bright   5            // BRIGHTNESS POT (VR2) ADDRESS (NOT USED)
#define sundn    3            // DUNDOWN POT (VR1) ADDRESS (NOT USED)
#define sun      PIN_A1       // CDS CELL INPUT ADDRESS
#define ac       PIN_A0       // AC DETECT ADDEESS
#define PWM      PIN_A5       // PWN TO LD-1000 LED DRIVER (0%= OFF, 100%= MAX)

// ---------- CONSTANTS ----------
const int ints_sec=15;         // seconds calibrate for timer ISR
const float vr1cal=11;        // ADC calibration value for sundown (1-100)
const float vr2cal=11;        // ADC calibration value for brightness (1-100)
const long timeout1=1;         // first timeout  (MINUTES)
const long timeout2=5;         // second timeout (MINUTES)

// ---------- GLOBAL VARIABLES ----------
long count1=ints_sec;            // decrement counter for timer ISR
long seconds=0;                  // seconds counter
long minutes=0;                  // minutes counter
long hours=0;                    // hours counter
long tic=0;                      // seconds count down timer
long brightness=50;               // Brightness value for PWM (0 - 100)
long sundown=0;                   // Sundown value for dcs (0 - 100)
short acon=0;                    // AC detect flag (0= NO AC, 1= AC ON)
short sunflg=0;                  // Sun flag, (0= sun down, 1 = sun up)
long period=5000;                // pwm period in microseconds (200Hz)
long ontime=0;                   // PWM on time (MILLISECONDS)
long offtime=0;                  // PWN off time (MILLISECONDS)
long blinktime=5;                // time between blinks (seconds)


//---------- INTERRUPT ROUTINES ----------
#int_TIMER1
void  TIMER1_isr(void)
{
   --count1;            // decrement the counter
if (count1<=0)
   {
   count1=ints_sec;     // reload counter
   seconds++;           // increment seconds
   tic--;               // decrement count down timer
   if (tic<0)
      tic=0;
   }   
if (seconds>=60)        // seconds
   {
   seconds=0;           // reset sedonds
   minutes++;           // increment minutes counter
   }
if (minutes>=60)        // minutes
   {
   minutes=0;           // reset minutes
   hours++;             // increment hours counter
   }
if (hours>=24)          // hours
   hours=0;
}

// ---------- functions ----------
void get_sun(void);        // read the CDS cell
void chk_ac(void);         // check for AC

//-------- MAIN CODE SEGMENT --------
void main()
{

//------- setup ports -------
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);   // Timer1 ISR(hours, min, sec)
   delay_us(100);                            // wait 100uS
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   port_a_pullups(FALSE);


//   ----- INITIAL SETTINGS -----
delay_ms(100);                 // wait 100ms
output_low(PWM);               // backup light off
blinktime=blinktime*1000;      // convert to milliseconds



// ---------- MAIN LOOP ----------
while(1)                         // Do forever (main) loop
  {
// --- pwm loop ---
ontime=period/100*brightness;         // on time (ms) for WM
offtime=period-ontime;;               // off time (ms) for PWM
get_sun();
chk_ac();
hours=0;                              // reset timer
minutes=0;                            // reset timer
seconds=0;
while(sunflg==0&&acon==0&&minutes<timeout1) // backup light on time
   {
   output_high(PWM);
   delay_us(ontime);
   output_low(PWM);
   delay_us(offtime);
   get_sun();
   chk_ac();
   }
hours=0;                              // reset timer
minutes=0;                            // reset timer
seconds=0;
while(sunflg==0&&acon==0&&minutes<timeout2)  // backup light on standby
   {
   // --- blink light ---
   output_high(PWM);                   // turn pwm on
   //delay_us(50);                       // wait for on time
   delay_ms(100);      // debug
   output_low(PWM);                    // turn pwm off
   delay_ms(blinktime);                // wait for off time
   get_sun();
   chk_ac();
   }
      
  }
// ----- END MAIN -----
  

  }

// ---------- FUNCTIONS ----------


//******************************************************
//    Function:    get_sun                             *
//    Parameters:  sun                                 *
//    Returns:     sunflg (1= sunup, 0= sundown)       *
//    Description: check the CDS cell.                 *
//                 if sun down sunflg=0.               *
//******************************************************
void get_sun(void)
   {
   long n=0;
   sunflg=1;            // preset to sun up
   n=input(sun);        // read the cds cell
   if(n==0)
      sunflg=0;         // sundown
   
   }
//******************************************************
//    Function:    chk_ac                              *
//    Parameters:  ac                                  *
//    Returns:     acon                                *
//    Description: read the ac detect pin 7.           *
//                 return 1 if ac on, 0 if no ac       *
//******************************************************
void chk_ac(void)
   {
   int n=0;
   acon=0;                 // 0= ac off
   // --- check the ac pin ---
   n=input(ac);            // read the acon port
   switch (n)
      {
      case 0:              // ac is on
         acon=1;
         break;
      case 1:              // ac is off
         acon=0;
         break;
      }
   }



回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-6 01:47

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表