use switch to turn on led(HTC)

Discussion in 'Programmer's Corner' started by Jaden5165, Sep 14, 2011.

  1. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    if i want to try by using switch to turn on led.when switch is pressed led on when release led off.how the coding going to be?need to use INTERRUPT?what is the funvtion of ANSEL and ANSELH?have to configure too?
    thank for your reply^^
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Jaden I think need a good C tutorial. This is old but still very good http://en.wikipedia.org/wiki/The_C_Programming_Language I guess you will find it in your school library. In the simplest form you do not need any interrupts. And for the ANSEL and ANSELH registers which I am not sure are on your PIC 16f877. Refer to the datasheet.
    So to the testing of one input PIN. In HTC this is quite simple. Say you want to use RD7 as input pin (or any pin that can be used as digital input). If it is set up correctly You can write something like this given that RD0 is set up as output
    Code ( (Unknown Language)):
    1.  
    2. [LEFT]if (RD7)
    3. RD0=1;
    4. else [/LEFT]
    5. RD0=0;
    6.  
    I have not touched the topic debouncing as you must learn to walk walk before you can run. For this simple example it is not very important. I will also recommend taking a look at the pic16f877.h file in your compilers include folder
     
  3. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    i forgot what is the internal pull up resistor used for?can someone explain to me again?what is the different with and without the pull up resistor?
     
  4. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
  5. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. __CONFIG(0x3F39);
    4.  
    5. void main(void)
    6. {
    7. while(1) // continually
    8. {
    9. CLRWDT();
    10. RBPU = 0; // enable internal pullups on PORTB
    11. TRISB0=0;
    12. TRISB7=1;
    13.  
    14.  
    15. if (RB7==0) // if the switch is pressed
    16. {
    17. RB0= 1; // turn the LED on
    18. }
    19. else
    20. {
    21. RB0= 0; //off
    22.  
    23. }
    24. }
    25. }
    26.  

    why the 1st and 2nd time i press my button my led can on.but when 3rd or so on the led canot work.what is the problem of my coding?
     
  6. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    To start with, the initialisation code
    Code ( (Unknown Language)):
    1. RBPU = 0; // enable internal pullups on PORTB
    2. TRISB0=0;
    3. TRISB7=1;
    should only be called once, so you need that moving outside the while loop. IIRC from your last posts 0x3f39 for the config word means the watchdog timer is disabled so I wouldn't have thought you'd need the CLRWDT() in there either. I do seriously suggest you look at the link I posted for Googligum. They're fantastic tutorials and will take you through the basics quickly, they cover things like debouncing switches which is likely to be a problem for you... they'll also teach you how to use the config so that people can tell at a glance how that's set up. and not have to refer to your previous post.
     
  7. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0



    ya,i have referred to the Gooligum PDF.but i dont understand the switch or decounced part.coz the book used different symbol.but i know the book really good for high tech c beginner like me!
     
  8. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Code ( (Unknown Language)):
    1.  
    2. //C PROGRAM (use High TECH C compiler)
    3. #include <htc.h>
    4. __CONFIG(0x3F39);   //configuration bits
    5. ////////////////////////////////////////////////////////////////////////////////////////////////
    6. void delay()                          //delay function
    7.     {
    8.     for(int i=0;i<100;i++)
    9.         {
    10.             for(int j=0;j<1000;j++);
    11.         }
    12.     }
    13. ///////////////////////////////////////////////////////////////////////////////////////////////
    14. main()
    15. {
    16. int a=1;
    17. TRISD=0;                     //set all PORTD bits as output
    18. while(1)                    //infinite loop
    19.     {
    20.     PORTD=a;              
    21.     delay();                //call delay function
    22.     a<<=1;                   //new a = old a left shift by one
    23.     if(a>0b10000000)           //if a exceeds limit of 8 LEDs,
    24.         {                      //then a is made 1
    25.             a=1;
    26.         }
    27.    
    28.     }
    29. }
    30.  

    the PORTD=a
    not turn on all the PORTD led?but why it function by turn on only the RD0 LED?
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Why are not using the built in dealy functions in HI-Tech? Is it your teacher that say so? In this case I would recommend that you fire up the debugger. Also after a quick look. int is 16 bit. Use the char data type then working with SFRs like PORTD. Remember programming is attention to details. And your best friend is the MPLAB sim debugger. Consult it first. Then come to us for help if you are still stuck. Have you looked at the webseminars I recommend here http://forum.allaboutcircuits.com/showthread.php?t=44852
    So one lesson for you. Have you looked at the pic16f877.h (or 16f877a.h if you use that chip) header file as I told you to do. In this file you will very near the top find this
    Code ( (Unknown Language)):
    1.  
    2. // Config Register: CONFIG
    3. #define CONFIG               0x2007
    4. // Oscillator Selection bits
    5. // RC oscillator
    6. #define FOSC_EXTRC           0xFFFF
    7. // HS oscillator
    8. #define FOSC_HS              0xFFFE
    9. // XT oscillator
    10. #define FOSC_XT              0xFFFD
    11. // LP oscillator
    12. #define FOSC_LP              0xFFFC
    13. // Watchdog Timer Enable bit
    14. // WDT enabled
    15. #define WDTE_ON              0xFFFF
    16. // WDT disabled
    17. #define WDTE_OFF             0xFFFB
    18. // Power-up Timer Enable bit
    19. // PWRT disabled
    20. #define PWRTE_OFF            0xFFFF
    21. // PWRT enabled
    22. #define PWRTE_ON             0xFFF7
    23. // FLASH Program Memory Code Protection bits
    24. // Code protection off
    25. #define CP_OFF               0xFFFF
    26. // 1F00h to 1FFFh code protected
    27. #define CP_UPPER_256         0xEFEF
    28. // 1000h to 1FFFh code protected
    29. #define CP_HALF              0xDFDF
    30. // 0000h to 1FFFh code protected
    31. #define CP_All               0xCFCF
    32. // Brown-out Reset Enable bit
    33. // BOR enabled
    34. #define BOREN_ON             0xFFFF
    35. // BOR disabled
    36. #define BOREN_OFF            0xFFBF
    37. // Low Voltage In-Circuit Serial Programming Enable bit
    38. // RB3/PGM pin has PGM function; low-voltage programming enabled
    39. #define LVP_ON               0xFFFF
    40. // RB3 is digital I/O, HV on MCLR must be used for programming
    41. #define LVP_OFF              0xFF7F
    42. // Data EE Memory Code Protection
    43. // Code Protection off
    44. #define CPD_OFF              0xFFFF
    45. // Data EEPROM memory code-protected
    46. #define CPD_ON               0xFEFF
    47. // FLASH Program Memory Write Enable
    48. // Unprotected program memory may be written to by EECON control
    49. #define WRT_ON               0xFFFF
    50. // Unprotected program memory may not be written to by EECON control
    51. #define WRT_OFF              0xFDFF
    52. // In-Circuit Debugger Mode bit
    53. // In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
    54. #define DEBUG_OFF            0xFFFF
    55. // In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
    56. #define DEBUG_ON             0xF7FF
    57.  
    58.  
    Then creating the __config() word you just and the settings together. So let us say I would use external XT crystal and disable watchdog timer. I just write __config(FOSC_XT & WDTE_OFF.......) Not so hard. Your lesson to your next post is to swap the magic number with a readable config setting. No more help from me until you have come up with a suggestion for a readable config setting :D
     
    Last edited: Sep 15, 2011
    Jaden5165 likes this.
  10. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0


    nonono.the code is founded from somewhr.but the delay actually i change to the form you recommended coz i like the form too more accurate!
     
  11. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0




    I try to shift the code u mentioned.but still the same.it only can turn on when i pressed in the 1st time.thn cannot on again if i repressed.
     
  12. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    Yep, the fact you were repeating the port setup code was pretty much irrelevant to your problem of the switch working intermittantly, it was just one aspect that needed looking at. I imagine, though smarter people than me may correct me, that the problem lies with the lack of debouncing the switch. I really can't stress the importance of understanding the simple examples before moving on enough. I'd suggest working through some more of the tutorial's examples and asking questions here about parts you don't understand. And tbh I'd avoid whereever it is that thinks writing __config(0x3f39) and using for loops for delays is a good idea...
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Just one thing you do know that your programmer/debugger may take control of some pins on PORTB. At lest as long as it is plugged in. Until you get warmer using your PIC. Use port C and D instead.
     
  14. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    if i try to turn on a led.and when i try to press the reset button the led will not light again or will {relight}?
     
  15. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0



    @to6afre

    i just saw your comment.okay,i'll go for the lesson you said.thank!
     
Loading...