Pic12f675 code (XC8 compiler) for boost converter
Pic12f675 code (XC8 compiler) for boost converter
/*
* File: main.c
* Author: root
*
* Created on February 8, 2023, 4:56 PM
*/
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#include <xc.h>
#define _XTAL_FREQ 20000000
unsigned char PWM = 60;
void __interrupt() isr(void)
{
if (INTCONbits.T0IF == 1) {
if (GP0 == 1) {
TMR0 = PWM + 128; // 0-127 -> TMR0 128 - 255
GP0 = 0; // 100 - duty cycle
}
else {
TMR0 = 255 - PWM; // 0-127 -> TMR0 255 - 128
GP0 = 1;
}
INTCONbits.T0IF = 0; // Clear Timer1 overflow
}
}
void main(void) {
TRISIO = 0b00001110; // all output except GP2, GP3, and GP4 (ADC)
ANSEL = 0b01001110; // 7=0, 6-4 =conversion clock 101 (4uS), 3-0 analog pins 4,2,1,0
ADCON0 = 0b00001001; // // 7=0 right justified, 6=vdd, 5-4 empty, 3-2 ANS2, GO/DONE = 0, ADON=1
GPIO = 0x00;
CMCON = 0x07; //comparators CM2:CM0 off
VRCON = 0x00; // Shut off the Voltage Reference
OPTION_REG = 0b11001000; // pre scaler assigned to WDT timer module
// nGPPU = 1; // internal pull ups disabled
// INTEDG = 0; // falling edge trigger the interrupt__don't care
// T0CS = 0; // timer transition on cycle clock
// T0SE = 0 // increment on low to high transistion of GP2
// PSA = 0 // prescaler is set to the WDT or TIMER0 module
// ps2:ps0 > 000 = 1:2 101=1:64
INTCON = 0b10100000; // or 0b10100000;
//GIE = 1 //enable global interrupts
//PEIE = 1 //enable peripheral interrupts
//T0IE = 1 //enable TMR0 interrupts
//INTE = 0 //disables GP2 interrupt
//GPIE = 0 //disables GPIO port change interrupt
//T0IF = 0 //timer0 overflow flag
//INTF = 0 //GP2 interrupt flag
//GPIF = 0 //ext pin change interrupt
T1CON = 0x00; //clear the unused timer
PIE1 = 0x00; //nothing here is needed
// http://eng-serve.com/pic/pic_timer.html
while(1) {
__delay_ms(5);
GO_nDONE = 1;
while (GO_nDONE) {
// wait ADC to complete
}
PWM = (ADRESH >> 1); // 0-127
if (PWM < 5) PWM = 5;
// else if (PWM > 73) PWM = 73;
else if (PWM > 120) PWM = 120;
} // while
return;
}