Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Explanation of Timer Function for PIC12F675 CSS C Compiler

Status
Not open for further replies.

Shashi0401

Newbie level 4
Joined
Apr 19, 2016
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
57
PLEASE explain the set timer function here .What exactly it is doing .Also what is this number 60535. Please HELP ME!!!


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case 0: if (int_innercount == 0)
            {
               output_low(PIN_B5);
               output_low(PIN_B6);
               output_low(PIN_B1);
               int_innercount = 1+int_innercount;
               set_timer1(60535);
            }
            else if (int_innercount == 1)
            {
               output_high(PIN_B1);
               int_innercount = 1+int_innercount;
               set_timer1(65035);
            }
            else
            {
               output_low(PIN_B1);
               int_count = 1+int_count;
               int_innercount = 0;
               set_timer1(65035);
            }
         break;

 
Last edited by a moderator:

You should give us some clues - like what processor and compiler you are using....

However, I would guess the "set_timer1(xxxx)" is a library function to put value "xxxx" in timer 1 register.

Brian.
 
Sir
I am using PIC12F675 and I am using CSS C compiler . My question is set_timer1(65035)
This function sets time for led on/ff till ** secs. I want to know what is 65035. What exactly is this number .If I want to decide on this number.
 

Right. We need to know the compiler and the processor. But some general ideas can be got from the name:

1. It may be a hardware or a software timer. It just says wait for 65000 clock cycles and then get back to me... Like a delay function but in units of clock cycles.

2. It is also possible that it just loads a count into some hardware timer registers (most have more than one and timer1 hints at that) but count down and when you reach zero call me...

3. Most micro timers are 16 bit; they can hold a max count of 2^16 -1 (about 655xx). That is the max delay that can be obtained in one step (655xx cycles) in async mode.

4. I see that you are using a variable or something called innercount; most likely you are counting a larger number of cycles and that needs this kind of loops...

Tell us more so that someone can help you more.
 
This is my code



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <16F675.h>
#fuses MCLR, INTRC_IO, NOWDT, NoPROTECT 
#use delay (clock=4000000, RESTART_WDT) 
 
int8 int_count;
 
int8 int_innercount;
 
#INT_Timer1
 
void isr()
 
{
  switch (int_count){
  case 0: if (int_innercount == 0)
            {
               output_low(PIN_B5);
               output_low(PIN_B6);
               output_low(PIN_B1);
               int_innercount = 1+int_innercount;
               set_timer1(60535);
            }
            else if (int_innercount == 1)
            {
               output_high(PIN_B1);
               int_innercount = 1+int_innercount;
               set_timer1(65035);
            }
            else
            {
               output_low(PIN_B1);
               int_count = 1+int_count;
               int_innercount = 0;
               set_timer1(65035);
            }
         break;
 
  case 1: if (int_innercount == 0)
            {
               output_low(PIN_B5);
               output_low(PIN_B6);
               output_low(PIN_B2);
               int_innercount = 1+int_innercount;
               set_timer1(60535);
            }
            else if (int_innercount == 1)
            {
               output_high(PIN_B2);
               int_innercount = 1+int_innercount;
               set_timer1(65035);
            }
            else
            {
               output_low(PIN_B2);
               int_count = 1+int_count;
               int_innercount = 0;
               set_timer1(65035);
            }
         break;
 
  case 2: if (int_innercount == 0)
            {
               output_low(PIN_B5);
               output_low(PIN_B6);
               output_low(PIN_B3);
               int_innercount = 1+int_innercount;
               set_timer1(60535);
            }
            else if (int_innercount == 1)
            {
               output_high(PIN_B3);
               int_innercount = 1+int_innercount;
               set_timer1(65035);
            }
            else
            {
               output_low(PIN_B3);
               int_count = 1+int_count;
               int_innercount = 0;
               set_timer1(65035);
            }
         break;
 
  case 3: if (int_innercount == 0)
            {
               output_high(PIN_B5);
               output_high(PIN_B6);
               output_low(PIN_B4);
               int_innercount = 1+int_innercount;
               set_timer1(60535);
            }
            else if (int_innercount == 1)
            {
               output_high(PIN_B4);
               int_innercount = 1+int_innercount;
               set_timer1(65035);
            }
            else
            {
               output_low(PIN_B4);
               int_count = 0;
               int_innercount = 0;
               set_timer1(65035);
            }
         break;                 
 
  default: output_low (PIN_B5);
           output_low (PIN_B6);
           break;
           }
}
 
 
void main()
{
set_tris_b(0x00);
int_count = 0;
int_innercount = 0;
setup_timer_1 (T1_INTERNAL);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
for( ; ;)
   {
   }
 
}

 
Last edited by a moderator:

