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 CCS Technical Support

counter programming

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



Joined: 01 Jun 2017
Posts: 15

View user's profile Send private message

counter programming
PostPosted: Mon Jul 03, 2017 11:00 pm     Reply with quote

hello everyone,
I want to do counter coding. I'm using pic16f526 with 8Mhz internal oscillator. My compiler is CCS v5.071d. I'm first time doing counter programming i don't have any idea how to do this?
Plz help me to do this code without interrupt because pic16f526 don't have interrupt.

Thanks in advance.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Jul 04, 2017 2:45 am     Reply with quote

Please be more specific.

What are you wanting to count?
What frequency range?
What else has to be done?

Also helps if you show us a short sample program of what you've tried.
Your sample needs to be complete and compilable so we can copy and paste to test it.
We are not going to do the programming for you, but will gladly help,

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jul 05, 2017 5:29 am     Reply with quote

1st step is to cut/compile a '1Hz LED' program to show us that your PIC hardware works.

Along with Mike's queries, I'll add...

How will you display the counter data ? Local LCD module or send to PC?
If LCD module, you'll need a driver so I suggest the flex_LCD driver in the code library here. It does require 6 I/O pins though.
If sending to PC, you'll need an 'interface' module. I use a TTL<>USB module, about $2 these days. You're only sending to the PC, 1 I/O pin is needed.

We really need more information to help you better.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Wed Jul 05, 2017 1:06 pm     Reply with quote

I think the key point being made to you, is 'counter programming' means nothing....

Repeatedly add one to a number, and it is a 'counter'.
Repeatedly subtract one from a number and it is a 'counter'.
Increment or decrement a value when something happens, and this is a 'counter'.
There are hardware counters (on the PIC, the CCP), and innumerable software counters. More sophisticated PIC's have other types of hardware counters as well. Most of these can be programmed to do a huge variety of tasks.

If you are starting programming, you need to start with basic code and prove you can do basic tasks. If you want to know how to do a specific task with a 'counter', you need to tell us what you want to do, and what chip is involved.

Your question at the moment is along the lines of "I want to know how to use a ball". Not exactly easy to answer..... Sad
saksun



Joined: 01 Jun 2017
Posts: 15

View user's profile Send private message

PostPosted: Sat Jul 08, 2017 6:13 am     Reply with quote

Im scanning 4 pairs of ir led's and using 2 pic controllers for transmitter and receiver separately. I want to sound a buzzer when my ir beam cut for 15 sec continuosly. other wise not.

and my code is
Code:
 

void loop()
{
    int k=14;  // k as counter
     if(!input(PIN_B5)) //ir receiver output pin
     {
           while(!input(PIN_B5))
           {
                  k--;
                  delay_ms(1000); // 1 sec delay
               
                   if(k==0)
                    {                                           
                      buzzer();
                    }
           }


     }
}


is this code is correct?? correct me if im wrong...


thank you
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jul 08, 2017 7:02 am     Reply with quote

One problem is that when the PIC is executing the delay_ms(1000) function it is possible that INPUT_B5 could go high.
You need to test that pic WHILE also timing the 15 second delay.

I suggest looking in the CCS C manual, FAQ section, for a 'timed receive' code snippet. IT should provide you with a method of both reading the pin and timing at the same time.

Jay
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Sat Jul 08, 2017 8:51 am     Reply with quote

Step back, clear your mind, and consider this: "if I had people to handle this problem, how many people would I need, and what would I get each of them to do?" Also consider for the moment that these people are incapable of doing more than one thing at the same time. Let's call them simpletons for sake of argument. It's not derogatory, it's descriptive.

You're sending IR light pulses from a transmitter to a receiver, and you want the receiver to sound some sort of alarm if the pulses disappear for at least 15 seconds.

So, think about the crux of the problem and how you'd divide the task to allow several simpletons to do the job. Even though the people available are simpletons, they'll still do what you tell/ask them, but they can only do one thing.

I'd say that you could get away with "hiring" two simpletons. One has the job of watching a stopwatch - and nothing else - and if the stopwatch second hand gets to 15, he's going to raise a fuss (sound the alarm). The other's job is to "watch the window" for the light pulses, and if he sees a light pulse, he has to reach over to the "stopwatch simpleton" and hit the reset button on his stopwatch.

Does this shed proper light on the problem, or do you need more guidance? Major hint: start reading up on interrupts and timers.
saksun



Joined: 01 Jun 2017
Posts: 15

View user's profile Send private message

PostPosted: Mon Jul 10, 2017 12:54 am     Reply with quote

Quote:
start reading up on interrupts and timers.


In my pic i don't have interrupt. so want to do without interrupt.

is it possible without interrupt??
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Mon Jul 10, 2017 6:27 am     Reply with quote

saksun wrote:
Quote:
start reading up on interrupts and timers.


In my pic i don't have interrupt. so want to do without interrupt.

is it possible without interrupt??


Absolutely. Read chapter 7 of your PIC's data sheet. Your code will need to read the TMR0 register to see how much time has elapsed. TMR0 can be configured with a prescaler so that it counts up every N master clock cycles too. Keep track of how many counts have elapsed, and if the counter "rolls over" from 255 back to 0, then you can increment a "rollover counter". Figure out how many of these would approximately give you a 15 second timeout. If you see an IR light pulse, set this rollover counter = 0.

I'm assuming a 4MHz crystal (internal clock of 1MHz), and a prescaler value of 1/256. That means timer 0 will count up every 256us, and will roll over every 256us x 256 = 65,536us = 65.536ms. 15 seconds/0.065536 seconds = ~229. If your rollover counter hits 229, a hair over 15s have elapsed.
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