if (RD7)
RD0=1;
elseRD0=0;
#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
}
}
}
RBPU = 0; // enable internal pullups on PORTB
TRISB0=0;
TRISB7=1;
To start with, the initialisation codeshould 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.Rich (BB code):RBPU = 0; // enable internal pullups on PORTB TRISB0=0; TRISB7=1;
//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;
}
}
}
// 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
Why are not using the built in dealy functions in HI-Tech? Is it your teacher that say so?
To start with, the initialisation codeshould 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.Rich (BB code):RBPU = 0; // enable internal pullups on PORTB TRISB0=0; TRISB7=1;
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
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 settingRich (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
![]()