0

I am building a Voltmeter for a PIC18F using an XC8 compiler on MPLAB and I am having trouble getting my second digit value to update as I rotate my potentiometer. A potentiometer is connected to PORTA, the two digit seven segment display is connected to two 74LS47 decoders. and those two decoders are connected to PORTS B0-B7. PORT B0-B3 is attached to the first decoder, and B4-B7 the second decoder which drives the second digit of the display. When I rotate the potentiometer, the first digit changes accordingly, but the second digit just stays at zero. I've tried multiplying the voltage by 10 then using mod(%) to extract the second digit value, but it seems the compiler doesn't use %. I have tried using a work around to mod(in the code below) to see if it would work and nothing has so far. Also, I've tested the second digit by outputting "firstdig" to PORTS B4-B7 to make sure my circuit is built correctly and it was. Any guidance or suggestions would be greatly appreciated. I've been stuck on this for quite a while.

        int VREF = 5;                         
        float VOLTS = ((num*VREF)/1024);     
        int firstdig = floor(VOLTS);
        int seconddig = floor(VOLTS * 10) - (firstdig * 10);
        int LSB = firstdig;             
        int MSB = seconddig;   
         MSB = MSB<<4;
        PORTB = LSB | MSB;
        __delay_ms(50); ```
       
C_GNARWIN
  • 13
  • 3
  • 1
    Have you tried any kind of debugging? Like sending values over serial port and looking at them? Or stepping through the code in an actual debugger? – Some programmer dude Jul 21 '21 at 07:53
  • Take a look at the disassembly. Does `PORTB = LSB | MSB` write to the port once and once only, or does it write LSB, then read the port and OR with MSB? (the port may be write-only) I would be inclined to try putting `LSB | MSB` into another variable and then assigning it to PORTB. Also ensure that PORTB is declared `volatile` – Den-Jason Jul 21 '21 at 08:01
  • 1
    What type is `num`? If it is an integer, your calculation of `VOLTS` is always an integer. – the busybee Jul 21 '21 at 08:24
  • Have I tried debugging? Yes, I did debug it, but I always take the debug tool with a grain of salt since earlier in the semester it used to give me questionable results. I haven't sent values over a serial port though. I have put LSB|MSB into another variable and tried using that variable as my output to PORTB and yes PORTB is volatile. If I would have posted the line of code for **num** you all probably would have jumped on that. I changed it fro integer to float and it works now. I appreciate all of your help very much and thank you for your time. – C_GNARWIN Jul 21 '21 at 08:35

0 Answers0