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.

programming

Status
Not open for further replies.
include<pic.h>

unsigned short mask(unsigned short num) {
switch (num)
{
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
void main()
{
int c;
int d;
TRISD=0; //
TRISB=0X00;
TRISC=0X00;
PORTC=0X02;
PORTC=0X00;
c=0;
while(c<100)
{
d++;
while(d=5000)
{
c++;
d=0;
PORTD=c;
PORTB=0X01;
_delay(5000);
PORTB=0X00;
}

}


}



i am a beginner in c programming..i am getting the continuous display,bt that runs very fast.how to correct this program?how i can give proper delay for that?
 
Last edited:
Code:
void main()
{
    int c;
    int d;   
    TRISD=0;                           // 
    TRISB=0X00;
    TRISC=0X00;
    PORTC=0X02;
    PORTC=0X00;
   c=0;
   while(c<100)
       {
       _delay(100);      //<----new line here slows it down maybe too much
       d++;
       while(d=5000)
          {
          c++;
          d=0;
          PORTD=c; 
          PORTB=0X01;
         _delay(5000);   
         PORTB=0X00;
         }
 
     }
 
}
That's an unusual control flow. I would call _delay() whenever you increment d.
 
Code:
void main()
{
    int c;
    int d;   
    TRISD=0;                           // 
    TRISB=0X00;
    TRISC=0X00;
    PORTC=0X02;
    PORTC=0X00;
   c=0;
   while(c<100)
       {
       _delay(100);      //<----new line here slows it down maybe too much
       d++;
       while(d=5000)
          {
          c++;
          d=0;
          PORTD=c; 
          PORTB=0X01;
         _delay(5000);   
         PORTB=0X00;
         }
 
     }
 
}
That's an unusual control flow. I would call _delay() whenever you increment d.



program is working..but how can we control the runing speed?
 
simple:
Code:
void main()
{
    int c;
    int d; 
    int delaytime=50;   //NEW VARIABLE
    TRISD=0;                           // 
    TRISB=0X00;
    TRISC=0X00;
    PORTC=0X02;
    PORTC=0X00;
   c=0;
   while(c<100)
       {
       _delay(delaytime);      //<----new line here slows it down maybe too much
       d++;
       while(d=5000)
          {
          c++;
          d=0;
          PORTD=c; 
          PORTB=0X01;
         _delay(5000);   
         PORTB=0X00;
         }
 
     }
 
}

To control speed you can either manual set delaytime variable or have 2 buttons for the user. When button 1 is pressed it adds 1 to the delaytime variable and when button 2 is pressed it minus 1 from delaytime variable...

Be sure to check the buttons inside the loop... not before the delay to get a better feel for the buttons.... Also dont forget to delay after a button press so it wont alter the variable to fast .
 
simple:
Code:
void main()
{
    int c;
    int d; 
    int delaytime=50;   //NEW VARIABLE
    TRISD=0;                           // 
    TRISB=0X00;
    TRISC=0X00;
    PORTC=0X02;
    PORTC=0X00;
   c=0;
   while(c<100)
       {
       _delay(delaytime);      //<----new line here slows it down maybe too much
       d++;
       while(d=5000)
          {
          c++;
          d=0;
          PORTD=c; 
          PORTB=0X01;
         _delay(5000);   
         PORTB=0X00;
         }
 
     }
 
}

To control speed you can either manual set delaytime variable or have 2 buttons for the user. When button 1 is pressed it adds 1 to the delaytime variable and when button 2 is pressed it minus 1 from delaytime variable...

Be sure to check the buttons inside the loop... not before the delay to get a better feel for the buttons.... Also dont forget to delay after a button press so it wont alter the variable to fast .





i am a beginner i dont no much about c.is this program is correct?
 
Last edited:
It is correct.

While we're here, I would change the line:
Code:
       while(d=5000)
to read:
Code:
       if(d=5000)
 
Both while(d=5000) and if(d=5000) need to be while(d==5000) and if(d==5000).

Mike.
 
bt replacing '=' with '=='

Error [800] D:\C PROGRAMS\inc display\display.c; 787. undefined symbol "__delay"

this error is coming.y?
If your original code was correct, it's "_delay", with one underscore, not "__delay" with two.

Details, details. Computers are unforgiving.
 
Ouch. I totally missed that. That's my punishment for using assembly for the past few projects.

It's amazing how quickly we forget these things. I find that if you switch it around so it becomes while(5000==d) it will give an error if you forget the second equals.

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top