/* * File: newmain.c * Author: N.Mit.YAMAGUCHI * * Created on 2020/02/15, 14:07 */ // CONFIG #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 = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled) #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) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include #define _XTAL_FREQ 4000000 #include #include /* * */ unsigned an0in(void){ unsigned ad_data; ADCON0=0b10000001; __delay_ms(1); GO=1; while(GO){} ad_data=0; ad_data=ADRESH; ad_data<<=8; ad_data|=ADRESL; return(ad_data); } unsigned v_stat(unsigned v){ unsigned stat; if (v < 629) stat = 1; if ((v >= 629) && (v < 699)) stat = 2; if ((v >= 699) && (v < 757)) stat = 3; if ((v >= 757) && (v < 815)) stat = 4; if (v >= 815) stat = 5; return (stat); } void main(void) { unsigned vbat; unsigned bat_stat; unsigned char motor; unsigned timer; unsigned char chg_f; unsigned char load_f; unsigned char port; unsigned char sw; unsigned count; TRISIO = 0b00011001; // bit0,3,4 input WPU = 0b00010000; // pull-up GPIO4 VRCON = 0; // VRef Disable ANSEL = 0b00010001; // bit0 analog in INTCON = 0b00000000; // interrupt disables OPTION_REG = 0b00000000; motor = timer = chg_f = load_f = 0; port = 0; GPIO = port; while(1){ vbat = an0in(); bat_stat = v_stat(vbat); if (bat_stat == 1){ chg_f = 0; load_f = 1; } if (bat_stat == 2){ chg_f = 0; } if (bat_stat == 3){ load_f = 0; chg_f = 0; } if (bat_stat == 4){ load_f = 0; } if (bat_stat == 5){ load_f = 0; chg_f = 1; } if (timer == 0){ if (motor == 0){ motor = 1; port |= 0x02; port &= 0xfb; GPIO = port; sw = (~GPIO & 0x18) >> 3; timer = (sw+1)*11-1; }else{ motor = 0; port &= 0xfd; port |=0x04; GPIO = port; sw = (~GPIO & 0x18) >> 3; timer = (sw+1)*446-1; port &= 0xfb; GPIO = port; } }else{ timer--; } if (chg_f == 0){ port |= 0x20; GPIO = port; }else{ port &= 0xdf; GPIO = port; } count = 5 - bat_stat; while (count--){ __delay_ms(400); } while (bat_stat--){ port |= 0x04; GPIO = port; __delay_ms(200); port &= 0xfb; GPIO = port; __delay_ms(200); } __delay_ms(1000); } return; }