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

CCS C + PIC18F26K22 + Timer

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 20, 2011 6:01 pm     Reply with quote

Quote:
int8 milliseconds = 0;

if(milliseconds == 1000)

What's wrong here ? Data types, data types, data types.

Download the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look on page 43 (page 57 in the Acrobat reader). Look at this section:
Quote:
Basic and Special types

Note the range of each data type.
Wilksey



Joined: 14 Aug 2011
Posts: 36
Location: Somerset, UK

View user's profile Send private message

PostPosted: Sat Aug 20, 2011 6:40 pm     Reply with quote

Hmmm...I see what you're saying...i'm an idiot!!

So, I changed it to int16, and it still isn't giving me quite what I need.

The timer2 code still doesn't work, in fact it doesn't appear to fire at all!

If I remove the RTCC_8_BIT from timer0 code, I have to set RTCC_DIV_1 to get a 4/5 ms delay, but can't seem to acheive 1ms still.

Wilksey
Ttelmah



Joined: 11 Mar 2010
Posts: 19233

View user's profile Send private message

PostPosted: Sun Aug 21, 2011 2:27 am     Reply with quote

One thing I notice, you have the comment 'Master Clear pin used for I/O', but have the fuse 'MCLR' which means the master clear pin is enabled, and you must have a pull-up on the master clear pin.

I'd add:
setup_oscillator(OSC_64MHz);

as the first line of the main code. Some compiler versions don't turn the software PLL on, without this.

Don't enable the interrupts, until you have set them up. Won't matter really (just risks an extra trigger), but 'bad practice'.

One thing I notice, is you don't have the NOPBADEN fuse, which means port B will wake up as analog by default.

I rewrote the code to generate a 5 second time using a 1mSec tick on Timer2, as:
Code:

#include <18F25K22.h>
#FUSES NOWDT, WDT128, INTRC_IO, NOFCMEN, NOIESO, BORV42, WDT_NOSLEEP, NOPBADEN, NOLPT1OSC, NOHFOFST, NOMCLR, NOLVP, NOXINST
#use delay(clock=64000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1,errors)

int16 milliseconds=5000;
#int_timer2
void tick(void) {
   if (milliseconds) --milliseconds;
}

void main()
{
   setup_oscillator(OSC_64MHZ);
   setup_timer_4(T4_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_timer_2(T2_DIV_BY_16,99,10);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(GLOBAL);
   do {
      if (milliseconds==0) {
         milliseconds=5000;
         printf("5 seconds\n\r");
      }
   } while(TRUE);
}

and it merrily displays '5 seconds' on the serial, every 5 seconds exactly.

Using the 'countdown', makes it impossible for the value to be missed if the main loop takes too long (doesn't apply here).

Best Wishes
Wilksey



Joined: 14 Aug 2011
Posts: 36
Location: Somerset, UK

View user's profile Send private message

PostPosted: Sun Aug 21, 2011 5:07 pm     Reply with quote

Hi Ttelmah,

Thank you for your reply, I will check your code and see what it does on my board.

Can the code count up instead of down?

I tried adding the setup_oscillator to 64MHZ, but doesn't make any difference, both UARTS i have tried work fine, so im guessing the crystal setting is ok as if it wasn't i'd get garbage.

MCLR is pulled to +5V with a 10k pull up.

I dont think portb pullups are an issue, but I will add that fuse setting in for good practice!

Although, doesn't this override it?
Code:

setup_adc(ADC_OFF | NO_ANALOGS);


Wilksey
Wilksey



Joined: 14 Aug 2011
Posts: 36
Location: Somerset, UK

View user's profile Send private message

PostPosted: Sun Aug 21, 2011 6:08 pm     Reply with quote

Hi,

Just tried your code, changed the header to the 26K22, had to remove one of the fuses, cause it didn't exist on my chip, it just sits there and does nothing, I have tested the UART and it works ok, but your code just sits and does nothing?

Wilksey
Ttelmah



Joined: 11 Mar 2010
Posts: 19233

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 1:47 am     Reply with quote

What compiler version have you got?.
The fuses should be right. The 25K, and the 26K are identical, except the 26K has more memory. The code ran exactly as it is on a 25K, and compiles with no changes at all to run on a 26K. I wonder if you have an early compiler for the 26K, which has problems setting the chip up....

Best Wishes
Wilksey



Joined: 14 Aug 2011
Posts: 36
Location: Somerset, UK

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 10:08 am     Reply with quote

It was this fuse that caused errors:
NOLPT1OSC

I use 4.120

Thanks

Wilksey
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 1:47 pm     Reply with quote

I was able to make Ttelmah's code work with vs. 4.120 with the following
changes (in various places below). This is the program output:
Quote:

Start:
5 seconds
5 seconds
5 seconds
5 seconds

Here is the test program:
Code:

#include <18F45K22.h>
#FUSES INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN, NOHFOFST
#use delay(clock=64M)
#use rs232(baud=9600, UART1, ERRORS)

#byte OSCCON2 = getenv("SFR:OSCCON2")
#bit PLLRDY = OSCCON2.7

int16 milliseconds=5000;

#int_timer2
void tick(void)
{
if(milliseconds)
   --milliseconds;
}

//=========================
void main()
{
while(!PLLRDY);  // Wait until 64 MHz osc is running OK

printf("\n\rStart: \n\r");

setup_timer_2(T2_DIV_BY_16,99,10);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

do{
   if(milliseconds==0)
     {
      milliseconds=5000;
      printf("5 seconds\n\r");
     }
   }while(TRUE);

}

Note that testing the PLLRDY bit at the start of main() was the only way
I could get the program to display "Start" correctly at the beginning
of the program. I tried other methods and they didn't work.
Wilksey



Joined: 14 Aug 2011
Posts: 36
Location: Somerset, UK

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 4:00 pm     Reply with quote

Thanks PCM

I will try that code on my board tomorrow!

Thanks to both of you for all of your patience and help so far!!

Wilksey
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