![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
Can someone help explain what needs to be done for this problem?
Write a timing diagram, including values of the PC(program counter), for the following instruction sequence when (a) the BTFSC instruction results in a skip and (b) when it does not. Address Instruction 50 BTFSC 51 ADDLW 52 SUBLW All I know is that I need to show when each instruction Fetches and Executes, but I don't understand what the program counter is. |
|
|
|
|
|
|
(permalink) |
|
PC keeps track of which instruction should be executed next.
|
|
|
|
|
|
|
(permalink) | |
|
Quote:
|
||
|
|
|
|
|
(permalink) |
|
It contains a program byte of opcodes and operands. Opcode instructs what to do and operand say where in memory.
http://www.hyperdictionary.com/dicti...rogram+counter |
|
|
|
|
|
|
(permalink) |
|
The program counter keeps track of which instruction is next to be executed. See the PIC 16F877 data sheet, section 2.3 for a detailed description of PCL and PCLATH.
Basically, when your program runs, the next instruction's memory locaton is automatically loaded while the current instruction is being executed. Assume the program has been run up until instruction number 0049. At this point, the PC contains 0050. This lets the microcontroller know where the next instruction is that it fetches from memory to execute. So 0049 is done. The PIC asks "Where is the next instruction?" It looks at the PC, sees the value 0050, and loads whatever command is there, in this case: Code:
BTFSC So we will make two run throughs of the code. In the first case the bit is set (1), so things will flow normally. In the second case, the bit is clear (0), which will change things: Code:
Case 1, bit is set (1): 00xx Previous executes. Time unknown. PC = 0050 0050 BTFSC executes. Time = 1 cycle. PC = 0051 0051 ADDLW executes. Time = 1 cycle. PC = 0052 0052 SUBLW executes. Time = 1 cycle. PC = 0053 Case 2, bit is clear (0): 00xx Previous executes. Time unknown. PC = 0050 0050 BTFSC executes. Time = 2 cycle. PC = 0052 (1 to execute, 1 to change PC) 0052 SUBLW executes. Time = 1 cycle. PC = 0053 |
|
|
|
|
|
|
(permalink) | |
|
Quote:
The way me teacher has done it is like the following: Code:
PC: 10 11 11 11 11 12 12 40 40 41 41 41 41 41 42 11 call: F F F F E E E E addlw: F F F F return: F F F F E E E E sublw: F F F F for the following set of instructions: 10 call 40 11 addlw . . 40 return 41 sublw |
||
|
|
|
|
|
(permalink) |
|
As your timing diagram there demonstrates, the next instruction is fetched while the current instruction is executing. That's why instructions only take one cycle. 1 cycle = fetch & execute. That way when it is done executing one instruction, it is already ready to proceed to the next. When it's done with that one, the next one's ready. It's only whenever you have a jump occuring that this changes. A GOTO or a BTFSS and stuff will do that. When they are first entered and are about to execute, the following instruction is being fetched. But when the chip finds the fact that a jump is necessary, the next instruction that it has ready to go is no longer valid. It has to load the specified instruction, hence the additional cycle.
|
|
|
|
|
|
|
(permalink) |
|
Would this be correct for when the bit=1
Code:
PC: 50 50 50 50 51 51 51 51 52 52 52 52 BTFSC: F F F F E E E E ADDLW: F F F F SUBLW: F F F F |
|
|
|
|
|
|
(permalink) |
|
I don't understand your original timing diagram exactly. I am guessing you are paraphrasing what your instructor gave on the board? The PIC takes four oscillator ticks for one instruction cycle, so that is my assumption. Each of your instruction cycles should be four ticks at the top bar there, as opposed to five here, two there, etc.
|
|
|
|
|
|
|
(permalink) | |
|
Quote:
|
||
|
|
|
|
|
(permalink) | ||
|
Quote:
|
|||
|
|
|
|
|
(permalink) | |
|
Quote:
|
||
|
|
|
|
|
(permalink) |
|
Would the timing diagram for bit=0 look something like this:
Code:
PC: 50 50 50 50 51 51 51 51 52 52 52 52 BTFSC: F F F F E E E E BTFSC: F F F F E E E E SUBLW: F F F F |
|
|
|
|
|
|
(permalink) |
|
This would be better for bit = 0:
Code:
PC: 50 50 50 50 | 51 51 51 51 | 52 52 52 52 | 53 53 53 53
xx Previous: E E E E | | |
50 BTFSC: F F F F | E E E E | E E E E |
51 ADDLW: | F F F F | |
52 SUBLW: | | F F F F | E E E E
53 Following: | | | F F F F
----------------------------|-----------------|-----------------|---------------
Execute Prev. | Execute BTFSC. | Execute BTFSC. | Execute SUBLW
Fetch BTFSC. | Fetch ADDLW | ADDLW not next!!| Fetch Following
| in case it might| Perform jump by |
| be used next. | Setting PC=52. |
| | Fetch SUBLW. |
|
|
|
|
|