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
| #include<htc.h>
#include"lcd_fns_4bit_hitech_roll.h"
#define _XTAL_FREQ 20000000
#pragma config PBADEN = OFF
#pragma config FOSC = HS
#pragma config BOR = OFF
#pragma config WDT = OFF
unsigned int count=0;
//-----------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
void init_interrupt_ports()
{
PEIE=1;
GIE=1;
INT0IE=1;
INT1IE=1;
ei();
TRISB=255;
PORTB=0;
TRISC=0;
PORTC=0;
}
//-----------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
void tmr1_start (unsigned char sampling_freq)
{
//--------------------------------
//note : max sampling time (timer) = 520 ms => min sampling freq = 2
//max sampling time (unsigned char) = 255 => min sampling freq = 4
//--------------------------------
unsigned char sampling_time_ms;
unsigned int set_value;
float req_value;
T1CON=0b00110001;
sampling_time_ms = (1000/sampling_freq);
//--------------------------------
//fosc:20mhz ---fosc/4---> 5mhz ---1:8prescaler---> 625khz ==> time of a single colck tick = 1000/750000 = 1.6*(10^-3)msec
//1.3*(10^-3)msec * (2^16-1(no of tmr1 ticks)) = 87ms(timer max capacity)
//--------------------------------
//to determine what value to set in the timers regs to count a certain time
//max value = 2^16-1 = 65535
//required value = sampling_time_ms/(8*10^-3)
//set_value = max value - required valuue
//--------------------------------
req_value = ((sampling_time_ms*1000) / (1.6));
set_value = (int)(((2^16)-1)- req_value);
TMR1L=set_value;
TMR1H=(set_value>>8);
}
//-----------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
void interrupt ISR(void)
{
if(INT0IF==1)
{
count++;
INT0IF=0;
}
}
//-----------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
void main()
{
init_lcd();
init_interrupt_ports();
tmr1_start(20);
while(1)
{
if(TMR1IF==1)
{
TMR1IF=0;
gotorc(0,0);
show_number(count);
tmr1_start(20);
}
}
} |
this is the code i don't know where is the problem the intif is always zero never goes high
Last edited by a moderator: