stardust-remixer/src/blink_led.c

75 lines
1.8 KiB
C

// led blink example Copyright (C) 2014 Diego Herranz
#define NO_BIT_DEFINES
#include <pic14regs.h>
#include <stdint.h>
// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN),
// disable watchdog,
// and DO NOT disable low voltage programming.
// The rest of fuses are left as default.
__code uint16_t __at (_CONFIG1) __configword = _FOSC_INTOSC & _CLKOUTEN_OFF & _WDTE_OFF & _BOREN_ON & _CP_OFF;
#define LED_PORT PORTCbits.RC5
#define LED_TRIS TRISCbits.TRISC5
// board layout:
// mosfets clockwise from power feed:
// RA5, RA4, RC2, RC3
// 'input' pin: RC5
// Uncalibrated delay, just waits a number of for-loop iterations
void delay(uint16_t iterations)
{
uint16_t i, j;
for (i = 0; i < iterations; i++) {
// Prevent this loop from being optimized away.
//for (j = 0; j < 12; j++) {
__asm nop __endasm;
//}
}
}
#define PPSO_PWM1 3
void main(void)
{
OSCCON = 0xf0;
TRISC = 0;
//SLRCONC = 0;
OPTION_REGbits.PSA = 1;
LED_PORT = 0;
PWM1CLKCON = 0x00; // From Fosc w/o prescaler
PWM1PRH = PWM1PRL = 0xFF;
PWM1PH = 0;
PWM1DCH = PWM1DCL = 0;
PWM1OF = 0;
PWM1INTE = 0x00; // no interrupts enabled
PWM1INTF = 0x00; // clear interrupt flag
PWM1LDCON = 0x80; // load armed
PWM1OFCON = 0x00; // independent run mode
PWM1CON = 0x80; // enable, standard PWM mode, active high output
RC5PPS = PPSO_PWM1;
//PWM1OUT = 1;
while(1) {
delay(1);
while (PWM1DCL < 0xFF || PWM1DCH < 0xFE) {
if (PWM1DCL == 0xFF) PWM1DCH++;
PWM1DCL++;
PWMLD = 1;
delay(1);
}
while (PWM1DCL > 0 || PWM1DCH > 0) {
if (PWM1DCL == 0) PWM1DCH--;
PWM1DCL--;
PWMLD = 1;
delay(1);
}
}
}