Code:
' -----[ Title ]-------------------------------------------------
'
' File...... WVL 877 VERTOR AVERAGE
' Purpose... Experiment with PIC16F877A
' Date...... July 2009
'
' -----[ Program Description ]---------------------------
'
' 1. Devices used: PIC16F877A on EasyPIC5
'
' ----[ Includes / Defines ]-----------------------------------------
'
define OSC 20 ' Define Xtal as 20 MHz. Use "HS" for programming
'
include "modedefs.bas" ' Include serout defines
'
' -----[ Initialization ]--------------------------------------------
'
ADCON1 = %00001110 ' A0 is analog, remainder digital
CMCON = %00000111 ' Comparators OFF
INTCON = $80 ' Turns off interrupts. See PBP page 187
TRISA = %00000001 ' A0 is A/D input
TRISB=0 : TRISC=0 : TRISD=0 : TRISE=0
PORTA=0 : PORTB=0 : PORTC=0 : PORTD=0 : PORTE=0
'
' ----[ Variables ]--------------------------------------------------
'
X VAR BYTE ' First vector READ
Y VAR byte ' Second vector READ
Vector var byte[31] ' Stores 16 A/D readings of wind direction and averages
Difference var byte ' Difference between vectors, always +
Average var word ' Average of two vectors
J var byte ' EEPROM memory address
K var word
Memory var byte ' Contents of EEPROM address J
TxPin var PORTC.6
Heartbeat var PORTC.0
'
'-----[ Main ]-------------------------------------------------------
'
Main:
for J = 0 to 15 ' Create 16 random numbers 1 to 255
random K
write J, K.lowbyte ' Load EEPROM with random numbers
next J
for J = 0 to 28 step 2 ' Read pairs of vectors
read J, X
read J+1,Y
if Y>X then swap X,Y ' X is always the larger variable
Difference = X-Y
Average = (Difference/2) + Y ' Half difference and add to smaller vector
IF DIFFERENCE>128 then
Average=Average+128 ' Reverse direction of vector
if Average>255 then Average=Average-255 ' Whole revolution? Subtract 255 if necessary
endif
write J/2 + 16,Average
next J
for J = 0 to 30 ' Send to PC at 19,200 Baud
read J, Vector[J]
next J
serout2 TxPin,32,["VECTOR ADDITION",13,13]
serout2 TxPin,32,[dec3 Vector[0],9,dec3 Vector[16],9,dec3 Vector[24],9,dec3 Vector[28],9,dec3 Vector[30],13]
serout2 TxPin,32,[dec3 Vector[1],13]
serout2 TxPin,32,[dec3 Vector[2],9,dec3 Vector[17],13]
serout2 TxPin,32,[dec3 Vector[3],13]
serout2 TxPin,32,[dec3 Vector[4],9,dec3 Vector[18],9,dec3 Vector[25],13]
serout2 TxPin,32,[dec3 Vector[5],13]
serout2 TxPin,32,[dec3 Vector[6],9,dec3 Vector[19],13]
serout2 TxPin,32,[dec3 Vector[7],13]
serout2 TxPin,32,[dec3 Vector[8],9,dec3 Vector[20],9,dec3 Vector[26],9,dec3 Vector[29],13]
serout2 TxPin,32,[dec3 Vector[9],13]
serout2 TxPin,32,[dec3 Vector[10],9,dec3 Vector[21],13]
serout2 TxPin,32,[dec3 Vector[11],13]
serout2 TxPin,32,[dec3 Vector[12],9,dec3 Vector[22],9,dec3 Vector[27],13]
serout2 TxPin,32,[dec3 Vector[13],13]
serout2 TxPin,32,[dec3 Vector[14],9,dec3 Vector[23],13]
serout2 TxPin,32,[dec3 Vector[15],13,13,13]
goto Main
END
Bookmarks