Simple LEB blinking problem

Status
Not open for further replies.

MrNobody

New Member
The code below is a simple one to turn the LED on for 1 second and then off for 1 second. External crystal of 10MHz is used and the PLL is turned on which brings the FOSC up to 40MHz. However when I program and run the code, The LED appears to stay ON for only 1/4 of a second.
Am I missing something..? Thanks.

Code:
#include <p18F4620.h>
#include <delays.h>
#include <stdio.h>

#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config LVP = OFF
#pragma config OSC = HSPLL
#define CLOCK_FREQ		(40000000ul)      // Hz 

void main()
{
	TRISB = 0x00;
	while(1)
	{
		PORTBbits.RB0 = 1;
		Delay10KTCYx(1000);
		PORTBbits.RB0 = 0;
		Delay10KTCYx(1000);
	}
}
 
Hi,
I don't use C18. Is it because Delay10KTCYx works up to 255 only? I mean Delay10KTCYx(255).
Use LAT registers for outputting as good practice, but it's okay here.
 
Sorry for double post..
Refer to the post below..
 

Attachments

  • LED Blink.zip
    538 bytes · Views: 153
Last edited:
Thanks..
In the code below, I have commented the PLL and change the FOSC to 10MHZ. Again, I use 10MHz crystal for the PIC. I have also changed the delay from 1000 to 250 so that it is within 8 bit. Again, the code is to turn the LED ON for 1 second and then OFF for 1 second. However, the problem still remain..
BTW, I am using C18 compiler.
I have also attached the hex file and the asm code.


Code:
#include <p18F4620.h>
#include <delays.h>
#include <stdio.h>

#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config LVP = OFF
//#pragma config OSC = HSPLL
#define CLOCK_FREQ		(10000000ul)      // Hz 

void main()
{
	TRISB = 0x00;
	while(1)
	{
		PORTBbits.RB0 = 1;
		Delay10KTCYx(250);
		PORTBbits.RB0 = 0;
		Delay10KTCYx(250);
	}
}

Code in asm.
Code:
1:                 #include <p18F4620.h>
2:                 #include <delays.h>
3:                 #include <stdio.h>
4:                 
5:                 #pragma config WDT = OFF
6:                 #pragma config MCLRE = ON
7:                 #pragma config LVP = OFF
8:                 //#pragma config OSC = HSPLL
9:                 #define CLOCK_FREQ		(10000000ul)      // Hz 
10:                
11:                void main()
12:                {
13:                	TRISB = 0x00;
  00F8    6A93     CLRF 0xf93, ACCESS
14:                	while(1)
  0112    D7F3     BRA 0xfa
15:                	{
16:                		PORTBbits.RB0 = 1;
  00FA    8081     BSF 0xf81, 0, ACCESS
17:                		Delay10KTCYx(250);
  00FC    0EFA     MOVLW 0xfa
  00FE    6EE6     MOVWF 0xfe6, ACCESS
  0100    EC65     CALL 0xca, 0
  0102    F000     NOP
  0104    52E5     MOVF 0xfe5, F, ACCESS
18:                		PORTBbits.RB0 = 0;
  0106    9081     BCF 0xf81, 0, ACCESS
19:                		Delay10KTCYx(250);
  0108    0EFA     MOVLW 0xfa
  010A    6EE6     MOVWF 0xfe6, ACCESS
  010C    EC65     CALL 0xca, 0
  010E    F000     NOP
  0110    52E5     MOVF 0xfe5, F, ACCESS
20:                	}
21:                }	
  0114    0012     RETURN 0
 

Attachments

  • LED Blink.zip
    538 bytes · Views: 131
I had a problem on the PIC12F629 that the LEDs wouldnt flash. I solved it by putting :
Code:
 GPIO = 0;               // Turn off all pins

so in your casae try putting this line in after your trisb line:
Code:
PORTB = 0;               // Turn off all pins
 
i have a problem

hi every one i have aproblem of this code , but i dont know what it is i need some one to help me please

void main()
{
PORTB=0;
TRISB=0;//as output
TRISD=1;//as input
while(1)
{
if (PORTD.f1=1)
PORTB.f7=1 ;
else
PORTB.f7=0;



}
}
 
MedoLee said:
hi every one i have aproblem of this code , but i dont know what it is i need some one to help me please

void main()
{
PORTB=0;
TRISB=0;//as output
TRISD=1;//as input
while(1)
{
if (PORTD.f1=1)
PORTB.f7=1 ;
else
PORTB.f7=0;



}
}

I am assuming you are using mikroC compiler.
Try this
Code:
void main()
{
PORTB=0;
TRISB=0;//as output
TRISD=[B]2[/B];//as input
while(1)
{
 if (PORTD.f1=1)
        PORTB.f7=1 ;
 else
        PORTB.f7=0;
}
}

You wanted to use RD1 as input but you are setting RD0 as input instead. So I changed it for you.
 
try to use this:
Code:
void main()
{
PORTB=0;
TRISB=0;//as output
TRISD=1;//as input
PORTB=0; // turn off all pins
PORTD=0: // turn off all pins
while(1)
{
if (PORTD.f1=1){
PORTB.f7=1 ;
}
else
{
PORTB.f7=0;
}


}
}
 
Somehow (and i dont know why yet) you have turn off all ports or set all registers with all 0.

MedoLee:
If you are using MikroC you have to set the config parameterse correctly ( that can be hard if its you first time)
 
And using
Code:
 TRISD=2 // just plain stupid
Wont work as the registers are binary alternatively you can use the hex value of the pins you want to use :
if you want to use pins 2 and 4 as inputs and the rest as inputs it would look like this binary 00101000 and hex it would be 0x28
Code:
TRISD= 00101000 // or in hex
TRISD=0x28
 
Hey you're insulting. Besides,
Code:
TRISD=2
is working. decimal 2 = 0b10 = 0x2

If you want to use pin 2 and 4 as inputs,
Code:
TRISD = 0b00010100;
TRISD = 0x14;
TRISD = 20;
are the same.
 
Each instruction takes 4 clock cycles and so if you have a 10MHz crystal and the PLL at *4 to get 40MHz, you will only get 10M instructions per second. Without the PLL you get 2.5M instructions.

Edit, just realised you have taken this into account. I think bananasoing is on the right track. OSC=HS; is still required.

Mike.
 
Last edited:
Pommie said:
Each instruction takes 4 clock cycles and so if you have a 10MHz crystal and the PLL at *4 to get 40MHz, you will only get 10M instructions per second. Without the PLL you get 2.5M instructions.

Mike.
Yeah...
With each instructions being 1/2.5M, or 0.4us, for the LED to remain ON for 1 second, i need to use a delay of 2.5M instructions right..?
Thats why i use Delay10KTCYx(250) cause 10K x 250 = 2.5M.
However, my LED only remains ON for only 1/4 or 1/2 of that time..
 
1/4 or 1/2? The delay time is not constant?
 
The delay is constant.. Its just that I don't have an oscilloscope so I can't tell the pulse width whether it is 1/4 or 1/2..

Anyway.. it is very strange as I have just run the code but this time, it works.. The LED remain on for 1 second. I did not add anything or change anything since the last time i posted.

However, as per the coding, the coding is correct right..? I mean, the delay etc..
 
Correction to the previous post.
I did make some changes.. I added the #pragma config OSC = HS and it works..
Thanks bananasiong..
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…