CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC18F65J10 delay function problem in MPLAB

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
baybarsk



Joined: 06 Mar 2010
Posts: 3

View user's profile Send private message

PIC18F65J10 delay function problem in MPLAB
PostPosted: Sat Mar 06, 2010 5:42 am     Reply with quote

Hello,
I have some difficulties with using delay function.
I used PIC18F65J10 and 8MHZ crystal oscillator. Connection of crystal is like in this link:
http://www.stanford.edu/class/ee281/projects/aut2001/avrtetris/hardware_files/image008.jpg
Depend on code, led on Port A, 4th bit, must toggle every 500 ms. However, it toggles after 8 times more time. It takes about 4 seconds to toggle. I used
Code:

#use delay (clock=8000000)

But as I said, it toggles about 4 second. When i use
Code:

#use delay (clock=1000000)

even though its connected 8Mhz crystal, it toggles about right time.
I used different times, about 100ms, 1000ms and result not different.

I am using PCH v4.093, and as an editor MPLAB. I am not using CCS's editor. I compile code in MPLAB.

What can be the problem that cause delay function can't work right? I am thinking maybe because of MPLAB. In MPLAB, checkbox is checked, that says "configuration bits is set in code" .

Thanks

Code is below
Code:

#include <18F65J10.h>
#device adc=8

#FUSES NOWDT
#FUSES HS                      //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NODEBUG                   
#FUSES NOPROTECT                //Code not protected from reading
#use delay (clock=8000000)      
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);
   setup_spi2(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   disable_interrupts(INT_TIMER1);
   disable_interrupts(GLOBAL);
   
 do{
   output_toggle(pin_a4);
   delay_ms(500);
   }while(TRUE);
   
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 06, 2010 11:05 am     Reply with quote

How are testing this ? Are you testing it on a hardware board, or in
the MPLAB simulator ?
Guest








PostPosted: Sat Mar 06, 2010 2:22 pm     Reply with quote

I used DEBUG, also I used MPLAB in programmer mode, but in both result is same. I didn't use mplab sim.

It looks me strange. Its so simple program. I face. First I thought its because I use 18F65J10 first time and don't know well. But as I read till now, what I did is correct.
Guest








PostPosted: Sat Mar 06, 2010 2:23 pm     Reply with quote

Sorry I forget indicate, I used hardware debug, and programmer.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 06, 2010 3:20 pm     Reply with quote

Have you tried that crystal (and capacitors) on any other PIC ?
I mean, a different PIC than an 18F65J10. Try that and see if
the other PIC runs at the correct speed.
baybarsk



Joined: 06 Mar 2010
Posts: 3

View user's profile Send private message

PostPosted: Tue Mar 09, 2010 7:25 am     Reply with quote

I am sorry, Guest was me,

I looked OSC pins with 100MHz oscilloscope, and there is a clean 8MHz.

I looked how my C code converted to assembly,

here code
Code:

1:                 #include <18F65J10.h>
  0000    EF16     GOTO 0x2c
2:                 #device adc=8
3:                 
4:                 #FUSES NOWDT
5:                 #FUSES HS                      //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
6:                 #FUSES NODEBUG                   
7:                 #FUSES NOPROTECT                //Code not protected from reading
8:                 #use delay (clock=8000000)      
  0004    6AEA     CLRF 0xfea, ACCESS
  0006    0E07     MOVLW 0x7
  0008    6EE9     MOVWF 0xfe9, ACCESS
  000A    50EF     MOVF 0xfef, W, ACCESS
  000C    E00D     BZ 0x28
  000E    0E02     MOVLW 0x2
  0010    6E01     MOVWF 0x1, ACCESS
  0012    6A00     CLRF 0, ACCESS
  0014    2E00     DECFSZ 0, F, ACCESS
  0016    D7FE     BRA 0x14
  0018    2E01     DECFSZ 0x1, F, ACCESS
  001A    D7FB     BRA 0x12
  001C    0E97     MOVLW 0x97
  001E    6E00     MOVWF 0, ACCESS
  0020    2E00     DECFSZ 0, F, ACCESS
  0022    D7FE     BRA 0x20
  0024    2EEF     DECFSZ 0xfef, F, ACCESS
  0026    D7F3     BRA 0xe
  0028    EF5E     GOTO 0xbc
9:                 #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
10:               
11:               
12:               
13:                void main()
14:               
15:                {
  002C    6AF8     CLRF 0xff8, ACCESS
  002E    9ED0     BCF 0xfd0, 0x7, ACCESS
  0030    6AEA     CLRF 0xfea, ACCESS
  0032    6AE9     CLRF 0xfe9, ACCESS
  0034    967E     BCF 0xf7e, 0x3, ACCESS
  0036    0E0C     MOVLW 0xc
  0038    6EAF     MOVWF 0xfaf, ACCESS
  003A    0EA2     MOVLW 0xa2
  003C    6EAC     MOVWF 0xfac, ACCESS
  003E    0E90     MOVLW 0x90
  0040    6EAB     MOVWF 0xfab, ACCESS
  0042    50C1     MOVF 0xfc1, W, ACCESS
  0044    0BC0     ANDLW 0xc0
  0046    6EC1     MOVWF 0xfc1, ACCESS
  0048    0E07     MOVLW 0x7
  004A    6EB4     MOVWF 0xfb4, ACCESS
16:                   setup_adc_ports(NO_ANALOGS|VSS_VDD);
  004C    50C1     MOVF 0xfc1, W, ACCESS
  004E    0BC0     ANDLW 0xc0
  0050    090F     IORLW 0xf
  0052    6EC1     MOVWF 0xfc1, ACCESS
17:                   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
  0054    90C2     BCF 0xfc2, 0, ACCESS
18:                   setup_psp(PSP_DISABLED);
  0056    98B0     BCF 0xfb0, 0x4, ACCESS
19:                   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);
  0058    9AC6     BCF 0xfc6, 0x5, ACCESS
  005A    9A94     BCF 0xf94, 0x5, ACCESS
  005C    8894     BSF 0xf94, 0x4, ACCESS
  005E    9694     BCF 0xf94, 0x3, ACCESS
  0060    0E20     MOVLW 0x20
  0062    6EC6     MOVWF 0xfc6, ACCESS
  0064    0E40     MOVLW 0x40
  0066    6EC7     MOVWF 0xfc7, ACCESS
20:                   setup_spi2(SPI_SS_DISABLED);
  0068    9A63     BCF 0xf63, 0x5, ACCESS
  006A    9895     BCF 0xf95, 0x4, ACCESS
  006C    8A95     BSF 0xf95, 0x5, ACCESS
  006E    9C95     BCF 0xf95, 0x6, ACCESS
  0070    0E01     MOVLW 0x1
  0072    6E63     MOVWF 0xf63, ACCESS
  0074    0E00     MOVLW 0
  0076    6E64     MOVWF 0xf64, ACCESS
21:                   setup_timer_0(RTCC_INTERNAL);
  0078    0E80     MOVLW 0x80
  007A    6ED5     MOVWF 0xfd5, ACCESS
22:                   setup_timer_1(T1_DISABLED);
  007C    6ACD     CLRF 0xfcd, ACCESS
23:                   setup_timer_2(T2_DISABLED,0,1);
  007E    0E00     MOVLW 0
  0080    6ECA     MOVWF 0xfca, ACCESS
  0082    0E00     MOVLW 0
  0084    6ECB     MOVWF 0xfcb, ACCESS
24:                   setup_timer_4(T4_DISABLED,0,1);
  0086    6E76     MOVWF 0xf76, ACCESS
  0088    0E00     MOVLW 0
  008A    6E77     MOVWF 0xf77, ACCESS
25:                   setup_comparator(NC_NC_NC_NC);
  008C    0E07     MOVLW 0x7
  008E    6EB4     MOVWF 0xfb4, ACCESS
  0090    5097     MOVF 0xf97, W, ACCESS
  0092    6E97     MOVWF 0xf97, ACCESS
  0094    0E06     MOVLW 0x6
  0096    6E00     MOVWF 0, ACCESS
  0098    2E00     DECFSZ 0, F, ACCESS
  009A    D7FE     BRA 0x98
  009C    0000     NOP
  009E    50B4     MOVF 0xfb4, W, ACCESS
  00A0    9CA1     BCF 0xfa1, 0x6, ACCESS
26:                   setup_vref(FALSE);
  00A2    6AB5     CLRF 0xfb5, ACCESS
27:                   disable_interrupts(INT_TIMER1);
  00A4    909D     BCF 0xf9d, 0, ACCESS
28:                   disable_interrupts(GLOBAL);
  00A6    9CF2     BCF 0xff2, 0x6, ACCESS
  00A8    9EF2     BCF 0xff2, 0x7, ACCESS
  00AA    BEF2     BTFSC 0xff2, 0x7, ACCESS
  00AC    D7FD     BRA 0xa8
29:                   
30:                   
31:                 do{
32:                 
33:                   output_toggle(pin_a4);
  00AE    9892     BCF 0xf92, 0x4, ACCESS
  00B0    7889     BTG 0xf89, 0x4, ACCESS
34:                   delay_ms(500);
  00B2    0E02     MOVLW 0x2
  00B4    6E06     MOVWF 0x6, ACCESS
  00B6    0EFA     MOVLW 0xfa
  00B8    6E07     MOVWF 0x7, ACCESS
  00BA    D7A4     BRA 0x4
  00BC    2E06     DECFSZ 0x6, F, ACCESS
  00BE    D7FB     BRA 0xb6
35:                   
36:                   }while(TRUE);
37:                   
38:                }
  00C0    D7F6     BRA 0xae
