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.

Djsarkar

Member
I have PIC18F45K80. I am using MPLABX 5.40 and XC8 2.30. I have written program to blink LED and its working fine

Code:
#define _XTAL_FREQ 20000000
#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)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

void main(void) {
   TRISB = 0; //RB as Output Port
 
  while(1)
  {
    RB7 = 1;  // LED ON
    __delay_ms(1000); //  Delay
    RB7 = 0;  // LED OFF
    __delay_ms(1000); //  Delay
  }
    
    
    return ;
}

how much delay this line __delay_ms(1000); gives ?
 
If the MCU clock osc configuration is set properly, ii should be one second.

The compiler timing calculation is based on what you tell the compiler the CPU clock is, with this line:
#define _XTAL_FREQ

With devices that have PLL multipliers or internal clock, that's not necessarily the actual crystal frequency, if there even is one.
eg. A device may have a 10MHz crystal but be running at 80MHz clock via its internal PLL.

You need to look through the actual osc and PLL setup you use in the #pragmas and cross reference with the device data sheet, to set up the real operating frequency.
 
If the MCU clock osc configuration is set properly, ii should be one second.

The compiler timing calculation is based on what you tell the compiler the CPU clock is, with this line:
#define _XTAL_FREQ

With devices that have PLL multipliers or internal clock, that's not necessarily the actual crystal frequency, if there even is one.
eg. A device may have a 10MHz crystal but be running at 80MHz clock via its internal PLL.

You need to look through the actual osc and PLL setup you use in the #pragmas and cross reference with the device data sheet, to set up the real operating frequency.
#define _XTAL_FREQ 20000000 // 20 Mhz

I have attcahed two pages that gives information about oscillator datasheet

Does device running with external 20 Mhz or internal 80MHz?
 

Attachments

  • osc 1.png
    osc 1.png
    53.1 KB · Views: 206
  • osc 2.png
    osc 2.png
    42.7 KB · Views: 217
Just run the program and see how fast the LED flashes, as you're calling a 1000mS delay it should be 1 second ON, and 1 second OFF.

If your clock settings are wrong it will be wildly different - and blindingly obvious.

It's a simple way to check your clock settings are correct :D

On these modern VERY complicated PIC's it's a good idea to use the MCC to set up the clock (then cut and paste the code from it), but still check it with the flashing as well. If your clock is running at the wrong speed it makes life very difficult!.
 
On these modern VERY complicated PIC's it's a good idea to use the MCC to set up the clock (then cut and paste the code from it), but still check it with the flashing as well. If your clock is running at the wrong speed it makes life very difficult!.

I am not sure but I think simulator show the correct delay time ?
sim1.png
 
When I ran code in first post, LED is flashing but I don't know the exact time and frequency ? That's why I want to know delay time

Are you building it?, or using the simulator?.

As I said earlier, it will be blindingly obvious with hardware that it's right or not. But as Ian pointed out that 20MHz isn't even an option using the internal oscillator, then it's not going to correct.
 
If you are using the MPLABX ide then the delay is not real time... You can say Mississipi a million times..

Simulators DO NOT MODEL the xtal... Protues does a "sort of " precalculation of Tcycle and gives reasonable results.. MPLAB just keeps clocking so if a tcycle is 1ms then 50000 will be 50 seconds.. best to run it on your dev board.. pickit 3 is simple to use as the debugger.. in your project properties select pickit3 instead of mplab sim...
 
.
in your project properties select pickit3 instead of mplab sim...
I am trying to debug code using PIC kit 3

Code:
/*
* File:   blink.c
* Author: Embedded System
*
*/
#define _XTAL_FREQ 20000000
#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)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.



void main(void) {
   TRISB = 0; //RB as Output Port

  while(1)
  {
    RB7 = 1;  // LED ON
    __delay_ms(1000); //  Delay
    RB7 = 0;  // LED OFF
    __delay_ms(1000); //  Delay
  }
  
  
    return ;
}

When I set break point at two lines

RB7 = 1; // LED ON
__delay_ms(1000); // Delay

RB7 should be high and LED should be turn ON but it doesn't happen ?
 

Attachments

  • select PICk3.jpg
    select PICk3.jpg
    43.7 KB · Views: 202
  • Port value.jpg
    Port value.jpg
    177.3 KB · Views: 199
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.
Edit2, Phew, just found the correct datasheet and all's well.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top