Hi
When i call the ReadADC() function, controller will restarted. what is the reason?
im using Hitech c compiler and MPLABX software.
Code:
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
#include<htc.h>
#include "adc.h"
__CONFIG( 0x20A4 ) ;
__CONFIG( 0X0700 ) ;
unsigned int ReadADC( ) ;
unsigned int ReadADC1( ) ;
void InitADC ( ) ;
unsigned char ucMenu_flag= 0 ;
unsigned int uiHigh_volt= 0 , uilow_volt= 0 , uiDR_Amps= 0 , uiON_Time= 0 ;
void main( )
{
unsigned int value, ivalue= 0 , Display= 0 ;
TRISA= 0x03 ;
PORTA= 0x00 ;
TRISB= 0x00 ;
PORTB= 0x00 ;
TRISC= 0x01 ;
PORTC= 0x00 ;
ANSEL = 0x03 ; /* AN0,AN1 as analog pins */
ANSELH = 0x00 ;
OSCCON = 0b01101101 ; /*Device runing External Osc*/
OSCTUNE = 0x00 ;
/* Initialize the ADC */
InitADC( 0 ) ;
GIE= 1 ;
PEIE= 1 ;
while ( 1 )
{
for ( value= 0 ; value< 60000 ; value++ ) ; //Delay
ivalue = ReadADC( ) ;
for ( value= 0 ; value< 10000 ; value++ ) ;
Display = ReadADC1( ) ;
Lcd_Decimal( 2 , 13 , 3 , Display) ;
for ( value= 0 ; value< 20000 ; value++ ) ;
}
}
unsigned int ReadADC( )
{
unsigned int ADC_VALUE= 0 ;
// ADCON1=0b10000000;
ADCON0= 0x43 ;
ADIE = 1 ; /* Masking the interrupt */
ADIF = 0 ; /* Resetting the ADC interupt bit */
ADRESL = 0 ; /* Resetting the ADRES value register */
ADRESH = 0 ;
while ( ! ADIF) ; /* Wait for conversion complete */
ADC_VALUE = ( ADRESH* 256 ) + ADRESL;
return ( ADC_VALUE) ; /* Return the value of the ADC process */
}
unsigned int ReadADC1( )
{
unsigned int ADC1_VALUE= 0 ;
// ADCON1=0b10000000;
ADCON0= 0x43 ;
ADIE = 1 ; /* Masking the interrupt */
ADIF = 0 ; /* Resetting the ADC interupt bit */
ADRESL = 0 ; /* Resetting the ADRES value register */
ADRESH = 0 ;
while ( ! ADIF) ; /* Wait for conversion complete */
ADC1_VALUE = ( ADRESH* 256 ) + ADRESL;
return ( ADC1_VALUE) ; /* Return the value of the ADC process */
}
void InitADC ( )
{
ADCON1= 0b10000000 ;
}
Last edited by a moderator: Jun 16, 2014