diff options
| author | Väinö Kauppila <vaino@vke.fi> | 2026-04-19 20:16:18 +0300 |
|---|---|---|
| committer | Väinö Kauppila <vaino@vke.fi> | 2026-04-19 20:16:18 +0300 |
| commit | 29c81849a0993dcf7f9927ae2aa97839ec2f8337 (patch) | |
| tree | 97da63dd9465ca67c964dee1aaa742f0ac9f7fcc /fw/ds18b20.c | |
| parent | 46e1210381106707c2df7f07c56023eb6b760763 (diff) | |
| download | vhdl_pic-29c81849a0993dcf7f9927ae2aa97839ec2f8337.tar.gz vhdl_pic-29c81849a0993dcf7f9927ae2aa97839ec2f8337.zip | |
interrupts working now with timer
Diffstat (limited to 'fw/ds18b20.c')
| -rw-r--r-- | fw/ds18b20.c | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/fw/ds18b20.c b/fw/ds18b20.c index 0b57732..74ac1be 100644 --- a/fw/ds18b20.c +++ b/fw/ds18b20.c @@ -30,22 +30,26 @@ static const uint8_t frac_tens[16] = {0,0,1,1,2,3,3,4,5,5,6,6,7,8,8,9}; static const uint8_t frac_hund[16] = {0,6,2,8,5,1,7,4,0,6,2,8,5,1,7,3}; /* - * Timer0 delay. OPTION_REG 0x07: T0CS=0 (Fosc/4), PSA=0, PS=111 (1:256). + * Timer0 interrupt-driven delay. OPTION_REG, T0IF, T0IE, GIE from pic16f84a.h. + * OPTION_REG 0x07: T0CS=0 (Fosc/4), PSA=0, PS=111 (1:256). * At 50 MHz: overflow every 256*256 / (50M/4) = 5.24 ms. * 143 overflows ≈ 750 ms. */ -#define STATUS_RP0 0x20 -#define T0IF 0x04 +#define OVF_750MS 143 -static void wait_overflows(uint8_t n) { - INTCON &= ~T0IF; - while (n--) { - while (!(INTCON & T0IF)); - INTCON &= ~T0IF; +static volatile uint8_t ovf_count = 0; + +void isr(void) __interrupt(0) { + if (INTCON & 0x04) { /* T0IF = bit 2 */ + ovf_count++; + INTCON &= ~(uint8_t)0x04; } } -static void delay_750ms(void) { wait_overflows(143); } +static void delay_750ms(void) { + ovf_count = 0; + while (ovf_count < OVF_750MS); +} static void uart_send(uint8_t b) { while (UART_STATUS & 0x01); @@ -88,10 +92,11 @@ void main(void) { uint8_t lo, hi, sign, temp_int, frac; uint8_t i; - /* Configure Timer0: bank 1, OPTION_REG = 0x07 (Fosc/4, prescaler 1:256) */ - STATUS |= STATUS_RP0; - TMR0 = 0x07; - STATUS &= ~STATUS_RP0; + /* T0CS=0 (Fosc/4), PSA=0, PS=111 (1:256 prescaler) */ + OPTION_REG = 0x07; + + TMR0 = 0; + INTCON = 0xa0; /* T0IE | GIE */ /* enable Timer0 overflow interrupt */ uart_puts("\x1B[2J\x1B[H"); uart_puts("DS18B20 Temperature\r\n"); |
