Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic16f72 hitech C...

Status
Not open for further replies.

koolguy

Active Member
Hi,

I am using PIC16F72 the
__delay_ms(55);
is not working showing error after removing it the code work fine for led flash/blink...?
i am using #include <htc.h> header file...
 
here is the code.....

Code:
#include <htc.h>

__CONFIG( BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS & CP_OFF );

#define _XTAL_FREQ 20000000;



void main()
    {
    TRISC = 0 ;

while(1){
PORTC=0XFF;
__delay_ms(500);
PORTC=0X00;
__delay_ms(500);

}
}
 
C:
#include <htc.h>
__CONFIG( BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS & CP_OFF );
#define _XTAL_FREQ 20000000 //; <--- This semi colon  shouldn't be here

void main()
    {
    TRISC = 0 ;

    while(1){
        PORTC=0XFF;
        __delay_ms(500);
        PORTC=0X00;
        __delay_ms(500);

        }
    }

Simples!!!
 
Hi i want to calculate delay given by this subroutine....
total 16 inst cycle taken is this right? if yes that mean 16 x .2 x10-6

Code:
DELAY MOVLW 0X01                  ;  1 inst cycle
              MOVWF count1                 ;  1 inst cycle      2
   
delay    MOVLW 0X02                     ;  1 inst cycle  2+
              MOVWF counta                    ;  1 inst cycle            
     
delay_0
             DECFSZ counta,f                 ;  2 inst cycle    2+2
             GOTO delay_0                 ;  2 inst cycle    2+
             DECFSZ count1,f                   ;  2 inst cycle   2+
             GOTO delay                        ;  2 inst cycle   2+
     
RETURN                 ;  2 inst cycle  2
 
According to data sheet MOVlW/WF take 1 inst and GOTO /CALL/RETURN/DECFSZ take 2 instruction cycle...
 
Not quite. The conditional tests take one cycle if false and two cycles if true:
upload_2013-12-11_5-27-4.png


upload_2013-12-11_5-30-13.png


John
 
yes, it is right it is taking 1 cyle on incorrect


Delay
;19 cycles
movlw 0x06
movwf d1
Delay_0
decfsz d1, f
goto Delay_0
 
Last edited:
yes, it is right it is taking 1 cyle on incorrect
Code:
Delay
                                            ;19 cycles
         movlw 0x06
         movwf d1
Delay_0
        decfsz d1, f
         goto Delay_0
        NOP                                 ;added

I am not sure what you are saying. Is your comment a question or a statement. When D1 =0 , what do you want to happen? Presumably, this delay is a subroutine, so you need a return. That return will also add two cycles. The call also takes two cycles. I agree, it is 19 cycles from movlw to the NOP that I added.
 
I am saying that you are correct as per your instruction cycle was right and it is taking DECFSZ 1 instruction when the contd is wrong.
 
That was not a right or wrong opinion. It was in the datasheet. The program counter is advanced before fetching the instruction. If the result of the instruction requires a change to the program counter, then it seems logical that another cycle is needed.

Here are some relevant quotes from the Mid-Range MCU Family Reference Manual. The datasheet for each MCU has a similar discussion about single-cycle instructions.

PC incremented before fetching instruction (page 4-5):
Internally, the program counter (PC) is incremented
every Q1, and the instruction is fetched from the program memory and latched into the
instruction register in Q4.

Pipelining (page 4-6):
An “Instruction Cycle” consists of four Q cycles (Q1, Q2, Q3, and Q4). Fetch takes one instruction
cycle while decode and execute takes another instruction cycle. However, due to Pipelining, each
instruction effectively executes in one cycle. If an instruction causes the program counter to
change (e.g. GOTO) then an extra cycle is required to complete the instruction (Example 4-1).

John
 
C:
#include <htc.h>
__CONFIG( BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS & CP_OFF );
#define _XTAL_FREQ 20000000 //; <--- This semi colon  shouldn't be here

void main()
    {
    TRISC = 0 ;

    while(1){
        PORTC=0XFF;
        __delay_ms(500);
        PORTC=0X00;
        __delay_ms(500);

        }
    }

Simples!!!

Extra semicolons are not a problem.. they do nothing.

EDIT: Sorry, that semicolon was in a define statement. That is a problem.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top