Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Learning PIC MCU Programming - Switch

Status
Not open for further replies.
How do you know RB7 is not high? Is it an analogue pin? If so, you need to make it digital. Are you overloading the pin with the wrong series resistor or the LED incorrect? Is RB7 a debug pin?

Mike.
Edit, Ian beat me to it. BTW, I tried to download the datasheet but Microchip isn't responding. I could get one from Mouser but it seemed to be a shortened version, didn't even have the pinout!! I hope this isn't how the new datasheets are going to be.

Might just be a short version for advertising purposes?, I've certainly never seen a less than 'far too much' datasheet :D

The 18F27K40 is 818 pages :nailbiting:
 
How can it!!!!! You are using RB7 as a debug pin... You cant use RB7 when debugging, its using RB6 and RB7 to run the program..

I must admit to a moment of panic the other day - I'd ordered some PIC boards from JLCPCB, and I'd wired the ICSP connector to RB6/RB7 as usual (without consulting the datasheet) - and a couple of days later I was looking at a previous design (with a different PIC), to find that the ICSP pins weren't RB6/7, they were RA0 and RA1 :nailbiting:

Anyway, I consulted the datasheet, and luckily it was still RB6/7 on the device I was using.
 
the ICSP pins weren't RB6/7, they were RA0 and RA1
Yeah.. All the pics with Vdd on pin 1 are like that... I have a 28pin dev board I use for these newer footprints..
 
How can it!!!!! You are using RB7 as a debug pin... You cant use RB7 when debugging, its using RB6 and RB7 to run the program..
you are correct I didn't see datasheet before that RB7 is PGD pin

I am using RB5 instead of RB7

Please find the two screenshot my. goal was to understand the process of hardware debugging. I am setting break point to check pin status. I think I understood the hardware debugging process using PIC kit 3
 

Attachments

  • Pin High.jpg
    Pin High.jpg
    207.9 KB · Views: 214
  • Pin Low.jpg
    Pin Low.jpg
    206.4 KB · Views: 213
Last edited:
A link to the scary datasheet I found.

Mike.
I have attached datasheet I am following

Do I need to configure following register to set timer interrupt ?

What should be value of timer register to generate interrupt at every 1 seconds ?


Timer register.jpg
 

Attachments

  • pic18f66k80 family Datasheet.pdf
    4.6 MB · Views: 223
I have attached datasheet I am following

Do I need to configure following register to set timer interrupt ?

What should be value of timer register to generate interrupt at every 1 seconds ?


View attachment 127165

I'll repeat again - use the MCC - this allows you to easily set your clock oscillator and the various peripherals, such as the timers.

However, depending on your clock speed it's VERY unlikely you will be able to get a 1 second interrupt from TMR0.

I do it in two different ways:

1) fit an external 32KHz crystal for the timer, this allows easy 1 second interrupts and low power consumption with the PIC in sleep.

2) generate a fast interrupt (I usually use 1mS), count the interrupts in the ISR, and set a flag when the required total (1000 in my case) is reached. I use this method if I'm not using sleep.
 
2) generate a fast interrupt (I usually use 1mS), count the interrupts in the ISR, and set a flag when the required total (1000 in my case) is reached. I use this method if I'm not using
I do this with (almost) all my projects.

Mike.
 
I am trying to turn on LED at button pressed

switch + goes to RC0 of MCU
switch - goes to ground

when I press button LED is not turning ON

C:
#define _XTAL_FREQ 8000000

#include <xc.h>

// PIC18F45K80 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)


void main(void)
{
  
    PORTC = 0b00000001;  //RC0 pin for button
    PORTB = 0b00000000;  //RB7 pin  for LED
     
    ANCON0 = 0x00;
    ANCON1 = 0x00;
   
    RB4 = 0;  // LED OFF

    while (1) {
        if (RC0 == 1)  // check switch if pressed 
            __delay_ms(40); // wait 
        {
           if (RC0 == 1)   // check switch if pressed 
           {
             RB4 = 1;   // LED ON
           }
        }
}

}
 
switch + goes to RC0 of MCU
switch - goes to ground

That would give a low signal for switch pressed, IF you also had a pullup resistor on it.
Without a pullup, it's most likely low and pressing the switch just holds it low?

Try connecting the switch between VDD and input, with a moderate value resistor from input to ground, eg. 1K or 10K; that will give a high input when the switch is pressed.
 
Try connecting the switch between VDD and input, with a moderate value resistor from input to ground, eg. 1K or 10K; that will give a high input when the switch is pressed.
I have done so when I press button and measure voltage on RC0 it show 5.2V DC but LED is not turning ON
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top