39:                   
40:                   
41:                 
42:                   
43:                   
44:                   
45:                   
46:                   
  00C2    0003     SLEEP



I cant understand what happened Question Question Question
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 09, 2010 11:42 am     Reply with quote

Try a more simple program. This should turn on an LED briefly for
100 ms every 1 second. It should be easier to see the true blinking rate.

Also:
Have you tried testing a different type of PIC with the same crystal
and capacitors ?
Code:

#include <18F65J10.h>
#fuses HS, NOWDT
#use delay(clock=8000000)       

//==================================
void main()
{
   
while(1)
  {
   output_high(PIN_A4);
   delay_ms(100);
   output_low(PIN_A4);
   delay_ms(900);
  }
   
}
baybarsk



Joined: 06 Mar 2010
Posts: 3

View user's profile Send private message

PostPosted: Wed Mar 10, 2010 9:07 am     Reply with quote

Thanks PCM programmer, i tried your code,
but result still same. Sad


I couldn't try crystal on other kind of PIC yet, but i will and then i'll write results.

But i can see 8Mhz sinus wave on OSC1 and OSC2 pins of PIC, with Oscilloscope.

I also deleted
Code:
#fuses HS, NOWDT
line,



and config bits from MPLAB menu, i tried HS, HS+PLL, but in both result same. I cant understand why its 8 times slower?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 10, 2010 1:56 pm     Reply with quote

I'm not sure what would cause the PIC to run 8x slower.

Did you build this board yourself, or was it built by a company ?
If it came from a factory, then post the manufacturer and part number
of the board. If possible, post a link to the web page for it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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