Here is my Code.
Code: Select all
{
*****************************************************************************
* Name : Dataloger_C.BAS *
* Author : Chris S *
* Notice : Copyright (c) 2015 Open Source *
* : All Rights Reserved *
* Date : 7/22/2015 *
* Version : 1.0 *
* Notes : TX on Micro goes to RX on transmitter *
* : AN2, AN4 and AN5 Are Available ports *
* : TO DO: FVR (2.048v) on All channels. *
* : FVR Varies. Check against theoretical (2.048) value and then *
* : Subtract the measured value to get the difference. *
* : Call this Var "MeasuredFVR" and "TheoreticalFVR" *
* : DeltaFVR=Theoretical-Measured *
* :RealFVF=Theoretical-Delta if less than Theo *
* :RealFVR=Theoretical+Delta if More than Theo
*^^^ This wont work *
* : Check battery voltage once per hour. Same with Solar Cell *
*****************************************************************************
}
//Relavent settings
// ADCON0 Settings
// ADCON0= %00001000 = Channel 2
// ADCON0= %00010000 = Channel 4
// ADCON0= %00101000 = Channel 5
//
//ANSEL Settings
//ANSEL.3 = 1 For channel 4
//ANSEL.5 = 1 For Channel 5
//
//TRISA Settings
//TRISA.5 = 1 For Channel 5
Device = 18F13K22
Clock = 4
Include "IntOSC.bas"
Include "USART.bas"
Include "utils.bas"
Include "convert.bas"
Include "ADC.bas"
Config FOSC = IRC
Dim
Channel2 As Integer, //changed datatypes from word to avoid negative number errors
Channel4 As Integer,
Channel5 As Integer,
TMR1IE As PIE1.0, // TMR1 Interrupt Enable
TMR1IF As PIR1.0, // TMR1 Interrupt Flag
Timer1 As TMR1L.AsWord, // A quick way of creating a Word Alias
Seconds As Byte,
Minutes As Byte,
Hours As Byte,
Days As Word, //179 Years before this fills up...
Years As Byte
//Const
// TMR1StartVal = $80, // User defined TMR1 starting value
// TMR1ReloadVal = TMR1StartVal + 5
Interrupt TMR1_Interrupt()
Save(0) // Back up system variables
If TMR1IF = 1 Then
TMR1IF = 0 // Clear the TMR1 Interrupt
T1CON.0 = 0 //Turn timer 1 off
TMR1H = $80 //Preload high byte with 128
TMR1L = 0 // Preload Low byte with 0
T1CON.0 = 1 // timer 1 on
Seconds= Seconds +1
//USART.Write (DecToStr(Seconds,2),13,10) //Just a test string. Can be deleted in final version.
If Seconds = 10 Then //count to 59. Take ADC reading
TRISA.2 = 1 //set channel 2 as a input
//read channel 2
ADCON0= %00001000 //channel 2 as ADC input
ANSEL.2 = 1 // Channel 2
ADCON0.0 = 1 //enable ADC
ADCON0.1 = 1 //GOGOGOGO
While ADCON0.1 = 1
ADResult = (ADRESL)
Wend
Channel2 = ((ADResult+ 1) * 500) / 1024 //MATHMATICAL!
Channel2 = Channel2 - 50 //Subtract 50 to get the current temp in C
//Next Channel
TRISA.4 = 1 //set channel 4 as input
ADCON0= %00010000 //Channel 4
ANSEL.3 = 1 //Channel 4
ADCON0.0 = 1 //enable ADC
ADCON0.1 = 1 //GOGOGOGO
While ADCON0.1 = 1
ADResult = (ADRESL)
Wend
Channel4 = ((ADResult+ 1) * 500) / 1024 //MATHMATICAL!
//Place holder for Math for Battery.
//USART.Write ("Channel 2: ",DecToStr(Channel2 / 100), ".", DecToStr(Channel2, 2), " ",13,10)
USART.Write ("Current Temp: ", DecToStr(Channel2), " ",13,10)
USART.Write ("Battery Voltage: ", DecToStr(Channel4), " ",13,10)
Seconds = 0
Minutes = Minutes + 1
USART.Write ("Minutes:",DecToStr(Minutes,2),13,10)
End If
If Minutes = 59 Then //Count up hours
Hours = Hours + 1
Minutes = 0
USART.Write ("Hours:",DecToStr(Hours,2),13,10)
End If
If Hours = 24 Then
Days = Days + 1
Hours = 0
USART.Write ("Days:",DecToStr(Days,3),13,10)
End If
If Days = 365 Then
Years = Years +1
Days = 0
USART.Write ("Years:",DecToStr(Years,2),13,10)
End If
EndIf
Restore // Restore system variables
End Interrupt
Sub TMR1_Initialize()
TRISC.0 = 1 // If External clock, then set clock as an input
T1CON.0 = 0 // Timer 1 off
TMR1H = $80 //Preload with 128
TMR1L = 0 //
T1CON.0 = 1 // timer 1 on
T1CON.1 = 1 // 1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
// 0 = Internal clock (FOSC/4)
T1CON.2 = 0 // 1 = Do not synchronize external clock input
// 0 = Synchronize external clock input
// When T1CON.1 = 0;
// this bit is ignored.
T1CON.3 = 1
T1CON.4 = 0
T1CON.5 = 0
T1CON.6 = 0
T1CON.7 = 0
TMR1IE = 1 // Enable TMR1 Interrupts
Enable(TMR1_Interrupt) // Enable TMR1 Interrupt Handle
End Sub
// Start Of Main Program...
Seconds = 0
Minutes = 0
Hours = 0
Days = 0
Years = 0
Channel2 = 0
Channel4 = 0
TMR1_Initialize // Setup and enable TMR1
USART.SetBaudrate(br9600)
//TRISA.2 = 1 //set channel 2 as a input
//TRISA.4 = 1 //set channel 4 as input
ADCON2 =%10110001 //12 TAD, FOSC/8
While True
OSCCON.7 = 0
Asm
sleep
Nop
Nop
End Asm
Wend
Code: Select all
Config fosc = IRC
OSCCON = %11010011