XC8 Code for an dual alternating flasher (not the person in a trench coat) – AKA Model Train Level Crossing Lights.

The LED is hooked up to PortA.0 and PortA.1  

 
#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)

//Prototypes
void setup(void);
void blink_led(void);



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

void setup(void)
{
    ANSELAbits.ANSA0 = 0; //Disable Analog A.-
    ANSELAbits.ANSA1 = 0; //Disable Analog A.1
    
    TRISAbits.TRISA0 = 0; //Set PortA.0 as output
    TRISAbits.TRISA1 = 0; //Set PortA.1 as output
}

void blink_led(void)
{
    
    LATAbits.LATA0 = 1;  //Set PortA.0 High (turn on LED)     
    LATAbits.LATA1 = 0;  //Set PortA.1 Low  (turn off LED)    
    //Wait 1 second
    __delay_ms(1000);
        
   
    LATAbits.LATA0 = 0;  //Set PortA.0 Low  (turn off LED)
    LATAbits.LATA1 = 1;  //Set PortA.1 High (turn on LED)     
   
    //Wait 1 second
    __delay_ms(1000); 
}

Something You Might Be Intrested In


 

10F32X Rapid Prototype Board

Make The PIC10F322 and other PIC10F Processors Breadboard Friendly for Quick and Easy Prototyping.

{Click to learn more}

PIC10F322 - XC8 Code - Dual Alternating Flasher - Model Train Level Crossing Lights