Hello,
I was just curious if I am doing something wrong with the code or simulator, or if the simulator just cannot simulate what I am trying to do? I am using SourceBoost IDE to program and simulate.
I am new to PIC and have been trying things out on the simulator to get better at it. One thing I am trying to learn right now is how to read ADC values. I downloaded evandude's adc code for the pic 16f88 and modified it slightly:
//////Begin Code///////
#include <system.h>
#pragma DATA 0x2007, 0x3F50//_INTRC_IO & _WDT_OFF & _LVP_OFF
//important bits in SFR's for ADC, UART, and interrupt.
volatile bit go @ ADCON0.GO;
volatile bit gie @ INTCON.GIE;
volatile bit rcie @ PIE1.RCIE;
volatile bit peie @ INTCON.PEIE;
volatile bit rcif @ PIR1.RCIF;
volatile bit trmt @ TXSTA.TRMT;
char data_in;
bit newdata;
//Function: Reads one value from the ADC
//Returns: most significant byte of result
inline char adc_read()
{
go = 1;
while (go);
return adresh;
}
void main()
{
//*********************//
// configure I/O ports //
//*********************//
portb = 00000000b;
trisb = 11111111b; //0 = Output, 1 = Input
porta = 00000000b;
trisa = 11111111b; //0 = Output, 1 = Input
//***************//
// configure ADC //
//***************//
osccon = 01110000b; //internal oscillator @ 8MHz
cmcon = 00000111b; //comparators off
ansel = 00000001b; //AN0 on (RA0)
adcon1 = 00000000b; //left justified, fosc/2 clock, Vdd & Vss ref voltages
adcon0 = 00000101b; //set to AN0, ADC operating
char adcval,temp;
while(1)
{
temp=adc_read();
delay_ms(5);
}
}
/////END CODE/////
I run the program, put XX voltage on pin RA0 with the Variable PSU and I watch temp. Temp is always 0. When I step through the program it hangs when it goes to function adc_read(). The simulator apparently is not clearing the go bit when it gets a value.
I have modified the adc_read() so it isn't in the infinite loop and temp still remains 0 all the time.
Am I doing something wrong or was the simulator just not made for what I am doing?
I couldn't get the original code to work correctly, available:
http://www.eegeek.net/electronics/tutorials/PIC/adctest.c
Thanks
I was just curious if I am doing something wrong with the code or simulator, or if the simulator just cannot simulate what I am trying to do? I am using SourceBoost IDE to program and simulate.
I am new to PIC and have been trying things out on the simulator to get better at it. One thing I am trying to learn right now is how to read ADC values. I downloaded evandude's adc code for the pic 16f88 and modified it slightly:
//////Begin Code///////
#include <system.h>
#pragma DATA 0x2007, 0x3F50//_INTRC_IO & _WDT_OFF & _LVP_OFF
//important bits in SFR's for ADC, UART, and interrupt.
volatile bit go @ ADCON0.GO;
volatile bit gie @ INTCON.GIE;
volatile bit rcie @ PIE1.RCIE;
volatile bit peie @ INTCON.PEIE;
volatile bit rcif @ PIR1.RCIF;
volatile bit trmt @ TXSTA.TRMT;
char data_in;
bit newdata;
//Function: Reads one value from the ADC
//Returns: most significant byte of result
inline char adc_read()
{
go = 1;
while (go);
return adresh;
}
void main()
{
//*********************//
// configure I/O ports //
//*********************//
portb = 00000000b;
trisb = 11111111b; //0 = Output, 1 = Input
porta = 00000000b;
trisa = 11111111b; //0 = Output, 1 = Input
//***************//
// configure ADC //
//***************//
osccon = 01110000b; //internal oscillator @ 8MHz
cmcon = 00000111b; //comparators off
ansel = 00000001b; //AN0 on (RA0)
adcon1 = 00000000b; //left justified, fosc/2 clock, Vdd & Vss ref voltages
adcon0 = 00000101b; //set to AN0, ADC operating
char adcval,temp;
while(1)
{
temp=adc_read();
delay_ms(5);
}
}
/////END CODE/////
I run the program, put XX voltage on pin RA0 with the Variable PSU and I watch temp. Temp is always 0. When I step through the program it hangs when it goes to function adc_read(). The simulator apparently is not clearing the go bit when it gets a value.
I have modified the adc_read() so it isn't in the infinite loop and temp still remains 0 all the time.
Am I doing something wrong or was the simulator just not made for what I am doing?
I couldn't get the original code to work correctly, available:
http://www.eegeek.net/electronics/tutorials/PIC/adctest.c
Thanks