Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] 18F4550 to read Analog value

Status
Not open for further replies.

ycnr

Newbie level 5
Joined
Feb 9, 2015
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
98
Hi, I use 18 F 4550 to read 12 chanels analog values. I couldn't read values from portB. As you see i use PortE and PortB. I can read form PortE (except one pin) but i couldn't read any value from PortB. I tried many ways but i couldn't find the reason. is there anybody to help me.




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <18f4550.h>
#device ADC=10
#FUSES INTRC, NOWDT, NOLVP, PBADEN, NOBROWNOUT
#use delay(clock=4Mhz)
#include <lcd420_pc.c>
float inpt,outpt;
main(){
         setup_oscillator(OSC_4MHZ);
         setup_psp(PSP_DISABLED);       
         setup_comparator(NC_NC_NC_NC);
         setup_spi(SPI_SS_DISABLED); 
         DISABLE_INTERRUPTS(GLOBAL);
         setup_timer_1(T1_DISABLED);   
         setup_timer_2(T2_DISABLED,0,1); 
         setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
         setup_CCP1(CCP_OFF);            
         setup_CCP2(CCP_OFF);        
         setup_vref(FALSE);
         
         setup_adc_ports(ALL_ANALOG);
         setup_adc(ADC_CLOCK_INTERNAL);
        
         set_tris_a(0xff);
         set_tris_b(0xff);
         set_tris_e(0xff);
         set_tris_c(0x00);
         set_tris_d(0x00);
         
         lcd_init();
         delay_ms(10);
         
        while(true){
         kanal_1:
         if(input(PIN_B7)==0) {while(true) {if(input(PIN_B7)==1) goto kanal_2;}}
         if(input(PIN_B6)==0) {while(true) {if(input(PIN_B6)==1) goto kanal_6;}}
         set_adc_channel(6);
         delay_us(10);    
         inpt=read_adc()*0.00977517106549364613880742913001;
         output_c(0x00);output_bit(PIN_C0,1);
         set_adc_channel(0);
         delay_us(10);    
         outpt=read_adc()*0.02346041055718475073313782991202;
         lcd_gotoxy(1,1); printf(lcd_putc,"1.IN=%3.1f",inpt);
         lcd_gotoxy(1,2); printf(lcd_putc,"1.OT=%3.1f",outpt);
         }


...
...
...
similar codes for each channels
 
Last edited by a moderator:

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,755
Helped
4,347
Reputation
8,703
Reaction score
4,309
Trophy points
1,393
Activity points
130,768
Hi,

I can´t help with the PIC..

Is the numbering of the ADC channels correct? --> When you select channel6, is this referring to correct port pin?

but some hints, if you don´t mind.
* a capacitor next to the ADC input pins helps to make readings more stable
* a display update every 300ms is enough, so i recommend to use a 300ms dely in your main loop
* It may be a 10 or 12 bit ADC, so you factor 0.00977517106549364613880742913001 won´t make a difference to: 0.009775. Even the precision of a 32 bit float is limited to : 0.009775171
I wonder if the compiler can handle your value.
* The factor values make me think you want a precise measurement. But on the other hand it seems you use VCC as reference. Can you rely on VCC to be that accurate?..with change in load current, temperature, time...
* you should use bulk and decoupling capacitors on VCC, VREF and other importand voltages.

Klaus
 

ycnr

Newbie level 5
Joined
Feb 9, 2015
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
98
Thank you Klaus.

I added capasitor, tried 12 bit ADC. But these can't solve my problem.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top