Page 1 of 1

controlled running light

PostPosted: Mon Sep 10, 2012 11:34 am
by zulaikha
hye all
i am new here,my name is zulaikha n i am a student
now i just start study pic in my uni..n i am trying to do blinking led.
i googling for the sourcecode n o found this
CODE: SELECT_ALL_CODE
-----------------------------------------------------------------------------------------------------------------------------
// PIC16F877A LED Blinking      
//===============================================================

//device definition header file
#include <htc.h>

//device configuration bit
__CONFIG(0x3F3A);

//define
#define LED1   RB7
#define LED2   RB6

//function prototype
void blink_LED1(unsigned char n);
void blink_LED2(unsigned char n);

//main function
//================================================================
void main(void)
{
   //device peripheral configuration
   TRISB7=0;      //set RB7 as output
   TRISB6=0;      //set RB6 as output
   
   //infinity loop
   while(1)
   {
      //blink LED1 for 3 times
      blink_LED1(3);
      
      //blink LED2 for 3 times
      blink_LED2(3);
   }
}

//function to blink LED1 for n times
void blink_LED1(unsigned char n)
{
   //loop for n times
   for(n+=1;n>0;n-=1)
   {
      //set LED1 to 1
      LED1=1;
      
      //short delay
      for(unsigned int i=0;i<20000;i+=1);    //max value is 65535
         
      //set LED1 to 1
      LED1=0;
      
      //short delay
      for(unsigned int i=0;i<20000;i+=1);    //max value is 65535         
   }   
}

//function to blink LED2 for n times
void blink_LED2(unsigned char n)
{
   //loop for n times
   for(n+=1;n>0;n-=1)
   {
      //set LED2 to 1
      LED2=1;
      
      //short delay
      for(unsigned int i=0;i<20000;i+=1);    //max value is 65535
         
      //set LED2 to 1
      LED2=0;
      
      //short delay
      for(unsigned int i=0;i<20000;i+=1);    //max value is 65535         
   }      
}      
---------------------------------------------------------------------------------------------------------------------

what i understand from the prgram is blink 2 led for 3 times.this is sound really basic for u but for me it is new world! :lol:
now i want to modified the program..iam going to used variable capsitor so that i can controlled the speed of led blinking.
i want to make running light that we can controlled the speed of their "running"..for the first time,i want to use 2 led.
can anyone help me plzz..if u can show which part must me added..it really help me..

Re: controlled running light

PostPosted: Mon Sep 10, 2012 2:31 pm
by shahrul
Like this you understand?
CODE: SELECT_ALL_CODE
for(;;){
value=read_a2d(0); //read analog input
LED1=1;
LED2=0;
for(i=0;i<value;i++) __delay_ms(1);
LED1=0;
LED2=1;
for(i=0;i<value;i++) __delay_ms(1);
}

Re: controlled running light

PostPosted: Mon Sep 10, 2012 2:52 pm
by zulaikha
shahrul WROTE:Like this you understand?
CODE: SELECT_ALL_CODE
for(;;){
value=read_a2d(0); //read analog input
LED1=1;
LED2=0;
for(i=0;i<value;i++) __delay_ms(1);
LED1=0;
LED2=1;
for(i=0;i<value;i++) __delay_ms(1);
}


sorry mr shahrul i dunt get it..

value=read_a2d(0); //read analog input----------->this is for what ?

sorry i really new to this..im using variable resistor to control the led speed...what do i must put in the source code huh?let say im using 2 led for my learning...

if u can attach full of the source code its really hepl me

Re: controlled running light

PostPosted: Mon Sep 10, 2012 4:10 pm
by shahrul
Variable resistor will give analog value. Then PIC can read analog value. read_a2d(0) is my function to read analog.
This is my full ADC function.
CODE: SELECT_ALL_CODE
   int read_a2d(unsigned char channel)
   {
   #if defined (_16F877A)
      ADCON0=0b00000001;
      ADCON1=0b10000000;
      ADCON0=(ADCON0&0xC7)|(channel<<3);
   #elif defined (_16F886) || (_16F887)
      ADCON0=0b00000001;
      ADCON1=0b10000000;
      if(channel<=7) ANSEL=ANSEL|(0b00000001<<channel);
      else ANSELH=ANSELH|(0b00000001<<(channel-8));
      ADCON0=(ADCON0&0xC3)|(channel<<2);
   #elif defined (_18F2550) || (_18F4550)
      ADCON0=0b00000001;
      ADCON1=0b00000111;                  
      ADCON2=0b10000110;
      ADCON0=(ADCON0&0xC3)|(channel<<2);
   #endif
   __delay_ms(2);
   #if defined (_16F877A) || (_16F886) || (_16F887)
      GO=1;
      while(GO==1) continue;
   #elif defined (_18F2550) || (_18F4550)
      GODONE=1;
      while(GODONE==1) continue;
   #endif
   return(256*ADRESH+ADRESL);
   }

Example ADC here, Lesson ADC.

Re: controlled running light

PostPosted: Mon Sep 10, 2012 4:22 pm
by zulaikha
tq mr shahrul
i will study more on that n try it on board

Re: controlled running light

PostPosted: Tue Sep 11, 2012 10:48 am
by yonghui
i think your project does not need variable capacitor , instead variable resistor. PIC can read the analog value from the potentiometer to get voltage from the voltage divider. blink speed can be change with delays which depends on the voltage from the divider.

Re: controlled running light

PostPosted: Tue Sep 11, 2012 1:47 pm
by hyng
For beginner, i hope the tutorial below will be helpful.
http://tutorial.cytron.com.my/2011/08/05/sk40c-tutorial/

Re: controlled running light

PostPosted: Wed Sep 12, 2012 1:47 pm
by zulaikha
tq very super mod hyng.
hope u guys can guide me tru this learning.
my email is mrsdealova7@yahoo.com