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

PIC10F220 CODE

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



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PIC10F220 CODE
PostPosted: Sun Jul 11, 2010 9:29 am     Reply with quote

Dear All;

My name is GUTI, I have started programing PICS, I find them interesting, but I have very little understanding of them.
I have some pdfs that are very helpful but I am a bit stuck at the moment.
I am able to blink the led, thats no problem. But now I am trying to do some debouncing code to count the number of time a Ir barrier is broken by the passing object.
I have the pic 10f220 connected in port gp0 the Ir emitter from an old ball mouse, and on port gp3 the Ir receiver. So thats the Ir barrier.
On port gp1 I have a mosfet gate connected. This mosfet will drive a dc motor.
The idea is when it counts 3 objects (3 times the Ir barrier is broken), the gp1 goes low for 1 second. And then starts again.
More or less this is my code at the moment, but I have no clue what so ever of how it goes. Crying or Very sad
I am using CCS pcb compiler with MPLAB 8.5
Any help much appreciated.
Best regards.
Code:

#include <10F220.h>
#define GP0 PIN_B0
#define GP1 PIN_B1
#define GP2 PIN_B2
#define GP3 PIN_B3

#use delay (clock=4000000)   //frecuencia

//for some reason fuses don't work, so i delete this line
#fuses MCLR,NOPROTECT,NOWDT,INTRC

