// Arlo, this may not be correct, but it does now compile. I do not have your
// hardware configuration to make things right, and of course, every author does
// things differently. So to rethink to this approach, especially takes more time
// than I have right now, to make the ADC stuff right. The dsPIC has a very complicated
// ADC with many ways to control the sampling. You just have to study it for hours.
// Like others said, make small steps, and MAKE SURE you understand what you did.
// This wil be a long journey! Good luck!
#define __p30F3010__
#include "p30F3010.h" // put compiler dsPIC.h file in root
#include "adc10.h"
#include "delay.h"
//-----------------------------------------------------------------------------
//Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR (PBOR_OFF & PWRT_16 & MCLR_EN);
_FGS(CODE_PROT_OFF);
//-----------------------------------------------------------------------------
//Program Specific Constants
// Arlo, I need to know your crystal frequency to get this part right
#define FCY 7372800 //Instruction cycle rate (Osc x PLL / 4)
#define MILLISEC FCY/10000 // 1 mSec delay constant
// subroutines or functions must be defined before they are used ===============
void DelayNmSec(unsigned int N){
unsigned int j;
while(N--)
for(j=0;j < MILLISEC;j++);
}
// ADC_Init() ==================================================================
// this ADC is VERY complicated Arlo, this took me two days to figure out originally
// I am not sure this is right because I do not know your hardware configuration
// Study dsPIC30F3010-2011-70141e.pdf file from Microchip Pages 127 to 138
// Study DSPic30F_Family_ReferenceManual_70046E.pdf file from Microchip pages 401 to 460
void ADC_Init(void) { // functions must be defined before you use them
ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x00E4; // Autoconvert starts conversion, auto sampling
ADCON2 = 0; // sample CH0 channel only
ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input in this example RB2/AN2 is the input
ADCON3 = 0x0080; // Tad = internal RC clock (4uS)
ADCSSL = 0; // not sure on this register, I will need to research it's effects
IFS0bits.ADIF = 0; // clear ADC interrupt flag
ADCON1bits.ADON = 1; // turn ADC ON
}
// main routine ================================================================
int main(void) {
unsigned int ADCValue;
// Arlo, I assume your LED is on Pin D0
LATD = 0xfffe; // Writes to the latch, sets DO to off state for LED
TRISD = 0xfffe; // O sets pin as output for driving LED on DO
// This should flash your LED 5 times at startup
int ijk;
for (ijk = 0; ijk < 5; ijk++) {
PORTDbits.RD0 = 1; // turn on D0 for LED
DelayNmSec(500); // delay a half second
PORTDbits.RD0 = 0; // turn off DO
DelayNmSec(500); // delay a half second
} // end of for
// now lets play with the adc
ADC_Init(); // call Initialize ADC to initialize it
while (1) {
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
} // end of the while(1) loop
} // end of main
5Mhz Thanks so much. I just realized today this could be set as a pour mans Brushed motor controller. I have to think about that I might build something to help the drag race hi out with the s10 build... A brushed controller will be a walk in the park after this.bigmoose said:Arlo, this one compiles at least!
//Program Specific Constants
//Fcy = cycle clock = Fosc/4 = (XTAL * PLLmultiplier)/(4*PostScaler)
#define FCY 5000000 // 5 MHz Xtal*PLLof4/(4*Postscalerof1)
#define MILLISEC FCY/10000 // 1 mSec delay constant
Ill keep digging as well I will try to figure out how to use the ADC value for a timed pulse.//ADCSSL: A/D Input Scan Select Register
ADCSSL = 0; //clear register
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
} // end of the while(1) loop
Yeh Im working on this all day today. And every evening this week. Then Im off from the 25-the3rd so Ill be working on it then too.Njay said:Arlo, you're still focused at the "control LED flash speed with throttle" program, right? Do you intent to follow my suggested algorithm?
1) Read the throttle position (ADC)
2) Transform the throttle position into a time delay value
3) Wait for the amount of time calculated in 2) (this will be half the time period or your flashing LED)
4) Light up your LED (port perform I/O, setting a bit)
5) Do another delay as in 3)
6) Turn off your LED
7) Go back to 1) to repeat it all over again
If so, it's time to copy the steps above into comments in your program as Lebowski suggests, that will help you a lot to get this through. In the last code posted by BigM you already have an almost complete set of steps to read an ADC
#ifdef debug
i=1;
printf("***%4u ThrottleLI %6ld PDC2 %6u PTPERloadLU %6lu SEVTCMP %6u\n\r",
ADCcount, ThrottleLI, PDC2, PTPERloadLU, SEVTCMP);
#endif
// Arlo, this may not be correct, but it does now compile. I do not have your
// hardware configuration to make things right, and of course, every author does
// things differently. So to rethink to this approach, especially takes more time
// than I have right now, to make the ADC stuff right. The dsPIC has a very complicated
// ADC with many ways to control the sampling. You just have to study it for hours.
// Like others said, make small steps, and MAKE SURE you understand what you did.
// This wil be a long journey! Good luck!
#define __p30F3010__
#include "p30F3010.h" // put compiler dsPIC.h file in root
#include "adc10.h"
#include "delay.h"
//-----------------------------------------------------------------------------
//Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR (PBOR_OFF & PWRT_16 & MCLR_EN);
_FGS(CODE_PROT_OFF);
//-----------------------------------------------------------------------------
//Program Specific Constants
//Fcy = cycle clock = Fosc/4 = (XTAL * PLLmultiplier)/(4*PostScaler)
#define FCY 5000000 // 5 MHz Xtal*PLLof4/(4*Postscalerof1)
#define MILLISEC FCY/10000 // 1 mSec delay constant
// subroutines or functions must be defined before they are used ===============
void DelayNmSec(unsigned int N){
unsigned int j;
while(N--)
for(j=0;j < MILLISEC;j++);
}
// ADC_Init() ==================================================================
// this ADC is VERY complicated Arlo, this took me two days to figure out originally
// I am not sure this is right because I do not know your hardware configuration
// Study dsPIC30F3010-2011-70141e.pdf file from Microchip Pages 127 to 138
// Study DSPic30F_Family_ReferenceManual_70046E.pdf file from Microchip pages 401 to 460
void ADC_Init(void) { // functions must be defined before you use them
ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x00E4; // Autoconvert starts conversion, auto sampling
ADCON2 = 0; // sample CH0 channel only
ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input in this example RB2/AN2 is the input
ADCON3 = 0x0080; // Tad = internal RC clock (4uS)
ADCSSL = 0; // not sure on this register, I will need to research it's effects
IFS0bits.ADIF = 0; // clear ADC interrupt flag
ADCON1bits.ADON = 1; // turn ADC ON
}
// main routine ================================================================
int main(void) {
unsigned int ADCValue;
// Arlo, I assume your LED is on Pin D0
LATD = 0xfffe; // Writes to the latch, sets DO to off state for LED
TRISD = 0xfffe; // O sets pin as output for driving LED on DO
// This should flash your LED 5 times at startup
int ijk;
for (ijk = 0; ijk < 5; ijk++) {
PORTDbits.RD0 = 1; // turn on D0 for LED
DelayNmSec(500); // delay a half second
PORTDbits.RD0 = 0; // turn off DO
DelayNmSec(500); // delay a half second
} // end of for
// now lets play with the adc
ADC_Init(); // call Initialize ADC to initialize it
while (1) {
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
} // end of the while(1) loop
} // end of main
Ok Njay like this?Njay said:No Arlo, what you have in a function (ADC_Init()) is just the initialization of the ADC (hw module). The code that really takes an ADC reading is composed of the few steps I copied into my post, and those are not inside a function (of its own), they are just inside the main infinite loop (while (1) { ... }). If you move those steps to the inside of a function, you'll be able to execute them in the main infinite loop by just calling the function's name and saving the returned value in a variable to use later, like
ADCValue = ReadADC();
assuming you name the function "ReadADC".
// Arlo, this may not be correct, but it does now compile. I do not have your
// hardware configuration to make things right, and of course, every author does
// things differently. So to rethink to this approach, especially takes more time
// than I have right now, to make the ADC stuff right. The dsPIC has a very complicated
// ADC with many ways to control the sampling. You just have to study it for hours.
// Like others said, make small steps, and MAKE SURE you understand what you did.
// This wil be a long journey! Good luck!
#define __p30F3010__
#include "p30F3010.h" // put compiler dsPIC.h file in root
#include "adc10.h"
#include "delay.h"
//-----------------------------------------------------------------------------
//Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR (PBOR_OFF & PWRT_16 & MCLR_EN);
_FGS(CODE_PROT_OFF);
//-----------------------------------------------------------------------------
//Program Specific Constants
//Fcy = cycle clock = Fosc/4 = (XTAL * PLLmultiplier)/(4*PostScaler)
#define FCY 5000000 // 5 MHz Xtal*PLLof4/(4*Postscalerof1)
#define MILLISEC FCY/10000 // 1 mSec delay constant
// subroutines or functions must be defined before they are used ===============
void DelayNmSec(unsigned int N){
unsigned int j;
while(N--)
for(j=0;j < MILLISEC;j++);
}
// ADC_Init() ==================================================================
// this ADC is VERY complicated Arlo, this took me two days to figure out originally
// I am not sure this is right because I do not know your hardware configuration
// Study dsPIC30F3010-2011-70141e.pdf file from Microchip Pages 127 to 138
// Study DSPic30F_Family_ReferenceManual_70046E.pdf file from Microchip pages 401 to 460
void ADC_Init(void) { // functions must be defined before you use them
ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x00E4; // Autoconvert starts conversion, auto sampling
ADCON2 = 0; // sample CH0 channel only
ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input in this example RB2/AN2 is the input
ADCON3 = 0x0080; // Tad = internal RC clock (4uS)
ADCSSL = 0; // not sure on this register, I will need to research it's effects
IFS0bits.ADIF = 0; // clear ADC interrupt flag
ADCON1bits.ADON = 1; // turn ADC ON
}
// ReadADC function=====================================================
int ReadADC () {
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
}
// main routine ================================================================
int main(void) {
unsigned int ADCValue;
// Arlo, I assume your LED is on Pin D0
LATD = 0xfffe; // Writes to the latch, sets DO to off state for LED
TRISD = 0xfffe; // O sets pin as output for driving LED on DO
// This should flash your LED 5 times at startup
int ijk;
for (ijk = 0; ijk < 5; ijk++) {
PORTDbits.RD0 = 1; // turn on D0 for LED
DelayNmSec(500); // delay a half second
PORTDbits.RD0 = 0; // turn off DO
DelayNmSec(500); // delay a half second
} // end of for
// now lets play with the adc
ADC_Init(); // call Initialize ADC to initialize it
while (1) {
ADCValue = ReadADC(); // ReadADC = value of ADC
} // end of the while(1) loop
} // end of main
Thanks Bigmoose. I posted what I was trying 2 posts ago. Im going back over it now. Its a matter of understanding C programing better and understanding what Njay is saying. I got called for some work at my shop so I went and cleaned and organized while I waited for 2 no shows :? I Realy need to lock my self in a room with some soothing music to get down to this.bigmoose said:Arlo, I have a folder for you on my compiler, so post in the code section exactly what you have and I'll find why it will not compile and post the solution for you.
// ReadADC function=====================================================
int ReadADC ()
{
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // wait for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
}
For one I have been looking to learn C for a while. I will get it not to worry.Lebowski said:I'm starting to wonder if Assmebly with it's limitted instruction set wouldn't be easier ? Though that will have penalties when it comes to math though.
Anyway (don't want to start that discussion again), I see you assign values to an output
pin on D by using 'PORTD'. Because of the speed of these devices it's better to write to
a pin on D using 'LATD' and to read from a pin using 'PORTD'. (see datasheet)
function readADC()
{
int value;
//...
value = 0; // read hardware here
return value; // return value
}
void main()
{
//...
a = readADC();
//...
}
// Arlo, this may not be correct, but it does now compile. I do not have your
// hardware configuration to make things right, and of course, every author does
// things differently. So to rethink to this approach, especially takes more time
// than I have right now, to make the ADC stuff right. The dsPIC has a very complicated
// ADC with many ways to control the sampling. You just have to study it for hours.
// Like others said, make small steps, and MAKE SURE you understand what you did.
// This wil be a long journey! Good luck!
#define __p30F3010__
#include "p30F3010.h" // put compiler dsPIC.h file in root
#include "adc10.h"
#include "delay.h"
//-----------------------------------------------------------------------------
//Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR (PBOR_OFF & PWRT_16 & MCLR_EN);
_FGS(CODE_PROT_OFF);
//-----------------------------------------------------------------------------
//Program Specific Constants
//Fcy = cycle clock = Fosc/4 = (XTAL * PLLmultiplier)/(4*PostScaler)
#define FCY 5000000 // 5 MHz Xtal*PLLof4/(4*Postscalerof1)
#define MILLISEC FCY/10000 // 1 mSec delay constant
// subroutines or functions must be defined before they are used ===============
void DelayNmSec(unsigned int N){
unsigned int j;
while(N--)
for(j=0;j < MILLISEC;j++);
}
// ADC_Init() ==================================================================
// this ADC is VERY complicated Arlo, this took me two days to figure out originally
// I am not sure this is right because I do not know your hardware configuration
// Study dsPIC30F3010-2011-70141e.pdf file from Microchip Pages 127 to 138
// Study DSPic30F_Family_ReferenceManual_70046E.pdf file from Microchip pages 401 to 460
void ADC_Init(void) { // functions must be defined before you use them
ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x00E4; // Autoconvert starts conversion, auto sampling
ADCON2 = 0; // sample CH0 channel only
ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input in this example RB2/AN2 is the input
ADCON3 = 0x0080; // Tad = internal RC clock (4uS)
ADCSSL = 0; // not sure on this register, I will need to research it's effects
IFS0bits.ADIF = 0; // clear ADC interrupt flag
ADCON1bits.ADON = 1; // turn ADC ON
}
// ReadADC function=====================================================
unsigned int ReadADC () {
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
return ADCvalue; //Value of ADC
}
// main routine ================================================================
int main(void) {
unsigned int ADCValue;
// Arlo, I assume your LED is on Pin D0
LATD = 0xfffe; // Writes to the latch, sets DO to off state for LED
TRISD = 0xfffe; // O sets pin as output for driving LED on DO
// This should flash your LED 5 times at startup
int ijk;
for (ijk = 0; ijk < 5; ijk++) {
PORTDbits.RD0 = 1; // turn on D0 for LED
DelayNmSec(500); // delay a half second
PORTDbits.RD0 = 0; // turn off DO
DelayNmSec(500); // delay a half second
} // end of for
// now lets play with the adc
ADC_Init(); // call Initialize ADC to initialize it
while (1) {
// Take an ADC conversion and use it to set Timer0
TMR0H = ADC_Convert(); // MSB from ADC
TMR0L = 0; // LSB = 0
}
//Timer scale function
void Timer0_Init(void)
{
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
T0CON = 0b00000001; // prescale 1:4 - about 1 second maximum delay.
TMR0H = 0; // clear timer - always write upper byte first
TMR0L = 0;
T0CONbits.TMR0ON = 1; // start timer
}
// end of the while(1) loop
} // end of main
// Arlo, this may not be correct, but it does now compile. I do not have your
// hardware configuration to make things right, and of course, every author does
// things differently. So to rethink to this approach, especially takes more time
// than I have right now, to make the ADC stuff right. The dsPIC has a very complicated
// ADC with many ways to control the sampling. You just have to study it for hours.
// Like others said, make small steps, and MAKE SURE you understand what you did.
// This wil be a long journey! Good luck!
#define __p30F3010__
#include "p30F3010.h" // put compiler dsPIC.h file in root
#include "adc10.h"
#include "delay.h"
//-----------------------------------------------------------------------------
//Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR (PBOR_OFF & PWRT_16 & MCLR_EN);
_FGS(CODE_PROT_OFF);
//-----------------------------------------------------------------------------
//Program Specific Constants
//Fcy = cycle clock = Fosc/4 = (XTAL * PLLmultiplier)/(4*PostScaler)
#define FCY 5000000 // 5 MHz Xtal*PLLof4/(4*Postscalerof1)
#define MILLISEC FCY/10000 // 1 mSec delay constant
// subroutines or functions must be defined before they are used ===============
void DelayNmSec(unsigned int N){
unsigned int j;
while(N--)
for(j=0;j < MILLISEC;j++);
}
// ADC_Init() ==================================================================
// this ADC is VERY complicated Arlo, this took me two days to figure out originally
// I am not sure this is right because I do not know your hardware configuration
// Study dsPIC30F3010-2011-70141e.pdf file from Microchip Pages 127 to 138
// Study DSPic30F_Family_ReferenceManual_70046E.pdf file from Microchip pages 401 to 460
void ADC_Init(void) { // functions must be defined before you use them
ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x00E4; // Autoconvert starts conversion, auto sampling
ADCON2 = 0; // sample CH0 channel only
ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input in this example RB2/AN2 is the input
ADCON3 = 0x0080; // Tad = internal RC clock (4uS)
ADCSSL = 0; // not sure on this register, I will need to research it's effects
IFS0bits.ADIF = 0; // clear ADC interrupt flag
ADCON1bits.ADON = 1; // turn ADC ON
}
// ReadADC function=====================================================
unsigned int ReadADC ()
unsigned int ADCvalue
{
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
return ADCvalue; //Value of ADC
}
// main routine ================================================================
int main(void) {
unsigned int ADCValue;
// Arlo, I assume your LED is on Pin D0
LATD = 0xfffe; // Writes to the latch, sets DO to off state for LED
TRISD = 0xfffe; // O sets pin as output for driving LED on DO
// This should flash your LED 5 times at startup
int ijk; // From bigmoose
for (ijk = 0; ijk < 5; ijk++) {
PORTDbits.RD0 = 1; // turn on D0 for LED
DelayNmSec(500); // delay a half second
PORTDbits.RD0 = 0; // turn off DO
DelayNmSec(500); // delay a half second
} // end of for
// now lets play with the adc
ADC_Init(); // call Initialize ADC to initialize it
while (1) {
// Take an ADC conversion and use it to set Timer0
TMR0H = ADC_Convert(); // MSB from ADC
TMR0L = 0; // LSB = 0
}
//Timer scale function
void Timer0_Init(void)
{
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
T0CON = 0b00000001; // prescale 1:4 - about 1 second maximum delay.
TMR0H = 0; // clear timer - always write upper byte first
TMR0L = 0;
T0CONbits.TMR0ON = 1; // start timer
}
// end of the while(1) loop
} // end of main
----------------------------------------------------------------------
Release build of project `C:\Program Files (x86)\Microchip\Tps Led.mcp' started.
Language tool versions: pic30-as.exe v3.30, pic30-gcc.exe v3.30, pic30-ld.exe v3.30, pic30-ar.exe v3.30
Wed Dec 21 07:33:23 2011
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files (x86)\Microchip\mplabc30\v3.30c\bin\pic30-gcc.exe" -mcpu=30F3010 -x c -c "Arlo1.c" -o"Arlo1.o" -g -Wall
Arlo1.c: In function 'ReadADC':
Arlo1.c:51: error: syntax error before '{' token
Arlo1.c:69: error: syntax error before 'for'
Halting build on first failure as requested.
----------------------------------------------------------------------
Release build of project `C:\Program Files (x86)\Microchip\Tps Led.mcp' failed.
Language tool versions: pic30-as.exe v3.30, pic30-gcc.exe v3.30, pic30-ld.exe v3.30, pic30-ar.exe v3.30
Wed Dec 21 07:33:23 2011
----------------------------------------------------------------------
BUILD FAILED
#define __p30F3010__
#include "p30F3010.h" // put compiler dsPIC.h file in root
#include "adc10.h"
#include "delay.h"
//-----------------------------------------------------------------------------
//Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR (PBOR_OFF & PWRT_16 & MCLR_EN);
_FGS(CODE_PROT_OFF);
//-----------------------------------------------------------------------------
//Program Specific Constants
//Fcy = cycle clock = Fosc/4 = (XTAL * PLLmultiplier)/(4*PostScaler)
#define FCY 5000000 // 5 MHz Xtal*PLLof4/(4*Postscalerof1)
#define MILLISEC FCY/10000 // 1 mSec delay constant
// Put global variables here
unsigned int ADCValue;
// subroutines or functions must be defined before they are used ===============
// subroutines or functions must be defined before they are used ===============
// subroutines or functions must be defined before they are used ===============
// subroutines or functions must be defined before they are used ===============
// void DelayNmSec(unsigned int N) =============================================
void DelayNmSec(unsigned int N){
unsigned int j;
while(N--)
for(j=0;j < MILLISEC;j++);
}
// void ADC_Init() ==================================================================
void ADC_Init(void) { // functions must be defined before you use them
ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x00E4; // Autoconvert starts conversion, auto sampling
ADCON2 = 0; // sample CH0 channel only
ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input in this example RB2/AN2 is the input
ADCON3 = 0x0080; // Tad = internal RC clock (4uS)
ADCSSL = 0; // not sure on this register, I will need to research it's effects
IFS0bits.ADIF = 0; // clear ADC interrupt flag
ADCON1bits.ADON = 1; // turn ADC ON
}
// ReadADC function =====================================================
unsigned int ReadADC(void){
ADCON1bits.SAMP = 1; // sampling begins immediately after last conversion
DelayNmSec(100); // for 100 mS
while (!ADCON1bits.DONE); // conversion done?
ADCValue = ADCBUF0; // yes Now ADCValue holds the value for you to use
return ADCValue; // Value of ADC
}
// void Timer0_Init(void) ===============================================
/* ARLO, where did you get this code from? You cannot cut and paste code from different "families"
of PIC's dsPIC's etc. THEY ARE ALL DIFFERENT! dsPIC30F3010 does not have a timer 0! So none of this
would ever work!
*/
void Timer0_Init(void)
{
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
T0CON = 0b00000001; // prescale 1:4 - about 1 second maximum delay.
TMR0H = 0; // clear timer - always write upper byte first
TMR0L = 0;
T0CONbits.TMR0ON = 1; // start timer
}
// main routine ================================================================
int main(void) {
// Arlo, I assume your LED is on Pin D0
LATD = 0xfffe; // Writes to the latch, sets DO to off state for LED
TRISD = 0xfffe; // O sets pin as output for driving LED on DO
// This should flash your LED 5 times at startup
int ijk; // From bigmoose
for (ijk = 0; ijk < 5; ijk++) {
PORTDbits.RD0 = 1; // turn on D0 for LED
DelayNmSec(500); // delay a half second
PORTDbits.RD0 = 0; // turn off DO
DelayNmSec(500); // delay a half second
} // end of for
// now lets play with the adc
ADC_Init(); // call Initialize ADC to initialize it
while (1) {
// Take an ADC conversion and use it to set Timer0
//ARLO ADC_Convert(0) is another function that you did not define...
TMR0H = ADC_Convert(); // MSB from ADC
TMR0L = 0; // LSB = 0
//Timer scale function
} // end of the while(1) loop
} // end of main