我正在尝试为电池操作的设备建立
电源控制器。我选择PIC12F675。这个想法是将PIC置于睡眠模式。用秒表计时每秒唤醒它。检查引脚状态,决定是否接通或断开。决策代码工作正常。但是PIC不睡觉。它看起来像PIC跳过睡眠()宏。在问这个问题之前,我做了一个深入的研究并测试了我发现的所有东西。下面是配置:所有引脚都是IO。无外部晶体,无复位。没有浮针。我没有观察到任何睡眠情况,也重置条件。问题是我做错了什么PIC12F675没有睡觉?编程平台:MPLABX+XC8(最新版本免费)+PICIT3[&字体]配置。
以上来自于百度翻译
以下为原文
I'm trying to build a power controller for a battery operated device. I choose PIC12F675. The idea is put
ting pic in sleep mode. Wake it up with Watchdod timer approx each second. Check pin states, decide whether to switch on or off. The decision code is working as expected. But PIC does not sleep. It looks like PIC skips SLEEP() macro. Before asking this question i've made a deep research and tested everything I found. So here are the configuration:
----------
Vdd |1 8| GND/VSS
Dock test pin(out) GP5 |2 7| GP0 (in) Batt Measure
Power Fet Drive(out)GP4 |3 6| GP1 (in) Batt Measure reference voltage
Charge Status (in) GP3 |4 5| GP2 (in) Dock sense
----------
All pins are io. No external crystal, no reset. No floating pin.
I do not observe any sleep condition, also reset condition.
The question is what am i doing wrong that the PIC12F675 is not sleeping?
Programing platform: MPLABX + XC8 (Latest version-Free) + pickit3
[
Config.h
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON // Power-Up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#define _XTAL_FREQ 4000000 // this is used by the __delay_ms(xx) and __delay_us(xx) functions[main.c
unsigned int Read_ADC_Value(void)
{
unsigned int ADCValue;
ADCON0bits.GO = 1; // start conversion
while (ADCON0bits.GO); // wait for conversion to finish
ADCValue = ADRESH << 8; // get the 2 m***s of the result and rotate 8 bits to the left
ADCValue = ADCValue + ADRESL; // now add the low 8 bits of the resut into our return variable
return (ADCValue); // return the 10bit result in a single variable
}
unsigned int Check_Dock() {
GPIO5 = HIGH;
__delay_ms(5);
unsigned int result = GPIO2;
GPIO5 = LOW;
return result;
}
unsigned int Read_Batt(){
unsigned int adc=Read_ADC_Value();
float voltage = ADCSTEP * adc * 200.0;
return (unsigned int)(voltage);
}
void main(void) {
//Configure Watchdog to approx 1 sec
CLRWDT();
OPTION_REGbits.PSA = 1; //postscaler is for wdt
OPTION_REGbits.PS = 0x110; //postscaler 1:64
OPTION_REGbits.nGPPU = 0; //pullups enabled
WPU = 0xff; //enable pullups
TRISIObits.TRISIO0 = INPUT; //AN0-GPIO0 - input - analog okuma için pil değeri.
TRISIObits.TRISIO1 = INPUT; //AN1 -Vref
ANS2=0; TRISIObits.TRISIO2 = INPUT; //GP2 <- Dock sense low=docked high=undocked
ANS3=0; TRISIObits.TRISIO3 = INPUT; //Charge Status -> GP3
TRISIObits.TRISIO4 = INPUT;
TRISIObits.TRISIO5 = OUTPUT; //Dock test pin <- GP5
GPIO5 = LOW; //do not output anything
//Setup analog
ANSELbits.ANS0 = INPUT; //analog input 0 is used
ADCON0bits.ADON = 1; //adc is on
ADCON0bits.CHS = 0x00; //an0 is tied to the sampler
ADCON0bits.VCFG = 1; //1=>use vRref pin as voltage reference, 0=> use vdd
ADCON0bits.ADFM = 0x01; //ADC results are right-justifed
ANSELbits.ADCS = 0x011; //Conversion clock = FRC internal: measurement can take till 6usec
INTCON = 0; //disable every interrupt
PIE1=0;
PIR1=0;
INTCONbits.GIE = 0;
//Program Loop
while (1) {
SLEEP();
NOP();
INTCONbits.GPIF=0;
unsigned char sta = 0;
if(Read_Batt() <= BATTLOWTHRESHOLD){
//Batt low turn everything off
sta++;
}
unsigned int isCharging = GPIO3; //0:not charging; 1: charging;
sta+=isCharging;
unsigned int isnotDocked = Check_Dock();
if(isnotDocked) { //0:docked; 1:flying
NOP();
}else{
sta++;
}
if(sta) {
//disable fet
TRISIObits.TRISIO4 = INPUT;
WPU4=1; //enable port pullup
} else {
TRISIObits.TRISIO4 = OUTPUT;
GPIO4 = LOW; //enable fet
}
}//End of: Main loop
}