This is a code I use. Maybe it helps you. It's for HiTech C
#define max_millis 1953 //This is for a 2Mhz crystal
// 2000000 / 4 (cicles/instrucion) =3D 500000
// 500000 / 256 (8 bit timer interrupt) =3D 1953.12 interrupts
#define LED_PIN RB5 //Pin for led blink (1 sec on, 1 sec off)
#define delay1s t =3D seg2; while (t=3D=3Dseg2) //My 1 second delay
//Variable definitions as volatile
volatile unsigned int millis;
volatile unsigned char segundos;
volatile unsigned char minutos;
volatile unsigned char horas;
volatile unsigned char seg2;
//Temp variable for delay
unsigned char t;
void interrupt contador(void) {
if (millis =3D=3D max_millis) {
millis =3D 0;
if (LED_PIN)
LED_PIN =3D 0;
else
LED_PIN =3D 1;
if (segundos =3D=3D 59) {
segundos =3D 0;
LED_PIN =3D 0;
if (minutos =3D=3D 59) {
minutos =3D 0;
horas =3D horas + 1;
} else {
segundos =3D 0;
minutos =3D minutos + 1;
}
} else {
segundos++;
seg2++;
}
} else {
millis++;
}
T0IF =3D 0;
}
void main(void) {
millis =3D 0;
segundos =3D 0;
minutos =3D 0;
horas =3D 0;
seg2 =3D 0;
T0CS =3D 0; // select internal clock
T0IE =3D 1; // enable timer interrupt
GIE =3D 1; // enable global interrupts
while(1) {
//Every time I want a 1 sec delay I put:
delay1s;
//Do something
delay1s;
//And so on
}
} =
Best regards
Matias
On Mon, Feb 22, 2010 at 10:44 PM, Marcel Duchamp
wrote:
> Some random comments...
>
> 1. One of the top fundamental bits of knowledge on microcontrollers is
> how many clocks are required to execute one instruction. =A0If you are
> using pics from the PIC12, 16 or 18 families, the answer is 4. =A0This is
> in the data sheet - you should read it.
>
> So your pic (if it's one of the above pic families) takes 4 of the .279
> usec clocks - or about 1.1 Mips.
>
> 2. =A0To know what's going on, open the assembler listing your compiler
> generates - actually see what code instructions it makes.
>
> 3. I'm not a C guru but your loops will probably go faster if you
> preload and count down to zero. =A0For example, you preload count with 80
> and count up. =A0The processor will take possibly several more
> instructions to do a compare. =A0But the compare for zero is usually free;
> hitting zero, if done right, sets the Z flag so a bit test instruction
> can be used.
>
> 4. I think someone else may have mentioned to do a one second led blink
> to check your understanding of how it all works. =A0This is a good idea t=
oo.
>
> 5. You probably use some sort of IDE; these often come with cycle
> counters. =A0If you have one, use it. =A0See how many cycles it says your
> code is using. =A0Then try other code re-writes to attempt to improve it.
>
> On 2/22/2010 5:20 PM, Jason Hsu wrote:
>> Thanks for your responses.
>>
>> I tried writing a delay in C code, and the microcontroller doesn't
>> seem efficient in the counting process. =A0The microcontroller can
>> increment a variable only 80 times in 1 msec. =A0So that means that it
>> takes 12.5 usec to increment a variable just one time. =A0With a 3.57945
>> MHz crystal, a clock cycle is .279 usec. =A0So incrementing a variable
>> just one time takes 45 clock cycles. =A0I know that C isn't as efficient
>> as Assembly language, but I didn't expect it to be this inefficient.
>>
>> I understand now that delays are usually executed in Assembly code.
>> What is the proper way to insert Assembly language code into a C
>> program for PICC? =A0I recall trying to do this once but having
>> difficulty getting it to compile, so I gave up.
>>
>> My new code snippit is:
>>
>> void delay_1_msec (void)
>> =A0 =A0 =A0 {
>> =A0 =A0 =A0 count =3D 0;
>> =A0 =A0 =A0 while (count<80)
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 count++;
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>> =A0 =A0 =A0 }
>>
>> void delay_10_msec (void)
>> =A0 =A0 =A0 {
>> =A0 =A0 =A0 count_10_msec =3D 0;
>> =A0 =A0 =A0 while (count_10_msec<10)
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 delay_1_msec(); // 10 delays of 1 msec each.
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 count_10_msec++;
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>> =A0 =A0 =A0 }
>>
>> void delay_1_sec (void)
>> =A0 =A0 =A0 {
>> =A0 =A0 =A0 count_1_sec =3D 0;
>> =A0 =A0 =A0 while (count_1_sec<100)
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 delay_10_msec(); // 100 delays of 10 msec ea=
ch.
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 count_1_sec++;
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>> =A0 =A0 =A0 }
>>
>
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>
-- =
Matias Vidoni
eMail: matiasvidoni@mvcom.com.ar
MSN Messenger: matiasvidoni@mvcom.com.ar
Skype: matiasvidoni
WWW: http://www.matiasvidoni.com.ar
Google Talk: matiasvidoni@gmail.com
-- =
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist