PIC Traffic Lights Controller with PIC16F84A

Microcontroller Topics
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

PIC Traffic Lights Controller with PIC16F84A

Post by Rksk » Tue May 04, 2010 5:53 pm

There is an example PIC Traffic Lights Controller with PIC16F84A.
I have posted C and ASM source codes also.
circuit.jpg
circuit.jpg (25.51 KiB) Viewed 24435 times
gif.gif
gif.gif (21.14 KiB) Viewed 24359 times

ASM source code;

Code: Select all

;******************************************************************************
;***                                                           			***
;***   Published by RkskEkanayaka @ ROBOT.LK 2010    ***
;***                                                           			***
;******************************************************************************



           LIST    p=16F84 ; PIC16F844 is the target processor

           #include "P16F84.INC" ; Include header file

           CBLOCK 0x10   ; Temporary storage
              state
              l1,l2
           ENDC

           org     0               ; Start up vector.
           goto    setports        ; Go to start up code.

	   org     4               ; Interrupt vector.
halt       goto    halt            ; Sit in endless loop and do nothing.

setports   clrw                    ; Zero in to W.
           movwf   PORTA           ; Ensure PORTA is zero before we enable it.
           movwf   PORTB           ; Ensure PORTB is zero before we enable it.
           bsf     STATUS,RP0      ; Select Bank 1
           clrw                    ; Mask for all bits as outputs.
           movwf   TRISB           ; Set TRISB register.
           bcf     STATUS,RP0      ; Reselect Bank 0.

initialise clrw                    ; Initial state.
           movwf   state           ; Set it.

loop       call    getmask         ; Convert state to bitmask.
           movwf   PORTB           ; Write it to port.
           incf    state,W         ; Increment state in to W.
           andlw   0x03            ; Wrap it around.
           movwf   state           ; Put it back in to memory.
           call    wait            ; Wait :-)
           goto    loop            ; And loop :-)

           ; Function to return bitmask for output port for current state.
           ; The top nibble contains the bits for one set of lights and the
           ; lower nibble the bits for the other set. Bit 1 is red, 2 is amber
           ; and bit three is green. Bit four is not used.
getmask    movf    state,W         ; Get state in to W.
           addwf   PCL,F           ; Add offset in W to PCL to calc. goto.
           retlw   0x41            ; state==0 is Green and Red.
           retlw   0x23            ; state==1 is Amber and Red/Amber
           retlw   0x14            ; state==3 is Red   and Green
           retlw   0x32            ; state==4 is Red/Amber and Amber.

           ; Function using two loops to achieve a delay.
wait       movlw   5
           movwf   l1

w1         call    wait2
           decfsz  l1
           goto    w1

           return


wait2      clrf    l2
w2         decfsz  l2
           goto    w2
           return
           END

		




There the C source code;

Main.c

Code: Select all

		//;******************************************************************************
//;***                                                           			***
//;***   Published by RkskEkanayaka @ ROBOT.LK 2010    ***
//;***                                                           			***
//;******************************************************************************



#include <pic.h>
#include "delay.h"

#define sleep 5 //set the time between changes

int start()
{

    PORTB = 0 ;           // state==0 is Green and Red.
    RB2 = 1;
    RB4 = 1;
    
    DelayBigMs(sleep);//sleep a time
    
    PORTB = 0 ;            // state==1 is Amber and Red/Amber
    RB1 = 1;
    RB4 = 1;
    RB5 = 1;
    
    DelayBigMs(sleep);//sleep a time
    
    PORTB = 0 ;            // state==3 is Red   and Green
    RB0 = 1;
    RB6 = 1;
    
    DelayBigMs(sleep);//sleep a time
    
    PORTB = 100000 ;            // state==4 is Red/Amber and Amber.
    RB0 = 1;
    RB1 = 1;
    RB5 = 1;
    
    DelayBigMs(sleep);//sleep a time
    
 start(); //goto start again

 }
 

   
   
main()
{
   TRISB = 0; //set all RB 8pins as output
    PORTB = 0;  //set all RB 8pins low at startup
  start(); //goto start function

}
 
 
 

 
		

There is include files for main.c
c.zip
(4.45 KiB) Downloaded 2009 times


I think this example will give a guid for beginners.

Please add your comments.
Image
Last edited by Rksk on Sat May 08, 2010 11:20 pm, edited 2 times in total.
User avatar
Magneto
Major
Major
Posts: 430
Joined: Wed Jul 15, 2009 1:52 pm
Location: London

Re: PIC Traffic Lights Controller with PIC16F84A

Post by Magneto » Tue May 04, 2010 6:34 pm

Dear Friend ,

Nice article....

Neo told me , that you are expecting to sit for the Advance Level exam soon , in maths scheme. It seems you are doing lots of electronic stuff in those days. But my advice is concentrate most time for your studies. As we all know ,
this time you can not get never again in your life.

Actually Advance level is a big hurdle in Sri Lanka. So winning this, you have to do lots of work. Once you passed well and enter to a Engineering Faculty like Moratuwa or Peradeniya , you have lots of time to do these electronic stuff and also you have lots of people around you to take advices. But if you unable to enter to a University , it is some bit diffiucult to increse your knowlege in electronics , as there are very few places and also very less qualified lectures in electronic field except Universtiy.

