Overclocked
Member
Heres what I want to do; I have a interrupt routine that goes off ever second, samples the ADC and sends it over UART (via bluetooth, HC-05). Right now my Main while loop does nothing. I want to put the PIC to sleep while it does nothing, to try and get the current draw down (The whole system together consumes 25mA-The Bluetooth module takes up the majority of it). I must be missing something, because I tried putting in
Into my while loop, and the PIC just sits there. I must have missed a register or something. I am using Timer 1 with a 32kHz crystal.
Code:
asm
sleep
end asm
Into my while loop, and the PIC just sits there. I must have missed a register or something. I am using Timer 1 with a 32kHz crystal.
Code:
{
*****************************************************************************
* Name : Datalogger_A.BAS *
* Author : Chris S *
* Notice : Copyright (c) 2014 Open Source *
* : All Rights Reserved *
* Date : 10/4/2014 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
Device = 18F13K22
Clock = 16
Include "IntOSC.bas"
Include "USART.bas"
Include "utils.bas"
Include "convert.bas"
Include "ADC.bas"
Config FOSC = IRC
Dim
Channel2 As Word,
TMR1IE As PIE1.0, // TMR1 Interrupt Enable
TMR1IF As PIR1.0, // TMR1 Interrupt Flag
// TMR1ON As T1CON.0, // TMR1 Count Enable
Timer1 As TMR1L.AsWord // A quick way of creating a Word Alias
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
ADCON0.0 = 1 //enable ADC
ADCON0.1 = 1 //GOGOGOGO
While ADCON0.1 = 1
ADResult = (ADRESL)
Wend
//read channel 2
Channel2 = ((ADResult+ 1) * 499) / 1024 //MATHMATICAL!
USART.Write ("Channel 2: ",DecToStr(Channel2 / 100), ".", DecToStr(Channel2, 2), " ",13,10)
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;
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...
TMR1_Initialize // Setup and enable TMR1
USART.SetBaudrate(br9600)
Channel2 = 0
TRISA.2=1 //set channel 2 as a input
ADCON0= %00001000 //channel 2 as ADC input
ADCON2 =%10101010 //12 TAD, FOSC/32
ANSEL.2 = 1
//Main Loop
While True
Wend