/*********************************************************************
*
* Functions for controlling blinds
*
*********************************************************************
* FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canTemp/Source/blinds.c $
* Last changed: $LastChangedDate: 2007-02-25 20:51:08 +0100 (Sun, 25 Feb 2007) $
* By: $LastChangedBy: johboh $
* Revision: $Revision: 339 $
********************************************************************/
#include <blinds.h>
#ifdef USE_BLINDS
static unsigned int timerVal = 50536;
static unsigned int turnOffIO = 0;
static signed char currentPrecent = 0;
/*
* Function: blindsInit
*
* Input: none.
* Output: none.
* Pre-conditions: none
* Affects: none.
* Depends: none.
*/
void blindsInit()
{
// setup timer one on 0.5ms.
// 16 bit write, system clock, prescale 2 bit, oscillator off,
T1CON = 0b11000001;
TMR1H=((timerVal&0xFF00)>>8);
TMR1L=(timerVal&0xFF);
PIR1bits.TMR1IF = 0;
PIE1bits.TMR1IE = 1;
IPR1bits.TMR1IP = 0; // Low prior
}
/*
* Function: blindsGetAngle
*
* Input: none.
* Output: angle.
* Pre-conditions: none
* Affects: none.
* Depends: none.
*/
BYTE blindsGetPrecent()
{
return currentPrecent;
}
/*
* Function: blindsTurn
*
* Input: Angle to turn to.
* Output: none.
* Pre-conditions: none
* Affects: none.
* Depends: none.
*/
void blindsTurn(BYTE precent)
{
// 65536-TIME/0,0000001
// -90 0.5 ms = 60536 (5000)
// 0 1.5 ms = 50536 (15000)
// 90 2.5 ms = 40536 (25000)
BLINDS0P_IO=1;
turnOffIO = IO_TIMEOUT;
/*
if (angle<ANGLE_MIN) {timerVal=ANGLE_MIN_TIMER; currentAngle=ANGLE_MIN; }
else if (angle>ANGLE_MAX) {timerVal=ANGLE_MAX_TIMER; currentAngle=ANGLE_MAX; }
else
{
timerVal=60536-(unsigned int)((90+(signed int)angle)*111.111);
currentAngle=angle;
}
*/
/*
angle=precent*
-50=k*100+m
60=k*0+m
angle=-1.1x+60
*/
currentPrecent=precent;
if (precent>100) { currentPrecent=100; }
if (precent<0) { currentPrecent=0; }
timerVal=60536-(unsigned int)((90+(-1.05*(signed int)currentPrecent+60))*111.111);
}
/*
* Function: blindsISR
*
* Input: none.
* Output: none.
* Pre-conditions: none
* Affects: none.
* Depends: none.
*/
void blindsISR(void)
{
if (PIR1bits.TMR1IF == 1 && PIE1bits.TMR1IE == 1)
{
TMR1H=((timerVal&0xFF00)>>8);
TMR1L=(timerVal&0xFF);
BLINDS0_IO=~BLINDS0_IO;
if (turnOffIO--<1)
{
BLINDS0P_IO=0;
}
PIR1bits.TMR1IF = 0;
}
}
#endif