使用MPLIB自带的Hi-tech lite版c编译器编译。
不知道为何,Timer0定时中断总有问题,无法用作延时,只好采用实时延时方式,反正MCU也没别的事情好干。延时不是太精确,没仔细去调整。
欢迎老手拍砖!!!
/*
* 手电调光程序 for PIC12F629
* 6,7脚各控制一个7135, 4档调光顺序:单7135,双7135,单7135/10,单7135/2
* 在开启60ms到5秒之间关闭可以换档,5秒后锁定档位
*/
#include <htc.h>
#include <pic.h>
#define DEBUG
#define StepAddress 12
#define MaxStep 4
// 加溶丝配置
__CONFIG (INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BOREN);
void dispLED (unsigned char);
void soft1msPWM (unsigned int);
unsigned char Step;
main ()
{
unsigned char newStep;
unsigned int i;
OSCCAL = _READ_OSCCAL_DATA();
CMCON = 7; // 关闭比较器
TRISIO = 0x3F; // 先设置所有引脚为输入
IOCB = 0; // 禁止引脚电平变化引起中断
WPU = 0; // 禁止弱上拉
Step = EEPROM_READ (StepAddress);
if (Step >= MaxStep)
Step = 0;
dispLED (Step);
soft1msPWM (60);
if ((newStep = Step + 1) >= MaxStep)
newStep = 0;
#ifdef DEBUG
TRIS5 = 0;
GPIO5 = 1;
#endif
EEPROM_WRITE (StepAddress, newStep);
soft1msPWM (3000);
#ifdef DEBUG
GPIO5 = 0;
TRIS5 = 1;
#endif
EEPROM_WRITE (StepAddress, Step);
for (;;)
soft1msPWM (100);
}
void dispLED (unsigned char step)
{
switch (step) {
case 0:
{
TRISIO = (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1);
GPIO0 = 1;
break;
}
case 1:
{
TRISIO = (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
GPIO0 = 1;
GPIO1 = 1;
break;
}
case 2:
case 3:
{
TRISIO = (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1);
GPIO0 = 1;
break;
}
default:
break;
}
}
void soft1msPWM (unsigned int loops)
{
unsigned char index;
unsigned char us100;
while (--loops) {
for (index = 0; index < 10; index++) {
if (Step == 2) {
if (index == 0)
GPIO0 = 1;
else if (index == 2)
GPIO0 = 0;
} else if (Step == 3) {
if (index == 0)
GPIO0 = 1;
else if (index == 5)
GPIO0 = 0;
} else {
if (index == 0)
GPIO0 = 1;
}
// 延时100us
us100 = 100;
while (--us100)
;
}
}
}
关闭
站长推荐
/3
用户377235 2012-5-8 17:43
用户377235 2012-5-8 17:43
用户377235 2012-5-8 17:31