PDA

View Full Version : 16F1827 - Timer1 and ADC Conflict?



Ioannis
- 17th March 2011, 23:55
It seems that using the ADC Timer1 stops counting (No serial data output).

Can anyone see any conflict between ADC and Timer 1?

Timer stops when any of the ADCIN commands is un-commended.



ASM
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _WRT_OFF & _PLLEN_OFF & _LVP_OFF & _STVREN_OFF & _BORV_25
ENDASM

DEFINE OSC 8
DEFINE INTHAND Tmr1Int

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h '9600 baud rate, BRGH=1
DEFINE HSER_BAUD 9600
'DEFINE HSER_SPBRG 12
DEFINE HSER_CLROERR 1

DEFINE ADC_BITS 8 ' Set number of bits in result (8, 10 or 12)
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds

OSCCON= %01110000

flag1 var byte' bank0
t_flag var flag1.0
t_counter var byte
over var word
an_1 var byte
an_2 var byte
an_3 var byte

clear

GOTO Init ' jump over interrupt routine

ASM
Tmr1Int
; retfie auto-restores w, status, bsr, fsr and pclath
bcf T1CON,TMR1ON ; stop timer 1
bcf PIR1,TMR1IF ; clear over flow flag
movlw 0xBF ; load timer for 16,535 * 8 prescaler interrupt on 16 bit timer
movwf TMR1H ; load high byte
movlw 0x69
movwf TMR1L ; load low byte
bsf T1CON,TMR1ON ; re-enable timer 1
movlw 0x01
bsf _flag1,0 ;xorwf PORTB,f ; toggle RB0
retfie ; Return from interrupt
ENDASM

Init:
APFCON0=%00100000
APFCON1=%00000001
WPUB=%11000000

BAUDCON=%00010000
PORTB = 0
TRISB = %110001000
trisa = %11111100
adcon0 = %00001001
adcon1 = %11110000
CM1CON0.7=0
CM2CON0.7=0
CPSCON0=0
CPSCON1=0
daccon0=0

ANSELA = %00011100 ' all digital. A/D disabled
ANSELB = %01000000
PIR1 = 0 ' clear TMR1 int flag
PIE1 = %00000001 ' TMR1 int enabled
INTCON = %11000000 ' global & peripheral ints enabled
T1CON = %00110001 ' TMR1 1:8 prescale, timer1 on

Main:
if t_flag then
t_counter=t_counter+1
t_flag=0
endif

' HSEROUT ["Timer overflowed ",#over," times",13,10," ",#an_1," ",#an_2," ",#an_3,13,10]


if t_counter=20 then
t_counter=0
over=over+1
HSEROUT ["Timer overflowed ",#over," times",13,10," ",#an_1," ",#an_2," ",#an_3,13,10]
endif

' ADCIN 2,AN_1
' ADCIN 3,AN_2
' ADCIN 4,AN_3

GOTO Main

END


Ioannis

Bruce
- 18th March 2011, 13:34
Insert BANKSEL T1CON in the top of your interrupt handler. If the interrupt happens during ADCIN, it's in bank1, so your writes to Timer1 registers go to the wrong registers.

Ioannis
- 18th March 2011, 17:22
Why didn't I think that?? :)

Thanks Bruce. I was sure it had to be something that simple...

Works fine now. It is an impressive chip at a very good price. But a pain to setup.

Ioannis