Select Page

Here is a simple traffic light driven by a PIC10F322. After all what model train layout would not be complete without one?

The default times are 4 seconds green, 1 second yellow, 4 seconds red. rinse and repeat.

Change the timing by updating:

define GREEN_TIME 4000

define YELLOW_TIME 1000

define RED_TIME 4000

The timer is using the built in ms macro function – the value it takes is in milliseconds. 1000 is 1 second. You can do the math from there.

The Circuit

traffic light

The Code

/*
 * File:  traffic_light.c
 * Author: Jamie Starling - GizoFoundry.com 
 *
 * Created on:  September 2, 2021, 7:45 PM
 * 
 * Code provided as-is.
 */

#include <xc.h>
#include <stdint.h>

//Device Configuration
#pragma config FOSC = INTOSC  // Oscillator Selection 
#pragma config BOREN = ON    // Brown-out Reset
#pragma config WDTE = OFF    // Watchdog Timer
#pragma config PWRTE = ON    // Power-up Timer
#pragma config MCLRE = OFF   // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF      // Code Protection 
#pragma config LVP = OFF     // Low-Voltage Programming 
#pragma config LPBOR = ON    // Brown-out Reset Selection bits
#pragma config BORV = LO    // Brown-out Reset Voltage Selection
#pragma config WRT = OFF    // Flash Memory Self-Write Protection

//Used to calculate the delay time - Change depending on processor Speed
#define _XTAL_FREQ 8000000  //8 MHz (default after Reset)

//delay time value
#define GREEN_TIME 4000   
#define YELLOW_TIME 1000 
#define RED_TIME 4000 


//Prototypes
void setup(void);
void traffic_led(void);


void main(void)
{
    setup();
    
    while(1)
    {
      traffic_led(); 
    }
}

void setup(void)
{
    ANSELAbits.ANSA0 = 0; //Disable Analog A0
    ANSELAbits.ANSA1 = 0; //Disable Analog A1
    ANSELAbits.ANSA2 = 0; //Disable Analog A2
    
    TRISAbits.TRISA0 = 0; //Set Port.A0 as output
    TRISAbits.TRISA1 = 0; //Set Port.A1 as output
    TRISAbits.TRISA2 = 0; //Set Port.A2 as output
}

void traffic_led(void)
{
    
    //Green Light
    LATAbits.LATA0 = 1;  //Set Port.A0 High (turn on LED)  GREEN     
    LATAbits.LATA1 = 0;  //Set Port.A1 Low  (turn off LED) YELOW 
    LATAbits.LATA2 = 0;  //Set Port.A2 Low  (turn off LED) RED
    __delay_ms(GREEN_TIME);  //delay
        
    //Yellow Light
    LATAbits.LATA0 = 0;  //Set Port.A0 Low  (turn off LED) GREEN
    LATAbits.LATA1 = 1;  //Set Port.A1 High (turn on LED)  YELLOW   
    LATAbits.LATA2 = 0;  //Set Port.A2 Low  (turn off LED) RED
    __delay_ms(YELLOW_TIME); //delay
    
    //Red Light
    LATAbits.LATA0 = 0;  //Set Port.A0 Low  (turn off LED) GREEN
    LATAbits.LATA1 = 0;  //Set Port.A1 Low  (turn off LED)  YELLOW   
    LATAbits.LATA2 = 1;  //Set Port.A2 High  (turn on LED) RED
    __delay_ms(RED_TIME); //delay
}

Have a Project or Idea!?

I am Available for Freelance Projects

My skills are always primed and ready for new opportunities to be put to work, and I am ever on the lookout to connect with individuals who share a similar mindset.

If you’re intrigued and wish to collaborate, connect, or simply indulge in a stimulating conversation, don’t hesitate! Drop me an email and let’s begin our journey. I eagerly anticipate our interaction!

jamie@jamiestarling.com


Pin It on Pinterest

Share This