T
treez
Guest
Hi,
The following XC8 C Code just reads the ADC input…then if above a certain value, it then outputs a PWM out on a pin.
Do you think this code is OK… it builds fine.
The ADC had loads of registers and it didnt look like you had to bother setting all of them?
It would be good if microchip had like an "LTspice simulator " for their microcontrollers with C code in them. I couldnt find it on their website....do you know if there is one?
The following XC8 C Code just reads the ADC input…then if above a certain value, it then outputs a PWM out on a pin.
Do you think this code is OK… it builds fine.
The ADC had loads of registers and it didnt look like you had to bother setting all of them?
It would be good if microchip had like an "LTspice simulator " for their microcontrollers with C code in them. I couldnt find it on their website....do you know if there is one?
Code:
//This checks the voltage
//then if its ok, it PWMs the light level.
//PIC16F18856
//XC8 compiler
//MPLAB X V3.61
//date 23aug2019
// PIC16F18856 Configuration Bit Settings
//
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)
// CONFIG2
#pragma config MCLRE = OFF // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config WRT = OFF // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = not_available// Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF // DataNVM code protection bit (Data EEPROM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 4000000
uint16_t count;
uint16_t count1;
uint16_t vbusmeas;
void main(void) {
//Setup ports
TRISA = 0x10; //
TRISB = 0x19; //
TRISC = 0x02; //
ANSELA = 0x10; //A4=TEMP INPUT
ANSELB = 0x00; //B0=VBUS; B4=CURRENT
ANSELC = 0x00; //C1=SHORT CIRCUIT
WPUA = 0x00;
ODCONA = 0x00;
SLRCONA = 0x00;
INLVLA = 0x00; //ST?
CCDPA = 0x00;
CCDNA = 0x00;
WPUB = 0x00;
ODCONB = 0x00;
SLRCONB = 0x00;
INLVLB = 0x00; //ST?
CCDPB = 0x00;
CCDNB = 0x00;
WPUC = 0x00;
ODCONC = 0x00;
SLRCONC = 0x00;
INLVLC = 0x00; //ST?
CCDPC = 0x00;
CCDNC = 0x00;
INTCON = 0x00;
CM1CON0 = 0x00;
CM1CON1 = 0x00;
CM2CON0 = 0x00;
CM2CON1 = 0x00;
PWM6CON = 0x00;
PWM7CON = 0x00;
ZCDCON = 0x00;
ADCON0 = 0x00; //fosc;leftjust;
ADCON1 = 0x00; //
ADCON2 = 0x00; //
ADCON3 = 0x00; //
ADRESH = 0x00;
ADCLK = 0x3F; //FOSC/128
ADREF = 0x00; //Vref is vdd
ADPCH = 0x08; //adc chan B0....VBUS_MEAS
ADON = 1; //ENABLE ADC
//Initialize ports
LATAbits.LATA0 = 0; //nc
LATAbits.LATA1 = 0; //nc
LATAbits.LATA2 = 0; //DIM_AN OUTPUT
LATAbits.LATA3 = 0; //NC
//LATAbits.LATA4 = 0; //TEMP INPUT ADC
LATAbits.LATA5 = 0; //NC
LATAbits.LATA6 = 1; //DIM_PWM OUTPUT
LATAbits.LATA7 = 0; //NC
//LATBbits.LATB0 = 0; //VBUS_MEAS INPUT ADC
LATBbits.LATB1 = 0; //NC
LATBbits.LATB2 = 0; //DALI_TX
//LATBbits.LATB3 = 0; //DALI_RX
//LATBbits.LATB4 = 0; //LED CURRENT INPUT ADC
LATBbits.LATB5 = 0; //NC
LATBbits.LATB6 = 0; //SPCLK
LATBbits.LATB7 = 0; //SPDAT
LATCbits.LATC0 = 0; //NC
//LATCbits.LATC1 = 0; //SC_OUT INPUT ADC
LATCbits.LATC2 = 0; //NC
LATCbits.LATC3 = 0; //NC
LATCbits.LATC4 = 0; //NC
LATCbits.LATC5 = 1; //NC
LATCbits.LATC6 = 0; //CK_TEST PAD
LATCbits.LATC7 = 0; //RX TEST PAD
//800ms delay
for (count=1;count<=10;count++) {
__delay_ms(100);
}
while(1) {
ADGO = 1; //START CONVERSION
while(ADGO) {;}
vbusmeas = ADRESH;
if (vbusmeas > 59) {break;} //only start if vbus>28v
}
while(1) {
LATAbits.LATA6 = 1; //DIM_PWM OUTPUT
__delay_us(750);
LATAbits.LATA6 = 0;
__delay_us(250);
}
return;
}