Thread: timer 1 for 16f627

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    timer 1 for 16f627

    Hi guys i am very new to c code language and i am just now writing my first code with the velleman p8048 programming kit and i am trying to program the 16f627. what i want to do is have a timer be started by the input (rao) but i want the timer to leave (portb) on for 20-30 seconds. is this even possible. if it is possible i want it to reset after 30 seconds. please can anyone help i am very new to programming and i need much help. i can get the (portb) light to turn on i just want it to turn on for 30 seconds thanks and here is my

    Code:
    #include <conio.h>
    #define SW1  0x00;
    
    __CONFIG (0x3F61);
    
    void main (void)
    {
    CMCON = 0x07;
    TRISA = 0xFF;
    TRISB = 0;
    PORTB = 0x01;
    
    
              while ( 1 )
             {
              if(RA0 == 1)
                       PORTB = 0b000001;
              else 
                       PORTB = 0b000000;        
             }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What compiler are you using? There should be pre-defined functions, in the compiler documentation, for accessing/using the internal timers of the device.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    thank you for your response I am using the mplab ide compiler with HiTech

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't know if you can access the timer modules directly in HI-TECH C, but you can achieve the same results with the "__delay_ms(x)" function. To use this, you need to define a preprocessor symbol to represent the frequency of your oscillator.

    The specifics can be found in the manual (here). If you haven't already, save this on your drive for future reference. And read through it, too!

    If you have further questions, provide your updated code and ask away!

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    thank you guys so much i should be able to figure this out from here this is very exciting

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    okay so i defined my frequency but i cannot get it to compile

    Code:
    #include <htc.h> 
    #include <stdio.h> 
    #define SW1  0x00;
    #define _XTAL_FREQ 4000000;
    __CONFIG (0x3F61);
    void main (void)
    {
    CMCON = 0x07;
    TRISA = 0xFF;
    TRISB = 0;
    PORTB = 0x01;
     
     
     while ( 1 )
     {
      if(RA0 == 1)
            PORTB = 0b000001;
            __delay_ms(9000);
      else 
      PORTB = 0b000000;     
     }
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why can't you get it to compile?

    If you have compiler errors then post the complete error messages exactly as they appear in your development environment.

    Jim

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    #define SW1  0x00;
    #define _XTAL_FREQ 4000000;
    Get rid of those semi-colons. And do as jimblumberg said.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    this is the error message I am getting

    Clean: Deleting intermediary and output files.
    Clean: Deleted file "C:\Users\Jeremy\Desktop\test\test2,c.cce".
    Clean: Done.
    Executing: "C:\Program Files (x86)\HI-TECH Software\PICC-Lite\9.50\bin\picl.exe" -C -E"test2,c.cce" "test2,c.c" -O"test2,c.obj" -Zg9 -O -ASMLIST -Q -MPLAB -16F627
    Halting build on first failure as requested.
    BUILD FAILED: Wed Mar 06 12:34:54 2013

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you been able to successfully compile anything before this? I'm fairly sure this has more to do with the configuration of your environment than the code. Search the internet, you'll find a lot of information on this topic.

    Also see if you can open the *.cce file in a text editor (located in the project directory) - it might provide more specific information.

  11. #11
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    yes i can compile if i delete the delay_ function and it will compile and run on my velleman board

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm assuming you removed the semi-colons as I mentioned in post #8.

    I don't know the upper limit, but the delay function will give an error if the delay period is too large. Try it again with a smaller value (like 200) and see if it compiles.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Two issues; one C code issue.

    Code:
      if(RA0 == 1)
            PORTB = 0b000001;
            __delay_ms(9000);
      else
      PORTB = 0b000000;
    I added the missing braces below.
    Code:
      if(RA0 == 1) {
            PORTB = 0b000001;
            __delay_ms(9000);
      } else
      PORTB = 0b000000;
    Second __delay_ms has a limit of a value between 100 to 200.
    Note: This limit was removed in recent Hi_tech C compiler versions.

    Third, You need to use a define or use the Pro version to get __delay_ms to work.

    I forget the define/prototype that is needed.

    Edit: Found this in past Student's work. My MPLAB/HiTech C installation is bad so can not test if the is all that is needed.
    Code:
    #define _XTAL_FREQ 4000000
    void _delay_ms(unsigned long n);  //define a delay of n milliseconds
    //Note that this is built into HiTech C but is limited to 197 msec for an unknown reason

    Tim S.
    Last edited by stahta01; 03-06-2013 at 01:11 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c# timer from
    By jackofcyber in forum C# Programming
    Replies: 5
    Last Post: 04-16-2010, 07:08 AM
  2. 60 hz timer
    By crowe in forum Tech Board
    Replies: 18
    Last Post: 01-20-2010, 11:18 AM
  3. timer
    By fizz_uk83 in forum C Programming
    Replies: 1
    Last Post: 09-15-2003, 08:54 AM
  4. timer
    By abrege in forum C++ Programming
    Replies: 9
    Last Post: 11-21-2002, 07:29 AM
  5. Timer help
    By Blizzarddog in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2002, 04:19 PM

Tags for this Thread