'**************************************************************** ' Name: 10bit.bas ' Written in PICBASIC PRO, MicroEngineering Labs ' Size: 2968 words ' Date: 12/2/2008 '**************************************************************** ' ' Program stores two 10-bit readings to USB thumb drive using a Vdrive2 ' ' ' Vdrive jumper in UART MODE (UART/SPI pulled up datasheet V.99 is correct) ' Pin Name Description ' ' 1 GND 5V common ' 2 RTS Request to Send ' 3 Vdd Connects to +5V ' 4 RXD Receive Data ' 5 TXD Transmit Data ' 6 CTS Clear to Send (connect to common or make low) ' 7 NC No Connection ' 8 RI Ring Indicator (not used) ' ' ' ' PIC16F688 ' ' The program sends debug data through a DB9 serial port to PC DEFINE OSC 8 ' Run on 8.0 MHz INCLUDE "modedefs.bas" ' Include serial modes ' Set up DEBUG configurations DEFINE DEBUG_REG PORTC DEFINE DEBUG_BIT 0 DEFINE DEBUG_BAUD 9600 DEFINE DEBUG_MODE 1 'debug in not implimented DEFINE DEBUGIN_REG PORTC DEFINE DEBUGIN_BIT 1 DEFINE DEBUGIN_BAUD 9600 DEFINE DEBUGIN_MODE 1 'Varables x VAR BYTE filestring VAR BYTE[12] n VAR WORD ' counter w VAR WORD Tsec VAR BYTE Tsec10 VAR BYTE Tmin VAR BYTE Tmin10 VAR BYTE hr VAR BYTE hr10 VAR BYTE mm VAR BYTE mm10 VAR BYTE dd VAR BYTE dd10 VAR BYTE yy VAR BYTE yy10 VAR BYTE RX VAR PORTC.5 ' Receive Data TX VAR PORTC.4 ' Transmit Data Echo VAR BYTE ' Syncronizing byte ADCresult1 VAR word ' channe1 1 results ADCresult2 VAR word ' channel 2 results SCLpin VAR PORTC.2 ' i2c pin scl SDApin VAR PORTC.3 ' i2c pin sda rtc VAR BYTE[8] datestring VAR BYTE[14] 'varables for clock RTCSec VAR BYTE ' Seconds RTCMin VAR BYTE ' Minutes RTCHour VAR BYTE ' Hours RTCWDay VAR BYTE ' Weekday RTCDay VAR BYTE ' Day RTCMonth VAR BYTE ' Months RTCYear VAR BYTE ' Year RTCCtrl VAR BYTE ' Control SetTime VAR BYTE ' 12/24 Hour Clock SetSec VAR BYTE ' Seconds SetMin VAR BYTE ' Minutes SetHour VAR BYTE ' Hours SetDay VAR BYTE ' Day SetMonth VAR BYTE ' Months SetYear VAR BYTE ' Year ' Set inputs/outputs OSCCON = %01110001 'runs PIC internal clock at 8 mhz. Output PORTC.2 Input PORTA.2 Output PORTC.0 Input PORTA.0 Input PORTA.1 ' for adc1 Input PORTA.5 Output PORTC.3 Output PORTA.4 ' Setup to read a/d converter CH1 ADCON1 = %00110000 ' Set Frc ANSEL = %00000110 ' Make ANA1 and ANA2 analog CMCON0 = 0 'comparators off begin: High PORTA.4 'turn on backup pwr w=0 n=0 Debug "VDrive2 STARTING",13,10 Pause 200 High TX Pause 600 Debug "SYNCHRONIZING",13,10 syncro: Pause 250 Debug "Sending E",13,10 ' Show E has been sent SerOut2 TX,84,["E",$D] ' Send sync command SerIn2 RX,84,100,toolong, [echo] Debug "Sent E= ",echo,13,10 IF echo = "E" Then suspend_disc GoTo syncro toolong: Debug "No E returned",13,10 GoTo syncro suspend_disc: 'places usb stick in low power when idle Debug "Send SUD",13,10 ' Show SUD has been sent SerOut2 TX,84,["SUD",$D] readdate: SerOut2 TX,84,["RD date.txt",$D] SerIn2 RX, 84,600,startlog,[STR datestring\14] IF datestring[0]="C" Then startlog Debug "datestring= ",STR datestring\14,13,10 setclock: 'set clock if date.txt file is found Debug "setclock",13,10 RTCSec=$00 ' Seconds preset to 00 RTCMin= (datestring[13]-48) + ((datestring [12]-48) <<4) ' Minutes RTCHour= (datestring[10]-48) + ((datestring [9]-48) <<4) ' Hours (24 hr mode) RTCWDay=$00 ' Weekday preset to 0 (not used) RTCDay=(datestring[7]-48) + ((datestring [6]-48) <<4) ' Day of month RTCMonth=(datestring[4]-48) + ((datestring [3]-48) <<4) ' Months RTCYear=(datestring[1]-48) + ((datestring [0]-48) <<4) ' Year RTCCtrl=$10 ' Output 1 second 'Tick' on SQWpin I2CWrite SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl] Pause 100 'change file name so it does not change the date at next restart SerOut2 TX,84,["REN date.txt ndate.txt",$D] Debug "rename date.txt to ndate.txt ",STR datestring\14,13,10 startlog: GoSub mkfilename GoSub openfile collectdata: IF PORTA.5 =1 Then GoSub closefile Pause 1000 Low PORTA.4 Stop EndIF Pause 800 GoSub readclock GoSub readadc1 GoSub readadc2 GoSub wrtfile Debug "Step ",DEC n," File: ",DEC rtc(0),DEC rtc(1),DEC rtc(2),DEC rtc(3),DEC rtc(4),DEC rtc(5),DEC rtc(6),DEC rtc(7),".csv",13,10 Debug "Time ",DEC hr10,DEC hr,":",DEC Tmin10,DEC Tmin, 13,10 IF n < 7201 Then collectdata GoSub closefile n=0 GoTo startlog readadc1: ADCON0 = %10000101 ' right justified ADCON0.7=1 sel ch 1 ADCON0.1 = 1 ' Start conversion PauseUs 100 ' wait for conversion ADCresult1.highbyte = ADRESH 'move HIGH byte of result to ADCresult1 ADCresult1.lowbyte = ADRESL 'move LOW byte of result to ADCresult1 Debug "ADC1= ", DEC ADCresult1, 13,10 Return readadc2: ADCON0 = %10001001 ' right justified ADCON0.7=1 sel ch 2 ADCON0.1 = 1 ' Start conversion PauseUs 100 ' Wait for conversion ADCresult2.highbyte = ADRESH 'move HIGH byte of result to ADCresult2 ADCresult2.lowbyte = ADRESL 'move LOW byte of result to ADCresult2 Debug "ADC2= ", DEC ADCresult2, 13,10 Return wrtfile: n=n+1 Debug "Write ",DEC yy10,DEC yy,DEC mm10,DEC mm,DEC dd10,DEC dd,DEC hr10,DEC hr,DEC Tmin10,DEC Tmin,DEC Tsec10,DEC Tsec,",",DEC4 ADCresult1,",",DEC4 ADCresult2,13,10 SerOut2 TX,84,["WRF ",$00,$00,$00,$17,$0D,DEC yy10,DEC yy,DEC mm10,DEC mm,DEC dd10,DEC dd,DEC hr10,DEC hr,DEC Tmin10,DEC Tmin,DEC Tsec10,DEC Tsec,",",DEC4 ADCresult1,",",DEC4 ADCresult2,13,13] Return failread: Debug "Fail I2C",13,10 Return readclock: Debug "Read RTC",13,10 I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl],failread Tsec = RTCSec & %00001111 Tsec10 = RTCsec & %01110000 Tsec10 = Tsec10 >>4 Tmin = RTCMin & %00001111 Tmin10 = RTCMin & %01110000 Tmin10 = Tmin10 >>4 hr = RTCHour & %00001111 hr10 = RTCHour & %00110000 hr10 = hr10 >>4 dd = RTCDay & %00001111 dd10 = RTCDay & %00110000 dd10 = dd10 >>4 mm = RTCMonth & %00001111 mm10 = RTCMonth & %00010000 mm10 = mm10 >>4 yy = RTCYear & %00001111 yy10 =RTCYear & %11110000 yy10 = yy10 >>4 Return mkfilename: Debug "MK file name string",13,10 I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl],failread hr = RTCHour & %00001111 rtc[7] = hr hr10 = RTCHour & %00110000 hr10 = hr10 >>4 rtc[6] = hr10 dd = RTCDay & %00001111 rtc[5] = dd dd10 = RTCDay & %00110000 dd10 = dd10 >>4 rtc[4] = dd10 mm = RTCMonth & %00001111 rtc[3] = mm mm10 = RTCMonth & %00010000 mm10 = mm10 >>4 rtc[2] = mm10 yy = RTCYear & %00001111 rtc[1] = yy yy10 =RTCYear & %11110000 yy10 = yy10 >>4 rtc[0] = yy10 Return openfile: SerOut2 TX,84,["OPW ",DEC rtc(0),DEC rtc(1),DEC rtc(2),DEC rtc(3),DEC rtc(4),DEC rtc(5),DEC rtc(6),DEC rtc(7),".csv",13] Debug "Open file ",DEC rtc(0),DEC rtc(1),DEC rtc(2),DEC rtc(3),DEC rtc(4),DEC rtc(5),DEC rtc(6),DEC rtc(7),".csv",13,10 Return closefile: SerOut2 TX,84,["CLF ",DEC rtc(0),DEC rtc(1),DEC rtc(2),DEC rtc(3),DEC rtc(4),DEC rtc(5),DEC rtc(6),DEC rtc(7),".csv",13] Debug "Close file ",DEC rtc(0),DEC rtc(1),DEC rtc(2),DEC rtc(3),DEC rtc(4),DEC rtc(5),DEC rtc(6),DEC rtc(7),".csv",13,10 Return bad_data: Debug 10,10,"bad_data" End idd_error: Debug "ID error", 13,10 Stop alldone: Debug "DONE" ,13,10 Low PORTA.4 Stop