|
|
View previous topic :: View next topic |
Author |
Message |
seme1
Joined: 13 Jun 2009 Posts: 21
|
Timer on PIC18F46K20 |
Posted: Sat Jun 13, 2009 8:47 am |
|
|
PIC18F46k20
PCW 4.078
Code: |
#include <18F46K20.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES LP //Low power osc < 200 khz
#FUSES H4
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#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 NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#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 LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
void main()
{
unsigned int8 fd =0,sd=0,td=0;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
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_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
output_high(PIN_A1);
// implementing the delay ... seems like the code gets stock here for some reason and never reaches the output_high(PIN_A0);
for( fd=0;fd<20;fd++){
for (sd=0;sd<20;sd++){
for (td=0;td<20;td++){ }
}
}
output_high(PIN_A0);
while(TRUE){
for( fd=0;fd<200;fd++){
for (sd=0;sd<200;sd++){}
}
output_toggle(PIN_A0);
}
}
|
When running the above code, it doesn't reach output_high(PIN_A0); and it seems like it's stuck in the for loops for some reason.
I tried the following (having two inner for loops instead of three)
Code: | for( fd=0;fd<20;fd++){
for (sd=0;sd<20;sd++){
}
} |
and it reached the line output_high(PIN_A0); and executed it. However, there is never a delay inside the while loop (it is supposed to keep toggling A0 from low to high, and the opposite, to give a flashing effect to an LED)
Anybody has any idea what am I doing wrong here? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jun 13, 2009 9:10 am |
|
|
Quote: |
#FUSES LP //Low power osc < 200 khz
#FUSES H4 |
Don't use multiple oscillator fuses. Just use one of them.
Quote: | output_high(PIN_A0);
for( fd=0;fd<20;fd++){
for (sd=0;sd<20;sd++){
for (td=0;td<20;td++){ }
}
}
output_high(PIN_A0); |
Don't do this. Use the delay_us() and delay_ms() functions that are built
into the compiler. |
|
|
seme1
Joined: 13 Jun 2009 Posts: 21
|
|
Posted: Sat Jun 13, 2009 11:54 am |
|
|
Thanks a lot for your help
Now, I have the following:
Code: |
#include <18F46K20.h>
#device adc=8
#fuses H4, NOWDT, PUT ,BROWNOUT, NOLVP
#use delay(clock=16000000)
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
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_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
while(TRUE){
output_high(PIN_A1);
delay_ms(500);
output_high(PIN_A0);
delay_ms(500);
output_low(PIN_A1);
delay_ms(500);
output_low(PIN_A0);
delay_ms(500);
}
}
|
I am using the following to connect Pins 13 and 14 (CLKI and CLKO) on the PIC.
http://img5.imageshack.us/img5/9572/dsc09503w.jpg
The effect I see when running it on the PIC is that A1 stays ON all the time (no flashing effect) and A0 stays off with no blinking, too.
Removing the quartz crystal while the circuit is connected to power causes A1 to go OFF, putting it back makes A1 go ON again.
Any help for the newbie here is very much appreciated. |
|
|
Ttelmah Guest
|
|
Posted: Sat Jun 13, 2009 3:08 pm |
|
|
You need the NOXINST fuse back.
However the behaviour, sounds as if your oscillator is not actually running. The way you talk about 'removing the crystal', suggests you are using something like a plug in breadboard?. If so, be aware that the capacitance of these can be enought to stop the oscillator running on some versions....
Best Wishes |
|
|
seme1
Joined: 13 Jun 2009 Posts: 21
|
|
Posted: Sat Jun 13, 2009 5:56 pm |
|
|
I have tried the following, and the result is that A0 and A1 are always HIGH !! (there is no flashing effect at all)
Code: |
#include <18F46K20.h>
#device adc=8
#fuses INTRC, NOWDT, PUT ,BROWNOUT, NOLVP
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=4000000)
void main()
{
while(TRUE){
output_high(PIN_A1);
output_high(PIN_A0);
delay_ms(70);
output_low(PIN_A1);
delay_ms(70);
output_low(PIN_A0);
delay_ms(70);
}
}
|
My understanding is that this does not require the external quartz crystal to work. When running the above, removing the quartz crystal does not cause A1 and A0 to go down as what happened previously with the fuse H4.
Any clues what could be causing this ?? |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|