T
treez
Guest
Hi
We have written this C code (XC8 compiler by microchip) for PIC16F1503.
All it is supposed to do is repetitively read the voltage from the opamp and then every 10 seconds or so…it will give out a voltage on the DAC output which is representative of the maximum level of the voltage read on the ADC input.
However, the DAC output is just always giving zero volts.
Do you know what’s wrong, and which document tells the names that one must use to refer to the various registers of the PIC16F1503?
We have written this C code (XC8 compiler by microchip) for PIC16F1503.
All it is supposed to do is repetitively read the voltage from the opamp and then every 10 seconds or so…it will give out a voltage on the DAC output which is representative of the maximum level of the voltage read on the ADC input.
However, the DAC output is just always giving zero volts.
Do you know what’s wrong, and which document tells the names that one must use to refer to the various registers of the PIC16F1503?
Code:
// PIC16F1503 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#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 BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#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 LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdint.h>
#include <pic16f1503.h>
#define _XTAL_FREQ 4000000
uint8_t count;
uint8_t count1;
uint8_t count2;
uint8_t inamp;
uint8_t ADCval;
uint8_t MAXval;
void doADC(void);
void doADC(void) {
//Initiate ADRESH, ADRESL
ADRESL = 0x00;
ADRESH = 0x00;
__delay_us(50);
ADCON0bits.GO = 1; //start conversion (GODONE=1)
while (ADCON0bits.GO) {;} //Wait for conversion to finish (GODONE CLR)
__delay_us(10);
ADCval = ADRESH;
__delay_us(10);
return;
}
//Pin useage
//RA4 (PIN 3) = AN3 = ADC input
//RA2 (PIN 11) = AN2 = DACOUT1)
//RC3 = PIN7 = General output
void main(void) {
//Setup registers
TRISA = 0x10; //ADC input = RA4
TRISC = 0x00; //unused so make all outputs.
ANSELA = 0x14; //RA4 = ADC INPUT; RA2 = DAC output
ANSELC = 0x00;
INTCON = 0x00; //disable all interrupts
FVRCON = 0x00;
ADCON0 = 0x0C; //Select ADC channel AN3
ADCON1 = 0x60; //Left justified; FOSC/64;VDD=REF
ADCON0bits.ADON = 1; //Turn on ADC module
DACCON0 = 0xA0; //DAC enabled and vref=vdd
WPUA = 0x00; //weak pullups disabled
PORTA = 0x00; //zero outputs
PORTC = 0x00; //zero outputs
CM1CON1 = 0x00; //disabled comparator
CM2CON1 = 0x00; //disabled comparator
__delay_ms(10);
MAXval = 0;
count = 0; //Do ten flashes to show your in the "zone".
while(1){
LATCbits.LATC3 = 0;
__delay_ms(10);
LATCbits.LATC3 = 1;
__delay_ms(10);
count = count + 1;
if (count >= 10) {break;}
}
here:
count1 = 0;
while (1) {
doADC();
if (ADCval >= MAXval) {MAXval == ADCval;}
count1 = count1 + 1;
if (count1 >= 100) {break;}
}
//Now put MAXval representor out on DAC.
if (MAXval >= 0x7F) {DACCON1bits.DACR = 0x1F;}
if (MAXval <= 0x7F) {DACCON1bits.DACR = 0x00;}
goto here;
while(1) {;}
//-----------------------------------------------------------------
return;
}