nerdegutta.no
PIC12F1822 - Blink a LED __delay_ms - version
02.01.25
Embedded
This is the basic of a program to blink a LED connected to PORTbits.RA4. The LED blinks at a rate of 500ms. The program use the delay function, which isn't very good, because the MCU halts/stops for the time the delay is going on. It's much better to use interrupt.
// INCLUDE LIBRARIES #include < xc.h > // CONFIGURATION BITS // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT enabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = ON // PLL Enable (4x PLL enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config DEBUG = OFF // In-Circuit Debugger Mode (In-Circuit Debugger disabled, ICSPCLK and ICSPDAT are general purpose I/O pins) #pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled) #define _XTAL_FREQ 4000000 // Reference frequency for the compiler #define led PORTAbits.RA4 // Assign the LED to RA4 void main(void) { TRISA = 0; // Set TRISA to Output PORTA = 0; // Set PORTA to LOW OSCCON = 0b11101010; // PLL enabled, 4MHz, Internal osc while (1) { led = 1; __delay_ms(500); led = 0; __delay_ms(500); } return; }