View previous topic :: View next topic |
Author |
Message |
exodia505
Joined: 07 Feb 2012 Posts: 34
|
Pic18f4550 Adc problem |
Posted: Tue Mar 13, 2012 6:25 am |
|
|
Hello evrybody!!!
i would like to learn 4 adc values with pic18f4550 , but when i receive them , i dont receive int values , i just receive some chars
i want to know , where is the problem!!
#include <18F4550.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES MCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES XINST //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL12 //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4 //System Clock by 4
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
#FUSES ICPRT //ICPRT enabled
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int a=0;
int val,val1,val2,val3;
#int_TIMER2
void TIMER2_isr(void)
{
switch(a)
{
case 0: set_adc_channel(0);
delay_us(20);
val=read_adc();
a=a+1;
break;
case 1: set_adc_channel(1);
delay_us(20);
val1=read_adc();
a=a+1;
break;
case 2:set_adc_channel(2);
delay_us(20);
val2=read_adc();
a=a+1;
break;
case 3:set_adc_channel(3);
delay_us(20);
val3=read_adc();
a=0;
break;
}
}
void main()
{
setup_adc_ports(AN0_TO_AN4|VSS_VDD);
setup_adc(ADC_CLOCK_Div_8);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,60,5);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(1){
printf("%2u %2u %2u %2u \n\r",val,val1,val2,val3);
}
} _________________ NOway!!
Last edited by exodia505 on Tue Mar 13, 2012 7:03 am; edited 1 time in total |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Tue Mar 13, 2012 6:58 am |
|
|
Problem starts with no indentation in the code and no comments so people know what you intended to do (including yourself). First thing I see is no "break" in case 2 however, that isn't going to kill the whole program. How do you even know your interrupts are running as expected ? If your code actually does have formatting, then see #5 in http://www.ccsinfo.com/forum/viewtopic.php?t=26245 for posting your example code. _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Tue Mar 13, 2012 7:38 am |
|
|
First thing, get rid of these two fuses;
XINST and LVP
They will cause problems. Extended instruction set is not supported and Low
Voltage programming is not being used.
You also want to change NOPUT to PUT.
You need to get rid of the following fuses as well:
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOCPD //No EE protection
#FUSES PLL12 //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4 //System Clock by 4
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
This will simplify things a lot... _________________ Google and Forum Search are some of your best tools!!!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9215 Location: Greensville,Ontario
|
|
Posted: Tue Mar 13, 2012 7:57 am |
|
|
also always add 'errors' to the use rs232(....) statement when using the hardware UART. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Tue Mar 13, 2012 8:04 am |
|
|
These lines are trying to prinf() multiple times per second which you do not need.
Code: | while(1){
printf("%2u %2u %2u %2u \n\r",val,val1,val2,val3);
}
|
You are also trying to put 10 bit ADC values into an 8 bit integer variable.
INT defaults to 8 BIT in this compiler. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Mar 13, 2012 9:24 am |
|
|
instead of setting the ADC channel - THEN adding a delay - THEN reading
( which flirts with the MINIMUM ACQ time)
WHY NOT set the NEXT Channel you will read AFTER taking a reading on the current selected input- and THEN when you come back on the timer INT - just READ the LAST channel that was set to acquire ??
try this
Code: |
// in your INIT code
STATIC int a=0;
STATIC int16 val[4];
set_adc_channel(a);
// ....
#int_TIMER2
void TIMER2_isr(void) {
val[a]=read_adc();
a++;
if (4==a) a=0;
set_adc_channel(a);
}
|
Learn to THINK in the time domain and all your PIC code will get better
Last edited by asmboy on Tue Mar 13, 2012 4:16 pm; edited 1 time in total |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Mar 13, 2012 11:52 am |
|
|
oh yes
add "ERRORS" to your #USE RS232 directive |
|
|
|