Menu

18F1320 and 7-segment LED's--it works, but...

2008-10-25
2013-05-30
  • Nobody/Anonymous

    I have two 7-segment LED's connected to port B in parallel. Port A.0 and port A.4 go to the bases of two 2n3906 PNP transistors, which in turn switch on and off the appropriate segment's anode (to show either "tens" or "ones"). Please note, I am not using the 7-segment library because my LED's are common anode and I am not sure that I understand how to set the relevant constants.

    The following code works:

    'Chip model
    #chip 18F1320, 8
    #config OSC=INTIO1 '1

    'This program count from 0 to 99 on a common anode
    'two 7segment LED displays
    'The display segments connected to PORTB and the
    'two andodes to PORTA.0 and PORTA.4

    dir PORTB OUT
    dir PORTA.0 OUT
    dir PORTA.4 OUT

    'Main routine
    set PORTA.4 = ON
    set PORTA.0 = ON
    wwait = 2

    Start:
    for num = 0 to 99
        for i=1 to 200
            set PORTA.4 = Off
            set PORTA.0  = On
            ddigit = num/10
            tens = result
            PORTB = tens
            wait wwait ms
            PORTB=255   
            set PORTA.4 = On
            set PORTA.0  = Off
            ddigit = num%10
            ones = result
            PORTB = ones
            wait wwait ms
            PORTB=255
        next
    next
    goto Start

    function result
      select case ddigit
        case 0 
            result = b'11000000'
        case 1 
            result = b'11111001'
        case 2 
            result = b'10100100'
        case 3 
            result = b'10110000'
        case 4 
            result = b'10011001'
        case 5 
            result = b'10010010'
        case 6 
            result = b'10000010'
        case 7 
            result = b'11111000'
        case 8 
            result = b'10000000'
        case 9 
            result = b'10010000'
      end select
    end function 

    Originally, instead of PORTA.0 and PORT.4, for anode switching I've used PORTA.0 and PORTA.1. However, only the PORTA.0 was turning its segment ON and OFF, while the other segment was always on, meaning that PORTA.1 was not doing its job. I've checked the 18F1320 manual and could not find anything that would point to this behaviour. I'd appreciate if anyone can come with an explanation. My guess is that it has to do with the chip configuration, but I don't understand what is missing here.

     
    • kent_twt4

      kent_twt4 - 2008-10-25

      I think you are alright except you would want to blank the PortB to zero not 255.  PortA.1 should have worked.  PortA.4 will not because it is an open drain output.  PortA.4 can only go low if the pin direction is  output and set to on, and only go high if an external pullup is used and the pin direction is changed to input.

       
    • kent_twt4

      kent_twt4 - 2008-10-25

      Oops!  Forgot you have common anode displays.   So blanking display by using PortB=255 is correct.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.