This is my personal experiece. I also very much like to make elecronic circuits from my childhood. I started my elecronic life in year six , by making a circuit which on and off two LED bulbs. :)

But when I did the Advance Level exam , I stop my elecronic experiments for some time , as I knew it is very tough exam.
Because very few students can enter to a Engineering Faculty. I Passed well from A/L and then I entered to the University of Moratuwa, Faculty of Enginnering. There I did lots of experiments in electronics , because I had more freedom there and also I could learn lots of electronic theories from University from very qualited lectures. And also there were lots of friends around me , for help me , When I stuck in some points.

So think about this .... If you want to be a good enginner , First 100% concentrate on A/L exam and do your best to enter to a Enginnering Faculty. If your parents are not rich , to send you aborad and to have a foreign universtiy degree ,
this is the only way you have ..... and also I had .....

Good Luck !!!!
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: PIC Traffic Lights Controller with PIC16F84A

Post by Rksk » Tue May 04, 2010 7:27 pm

Dear magneto,

friest thank you very much for your words.

It did a large change in my mind.
I was doing my A/L studies, but I didn't them with a good concentrate.

You have given me a great advice and it can do more things in my life.

since now, I'm doing my studies with a well concentrate. also now I have a goal.

Thank you very much.
Image
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: PIC Traffic Lights Controller with PIC16F84A

Post by Neo » Thu May 06, 2010 2:07 am

Well said Magneto and it matches the same thing I told RKSK over the chat few days back.
Education through local universities is invaluable in terms of knowledge & freedom and that's what we all enjoy today trough our knowledge, jobs, status, etc.... Local university is like a lottery. If you win, you get a degree worth more than a million which will help you to build up your entire life. So we always need to encourage people to do A/Ls their level best and pass to a local university. As RKSK is very much interested with electronics, Moratuwa & Peradeniya are the best places for him.

If you learn and practice the subjects taught in classes without keeping things on the pending list and finally do some past papers for about 10 years (under your syllabus) without any problem, you have about 90% chance to enter. The rest of the 10% is luck to have the question of your interest on the paper. If you dedicate perfectly and focus your life 100% on the exam, I'm almost sure you will definitely pass with great results.
If your parents are not rich , to send you aborad and to have a foreign universtiy degree ,
this is the only way you have ..... and also I had .....
This matches with what Magneto said so thought of adding to boost some energy in you :mrgreen:
If you’re born poor, it’s not your mistake. But if you die poor, it’s your mistake.
Bill Gates
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: PIC Traffic Lights Controller with PIC16F84A

Post by Rksk » Thu May 06, 2010 3:24 pm

Dear neo,

both of you and magneto are boosting my energy to face A/L.

You have given worth advices to all students who will sit to A/L.

Thank you very much.


If you’re born poor, it’s not your mistake. But if you die poor, it’s your mistake.
Image
User avatar
hanukaran
Posts: 2
Joined: Thu Apr 07, 2011 7:09 pm

Re: PIC Traffic Lights Controller with PIC16F84A

Post by hanukaran » Thu Apr 07, 2011 7:13 pm

Hi sir....im using mikro c pro...can you tell what is the clock for this traffic light
User avatar
Magneto
Major
Major
Posts: 430
Joined: Wed Jul 15, 2009 1:52 pm
Location: London

Re: PIC Traffic Lights Controller with PIC16F84A

Post by Magneto » Thu Apr 07, 2011 11:30 pm

Dear Friend ,

Do not much worry about the clock speed. first you decide the time , which you want to on and off each LED.
then write your own delay function for , model timing.

Code: Select all

void delay(void)
{
  volatile long i ,k;

  for(k=0; k <1000 ;k++){
       for(i=0; i < 65535 ; i++) {};
  } 

}
like this you can roughly model the required timing . You have to select the limit values of variable i and k , according to the oscillator value of the micro controller. You can do this by some trial and error method.
User avatar
hanukaran
Posts: 2
Joined: Thu Apr 07, 2011 7:09 pm

Re: PIC Traffic Lights Controller with PIC16F84A

Post by hanukaran » Fri Apr 08, 2011 7:11 pm

hi,,,,if you dnt mind can you tell me what compiler you use..im using MPLAB..can i use MPLAB to compile it
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: PIC Traffic Lights Controller with PIC16F84A

Post by Neo » Sun Apr 10, 2011 12:38 am

Magneto recommends Hi-Tech C and I found that it is very good
User avatar
Magneto
Major
Major
Posts: 430
Joined: Wed Jul 15, 2009 1:52 pm
Location: London

Re: PIC Traffic Lights Controller with PIC16F84A

Post by Magneto » Mon Apr 11, 2011 2:10 pm

Dear friend ,

You can compile codes with MPLAB. But then you have to write the code in Assembly language. Because MPLAB is a IDE and it is not coming with any C compilers. But it has MP LAB Assembler attached to MPLAB IDE , which can compile your assemble coding into machine language.

for programming PIC 16F series , you can use Hitech C compiler. It is a ANCII C compiler. So you can easily port any ANCII C code into your project.

For programming dsPIC Series, it is best to use C30 compiler. Because you can not compiler codes , which was written for dsPIC series using Hitech C

Regards,
Chaminda
Post Reply

Return to “Microcontrollers”