Continue to Site

# Why do LEDs blink every 32 seconds in PIC16f1827 with internal oscillator at 500kHz?

Status
Not open for further replies.

#### simonwai999

Hi all,

I am using 16f1827 with the default internal osc at 500khz.

In a simple led blinking programme, the leds blink every 32 seconds.

I am confused.

500k/4? F=1/ T T = 1/ F If I want to find out why it takes the leds to blink every 32 seconds
with this frequency, what should i do?

Could anyone show me how to calculate the 32 seconds, please?

Thanks a lot.

Re: 16f1827 internal osc

Hi,

500k oscillator / 4 = 1 machine cycle = 1 instruction (typically, a few take 2 cycles)

You need to post your code using the # tags so well can see whats gone wrong.

simonwai999

### simonwai999

Points: 2
Re: 16f1827 internal osc

As was explained, your PIC is processing instructions at the rate of 4/500k seconds per instruction cycle.

If your program looks like this:
Code:
Turn LED on
Turn LED off
Turn LED on
TURN LED off

You can see that the LED will blink very fast, much faster than 32 times per second. And if your program looks like this:
Code:
Turn LED on
Delay 1 second
Turn LED off
Delay 1 second
Turn LED on

You can see that the LED will blink very slowly, once every second. So it depends on how much time is spent between turning the LED on and turning it off, which is calculated by understanding how many instructions are executed and how much time each instruction takes to execute.

simonwai999

### simonwai999

Points: 2
Re: 16f1827 internal osc

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Project name:
(c) Mikroelektronika, 2009.
* Revision History:
20080930:
- initial release;
- 20090720 - modified by Slavisa Zlatanovic;
* Description:
This is a simple 'Hello World' project. It turns on/off LEDs connected to
PORTA, PORTB, PORTC and PORTD.
* Test configuration:
MCU:             PIC16F887
Dev.Board:       EasyPIC6 - ac:LEDs
[url=https://www.mikroe.com/eng/products/view/297/easypic6-development-system/]MikroElektronika - EasyPIC6 Development System - PIC Development Board[/url]
Oscillator:      HS, 08.0000 MHz
Ext. Modules:    -
SW:              mikroC PRO for PIC
[url=https://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/]mikroElektronika - mikroC PRO for PIC - C compiler for Microchip PIC microcontroller development[/url]
* NOTES:
- Turn ON the PORT LEDs at SW9.
*/

void main() {

ANSEL  = 0;            // Configure AN pins as digital
ANSELH = 0;
C1ON_bit = 0;          // Disable comparators
C2ON_bit = 0;

TRISA = 0x00;          // set direction to be output
TRISB = 0x00;          //  set direction to be output
TRISC = 0x00;          // set direction to be output
TRISD = 0x00;          // set direction to be output

do {
PORTA = 0x00;        // Turn OFF LEDs on PORTA
PORTB = 0x00;        // Turn OFF LEDs on PORTB
PORTC = 0x00;        // Turn OFF LEDs on PORTC
PORTD = 0x00;        // Turn OFF LEDs on PORTD
Delay_ms(1000);      // 1 second delay

PORTA = 0xFF;        // Turn ON LEDs on PORTA
PORTB = 0xFF;        // Turn ON LEDs on PORTB
PORTC = 0xFF;        // Turn ON LEDs on PORTC
PORTD = 0xFF;        // Turn ON LEDs on PORTD
Delay_ms(1000);      // 1 second delay
} while(1);            // Endless loop
}

EXTERNAL 8MHZ WITH DELAY_MS(1000) <====

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
http://embedded-lab.com/blog/?p=148#comment-4083

/*
Lab 1: Flashing LED with PIC16F688
Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
Oct 7, 2010
*/
// Define LED @ RC0
sbit LED at RC0_bit;
void main() {
ANSEL = 0b00000000; //All I/O pins are configured as digital
CMCON0 = 0×07 ; // Disbale comparators
TRISC = 0b00000000; // PORTC All Outputs
TRISA = 0b00001000; // PORTA All Outputs, Except RA3

do {
LED = 1;
Delay_ms(1000);
LED = 0;
Delay_ms(1000);
} while(1);  // Infinite Loop
}

We used the in-built library function Delay_ms() to create the 1 sec delay for flashing On and Off. Delay_ms() returns a time delay in milliseconds.

Output

You will see the LED flashing On and Off with 1 sec duration.

INTERNAL 4MHZ WITH THE SAME DELAY_MS(1000)<===
My question is how come we can use the same delay_ms(1000) with internal 4Mhz
and external 8Mhz to acheive the same blinking speed?

Is it because of this in-built library function Delay_ms() ?

Re: 16f1827 internal osc

The Delay_ms() function is likely built at compile time, having loop calculations that are dependent on the oscillator speed selected. I don't see an oscillator setting in your program so perhaps you have a project file setting for it?

simonwai999

### simonwai999

Points: 2
Re: 16f1827 internal osc

hi upand,

Thanks. I set the frequency in my project