Just looking at your main, I can see that setup_timer_1 is a system or library routine. You need to look up the corresponding codes for the processor 16F675 (PIC processor?) and the respective help files. At the same time look for set_timer1 library call too. If you cannot, please tell me and I shall try to look up.
 

65535 - 65035 = 500
If time base is 1us, it will give 0.5mS delay.

You have great eyes!

Right, the crystal is 4 MHz (line 3 in the code) and the actual clock will be 4 periods or 1 us. The timer is set for count up and makes an interrupt on overflow.
 
How did you arrive to conclusion that actual clock will be 4 periods or 1 us .Kindly explain this .Also please explain the code . I am very naive in coding and new to micro controllers.
 

65535 - 65035 = 500
If time base is 1us, it will give 0.5mS delay.

I spotted that too but the number in the first instance of each case statement is 60535, perhaps that is a 5000 count delay.

Shashi0401, the Timer 1 counter increases in value at timed intervals decided by the clock frequency and the prescaler register. when the count reaches maximum (65535) the next increase rolls it back to zero and at the same time generates an interrupt. The interrupt is what calls the ISR code. By loading a starting value into the timer, you reduce the number of counts it has to make before reaching 65535 so you can change how long it will take. In other words the number decides how long a delay it will create.

The burning question is "What is a 16F675 ?"
It isn't listed on Microchip's web site and I can't find any assembler/compiler support for it. Should the program start with "#include <12F675.h>" ?

Brian.
 

I am sorry , Its PIC12F675 .I am sorry for typo .
Thank you all . With this I think can proceed with my course project.
 

How did you arrive to conclusion that actual clock will be 4 periods or 1 us

This is almost standard; four cycles of the actual external oscillator make one usable clock cycle for the microprocessor. The reasons for this is shrouded in mystery but if you see the block diagrams in the manual, you will see that. It actually derives some phase information from the external oscillator but I do not know the details. Therefore if you are using 16MHz crystal, the microprocessor clock will run at 4MHz. The same applies to the intel (desktop and laptop) processors too.

Also please explain the code . I am very naive in coding and new to micro controllers.

The code is actually simple; first take a look at the main program- it does some housekeeping and enters an infinite loop:
Code:
 for( ; ;)  {  }
- do you understand what this line of code does?

Basically the main does noting executable. The program is essentially driven by interrupts. - I guess you have some basic ideas about interrupts and how they work.

The line void isr() appears to be the interrupt service routine; unfortunately I cannot locate the appropriate return from this (return from interrupt) without which the program will not compile happily. Return from interrupts are similar to returns from subroutines (but they do different housekeeping jobs).

Within this isr, we have a switch case statement with four branches and one default. Look at the default case first: it just turns two pins low (digital output must have been configured for these two pins elsewhere) and that is all. If int_count is 0||1||2||3, it sets some pins high or low and increments another variable. Most interesting is that it resets the timer to a new value.

- - - Updated - - -

the Timer 1 counter increases in value at timed intervals decided by the clock frequency and the prescaler register.

I forgot about the prescaler.
 

The line void isr() appears to be the interrupt service routine; unfortunately I cannot locate the appropriate return from this (return from interrupt) without which the program will not compile happily. Return from interrupts are similar to returns from subroutines (but they do different housekeeping jobs).

I would make a guess that CCS reserve the name "isr" for the interrupt routine and insert the necessary "retfie" code automatically at the end. I've never used it so I can't be sure.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top