CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

CCS C Compiler PIC16F877A - To run Simultaneously

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

CCS C Compiler PIC16F877A - To run Simultaneously
PostPosted: Tue Nov 09, 2021 4:30 am     Reply with quote

Greetings.I would like to ask if anyone give me any suggestion.I would be glad. Here's the question:
"While all led on PortB is being on in turn. Pin "B5" will be on and 3-9 counter will start run on PortD. However, the leds will continue run on its way with 7 segment display on PortB simultaneously.
(B0-B1-B2-B3-B4-B5-B6-B7-B0-B1-B2-B3-B4-B5-B6-B7-...)
( 3 - 4 - 5 - 6 - 7 - 8 - 9 3 - 4 - 5 -...)
I tried something but I couldn't find any available algorithm.
Code:

signed int i,j;

const int segment[7]={0x4F,0x66,0x6D,0x7C,0x07,0x7F,0x6F};
const int led[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

void main()
{
     set_tris_b(0x00);
    output_b(0x00);
    output_a(0x02);

   while(TRUE)
   {
     
        for(i=0;i<=7;i++)
          {
             
             output_b(led[i]);
             delay_ms(250);
             if(i==5)
             {
               for(i=0,j=6; (i<=7 && j==7) ;i++,j++)
               {
                 delay_ms(250);
                 output_d(segment[i]);
                 output_b(led[j]);
               }
               
               
               
             i=-1;
             
             }
          }
         
     
     
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Nov 09, 2021 7:55 am     Reply with quote

If you want the two things to happen simultaneously, then do them in the
same loop:
Code:

signed int i,j;

const int segment[7]={0x4F,0x66,0x6D,0x7C,0x07,0x7F,0x6F};
const int led[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

void main()
{
    // set_tris_b(0x00);
    Unless you are using fast_io, don't fiddle with TRIS.
    output_b(0x00);
    output_a(0x02);

    while(TRUE)
    {
     
        for(i=0;i<=7;i++)
        {   
             output_b(led[i]);
             output_d(segment[i]);
             delay_ms(250);
        }
    }
}   


If you want the two things to happen at different rates, then instead of the
basic loop, use a 'state machine'. Have a single delay, and a single counter,
but have it counting to a much larger number. Then (say) on each loop do
one of the operations, while on every fourth loop do the other, Then the
two things will be simultaneous, but one will be at 1/4 the speed of the
other. Make the count needed to do the second operation, suit whatever
rate you want relative to the first.
jeremiah



Joined: 20 Jul 2010
Posts: 1315

View user's profile Send private message

PostPosted: Tue Nov 09, 2021 12:25 pm     Reply with quote

Another recent addition to the compiler is that you can also use event driven programming (versus a state machine) so if you are a using a compiler from the last 1.5 years or so, you can use the "timeouts" library to have events occur at various rates:

https://www.ccsinfo.com/newsdesk_info.php?newsPath=ALL&newsdesk_id=214

There's a much more thorough example in the Examples subdirectory of your compiler's installation directory.
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Tue Nov 09, 2021 1:33 pm     Reply with quote

I am a new learner.
Can you send me lots of examples that I will be able to apply, various way to do.Easiest one to complex one...
I am unfamiliar to these terms right now.
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Nov 09, 2021 3:03 pm     Reply with quote

First please understand that NOTHING on a PIC is done simultaneously. It is just a sequence of instructions, and they all take time. Because of the speed of those instructions being executed we feel that different things are done at the same time. At least that is the way for a human. 10ms doesn't mean anything.

Please give us exact specifications of what you are trying to do (timings, are those two outputs happening at the same time, portd changing the sequence every time portb counts to eight,...). It will be way easier to give you examples of different possibilities of how to do it.
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Tue Nov 09, 2021 5:37 pm     Reply with quote

Code:
const int segment[7]={0x4F,0x66,0x6D,0x7C,0x07,0x7F,0x6F};
int led=1;
int i;
void main()
{
     
    output_b(0x00);
    output_a(0x02);

   while(TRUE)
   {
     
       for(i=0;i<=7;i++)
       {   
          circle:
           
          delay_ms(250);    //00000001-00000010-00000100-00001000-00010000-00100000=0x20
          output_b(led);    //   B0-      B1-       B2-      B3-     B4-       B5-
         
          if(led==0x20)     // B5
          {
             output_d(0x4F);//                                                  3
             delay_ms(250);
             
            for(i=1;i<=7;i++)
            {                        //  1.index    2.index
              output_d(segment[i]);  //    4         5         
             
              led=led<<1;
              output_b(led);         // 01000000   10000000   
              delay_ms(250);         //  B6          B7         
              if(led==0x80)
              {     
                   
                   
                    led=1;
                    output_d(0x7C);   //  6
                    output_b(led);    // B0
                    delay_ms(250);   
                    led=led<<1;                     
                    output_d(0x07);   //  7
                    output_b(led);    // B1
                    delay_ms(250);   
                    led=led<<1; 
                    output_d(0x7F);   //  8
                    output_b(led);    // B2
                    delay_ms(250); 
                    led=led<<1;                 
                    output_d(0x6F);   //  9                         
                    output_b(led);    // B3                       
                   
                    led=led<<1;
                   
                   
                   
                   goto circle;
                   
              }
             
            }
          }
          led=led<<1;
         
       }
       
       
         
     
     
   }
}


This is what wanted us to do by our teacher.We have only 40 minutes at laboratory. I have been struggling for couple hours just finished. So it was just
a question.There are being always three else question. I couldn't do it in given time. If there is a better way to done this, I would like to learn. I will be happy if anyone gives suggestion about anything in this circumstance. I give my time and want to do it right. Thanks all, respects.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Nov 10, 2021 6:23 am     Reply with quote

PrinceNai, if you look at the original code, he talks about displays, and
the whole thing uses 250mSec delays. So we 'know' that in fact this is
a human watched 'thing', so a few uSec (or even mSec), means nothing.
So 'human simultaneous', is what is needed, not 'real simultaneous'. Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group