Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 29th April 2008, 05:58 AM   (permalink)
Default Simple LEB blinking problem

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);
	}
}
MrNobody is offline   Reply With Quote
Old 29th April 2008, 06:24 AM   (permalink)
Default

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.
__________________
Superman returns..
bananasiong is online now   Reply With Quote
Old 29th April 2008, 06:30 AM   (permalink)
Default

What compiler are you using?
__________________
Spency.

PIC Micro's - Your mind is the limit

PIC's and interfacing with other devices - a PIC Basic Guide @ digital-diy.net
gramo is offline   Reply With Quote
Old 29th April 2008, 06:40 AM   (permalink)
Default

Yes I got it! From MPLAB C18 Libraries page 143.
Code:
void Delay10KTCYx( unsigned char unit );
char is 8-bit
__________________
Superman returns..
bananasiong is online now   Reply With Quote
Old 29th April 2008, 07:14 AM   (permalink)
Default

Sorry for double post..
Refer to the post below..
Attached Files
File Type: zip LED Blink.zip (538 Bytes, 1 views)

Last edited by MrNobody; 29th April 2008 at 07:17 AM.
MrNobody is offline   Reply With Quote
Old 29th April 2008, 07:16 AM   (permalink)
Default

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
Attached Files
File Type: zip LED Blink.zip (538 Bytes, 0 views)
MrNobody is offline   Reply With Quote
Old 29th April 2008, 07:24 AM   (permalink)
Default

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
__________________
Simplicity rules

Good enought - its perfect ??

2 Gig of free online backup space

Are you a Chemist?
Kryten is offline   Reply With Quote
Old 29th April 2008, 07:35 AM   (permalink)
Default 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 is offline   Reply With Quote
Old 29th April 2008, 07:44 AM   (permalink)
Default

Quote:
Originally Posted by MedoLee
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=2;//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.
MrNobody is offline   Reply With Quote
Old 29th April 2008, 07:45 AM   (permalink)
Default

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;
}


}
}
__________________
Simplicity rules

Good enought - its perfect ??

2 Gig of free online backup space

Are you a Chemist?
Kryten is offline   Reply With Quote
Old 29th April 2008, 07:47 AM   (permalink)
Default

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)
__________________
Simplicity rules

Good enought - its perfect ??

2 Gig of free online backup space

Are you a Chemist?
Kryten is offline   Reply With Quote
Old 29th April 2008, 07:57 AM   (permalink)
Default

Hi MrNobody,
Even the PLL is not enabled, HS oscillator should still be selected, shouldn't it?
__________________
Superman returns..
bananasiong is online now   Reply With Quote
Old 29th April 2008, 07:59 AM   (permalink)
Default

No you have to set everything in the project setup in MikroC compiler
__________________
Simplicity rules

Good enought - its perfect ??

2 Gig of free online backup space

Are you a Chemist?
Kryten is offline   Reply With Quote
Old 29th April 2008, 08:01 AM   (permalink)
Default

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
__________________
Simplicity rules

Good enought - its perfect ??

2 Gig of free online backup space

Are you a Chemist?
Kryten is offline   Reply With Quote
Old 29th April 2008, 08:10 AM   (permalink)
Default

Quote:
Originally Posted by Kryten
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.
__________________
Superman returns..
bananasiong is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
PIC pin to MOSFET gate problem Futterama Electronic Projects Design/Ideas/Reviews 7 8th November 2007 05:30 PM
Simple switching/pulse project dbtoutfit Electronic Projects Design/Ideas/Reviews 2 1st November 2007 01:46 PM
Simple Robot From Scratch Parts? GreenAce92 Robotics Chat 7 27th October 2007 11:42 PM
Simple? op-amp problem... jrz126 Electronic Projects Design/Ideas/Reviews 2 20th September 2007 09:52 PM
PIC16F628 ... VERY SIMPLE PROBLEM micro571 Micro Controllers 14 30th December 2004 09:51 AM



All times are GMT. The time now is 09:40 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.