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

blinking LED using timer1?

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

blinking LED using timer1?
PostPosted: Sun Feb 03, 2013 6:49 am     Reply with quote

I want to blink 2 led's with different timings. Using internal Oscillator 4Mhz
LED1 ON 2sec and OFF 5 sec
LED2 ON 3sec and OFF 6sec
I have written the code. But the timings is not perfect.

Using timer1, i set up an interrupt to occur for every 1 ms to go into the interrupt routine.
Code:

unsigned int16 LED1_ON=0, LED1_OFF=0, LED2_ON=0, LED2_OFF=0;
int1 LED1_ON_flag = 0, LED1_OFF_flag = 0, LED2_ON_flag = 0, LED2_OFF_flag = 0;

// In timer1 routine, incrementing these variables,
LED1_ON++;
LED1_OFF++;
LED2_ON++;
LED2_OFF++;

if(LED1_ON == 2000)       // if equals 2 secs         
{
LED1_ON = 0;
LED1_ON_flag = 1;
}

if(LED1_OFF == 5000)   // if equals 5 secs         
{
LED1_OFF = 0;
LED1_OFF_flag = 1;
}

if(LED2_ON == 3000)      // if equals 3 secs      
{
LED2_ON = 0;
LED2_ON_flag = 1;
}

if(LED2_OFF == 6000)   / if equals 6 secs           
{
LED2_OFF = 0;
LED2_OFF_flag = 1;
}


// in the main program
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

output_low(LED1);
output_low(LED2);
while(1)
{
if(LED1_ON_flag == 1)
{
LED1_ON_flag = 0;
//LED1_ON = 0;
output_high(LED1);
}

if (LED1_OFF_flag == 1)
{
LED1_OfF_flag = 0;
//LED1_ON = 0;
output_low(LED1);
}

if(LED2_ON_flag == 1)
{
LED2_ON_flag = 0;
//LED2_ON = 0;
output_high(LED2);
}

if(LED2_OFF_flag == 1)
{
LED2_OFF_flag = 0;
//LED2_OFF = 0;
output_low(LED2);
}



I simulated in proteus, but the LED blinks for different timings. Wat could be the problem? please help
Mike Walne



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

View user's profile Send private message

PostPosted: Sun Feb 03, 2013 9:23 am     Reply with quote

Forget Proteus, search this forum to find out what others think.

If you must simulate use MPLAB.

Is your code complete and compilable?

Show us PIC, compiler version etc as advised in forum guidelines.

Tell us what your real hardware is doing.

Mike
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun Feb 03, 2013 9:47 am     Reply with quote

Looking at your code I guess that you should learn 'C' codding. We can help you, but you must have the minimum skills to do the
"Blinking LED test" for yourself.
To achieve you need: a basic knowledge of C language structure, how to define a function prepositions and call a functions,
know the C programm layout, etc.
Basic knowledge of the MCU hardware in your project (you do not mention ) and the minimum skills to handle the electronic
hardware involved.

I strongly suggest to read some book regarding "C + PIC MCU for Beginners":
http://www.ccsinfo.com/content.php?page=books
Read and study the CCS examples codes that is included with your CCS Compiler in the Examples Folder.

Take a trip for this site:
http://www.ccsinfo.com/faq.php

Last suggestion: DO NOT mention the word 'Proteus' in this forum (unless you are talking of Greek Mythology).
Read this and you will understand what I mean:
http://www.ccsinfo.com/forum/viewtopic.php?t=47549


Regards

Humberto


Last edited by Humberto on Mon Feb 04, 2013 1:08 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Sun Feb 03, 2013 11:57 am     Reply with quote

Also, programs to do exactly this, have been posted here inside the last month. Use search.

Best Wishes
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