Hardware execution problem

Status
Not open for further replies.

kgavionics

Full Member level 3
Joined
Jun 12, 2012
Messages
167
Helped
7
Reputation
14
Reaction score
11
Trophy points
1,298
Location
Alberta.Canada
Visit site
Activity points
2,482
Hi guys
i'm still learning C and i have a little issue when i run my program in hardware.
i have this little code to count from 0 to 255 and send the count to portb.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f452.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF

void main(void)
  {
    unsigned char z;
    TRISB = 0; 			
    for(z=0;z<=255;z++)
     
      PORTB = z;
 Delay10KTCYx(255);
    while(1);			
  }

when i use either Proteus or Pic simulator,every thing works fine and i can see the leds flushing.The problem comes when i burn the hex file to my pic18f452.The Led are always ON.
It seems the the delay function is not working on hardware.Can someone help me please?
 
Last edited:

Greetings,
Real World is very different from simulation/Virtual World.. Things working on simulator usually don't work in real time that is because of many factors, as conditions in simulation world are supposed to be ideal.
In your case playing with delay will work for you. Increase delay.
Actually your led's are working fine according to your program, but you can't see them off as they are flickering at high frequency and due to persistence of vision. (https://en.wikipedia.org/wiki/Persistence_of_vision)

Good Luck
 

Thx for reply tushki7
the problem that i come from assembly and i can tell you that 99 % time my simulation works in hardware when programming in assembly.With C is a different story.Since i'm using C in have problems to execute in hardware mode.
i've used this program that toggle 0xFF and 0 on PORTB and it's working pretty fine.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f452.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
void MSDelay(unsigned int);
void main(void)
  { 
    TRISB = 0;
 		
    while(1)			
      { 
     
        PORTB = 0xFF;
        MSDelay(250);
        PORTB = 0x00;
      
        MSDelay(250);
      }
  }
void MSDelay(unsigned int itime)
  {
    unsigned int i; unsigned char j;
    for(i=0;i<itime;i++)
      for(j=0;j<165;j++);
  }
i suspect the problem might come from the loop
Code:
for(z=0;z<=255;z++)
     
      PORTB = z;
 Delay10KTCYx(255);
can someone figure out what going wrong?
 

Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f452.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
[COLOR=#008000]#void MSDelay(unsigned int itime);[/COLOR]                   //[COLOR=#FF0000]void MSDelay(unsigned int);[/COLOR]   
void main(void)
  { 
    TRISB = 0;
         
    while(1)            
      { 
     
        PORTB = 0xFF;
        MSDelay(250);
        PORTB = 0x00;
      
        MSDelay(250);
      }
  }
void MSDelay(unsigned int itime)
  {
    unsigned int i; unsigned char j;
    for(i=0;i<itime;i++)
      for(j=0;j<165;j++);
  }

Code:
for(z=0;z<=255;z++)
     [COLOR=#ff0000]{[/COLOR]
      PORTB = z;
 Delay10KTCYx(255);
[COLOR=#ff0000]}[/COLOR]

And from your first post I can see there is no delay function program(where is Delay10KTCYx()???).

Does it help?
 

And from your first post I can see there is no delay function program(where is Delay10KTCYx()???).

Does it help

Unfortunately , it doesn't help.After declaring the function, the led are still on.No blanking at all.
 

I could not see at above code any configuration for TRIS register and others.



+++
Even i changed my program to this;
Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f452.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
void MSDelay(unsigned int);
void main(void)
  { 
unsigned char z;	
    TRISB = 0;
   	
    while(1)			
      { 
     
   
   			
    for(z=0;z<=255;z++)
   MSDelay(250);
      PORTB = z;
      }
  }
void MSDelay(unsigned int itime)
  {
    unsigned int i; unsigned char j;
    for(i=0;i<itime;i++)
      for(j=0;j<165;j++);
  }
It doesn't work and the leds are steady and this problem is getting me crazy:?::?::?::?::?::?:
 

Have you increased the delay, I told you in my first comment?
You need to increase delay .. use another for loop to increase delay.
or try this :
Code:
MSDelay(unsigned int itime)
{
unsigned int i,j,k;
  for(i=0;i<itime;i++)
   {
    for(j=0;j<255;j++)
   	{
          for(k=0;k<255;k++)
            {}
        }
   } 


}
 
Last edited:

Insertion of configuration commands to SFR registers must be done at begin of code.
Other point, is that you forgot to configure other registers, usually associated to port settings.



+++
 

Insertion of configuration commands to SFR registers must be done at begin of code.
Other point, is that you forgot to configure other registers, usually associated to port settings.



+++
Thx guys for help, but i tried everey kind of modification,i still stuck with this problem even though i declare TRISB=0 in the begining of the main function.
i tried also to increase delay time, and the Led are still ON.And if i power off the cicruit, and i power on after, the Leds remains OFF.
i think i will go back to assembly,it's time consuming, but it always works.

- - - Updated - - -

Hi guys
i modified my program to this :

Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f452.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
void MSDelay(unsigned int);
void main(void)
  { 

 TRISB = 0;

     
   
  for(; ;)
{ 
PORTB++;
Delay10KTCYx(80);
     
      }
}

It's working like charm.Now i'm pretty sure that the problem is coming from this
Code:
 for(z=0;z<=255;z++)
   MSDelay(250);
      PORTB = z;
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…