4X4 Dual Battery Monitor

Status
Not open for further replies.

Axle_80

New Member
Hi everyone,
So I'm trying to program a replacement pic16f818 to operate a dual battery monitor but as this is my first ever pic project I'm having some difficulty.
I'v attached the Schematic which uses Charlieplexing for the led indicators and the code I have so far.
The problems I'm facing right now are the code is too large for the pic and I don't know how to reduce it and also the ADC doesn't operate on both channels.
If I comment out the code for one channel the other will work.
any help here would be much appreciated.
Thanks.


Code:
float VM;
float VA;
void main(void){

    OSCCON = 0b01110000;
    TRISA = 0b00000011;
    TRISBbits.TRISB6 = 1;
    TRISBbits.TRISB7 = 1;
    PORTAbits.RA6=0;
    PORTAbits.RA2=1;
    ADCON1 = 0b00000100;
       while(1){
// ADC RA0   
      
    ADCON0 = 0b11000001;
    //PIR1bits.ADIF = 0; //Clear ADIF bit
    ADCON0bits.GO_DONE = 1;
   
    if (ADCON0bits.GO_DONE==0)VM = (ADRESH*0.01960784)*3;
   
  
  
   /*
    if (VM>11.4){TRISB = 0b11101110,PORTB = 0b00000001;}//1
    __delay_ms(1);
    if (VM>11.6){PORTB = 0b00010000;}//2
    __delay_ms(1);
    if (VM>11.8){TRISB = 0b11011110,PORTB = 0b00000001;}//3
    __delay_ms(1);
    if (VM>12){PORTB = 0b00100000;}//4
    __delay_ms(1);
    if (VM>12.1){TRISB = 0b11111100,PORTB = 0b00000001;}//5
    __delay_ms(1);
    if (VM>12.2){PORTB = 0b00000010;}//6
    __delay_ms(1);
    if (VM>12.3){TRISB = 0b11111001,PORTB = 0b00000010;}//7
    __delay_ms(1);
    if (VM>12.4){PORTB = 0b00000100;}//8
    __delay_ms(1);
    */
    if (VM>12.5){TRISB = 0b11111010,PORTB = 0b00000001;}//9
    __delay_ms(1);
    if (VM>12.6){PORTB = 0b00000100;}//10
    __delay_ms(1);
    if (VM>13){TRISB   = 0b11001111,PORTB = 0b00010000;}//11
    __delay_ms(1);
    if (VM>13.2){PORTB = 0b00100000;}//12
    __delay_ms(1);
    if (VM>13.7){TRISB = 0b11100111,PORTB = 0b00001000;}//13
    __delay_ms(1);
    if (VM>14.2){PORTB = 0b00010000;}//14
    __delay_ms(1);
    TRISB = 0b11111111;
  
      
// ADC RA1
   
    ADCON0 = 0b11001001;
  //  PIR1bits.ADIF = 0; //Clear ADIF bit
    ADCON0bits.GO_DONE = 1;
   
    if (ADCON0bits.GO_DONE==0)VA = (ADRESH*0.01960784)*3;
 
    /*
    if (VA>11.4){TRISB = 0b11110011,PORTB = 0b00000100;}//15
    __delay_ms(1);
    if (VA>11.6){PORTB = 0b00001000;}//16
    if (VA>11.8){TRISB = 0b11101011,PORTB = 0b00000100;}//17
    __delay_ms(1);
    if (VA>12){PORTB = 0b00010000;}//18
    if (VA>12.1){TRISB = 0b11011011,PORTB = 0b00000100;}//19
    __delay_ms(1);
    if (VA>12.2){PORTB = 0b00100000;}//20
    if (VA>12.3){TRISB = 0b11110101,PORTB = 0b00000010;}//21
    __delay_ms(1);
    if (VA>12.4){PORTB = 0b00001000;}//22
     */
   
    if (VA>12.5){TRISB = 0b11101101,PORTB = 0b00000010;}//23
    __delay_ms(1);
    if (VA>12.6){PORTB = 0b00010000;}//24
    __delay_ms(1);
    if (VA>13){TRISB   = 0b11011101,PORTB = 0b00000010;}//25
    __delay_ms(1);
    if (VA>13.2){PORTB = 0b00100000;}//26
    __delay_ms(1);
    if (VA>13.7){TRISB = 0b11110110,PORTB = 0b00000001;}//27
    __delay_ms(1);
    if (VA>14.2){PORTB = 0b00001000;}//28
    __delay_ms(1);
    TRISB = 0b11111111;
      
       }  
     
}
 

Attachments

  • Battery Controller Dual User Guide NLBMDAS.pdf
    28.5 KB · Views: 159
I can give you a hint on the a/d, however there shoudl be enough room in the pic for that code, is the compiler setup right?

As for the a/d, you switch the i/p to the a/d then do a conversion immediately, if you look at the pic datasheet there is an aquisition time, you need t wait for a period after you change the a/d channel before a conversion, try the 1ms you've been using after selecting the i/p.
 

Thanks Dr Pepper
I uninstalled then reinstalled xc8 then added the delay before ADCON and its now compiling the full code and working on both channels.
Your help is greatly appreciated.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…