View previous topic :: View next topic |
Author |
Message |
andys
Joined: 23 Oct 2006 Posts: 175
|
adc problem |
Posted: Thu May 31, 2007 2:54 am |
|
|
I USE piC16F777 AND I HAVE PROBLEM WITH ADC.My code is bellow
#include <16F777.h>
#device ADC=10
#fuses XT,NOWDT,NOPROTECT,PUT
#use delay(clock=4000000)
void main()
{
long adc_result;
//SET_TRIS_A(0XFF);
//SET_TRIS_B(0X00);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
while(1){
set_adc_channel(0);
delay_us(1);
adc_result=read_adc();
output_high(PIN_B0);
//adc_result=read_adc(ADC_START_AND_READ);
if (adc_result<=4) output_high(PIN_B0); else output_low(PIN_B0);
if ((adc_result>4)&&(adc_result<=64)) output_high(PIN_B1); else output_low(PIN_B1);
if ((adc_result>64)&&(adc_result<=128)) output_high(PIN_B2); else output_low(PIN_B2);
if (adc_result>255)output_high(PIN_B3); else output_low(PIN_B3);
}
}
afterb the instruction adc_result=read_adc();
can not doing anything (can execute the instruction output_high(PIN_B0);) |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Thu May 31, 2007 3:45 am |
|
|
Hello,
here is your code more readable for anybody:
Code: |
#include <16F777.h>
#device ADC=10
#fuses XT,NOWDT,NOPROTECT,PUT
#use delay(clock=4000000)
void main (){
long adc_result;
//SET_TRIS_A(0XFF);
//SET_TRIS_B(0X00);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
while(1){
set_adc_channel(0);
delay_us(1);
adc_result=read_adc();
output_high(PIN_B0);
//adc_result=read_adc(ADC_START_AND_READ);
if (adc_result<=4) output_high(PIN_B0); else output_low(PIN_B0);
if ((adc_result>4)&&(adc_result<=64)) output_high(PIN_B1); else output_low(PIN_B1);
if ((adc_result>64)&&(adc_result<=128)) output_high(PIN_B2); else output_low(PIN_B2);
if (adc_result>255)output_high(PIN_B3); else output_low(PIN_B3);
}
}
//afterb the instruction adc_result=read_adc();
//can |
If you declare setup_adc_ports(ALL_ANALOG), then i think that The pin B0, B1, B2 and B3 are analogue on the 16F777.
Try to keep the digital to be digital with constant below:
Code: | // Constants used in SETUP_ADC_PORTS() are:
#define NO_ANALOGS 0x0F // None
#define ALL_ANALOG 0x00 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4 B0 B5
#define AN0_TO_AN12 0x02 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4 B0
#define AN0_TO_AN11 0x03 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4
#define AN0_TO_AN10 0x04 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1
#define AN0_TO_AN9 0x05 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3
#define AN0_TO_AN8 0x06 // A0 A1 A2 A3 A5 E0 E1 E2 B2
#define AN0_TO_AN7 0x07 // A0 A1 A2 A3 A5 E0 E1 E2
#define AN0_TO_AN6 0x08 // A0 A1 A2 A3 A5 E0 E1
#define AN0_TO_AN5 0x09 // A0 A1 A2 A3 A5 E0
#define AN0_TO_AN4 0x0A // A0 A1 A2 A3 A5
#define AN0_TO_AN3 0x0B // A0 A1 A2 A3
#define AN0_TO_AN2 0x0C // A0 A1 A2
#define AN0_TO_AN1 0x0D // A0 A1
#define AN0 0x0E // A0
#define AN0_TO_AN12_ANALOG 0x02 //!old only provided for compatibility
#define AN0_TO_AN11_ANALOG 0x03 //!old only provided for compatibility
#define AN0_TO_AN10_ANALOG 0x04 //!old only provided for compatibility
#define AN0_TO_AN9_ANALOG 0x05 //!old only provided for compatibility
#define AN0_TO_AN8_ANALOG 0x06 //!old only provided for compatibility
#define AN0_TO_AN7_ANALOG 0x07 //!old only provided for compatibility
#define AN0_TO_AN6_ANALOG 0x08 //!old only provided for compatibility
#define AN0_TO_AN5_ANALOG 0x09 //!old only provided for compatibility
#define AN0_TO_AN4_ANALOG 0x0A //!old only provided for compatibility
#define AN0_TO_AN3_ANALOG 0x0B //!old only provided for compatibility
#define AN0_TO_AN2_ANALOG 0x0C //!old only provided for compatibility
#define AN0_TO_AN1_ANALOG 0x0D //!old only provided for compatibility
#define AN0_ANALOG 0x0E //!old only provided for compatibility |
You can find them in your '16F777.h' file.
I suppose that your code will work with setup_adc_ports(AN0) or
somthink like setup_adc_ports(AN0_TO_AN4) it depend to the number of analogue pins you need.
dro _________________ in médio virtus |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
adc problem |
Posted: Thu May 31, 2007 5:25 am |
|
|
I try this and the problem is still there |
|
|
|