Continue to Site

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.

PIC16F72 microcontorller ADC interfacing

Status
Not open for further replies.

kushal nandanwar

Full Member level 3
Joined
Jun 9, 2013
Messages
177
Helped
6
Reputation
12
Reaction score
6
Trophy points
18
Activity points
1,258
I have uploaded my PIC16F72 code for interfacing 4 Analog channel, but ADC not reading any data from output channel, I have used 1.8MHz crystal, required 1KHz PWM with varying duty cycle, pls 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <htc.h>
#include<pic.h>
#include <stdio.h>
 
void DELAY( void );
 
#define DATA ADRES
#define _XTAL_FREQ 1800000
 
 
unsigned int   Power,Power1, battery_Power;
unsigned int  Voltage, Current, battery_Voltage, battery_Current, 
                  DUTY =0x00;
 
#pragma config WDTEN = OFF
#pragma config OSC = XT
   
 
 
 
void main(void)
{
    CCP1CON = 0x00;
    PR2 = 0x1D;                                            /* Set Time Period Value */
 
    TRISC=0x00;                                               /* Set RC as an output pin */ 
    PORTC=0x00;
       
    TRISA =1;                                             /* Set RA as an INPUT pin */
    PORTA =1;
                                                          
    T2CON = 0x03;                                          /* 16 Prescalers Timer 2 */
    CCP1CON = 0x0C;                                        /* PWM Mode */
    CCPR1L = DUTY;                            
    
    TMR2ON = 1;                                            /* Timer 2 ON */  
 
while(1)                                                   /* infinit loop*/
{ 
                                                             
 
 /* analog channel setting for first sample*/                     
 
 /* analog channel setting for voltage*/
 
 /* AN0 analog input for voltage*/
 
 
ADCON0 = 0b01000001;                                              /* strat ADC */
ADCON1 = 0x00;                                               /*define port A as an analog port*/ 
DELAY();
GODONE=1;                                                    /*begin  conversion */
while(GODONE == 0);                                          /* Wait for the conversion to end*/
Voltage = DATA;                                              /* Save voltage in ADRESH as Voltage_Step_Number*/
 
       /* analog channel setting for current*/
 
       /* AN1 analog input for current*/
 
ADCON0 = 0b01010000;                                               /* strat ADC */
ADCON1 = 0x00;                                                /*define port A as an analog port*/ 
ADON=1;
GODONE=1;                                                     /*begin  conversion */
while(GODONE == 0);                                           /* Wait for the conversion to end*/
Current = DATA;                                               /* Save voltage in ADRESH as Voltage_Step_Number*/
 
if((Current >0) && (Voltage > 0))
{
RC1=1;                                                        /*machine ON/OFF pin*/
RC3=1;                                                       /* program running indicator LED */
RC0=RC2;                                                     /* BATTERY CHARGER INDICATOR */
 
Power=Voltage*Current;                                        /* POWER CALCULATION */
 
 
      /* analog channel setting for second sample*/
 
      /* analog channel setting for voltage*/
 
      /* AN0 analog input for voltage*/
 
ADCON0 = 0b01000000;                                                /* strat ADC */
ADCON1 = 0x00;                                                 /*define port A as an analog port*/ 
ADON=1;
GODONE=1;                                                      /*begin  conversion */
while(GODONE == 0);                                            /* Wait for the conversion to end*/
Voltage = DATA;                                                /* Save voltage in ADRESH as Voltage_Step_Number*/
 
    /* analog channel setting for current*/
 
    /* AN1 analog input for voltage*/
 
ADCON0 = 0b01010000;                                                 /* strat ADC */
ADCON1 = 0x00;                                                  /*define port A as an analog port*/ 
ADON=1;
GODONE=1;                                                       /*begin  conversion */
while(GODONE == 0);                                             /* Wait for the conversion to end*/
Current = DATA;                                                 /* Save voltage in ADRESH as Voltage_Step_Number*/
 
Power1=Voltage*Current;                                         /* N+1 POWER CALCULATION */
 
 
    /* analog channel setting for batter voltage*/
 
    /* AN2 analog input for voltage*/
 
ADCON0 = 0b01001000;                                                  /* strat ADC */
ADCON1 = 0x00;                                                   /*define port A as an analog port*/ 
ADON=1;
GODONE=1;                                                        /*begin conversion */
while(GODONE == 0);                                              /* Wait for the conversion to end*/
battery_Voltage = DATA;                                          /* Save voltage in ADRESH as Voltage_Step_Number*/
 
    /* analog channel setting for batter  current*/
 
    /* AN3 analog input for voltage*/
 
ADCON0 = 0b01011001;                                                  /* strat ADC */
ADCON1 = 0x00;                                                   /*define port A as an analog port*/ 
ADON=1;
GODONE=1;                                                        /*begin conversion */
while(GODONE == 0);                                              /* Wait for the conversion to end*/
battery_Current = DATA;                                          /* Save voltage in ADRESH as Voltage_Step_Number*/
 
 
battery_Power=battery_Voltage*battery_Current;                   /* POWER CALCULATION FOR REFERANCE */
 
                                                                 /*comparision loop for changing duty cycle*/
                                                                                                                                                                                                   /* Compare Voltage and power to previous and increment/decrement DUTY cycle*/
 
if(Power1 >Power )
{    
 DUTY++;
 DELAY();
}
 else if (Power< Power1)
{
 DUTY--;
 DELAY();
} 
 
}
else
{
RC1=0;                                                             /*machine ON/OFF pin*/
DELAY();
}    
}                                                                                                      
}
 
 
void DELAY ()                                                     /*delay program*/
{ 
unsigned char cnt = 4000;                                      // Delay Cycle to achieve  delay 
while(--cnt != 0);                                              // Delay Timing is approximately 3 usec per loop 
                                                              
}



I have uploaded my PIC16F72 code for interfacing 4 Analog channel, but ADC not reading any data from output channel, I have used 1.8MHz crystal, required 1KHz PWM with varying duty cycle, pls help me.
 
Last edited by a moderator:

Hello there,

What happended for your hardware?

If you don't mind connect one of the adc channel to power supply (0V-2.5V) and try to read it. Make sure your circuit ground is common.

Best regards,
 

Hello sir ,

thanks too much ,my ADC start working ,but still there is problem, PWM is not generating at output ,pls send me program for PWM, i want PWM with frequency 1KHz ,with varying duty cycle. my crystal frequency is 1.8 MHz.

thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top