use switch to turn on led(HTC)

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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^^
 

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
if (RD7) RD0=1; else​
RD0=0;
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
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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?
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
Rich (BB code):
#include <htc.h>
__CONFIG(0x3F39);
 
void main(void)
{
while(1) // continually
{
CLRWDT();
RBPU = 0; // enable internal pullups on PORTB
TRISB0=0;
TRISB7=1;
 
 
if (RB7==0) // if the switch is pressed
{
RB0= 1; // turn the LED on
}
else
{
RB0= 0; //off
 
}
}
}

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?
 

cheezewizz

Joined Apr 16, 2009
82
To start with, the initialisation code
Rich (BB code):
RBPU = 0; // enable internal pullups on PORTB 
TRISB0=0; 
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.
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
To start with, the initialisation code
Rich (BB code):
RBPU = 0; // enable internal pullups on PORTB 
TRISB0=0; 
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.



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!
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
Rich (BB code):
//C PROGRAM (use High TECH C compiler)
#include <htc.h>
__CONFIG(0x3F39);   //configuration bits
////////////////////////////////////////////////////////////////////////////////////////////////
void delay()                          //delay function
    {
    for(int i=0;i<100;i++)
        {
            for(int j=0;j<1000;j++);
        }
    }
///////////////////////////////////////////////////////////////////////////////////////////////
main()
{
int a=1;
TRISD=0;                     //set all PORTD bits as output
while(1)                    //infinite loop
    {
    PORTD=a;              
    delay();                //call delay function
    a<<=1;                   //new a = old a left shift by one
    if(a>0b10000000)           //if a exceeds limit of 8 LEDs, 
        {                      //then a is made 1
            a=1;
        }
    
    }
}

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

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
// Config Register: CONFIG
#define CONFIG               0x2007
// Oscillator Selection bits
// RC oscillator
#define FOSC_EXTRC           0xFFFF
// HS oscillator
#define FOSC_HS              0xFFFE
// XT oscillator
#define FOSC_XT              0xFFFD
// LP oscillator
#define FOSC_LP              0xFFFC
// Watchdog Timer Enable bit
// WDT enabled
#define WDTE_ON              0xFFFF
// WDT disabled
#define WDTE_OFF             0xFFFB
// Power-up Timer Enable bit
// PWRT disabled
#define PWRTE_OFF            0xFFFF
// PWRT enabled
#define PWRTE_ON             0xFFF7
// FLASH Program Memory Code Protection bits
// Code protection off
#define CP_OFF               0xFFFF
// 1F00h to 1FFFh code protected
#define CP_UPPER_256         0xEFEF
// 1000h to 1FFFh code protected
#define CP_HALF              0xDFDF
// 0000h to 1FFFh code protected
#define CP_All               0xCFCF
// Brown-out Reset Enable bit
// BOR enabled
#define BOREN_ON             0xFFFF
// BOR disabled
#define BOREN_OFF            0xFFBF
// Low Voltage In-Circuit Serial Programming Enable bit
// RB3/PGM pin has PGM function; low-voltage programming enabled
#define LVP_ON               0xFFFF
// RB3 is digital I/O, HV on MCLR must be used for programming
#define LVP_OFF              0xFF7F
// Data EE Memory Code Protection
// Code Protection off
#define CPD_OFF              0xFFFF
// Data EEPROM memory code-protected
#define CPD_ON               0xFEFF
// FLASH Program Memory Write Enable
// Unprotected program memory may be written to by EECON control
#define WRT_ON               0xFFFF
// Unprotected program memory may not be written to by EECON control
#define WRT_OFF              0xFDFF
// In-Circuit Debugger Mode bit
// In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
#define DEBUG_OFF            0xFFFF
// In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
#define DEBUG_ON             0xF7FF
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:

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
To start with, the initialisation code
Rich (BB code):
RBPU = 0; // enable internal pullups on PORTB 
TRISB0=0; 
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.




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.
 

cheezewizz

Joined Apr 16, 2009
82
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...
 

t06afre

Joined May 11, 2009
5,934
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.
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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
Rich (BB code):
// Config Register: CONFIG
#define CONFIG               0x2007
// Oscillator Selection bits
// RC oscillator
#define FOSC_EXTRC           0xFFFF
// HS oscillator
#define FOSC_HS              0xFFFE
// XT oscillator
#define FOSC_XT              0xFFFD
// LP oscillator
#define FOSC_LP              0xFFFC
// Watchdog Timer Enable bit
// WDT enabled
#define WDTE_ON              0xFFFF
// WDT disabled
#define WDTE_OFF             0xFFFB
// Power-up Timer Enable bit
// PWRT disabled
#define PWRTE_OFF            0xFFFF
// PWRT enabled
#define PWRTE_ON             0xFFF7
// FLASH Program Memory Code Protection bits
// Code protection off
#define CP_OFF               0xFFFF
// 1F00h to 1FFFh code protected
#define CP_UPPER_256         0xEFEF
// 1000h to 1FFFh code protected
#define CP_HALF              0xDFDF
// 0000h to 1FFFh code protected
#define CP_All               0xCFCF
// Brown-out Reset Enable bit
// BOR enabled
#define BOREN_ON             0xFFFF
// BOR disabled
#define BOREN_OFF            0xFFBF
// Low Voltage In-Circuit Serial Programming Enable bit
// RB3/PGM pin has PGM function; low-voltage programming enabled
#define LVP_ON               0xFFFF
// RB3 is digital I/O, HV on MCLR must be used for programming
#define LVP_OFF              0xFF7F
// Data EE Memory Code Protection
// Code Protection off
#define CPD_OFF              0xFFFF
// Data EEPROM memory code-protected
#define CPD_ON               0xFEFF
// FLASH Program Memory Write Enable
// Unprotected program memory may be written to by EECON control
#define WRT_ON               0xFFFF
// Unprotected program memory may not be written to by EECON control
#define WRT_OFF              0xFDFF
// In-Circuit Debugger Mode bit
// In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
#define DEBUG_OFF            0xFFFF
// In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
#define DEBUG_ON             0xF7FF
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



@to6afre

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