KansaiRobot
Member
Thank you very much for all your responses and help so far to my question. I am humbled by your generosity.
Here is the state of things in my project so far.
I have managed to write a ADC program for my pic, compile it and write it in the PIC18F2550.
I am just not sure if the ADC is working or not.
Can someone please take a look and tell me if and what I am doing wrong??
First, a program that reads the analog value from AN0 and display it as 4 leds in binary (somehow the leds are always on)
2. A program that converts the value from ADC to angles (0 to 320 degrees) and based on that turns on or off a led (somehow, the led is always on)
Can someone please tell me what am I doing wrong?
Thanks a lot again
Kansai
Here is the state of things in my project so far.
I have managed to write a ADC program for my pic, compile it and write it in the PIC18F2550.
I am just not sure if the ADC is working or not.
Can someone please take a look and tell me if and what I am doing wrong??
First, a program that reads the analog value from AN0 and display it as 4 leds in binary (somehow the leds are always on)
Code:
/***********************************************************************************
* This is a ADC conversion program for the MPLAB C18-Lite compiler
* Continuosly sample analog input
* copy value to 4 leds
* this program is made for the PIC18F2550
********************************************************************************
* PIN assignments:
* AN0 : voltage to be measured
* RB0-3: output LEDs (RB3 is MSB)
*
**************************************************************************************/
// Includes
#include <p18f2550.h>
#include "adc.h"
unsigned int ADCResult=0;
/********************CONFIGURATION**************/
#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
#pragma config WDT = OFF //Disable watchdog timer
// Pin assignments
#define LEDS PORTB // output LEDs in port B
/**********MAIN PROGRAM*************/
void main()
{
unsigned char channel=0x00,config1=0x00,config2=0x00,config3=0x00,portconfig=0x00,i=0;
// initialization
TRISB=0b110000; // configure RB0-RB3 as outputs
// configure ADC
config1 = ADC_FOSC_RC | ADC_RIGHT_JUST | ADC_2_TAD ;
config2 = ADC_CH0 | ADC_INT_OFF | ADC_REF_VDD_VSS ;
portconfig = ADC_1ANA ; // Only A0 is analog. the rest are digital (so as to allow us digital output on B)
OpenADC(config1,config2,portconfig);
// main loop
while(1)
{
ConvertADC(); // start conversion
while(BusyADC()); // wait for result
ADCResult=(unsigned int) ReadADC();
LEDS=ADCResult>>6; // copy highes nybble (6 since this is a 10-bit ADC)
// LEDS= ADRESL>>4; // copy high nybble of result to LEDs (this is for 8-bit ADC)
}
CloseADC(); //turn off ADC
}
2. A program that converts the value from ADC to angles (0 to 320 degrees) and based on that turns on or off a led (somehow, the led is always on)
Code:
/***********************************************************************************
* This is the LED-on/off based on ADC conversion from a potentiometer program
* (compiled with MPLAB C18-Lite compiler)
* Continuosly sampled analog input
* based on the value from that input (after ADC) the LED is going to be
* turn ON/OFF,
* this program is made for the PIC18F2550
*
* The range of value of the 10-bit ADC is 0~1024 which correspond to a voltage 0~Vmax
*
* The range of motion of the potentiometer is 320 degrees
*
* We have to make a conversion between degrees and values of the ADC
*
* ZERO DEGREE ----> 0
* 320 DEGREE ----> 1024
*
*
*
********************************************************************************
* PIN assignments:
* AN0 : voltage to be measured
* RB3: output
**************************************************************************************/
/*********************Includes********************/
#include <p18f2550.h>
#include "adc.h"
unsigned int ADCResult=0;
double angle=0;
/********************CONFIGURATION**************/
#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
#pragma config WDT = OFF //Disable watchdog timer
/**********PIN Assignment*************************/
#define LEDPin3 LATBbits.LATB3 // the only one we will be using
/***********Other Definitions************************/
#define LOWANGLE 45
#define HIGHANGLE 135
#define TOTALANGLE 320
#define TOTALADC 1024
#define CONVADCTOANGLE (TOTALANGLE/TOTALADC)
#define CONVANGLETOADC (TOTALADC/TOTALANGLE)
/**********MAIN PROGRAM*************/
void main()
{
unsigned char channel=0x00,config1=0x00,config2=0x00,config3=0x00,portconfig=0x00,i=0;
// initialization
TRISB=0b110000; // configure RB0-RB3 as outputs
LEDPin3=0; // turn off the pin
// configure ADC
config1 = ADC_FOSC_RC | ADC_RIGHT_JUST | ADC_2_TAD ;
config2 = ADC_CH0 | ADC_INT_OFF | ADC_REF_VDD_VSS ;
portconfig = ADC_1ANA ; // Only A0 is analog. the rest are digital (so as to allow us digital output on B)
OpenADC(config1,config2,portconfig);
// main loop
while(1)
{
ConvertADC(); // start conversion
while(BusyADC()); // wait for result
ADCResult=(unsigned int) ReadADC();
angle=ADCResult*CONVADCTOANGLE; // we get the angle here.
if(angle>HIGHANGLE) // turn off the LED
LEDPin3=0; // turn off the LED
if(angle<LOWANGLE) // turn on the LED
LEDPin3=1;
}
CloseADC(); //turn off ADC
}
Can someone please tell me what am I doing wrong?
Thanks a lot again
Kansai