pic18f452 , PORTB

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, Sep 26, 2014.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Hello,
    i am working in real time with PIC18f452. i try this code but confusion is here on the output.
    Code (Text):
    1. #include<htc.h>
    2.  
    3.  
    4. // PIC 18F452 fuse configuration:
    5. // Config word 1 (Oscillator configuration)
    6. // 40Mhz crystal input
    7. __CONFIG(1, OSCSDIS & HSPLL);
    8. // Config word 2
    9. __CONFIG(2, BORDIS & PWRTDIS & WDTDIS);
    10. // Config word 3
    11. __CONFIG(3, CCP2RC1);
    12. // Config word 4
    13. __CONFIG(4, DEBUGEN & LVPEN & STVREN);
    14. // Config word 5, 6 and 7 (protection configuration)
    15. __CONFIG(5, UNPROTECT);
    16. __CONFIG(6, WRTEN);
    17. __CONFIG(7, TRU);
    18.  
    19. #define _XTAL_FREQ 40000000   //MHz
    20.  
    21. void delay_sec(unsigned char seconds)    // This function provides delay in terms of seconds
    22. {
    23.     unsigned char i,j;
    24.  
    25.     for(i=0;i<seconds;i++)
    26.         for(j=0;j<100;j++)
    27.             __delay_ms(10);
    28. }
    29.  
    30. void main()
    31. {
    32.            
    33.     TRISB = 0;    
    34.  
    35.     while(1)
    36.     {
    37.         PORTB = 0x55;      
    38.         delay_sec(1);   // delay of one second
    39.  
    40.         PORTB = 0xAA;      
    41.         delay_sec(1);    // delay of one second
    42.     }
    43. }
    44.  
    when i simulate this code in proteus then its shows all the blinking PINS of portb, but when i burn this code on PIC18f452 then RB0-RB4 blinking but RB5,RB6,RB7 not blinking...
    what is its reason??? i also check the voltage these pins have 0 volt. please guide me in this way.

    I also try this with
    LATB= 0x55 ;
    and
    LATB= 0xAA;

    Regards,
     
  2. paulfjujo

    New Member

    Mar 6, 2014
    20
    3
    hello,

    try to remove LVPEN in bits config
    Low Voltage ICSP Programming (LVP) is enabled by default, which disables the RB5 I/O function.
    LVP must be disabled to enable RB5 as an I/O pin


    Code (Text):
    1.  
    2. #include<htc.h>
    3.  
    4.  
    5. // PIC 18F452 fuse configuration:
    6. // Config word 1 (Oscillator configuration)
    7. // 40Mhz crystal input
    8. __CONFIG(1, OSCSDIS & HSPLL);
    9. // Config word 2
    10. __CONFIG(2, BORDIS & PWRTDIS & WDTDIS);
    11. // Config word 3
    12. __CONFIG(3, CCP2RC1);
    13. // Config word 4
    14. __CONFIG(4,STVREN);
    15. // Config word 5, 6 and 7 (protection configuration)
    16. __CONFIG(5, UNPROTECT);
    17. __CONFIG(6, WRTEN);
    18. __CONFIG(7, TRU);
    19.  
    20. #define _XTAL_FREQ 40000000   //MHz
    21.  
    22.  
    23.  
    24. void delay_sec(unsigned char seconds)    // This function provides delay in terms of seconds
    25. {
    26.     unsigned char i,j;
    27.  
    28.     for(i=0;i<seconds;i++)
    29.     {
    30.        for(j=0;j<100;j++) __delay_ms(10);
    31.     }      
    32. }
    33.  
    34. void main()
    35. {
    36.        
    37.     TRISB = 0;    
    38.     while(1)
    39.     {
    40.         LATB = 0x55;   // ->  0101 0101
    41.         delay_sec(1);   // delay of one second
    42.         LATB = 0xAA;    // ->  1010 1010    
    43.         delay_sec(1);    // delay of one second
    44.     }
    45. }
     
    ect_09 likes this.
  3. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i want to know about configuration bits and their uses.
    please guide me in this way
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Look at page 197 of the datasheet, it explains what the configuration bits are and their use.
     
    ect_09 likes this.
  5. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Keep in mind that most of the settings are dedicated to peripherics. Let's say that the most "basic" is with all them off.

    Interruptions aside, configuration was the major stumbling block I had in the past. Good luck.
     
  6. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    with this code RB5 is not blinking but the other pins are blinking on real hardware..
    what i do now..
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Now you debug...

    What value oscillator/crystal do you have attached?

    What are your connections?

    Post a schematic and a picture of your setup, and we can take a look..
     
  8. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    PIX is attached that i build up in my hardware..
    why RB5 is showing 0 volt on its pin.???

    circuitry is fine...working ok,
     
    • m.jpg
      m.jpg
      File size:
      294.2 KB
      Views:
      27
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    What circuitry is fine: simulation or hardware?

    The simulation layout looks fine, so, if it works in software, there is something with the hardware that should be examined...
     
    ect_09 likes this.
  10. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    am working on hardware..
    simulation shows all pins are working but on hardware RB5/PGM is not working...
    :(
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    See page 94 in the datasheet.

    Note 5 on a Table 9-3 says that with LVP enabled, RB5 is PGM and I/O functions are disabled...

    Turn off LVP in the configuration bits.
     
    ect_09 likes this.
  12. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Thanks , its working now...
     
  13. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Good to hear. This is why you must:
    1. Read the datasheet
    2. Understand that the simulation will not account for all of the nuances of a particular piece of hardware.

    If the simulation works, look at your hardware and configuration settings.
     
    ect_09 likes this.
  14. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    exactly, i ll start reading data sheet tomorrow.
    sir please recommend me book to learn PIC18f. although data sheet is enough. but i really want to read the programming style too.
     
  15. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I have not seen a book I can really get behind. The last one I saw was so fraught with errors it was next to useless.

    As you noted, the datasheet is what you need to have before you can effectively use a particular device.

    Programming styles are a personal preference, for instance, I use the following style:
    Code (Text):
    1. //any #includes
    2.  
    3. //function prototypes
    4. void Initialize();
    5.  
    6. void main()
    7. {
    8.     Initialize ();
    9.  
    10.     while(1)
    11.     {
    12.         //do stuff
    13.  
    14.     }
    15. }
    16.  
    17. //function implementations
    18. void Initialize()
    19. {
    20.     //do initialization
    21.  
    22. }
    23.  
    Though another person may dislike that layout.

    Look at plenty of examples of people having used the peripheral that you are attempting to use...
     
    Last edited: Sep 26, 2014
    ect_09 likes this.
Loading...