void main()
 {
   while (TRUE) {
      output_high(GP0); //ir led on
     
      output_high(GP1);  //mosfet on

}

And I don't know how to finish. Now I should count up to 3 interrupts and
then make low GP1, for 1 second and repeat loop, but I don't know how.
Any help much appreciated.
Thanks.
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Sun Jul 11, 2010 12:11 pm     Reply with quote

Try this.
Code:
#fuses NoMCLR, NoWDT, IOSC4
#use delay (Clock=4MHZ,OSC)
//#use rs232(baud=19200, xmit=PIN_B2, INVERT)


Take a look in the data sheet (IOSC4)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 11, 2010 2:57 pm     Reply with quote

Look at this thread for a simple example of reading an input signal.
http://www.ccsinfo.com/forum/viewtopic.php?t=42946
It uses a comparator. If your input signals are TTL or CMOS compatible
then you probably don't need comparator. You can just use the input()
function and read a digitial input pin on the PIC.
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Mon Jul 12, 2010 12:56 am     Reply with quote

Thanks for the advice guys.

GP3 is the ir receiver, and I think it should be set up like an "always on".
Every time the ir barrier is broken by the passing object, GP3 goes low. the counter will count 3 passing objects and make mosfet low.(stop).

It is like the example for lighting a led with a pressed button. But I need 3 times and then make it low. So it is a bit the other way around.

However, the GP3 is an input, and don't know how to set it up.
I will post a code later when I have a bit more time, so we can discuss a bit more in depth.
Then again, thanks for the help.

Kind Regards.
GUTI.
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Mon Jul 12, 2010 2:09 pm     Reply with quote

Ok, this is what I am working on at the moment. I will post results tomorrow probably.
Code:

#include <10F220.h>  //seting pic config and ports
#define GP0 PIN_B0
#define GP1 PIN_B1
#define GP2 PIN_B2
#define GP3 PIN_B3

#fuses NoMCLR, NoWDT, IOSC4
#use delay (Clock=4MHZ,OSC)

void main()
 {
      unsigned char db_cnt;  //debounce counter

      output_high(GP0);  //ir led on

      db_cnt=0;     //reseting counter

   while (TRUE) {     //loop

      output_high(GP1);  //mosfet on

      for (db_cnt=0;db_cnt<=3;db_cnt++){   //count upto 3 interrupts
   
           output_low(GP1);   //turn mosfet off

           delay_ms(1000);  //pause 1 s

            }
     }
}


amended thanks i didnt realize.
Any more errors?... Sad


Last edited by gutisie on Tue Jul 13, 2010 12:22 am; edited 1 time in total
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 12, 2010 4:46 pm     Reply with quote

Code:
           db_cnt=0;     //reseting counter
With this inside the for-loop the loop will never exit.
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Tue Jul 13, 2010 12:10 pm     Reply with quote

now that i think of it....will that pause the mosfet every count or after 3?...
Question
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 13, 2010 12:45 pm     Reply with quote

You can answer questions like this by yourself, if you use the MPLAB
simulator. Run your program and display the output using the "UART1"
serial port output feature of MPLAB. This post explains how to set it up:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1
You will need to use a PIC that has a hardware UART, to make this work.
This will take at least the PCM compiler (using a 16F PIC).

You had a question about how many times does your loop occur.
How many passes does it make ? Run the program below.
Here's what you get:
Quote:

cccc

You can see that your guess was pretty far from the mark.

If you can learn to use the MPLAB simulator (with UART1 for output)
this will empower you to answer all these little questions. You won't
be dependent upon the forum.

Test program:
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//==========================================
void main()
{
int8 db_cnt;

for(db_cnt=0; db_cnt<=3; db_cnt++)
   {   
    putc('c');
   }

while(1);
}
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Tue Jul 13, 2010 5:15 pm     Reply with quote

thanks for the advice!

I like to comment with experts, so I can understand choices that I may have not seen.
You can show me how to fish, but in this case, it is experience the only one which tells you the best bait to use.

That is why I have asked. I didn't want to bother you with rookie questions, but this is what I am at the moment. So don't expect brilliant questions cause is not going to happen.

I will give a try with that simulation thing, but I have a laptop with no serial port, only USBs.

I have been told that Proteus is good for simulation.

The comment of the "cccc", means that the db_cnt counts 4 events?....
So I will have to use db_cnt<3 instead of db_cnt<=3...ok...i will try that.

Thanks all.
regards.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 13, 2010 5:32 pm     Reply with quote

Quote:
I will give a try with that simulation thing, but I have a laptop with no serial port, only USBs.

The hardware serial port of the Laptop is not used. MPLAB emulates
a serial port entirely in the MPSIM simulator. It can run on any PC,
including your laptop. The output of the simulated PIC serial port is
displayed inside MPLAB. Your Laptop USB/RS-232 doesn't even matter.
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Wed Jul 14, 2010 12:27 pm     Reply with quote

ok thanks.

I have followed your instructions in that link to set up mplab sim, and it works.
But after following the program running line by line, i have realize that i don't mention that the input is from gp3, Laughing , do i need to specify in the code that the counting comes from gp3?, i think so...so i will have to change the macro for other line codes and include the gp3 port.
So it will be setting a reading of the port gp3, and write the value in the counter, no?....
Any suggestions? ... Embarassed
Thanks for your help guys. Very Happy
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Thu Jul 15, 2010 1:22 pm     Reply with quote

ok this is the final code at the moment, it has no compiling issues, and the simulator runs ok.
I will mount the circuit and i will post the results
thanks

Code:
#include <10F220.h> //configuracion pic y puertos
#define GP0 PIN_B0
#define GP1 PIN_B1
#define GP2 PIN_B2
#define GP3 PIN_B3

#fuses NoMCLR, NoWDT, IOSC4
#use delay (Clock=4MHZ,OSC) // frecuencia relo intenno

void main()
{
unsigned char db_cnt; //contador de ventos

//Inicio
output_high(GP0); //ir led on

//Bucle
while (TRUE) {

   output_high(GP1);  //mosfet on

  //cuenta de eventos (GP3 low), reseteo por cuenta de eventos.
    for (db_cnt=0;db_cnt<=3;db_cnt++){
         if (input(GP3) == 1) //si GP3 salta, cuenta y resetea
         db_cnt = 0;  // hasta 3 pasa a la siguiente linea de codigo
         }     

    output_low(GP1); //mosfet off

    delay_ms(1000); //pausa 1 s

    }
}
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 2:04 pm     Reply with quote

Does anybody know the correct osccal value of pic10f220?
I cannot program it.
I have read on the data sheet they come code protected, and need to do a bulk erase, and pickit2 tells me the osccal value is not correct.
Anybody??.. Rolling Eyes
Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 2:57 pm     Reply with quote

Every chip is different.
The whole 'point' of the OSCCAL value, is that it is programmed at the factory to calibrate the oscillator.

If you lose it, you have two solutions:
1) Program a 'middle of the range' value, do a simple test program that generates a pulse train, measure the frequency of this, and then 'tweak' the OSCCAL value to get it right.
2) Some programmers have code to do this automatically.

Your programmer should have an option to automatically save the OSCCAL value when performing a bulk erase.

Best Wishes
gutisie



Joined: 11 Jul 2010
Posts: 9

View user's profile Send private message

PostPosted: Tue Sep 07, 2010 12:08 pm     Reply with quote

hi, at the end, it did not work, so i used another pic the 12f629, and even though the code compiles ok, it does not work.
I believe that the pic, does not count when the switch closes, hence can not count up to 3 events to carry on with the code.
On real pic simulator, it does not even light the led, and it is supposed to start on!!

Any suggestions?

Best regards.
Wink
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