I am reading data from a sensor using SHIFTIN. While reading the data, the word-sized variable overflows. So I have to count the overflows. The problem is how to count these overflows without getting multiple counts while the YVALTOT is running. It seems to me that interrupts are the answer, but I can't figure out how. The code below is my latest attempt, but it gives multiple overflow counts when I only want one. Any help will be appreciated.
@ DEVICE pic16F689, intrc_osc_noclkout, wdt_off, mclr_off, protect_off
CM1CON0 = 0
CM2CON0 = 0
ADCON0 = 0
ANSEL = 0
ANSELH = 0
OSCCON = %01100001 '4 mhz. internal
TRISA = %101100
'RA5 = Switch high input
'RA4 = Output to Speaker
'RA3 = Input from MISO
'RA2 = Input from RB7
'RA1 = Output to SCLK
'RA0 = Output to MOSI
TRISB = %0000
'RB7 = Output to RA2 (EXT INT)
'RB6 = SCL output
'RB5 = Output to NCS
'RB4 = SDA output
TRISC = %00000000
'RC7 = Output to green led
'RC6 = Output to red led
'RC5 = Output to LCD ENABLE
'RC4 = Output to LCD RS
'RC3 = Output to LCD DB3
'RC2 = Output to LCD DB2
'RC1 = Output to LCD DB1
'RC0 = Output to LCD DB0
OPTION_REG = %11000000
Define LCD_DREG PORTC 'Set LCD data port
DEFINE LCD_DBIT 0 'Set LCD starting data bit
DEFINE LCD_RSREG PORTC 'Set LCD register select port
DEFINE LCD_RSBIT 4 'Set LCD register select bit
DEFINE LCD_EREG PORTC 'Set LCD enable port
DEFINE LCD_EBIT 5 'Set LCD enable bit
DEFINE LCD_BITS 4 'Set LCD bus size
DEFINE LCD_LINES 2 'Set LCD number of lines
DEFINE LCD_COMMANDUS 1500 'Set command delay time in us
DEFINE LCD_DATAUS 200 'Set data delay time in us
NCS VAR PORTB.5
SPEAKER VAR PORTA.4
MOSI VAR PORTA.0
MISO VAR PORTA.3
DATAIN VAR BYTE
MOTBYTE var BYTE
MOTBIT var BIT
MOTBIT = MOTBYTE.BIT7
YVAL VAR BYTE
XVAL VAR BYTE
YVALTOT VAR WORD
OVERFLOW VAR WORD
MOTBIT = 0
LOW PORTC.6
LOW PORTC.7
DATAIN = 0
PAUSE 1000
NCS = 0
NCS = 1
NCS = 0
SHIFTOUT MOSI,PORTA.1,5,[$3a\8] 'Reset sensor
PAUSE 10
SHIFTOUT MOSI,PORTA.1,5,[$5a\8]
ncs = 1
yval = 0
yvaltot = 0
OVERFLOW = 0
PORTB.7 = 0
Main:
ncs = 0
shiftout mosi,PORTA.1,5,[$02\8]
PAUSE 10
SHiftin miso,PORTA.1,6,[MOTBYTE\8]
Pause 10
ncs = 1
If MOTByTE = $e0 then YCOUNT
LCDOUT $fe,1,dec YVALTOT 'Display starting values
lcdout $fe,$c0,dec overflow
goto Main
YCOUNT:
ncs = 0
SHIFTOUT MOSI,PORTA.1,5,[$03\8] 'input values
SHIFTIN MISO,PORTA.1,6,[YVAL\8]
SHIFTOUT MOSI,PORTA.1,5,[$04\8]
SHIFTIN MISO,PORTA.1,6,[XVAL\8]
ncs = 1
yvaltot = yvaltot + yval 'count Y values
if yvaltot > 65000 then OPLUS 'keep track of y overflows
goto Main
OPLUS:
overflow = overflow + 1 'count overflows
goto OTest
OTest:
if yvaltot < 65000 THEN MAIN
goto Main
Bookmarks