PDA

View Full Version : A/D and counter can be read together?



edysan
- 18th October 2005, 20:15
Ciao. I am verry novice, and maybe I do some mistake to write (sorry!)

My intention are to read an A/D potentiometer and a external counter (I think internal if possible), 5 time on second at the same intervals. The counter are working from 12 ticks/sec to 275 ticks/sec. At every 1/5 sec I need to read the A/D potentiometer value and how many tiks from the last read.

I try with "ON Interrupt", but I can not find the right way. I have only compilation error.

I think also to use an external potentiometer (like DS1267), and a external counter (this I dont know what type) and to use the Pic only for read the value on interrupt.

This is my code but till now only for A/D potentiometer and interrupt.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ device pic16F876A, xt_osc, wdt_off, pwrt_off, bod_off, lvp_off, cpd_off, wrt_off, protect_off

DEFINE ONINT_USED 1 'If using ISP
DEFINE OSC 4 'Adjust to suit your design
DEFINE INTHAND Clock_Int ' Point to the Assembler interrupt handling routine

ADCON1 = 6 'SET RA.0 TO ANALOG INPUT, Vref = Vdd, ALL ELSE = DIGITAL I/O

INCLUDE "modedefs.bas"


'***************** PIC PIN ASSIGNMENTS *****************

'Serial Pins
out_pin VAR PORTC.6 'Serial OUT pin
in_pin VAR PORTC.7 'Serial IN pin
ser_baud CON 12 '19200 Baud

led VAR PORTB.7

INCLUDE "2k_Int.Inc" ' Load the Interrupt register context saving macros

TRISA = %00000001
' Allocate variables
x VAR BYTE
x1 VAR BYTE


' ** Declare the Variables **
Hours VAR BYTE ' Holds the hours value (0-23)
Minutes VAR BYTE ' Holds the minutes value (0-59)
Seconds VAR BYTE ' Holds the seconds value (0-59)
Ticks VAR BYTE ' Holds the pieces of seconds value (0-59)
U_Flag VAR BYTE ' Indicator to allow the update of the display

' ** Declare the bits and flags of the various registers **

T0IE VAR INTCON.5 ' Timer0 Overflow Interrupt Enable
T0IF VAR INTCON.2 ' Timer0 Overflow Interrupt Flag
GIE VAR INTCON.7 ' Global Interrupt Enable
PS0 VAR OPTION_REG.0 ' Prescaler division bit-0
PS1 VAR OPTION_REG.1 ' Prescaler division bit-0
PS2 VAR OPTION_REG.2 ' Prescaler division bit-0
PSA VAR OPTION_REG.3 ' Prescaler Assignment (1= assigned to WDT)
' (0= assigned to oscillator)
T0CS VAR OPTION_REG.5 ' Timer0 Clock Source Select (0=Internal clock)
' (1=External PORTA.4)

' ** THE MAIN PROGRAM STARTS HERE **
Hours=0 ' Set the initial time to 00:00:00
Minutes=0
Seconds=0
Ticks=0
U_Flag=1 ' Allow the display to update initially

' Set TMR0 to interrupt every 16.384ms. Using a prescaler value of 64 (64*256)
GIE=0 ' Turn off global interrupts
While GIE=1:GIE=0:Wend ' Make sure they are off
PSA=0 ' Assign the prescaler to external oscillator
PS0=1 ' Set the prescaler
PS1=0 ' to increment TMR0
PS2=1 ' every 64th instruction cycle
T0CS=0 ' Assign TMR0 clock to internal source
TMR0=0 ' Clear TMR0 initially
T0IE=1 ' Enable TMR0 overflow interrupt
GIE=1 ' Enable global interrupts

ON INTERRUPT GoTo Clock_Int ' Point to the interrupt handler

' ** THE MAIN LOOP STARTS HERE **

Main:

IF U_Flag=1 Then ' Check if the time has changed

GoSub getx ' Get x value
x1= x * 39/98
SerOut2 out_pin,ser_baud,[ DEC x1 ," % "," sec : ",DEC2 Seconds,10,13] 'Display on terminal

U_Flag=0 ' Clear the flag
EndIF

GoTo Main ' Loop forever

' ** INTERRUPT ROUTINE TO HANDLE EACH TIMER TICK **
' The interupt will be called every 16.384ms.
' Each time the interupt is called the variable TICKS will be incremented.
' When TICKS reaches 61, an approximate second has elapsed.
' Calculated by:- (16.384*61)=999.424ms or .999424 of a second

Disable ' Disable all interupts during the interrupt handler
Clock_Int:
Ticks=Ticks+1 ' Increment the TICKS variable
IF Ticks <=61 Then Clock_Exit ' Have we reached a second yet?

' One second has elasped so update the time variables
Ticks=0 ' YES! so reset TICKS
Seconds=Seconds+1 ' and increment the SECONDS variable
IF Seconds >=60 Then ' Have we reached a minute yet?
Seconds=0 ' YES! so reset SECONDS
Minutes=Minutes + 1 ' and increment MINUTES
IF Minutes >=60 Then ' Have we reached an hour yet?
Minutes=0 ' YES! so reset MINUTES
Hours=Hours+1 ' and increment HOURS
IF Hours >=23 Then ' Have we reached a 24 hours yet?
Hours=0 ' YES! so reset HOURS
EndIF
EndIF
EndIF
U_Flag=1 ' Inform the main program that the time has changed

Clock_Exit:
T0IF=0 ' Reset timer interrupt flag
Resume ' Exit the interupt
Enable ' Allow more interrupts

'<<<<<<<<<<<<<<<<<<<<<<

getad:
PauseUs 50 ' Wait for channel to setup

ADCON0.2 = 1 ' Start conversion
PauseUs 50 ' Wait for conversion

Return

' Subroutine to get pot x value
getx:
ADCON0 = $41 ' Set A/D to Fosc/8, Channel 0, On
GoSub getad
x = ADRESH
Return

End

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I need your best ideas.
Grazie.