#include "Includes.h"
#include <xc.h>
#include <stdio.h>
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#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 = OFF // PLL Enable (4x PLL disabled)
#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 LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
#define _XTAL_FREQ 4000000
void doad(void);
void interrupt intr(void){
INTCONbits.T0IF = 0;
doad();
}
void main(){
OSCCONbits.SPLLEN = 0;//4x PLL is disabled
OSCCONbits.IRCF = 0b1101;//4 MHz
ADCON0bits.CHS = 0b00000;//AN0 is Analog input
ADCON0bits.ADON = 1;// A/D converter module is operating
ADCON1bits.ADFM = 1;//Right justified
ADCON1bits.ADCS = 0b001;//Fosc/8
ADCON1bits.ADNREF = 0;//VREF- is connected to VSS
ADCON1bits.ADPREF = 0b11;//VREF+ is connected to internal Fixed Voltage Reference (FVR) module
FVRCONbits.FVREN = 1;//Fixed Voltage Reference is enabled
FVRCONbits.ADFVR = 0b11;//ADC Fixed Voltage Reference Peripheral output is 4x (4.096V)
CM1CON0bits.C1ON = 0;//Comparator 1 is disabledf
CM2CON0bits.C2ON = 0;//Comparator 2 is disabledf
ANSELAbits.ANSELA = 0b00000001;//RA0 as AN0
OPTION_REGbits.T0CS = 0;//TMR0 Clock Source is Internal
OPTION_REGbits.PSA = 0;//Prescaler is assigned to the TIMER0 module
OPTION_REGbits.PS = 0b111;// Prescaler 1:256
INTCONbits.GIE = 1;//Global Interrupt Enabled
INTCONbits.PEIE = 1;//Peripheral Interrupt Enabled
INTCONbits.T0IE = 1;//TMR0 Overflow Interrupt Enabled
INTCONbits.T0IF = 0;//TMR0 Overflow Interrupt Flag
InitSoftUART();//Intialize Soft UART
while(1){
}
}
void doad(){
unsigned char s[5];
unsigned char i;
__delay_us(50);
ADCON0bits.CHS = 0b00000;//AN0 is Analog input
__delay_us(50);
ADCON0bits.GO_nDONE = 1;// ADC start
while(ADCON0bits.GO_nDONE){}// ADC waiting
sprintf(s, "%d", ADRESH*256+ADRESL);
for(i=0;i<4;i++){
UART_Transmit(s[i]);
}
UART_Transmit(0x0D);
UART_Transmit(0x0A);
}