SHARE
TWEET

Untitled

a guest May 22nd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * File:   clima.c
  3.  * Author: Dragos
  4.  *
  5.  * Created on February 2, 2014, 11:39 PM
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #include <p18f8722.h>
  12.  
  13. #include "clima.h"
  14. #include "lcd.h"
  15. #include "uart.h"
  16.  
  17. // configuration bits
  18. #pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
  19. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  20. #pragma config WDT = OFF        // Watchdog Timer (WDT disabled (control is placed on the SWDTEN bit))
  21.  
  22.  
  23.  
  24.  
  25. #define DBG_MSG 0
  26. #if (DBG_MSG == 1)
  27. #define DBG(x)  UART_puts((char *)x)    // debug messages on UART
  28. #else
  29. #define DBG(x)                  // debug messages are lost
  30. #endif
  31.  
  32. //                           0123456789012345
  33. #define LCD_STATE_OFF_1     "     CLIMA      "
  34. #define LCD_STATE_OFF_2     "      OPRITA    "
  35. #define LCD_STATE_ON_COOL_1 "Te:+  C Ti:+  C "
  36. #define LCD_STATE_ON_COOL_2 "Rece       <  C>"
  37. #define LCD_STATE_ON_HEAT_1 "Te:+  C Ti:+  C "
  38. #define LCD_STATE_ON_HEAT_2 "Cald       <  C>"
  39. #define LCD_STATE_ON_VENT_1 "Te:+  C Ti:+  C "
  40. #define LCD_STATE_ON_VENT_2 "Vent          C "
  41.  
  42.  
  43.  
  44. const char LcdLines[STATE_MAX][2][18] =
  45. {
  46.     {LCD_STATE_OFF_1, LCD_STATE_OFF_2},
  47.     {LCD_STATE_ON_COOL_1,LCD_STATE_ON_COOL_2},
  48.     {LCD_STATE_ON_HEAT_1,LCD_STATE_ON_HEAT_2},
  49.     {LCD_STATE_ON_VENT_1,LCD_STATE_ON_VENT_2}
  50. };
  51.  
  52. typedef unsigned char byte;
  53. #define ON          1
  54. #define OFF         0
  55. #define TEMP_STEP   1
  56. #define FAN_STEPS   6
  57. #define TEMP_MIN    21
  58.  
  59. #define TIMER_CYCLE         (100)   // timer cyclic events  (ms)
  60. #define INPUT_DEBOUNCE_TIME (3000)  // time (ms)
  61. #define INPUT_DEBOUNCE_CNT  (INPUT_DEBOUNCE_TIME/TIMER_CYCLE) //   time(ms)/ cycle100(ms)
  62.  
  63. #define TEMP_SENS_MPC_OFFSET    (400)   // output voltage @ 0*C
  64. #define TEMP_SENS_LM_OFFSET     (300)  // output voltage @ 0*C
  65. #define TEMP_SENS_MPC_RES       (19)    // output voltage / *C
  66. #define TEMP_SENS_LM_RES        (10)    // output voltage / *C
  67.  
  68.  
  69. #define TRIS_OUT                0
  70. #define TRIS_HEAT_ELEMENT       (TRISDbits.TRISD3)
  71. #define TRIS_HEAT_VENT_FAN      (TRISDbits.TRISD4)
  72. #define TRIS_COOL_FAN           (TRISDbits.TRISD5)
  73.  
  74. #define PIN_OFF                 0
  75. #define PIN_ON                  1
  76. #define PIN_FAN_COOL            (PORTDbits.RD4)
  77. #define PIN_FAN_HEAT_VENT       (PORTDbits.RD5)
  78. #define PIN_HEAT_ELEMENT        (PORTDbits.RD3)
  79.  
  80.  
  81. void setSpeedFanCool(byte speed);
  82. void setSpeedFanHeatVent(byte speed);
  83. void setLevelHeat(byte level);
  84.  
  85. unsigned int ADCRead(unsigned char ch);
  86. void checkInputs(void);
  87. byte getOnOffButton(void);
  88. void test(void);
  89. void setLcd(void);
  90. void updateLcd(void);
  91. void checkInputs(void);
  92. void stateMachine(void);
  93. void initButtons(void);
  94. void initAdc(void);
  95. void initPwm(void);
  96. void init(void);
  97. void main(void);
  98.  
  99. /*******************************************************************************
  100.  * Clima state
  101.  */
  102. state_e climaState;         /* state of the clima: OFF, VENT, COOL, HEAT */
  103.  
  104. byte fanSpeedCool;          /* speed for cool fan */
  105. byte fanSpeedHeatVent;      /* speed for heat/ventilation fan */
  106. byte levelHeat;             /* heating level */
  107.  
  108. byte heatElement;           /* LED */
  109. byte coolElement;           /* LED */
  110. byte standbyLed;            /* LED */
  111. byte lcdBacklightLed;       /* LED */
  112.  
  113. byte leftButtonEv = 0;      /* event generated when the transition from NOT_PRESSED -> PRESSED is detected on left button */
  114. //byte rightButtonEv = 0;
  115. byte setTemp = 0;           /* desired temperature */
  116. unsigned int inTemp = 0;            /* interior temperature */
  117. unsigned int outTemp = 0;   /* outside temperature */
  118.  
  119. unsigned char tick = 0;
  120. unsigned char ev = 0;
  121. unsigned char cnt = 0;
  122.  
  123.  
  124. char msg[20] = {0};         /* used to format print messages */
  125.  
  126. unsigned char inDeb = 0;    /* debounce counter for temperature sensor */
  127.  
  128.  
  129.  
  130. /*******************************************************************************
  131.  * Set Standby LED Function
  132.  */
  133. void setStandbyLed(unsigned state)
  134. {
  135.     standbyLed = state; /* store new state */
  136. } /* void setStandbyLed(unsigned state) */
  137.  
  138.  
  139.  
  140. /*******************************************************************************
  141.  * Set LCD backlight LED Function
  142.  */
  143. void setLcdBacklightLed(unsigned state)
  144. {
  145.     lcdBacklightLed = state; /* store new state */
  146. } /* void setLcdBacklightLed(unsigned state) */
  147.  
  148.  
  149.  
  150. /*******************************************************************************
  151.  * Set Heating Element Function
  152.  */
  153. void setHeatElement(unsigned state)
  154. {
  155.     heatElement = state; /* store new state */
  156. } /* void setHeatElement(unsigned state) */
  157.  
  158.  
  159.  
  160. /*******************************************************************************
  161.  * Set Cooling Element Function
  162.  */
  163. void setCoolElement(unsigned state)
  164. {
  165.     coolElement = state; /* store new state */
  166. } /* void setCoolElement(unsigned state) */
  167.  
  168.  
  169.  
  170. /*******************************************************************************
  171.  * Set FAN Speed Function for cool fan
  172.  */
  173. void setSpeedFanCool(byte speed)
  174. {
  175.     if (speed)
  176.         speed += 3;
  177.     fanSpeedCool = speed; /* store new speed */
  178. } /* void setSpeedFanCool() */
  179.  
  180.  
  181. /*******************************************************************************
  182.  * Set FAN Speed Function for heat/vent fan
  183.  */
  184. void setSpeedFanHeatVent(byte speed)
  185. {
  186.     if (speed)
  187.         speed += 3;
  188.     fanSpeedHeatVent = speed; /* store new speed */
  189. } /* void setSpeedFanHeatVent() */
  190.  
  191.  
  192. /*******************************************************************************
  193.  * Set Heat level Function
  194.  */
  195. void setLevelHeat(byte level)
  196. {
  197.     if (level)
  198.         level += 3;
  199.     levelHeat = level; /* store new level */
  200. } /* void setLevelHeat() */
  201.  
  202.  
  203.  
  204. /*******************************************************************************
  205.  * Check Inputs Function
  206.  */
  207. byte getOnOffButton(void)
  208. {
  209.     return leftButtonEv;
  210. } /* unsigned getOnOffButton(void) */
  211.  
  212.  
  213. //byte getRightButton(void)
  214. //{
  215. //    return rightButtonEv;
  216. //} /* unsigned getOnOffButton(void) */
  217.  
  218.  
  219.  
  220. /*******************************************************************************
  221.  * Check Inputs Function
  222.  */
  223. void setLcd(void)
  224. {
  225.     LcdClear();
  226.     LcdGoTo(0); /* first Line */
  227.     LcdWriteString(LcdLines[climaState][0]);
  228.     LcdGoTo(0x40); /* second Line */
  229.     LcdWriteString(LcdLines[climaState][1]);
  230. } /* void setLcd() */
  231.  
  232.  
  233.  
  234. /*******************************************************************************
  235.  * Check Inputs Function
  236.  */
  237. void updateLcd(void)
  238. {
  239.     //TODO state_e climaState_old;
  240.     byte fanSpeed = 0;
  241.  
  242.  /* display set temperature */
  243.     if (climaState != STATE_OFF)
  244.     {
  245.         /* out temp */
  246.         sprintf(msg, "%02d", outTemp);
  247.  
  248.         LcdGoTo(0x00+4);
  249.         LcdWriteString(msg);
  250.  
  251.         /* in temp */
  252.         sprintf(msg, "%02d", inTemp);
  253.         LcdGoTo(0x00+12);
  254.         LcdWriteString(msg);
  255.  
  256.    /* mode */
  257.         LcdGoTo(0x40+11);
  258.         if (climaState == STATE_ON_COOL)
  259.             LcdWriteString("Rece");
  260.         else if (climaState == STATE_ON_HEAT)
  261.             LcdWriteString("Cald");
  262.         else if (climaState == STATE_ON_VENT)
  263.             LcdWriteString("Vent");
  264.  
  265.     /* fan speed */
  266.         LcdGoTo(0x40+5);
  267.         if (climaState == STATE_ON_COOL)
  268.             fanSpeed = fanSpeedCool;
  269.         else
  270.             fanSpeed = fanSpeedHeatVent;
  271.  
  272.         if (fanSpeed == 4)
  273.             LcdWriteString("*....");
  274.         else if (fanSpeed == 5)
  275.             LcdWriteString("**...");
  276.         else if (fanSpeed == 6)
  277.             LcdWriteString("***..");
  278.         else if (fanSpeed == 7)
  279.             LcdWriteString("****.");
  280.         else
  281.             LcdWriteString("*****");
  282.  
  283.         /* set temp */
  284.         sprintf(msg, "%d", setTemp);
  285.         LcdGoTo(0x40+1);
  286.         LcdWriteString(msg);
  287.  
  288.         if (setTemp == TEMP_MIN)
  289.         {
  290.             LcdGoTo(0x40);
  291.             LcdWriteString(" ");
  292.             LcdGoTo(0x40+4);
  293.             LcdWriteString(">");
  294.         }
  295.         else if (setTemp == TEMP_MIN+15)
  296.         {
  297.             LcdGoTo(0x40); /* second Line */
  298.             LcdWriteString("<");
  299.             LcdGoTo(0x40+4); /* second Line */
  300.             LcdWriteString(" ");
  301.         }
  302.         else
  303.         {
  304.             LcdGoTo(0x40); /* second Line */
  305.             LcdWriteString("<");
  306.             LcdGoTo(0x40+4); /* second Line */
  307.             LcdWriteString(">");
  308.         }
  309.     }
  310. } /* void updateLcd(state_e state) */
  311.  
  312.  
  313.  
  314. /*******************************************************************************
  315.  * Check Inputs Function
  316.  */
  317. void checkInputs(void)
  318. {
  319.     unsigned int adcVal = 0;
  320.     byte leftButton = 0;
  321.  
  322.     static byte leftButton_old = 0;
  323.  
  324. /* RB0 - check left push button event */
  325.     leftButton = PORTBbits.RB0;
  326.     if (   (leftButton == 0) /* push button pressed */
  327.         && (leftButton != leftButton_old) /* not pressed before */
  328.        )
  329.     {
  330.         leftButtonEv = 1;
  331.     }
  332.     leftButton_old = leftButton;
  333.  
  334.  
  335.     /* read ADC AN0 - on board potentiometer */
  336.     /* ADC 10 bit resolution
  337.      * AD values     : 0..1023  (1024 values)
  338.      * Set temp steps: 16..31 (16 steps)
  339.      * k = 1024/16 = 64
  340.      * setTemp = adcVal/64 + offset
  341.     */
  342.     adcVal = ADCRead(0);
  343.     setTemp = adcVal/64 + TEMP_MIN;
  344.  
  345.     /* debounce temperature measurement: counter elapsed */
  346.     if (inDeb == 0)
  347.     {
  348.         /* read ADC AN1 - on board temperature sensor */
  349.         /* MCP9701:
  350.          * Resolution: 19mV/*C
  351.          * OFFSET: U @ 0*C = 400mV
  352.          * T = (U-OFFSET)/resolution
  353.          *
  354.          * U = ADC*5000/1023 ~= ADC*5;
  355.          * T = (U-OFFSET)/RESOLUTION ~= (ADC*5 - OFFSET)/RESOLUTION
  356.          */
  357.         adcVal = ADCRead(1);
  358.         outTemp = (adcVal*5 - TEMP_SENS_MPC_OFFSET)/TEMP_SENS_MPC_RES;
  359.         DBG("-> Temperature out:");
  360.         sprintf(msg, "%d", outTemp);
  361.         DBG(msg);
  362.         DBG("\n\r");
  363.  
  364.         /* read ADC AN3 - inside temperature sensor */
  365.         /* LM35:
  366.          * Resolution: 10mV/*C
  367.          * Offset: U @ 0*C = 0mV
  368.          * T = (U-OFFSET)/resolution
  369.          *
  370.          * U = ADC*5000/1023
  371.          * T = (U-OFFSET)/RESOLUTION ~= (ADC*5 - OFFSET)/RESOLUTION
  372.          */
  373.        
  374.         adcVal = ADCRead(3);
  375.         inTemp = (adcVal*5 - TEMP_SENS_LM_OFFSET)/TEMP_SENS_LM_RES;
  376.         DBG("-> Temperature in:");
  377.         sprintf(msg, "%d", inTemp);
  378.         DBG(msg);
  379.         DBG("\n\r");
  380.        
  381.         inDeb = INPUT_DEBOUNCE_CNT;
  382.     }
  383.     else
  384.     {
  385.         inDeb--;
  386.     }
  387.  
  388. } /* void checkInputs(void) */
  389.  
  390.  
  391.  
  392. /*******************************************************************************
  393.  * State Machine Function
  394.  */
  395. void stateMachine(void)
  396. {
  397.     byte err;
  398.  
  399.     DBG("SM:");
  400.     sprintf(msg, "HS:%d, HL:%d, CS:%d, ", fanSpeedHeatVent, levelHeat, fanSpeedCool);
  401.     DBG(msg);
  402.     DBG("\n\r");
  403.  
  404.      switch (climaState)
  405.     {
  406.         case STATE_OFF:
  407.         {
  408.             if (getOnOffButton())
  409.         {
  410.             climaState = STATE_ON_VENT;
  411.             setLcd();
  412.             setLcdBacklightLed(OFF);
  413.             setStandbyLed(OFF);
  414.             setHeatElement(OFF);
  415.             setSpeedFanHeatVent(0);
  416.             setSpeedFanCool(0);
  417.         }
  418.         else
  419.         {
  420.            
  421.         }
  422.         break;
  423.         }
  424.         case STATE_ON_COOL:
  425.         {
  426.             if (getOnOffButton())
  427.         {
  428.             climaState = STATE_OFF;
  429.             setLcd();
  430.             setLcdBacklightLed(OFF);
  431.             setStandbyLed(ON);
  432.             setSpeedFanHeatVent(0);
  433.             setSpeedFanCool(0);
  434.             setHeatElement(OFF);
  435.             updateLcd();
  436.         }
  437.             else if (inTemp < setTemp)
  438.         {
  439.             climaState = STATE_ON_HEAT;
  440.             setSpeedFanHeatVent(1);
  441.             setSpeedFanCool(OFF);
  442.             setHeatElement(ON);                        
  443.             updateLcd();
  444.         }
  445.         else if ((inTemp == setTemp))
  446.         {
  447.             climaState = STATE_ON_VENT;
  448.             setSpeedFanHeatVent(1);
  449.             setSpeedFanCool(1);
  450.             updateLcd();
  451.         }
  452.         else
  453.         {
  454.             climaState = STATE_ON_COOL;
  455.             setHeatElement(OFF);
  456.             setSpeedFanHeatVent(OFF);
  457.             if((outTemp-setTemp) <= FAN_STEPS)
  458.                 err = outTemp-setTemp;
  459.             else
  460.                 err = FAN_STEPS;
  461.             setSpeedFanCool(err);
  462.             updateLcd();
  463.         }
  464.         break;
  465.  
  466.         }
  467.         case STATE_ON_HEAT:
  468.         {
  469.             if (getOnOffButton())
  470.         {
  471.             climaState = STATE_OFF;
  472.             setLcd();
  473.             setLcdBacklightLed(OFF);
  474.             setStandbyLed(ON);
  475.             setHeatElement(OFF);
  476.             setSpeedFanHeatVent(0);
  477.             setSpeedFanCool(0);
  478.             updateLcd();
  479.         }
  480.         else if (inTemp > setTemp)
  481.         {
  482.             climaState = STATE_ON_COOL;
  483.             setSpeedFanHeatVent(OFF);
  484.             setSpeedFanCool(1);
  485.             setHeatElement(OFF);
  486.             updateLcd();
  487.         }
  488.         else if (inTemp == setTemp)
  489.         {
  490.             climaState=STATE_ON_VENT;
  491.             setSpeedFanHeatVent(1);
  492.             setSpeedFanCool(1);
  493.             setHeatElement(OFF);
  494.             updateLcd();
  495.         }
  496.         else
  497.         {
  498.             climaState = STATE_ON_HEAT;
  499.             if((setTemp - inTemp) < FAN_STEPS)
  500.                 err = setTemp - inTemp;
  501.             else
  502.                 err = FAN_STEPS;
  503.             setSpeedFanHeatVent(err);
  504.             setSpeedFanCool(OFF);
  505.             setHeatElement(err);
  506.             updateLcd();
  507.         }
  508.         break;
  509.  
  510.         }
  511.         case STATE_ON_VENT:
  512.         {
  513.             if (getOnOffButton())
  514.         {
  515.             climaState= STATE_OFF;
  516.             setLcd();
  517.             setLcdBacklightLed(OFF);
  518.             setStandbyLed(ON);
  519.             setSpeedFanHeatVent(OFF);
  520.             setSpeedFanCool(OFF);
  521.             updateLcd();
  522.         }
  523.         else if (inTemp < setTemp)
  524.         {
  525.             climaState = STATE_ON_HEAT;
  526.             setSpeedFanHeatVent(1);
  527.             setSpeedFanCool(OFF);
  528.             setHeatElement(ON);
  529.             updateLcd();
  530.         }
  531.         else if (inTemp > setTemp)
  532.         {
  533.             climaState = STATE_ON_COOL;
  534.             setSpeedFanHeatVent(OFF);
  535.             setSpeedFanCool(1);
  536.             setHeatElement(OFF);
  537.             updateLcd();
  538.         }
  539.         else
  540.         {
  541.             climaState = STATE_ON_VENT;
  542.             setHeatElement(OFF);
  543.             setSpeedFanHeatVent(1);
  544.             setSpeedFanCool(1);
  545.             updateLcd();
  546.         }
  547.         break;
  548.         }
  549.         default:
  550.             break;
  551.     }
  552.     }
  553.  /* void stateMachine(void) */
  554.  
  555.  
  556.  
  557. /*******************************************************************************
  558.  * Update outputs Function
  559.  */
  560. void updateOutputs(void)
  561. {
  562.     /* put standby LED on the output */
  563.     PORTDbits.RD7 = standbyLed;
  564.  
  565.     /* put LCD backlight on the output */
  566.     PORTDbits.RD6 = lcdBacklightLed;
  567.  
  568.     /* put cooling element state on the output */
  569.     PORTDbits.RD1 = coolElement;
  570.  
  571.     /* put heating element on the output */
  572.     PORTDbits.RD0 = heatElement;
  573. } /* void updateOutputs(void) */
  574.  
  575.  
  576.  
  577. /*******************************************************************************
  578.  * Init Buttons Function
  579.  */
  580. void initButtons(void)
  581. {
  582.     /* RB0 left push button */
  583.     TRISB0 = 1; /* only RB0 as input */
  584. } /* void initButtons(void) */
  585.  
  586.  
  587.  
  588. /*******************************************************************************
  589.  * Init Buttons Function
  590.  */
  591. void initAdc(void)
  592. {
  593.     /* RA0 potentiometer */
  594.     TRISA = TRISA | (1<<0); /* RA0 as input */
  595.  
  596.     /* RA1 onboard temperature sensor */
  597.     TRISA = TRISA | (1<<1); /* RA1 as input */
  598.  
  599.     /* RA3 outside temperature sensor */
  600.     TRISA = TRISA | (1<<3); /* RA3 as input */
  601.  
  602. // ADCON0
  603.     ADCON0bits.CHS = 1;         // channel AN0
  604.     ADCON0bits.GO_nDONE = 0;    // ADC Idle
  605.     ADCON0bits.ADON = 0;        // turn OFF ADC module
  606. // ADCON1
  607.     ADCON1bits.VCFG = 0b00;     // Voltage ref AVdd AVss
  608.     ADCON1bits.PCFG = 0b0000;   // A/D Port config AN0-AN4 analog, AN5-AN15 digital
  609. // ADCON2
  610.     ADCON2bits.ADFM = 1;        // 0=left, 1=right justified
  611.     ADCON2bits.ACQT = 0b111;    // A/D Acquisition time 20 Tad
  612.     ADCON2bits.ADCS = 0b010;    // A/D Conversion clock Fosc/32
  613. } /* void initButtons(void) */
  614.  
  615.  
  616.  
  617. /*******************************************************************************
  618.  * ADC Read Function
  619.  */
  620. unsigned int ADCRead(unsigned char ch)
  621. {
  622.    if(ch>13) return 0;  //Invalid Channel
  623.  
  624.    ADCON0bits.ADON = 1;     // disable AD module
  625.    ADCON0bits.CHS = ch;      // select channel
  626.    ADCON0bits.ADON = 1;     // switch on the adc module
  627.    ADCON0bits.GO_nDONE = 1; //Start conversion
  628.    while(ADCON0bits.GO_nDONE); //wait for the conversion to finish
  629.    //ADCON0bits.ADON = 0;  //switch off adc
  630.  
  631.    return ADRES;
  632. } /* unsigned int ADCRead(unsigned char ch) */
  633.  
  634.  
  635.  
  636. /*******************************************************************************
  637.  * Init PWM Function
  638.  */
  639. void initPwm(void)
  640. {
  641.     /* set pin direction */
  642.  
  643.     /* set pin out for heat element */
  644.     TRIS_HEAT_ELEMENT = TRIS_OUT;
  645.     /* set pin out for heat/vent fan */
  646.     TRIS_HEAT_VENT_FAN = TRIS_OUT;
  647.     /* set pin out for cool fan */
  648.     TRIS_COOL_FAN = TRIS_OUT;
  649.  
  650. } /* void initPwm(void) */
  651.  
  652.  
  653. /*******************************************************************************
  654.  * Init Timer Function
  655.  */
  656. void initTmr(void)
  657. {
  658.     PORTJbits.RJ6 = 0;
  659.     PORTJbits.RJ7 = 0;
  660.     TRISJbits.TRISJ7 = 0;   // This sets pin RB7 to output
  661.     TRISJbits.TRISJ6 = 0;   // This sets pin RB6 to output
  662.  
  663.     PORTDbits.RD3 = 0;
  664.     PORTDbits.RD4 = 0;
  665.     PORTDbits.RD5 = 0;
  666.     TRISDbits.TRISD3 = 0;   // This sets pin RB7 to output
  667.     TRISDbits.TRISD4 = 0;   // This sets pin RB6 to output
  668.     TRISDbits.TRISD5 = 0;   // This sets pin RB6 to output
  669.  
  670.  
  671. // START - TMR0 setup
  672.     TMR0 = 0;
  673.     T0CON = 0;
  674.     T0CONbits.TMR0ON = 0;   // timer OFF
  675.     T0CONbits.T08BIT = 0;   // 16bit timer
  676.     T0CONbits.T0CS = 0;     // internal source clock
  677.     T0CONbits.T0SE = 0;     // source edge Low-2-High
  678.     T0CONbits.PSA = 0;      // prescaler active
  679.     T0CONbits.T0PS = 0;     // prescaler 3 bits (Fosc/4)/presc 1:2 => timer clock = (10MHz/4) / 2 = 1.25Mhz
  680.     /* 1000Hz */
  681.     /* 1khz => 1ms period
  682.      * 1.25Mhz / 1khz => 1250
  683.      * 65535 - 1250 = 64285 = FB1Dx;
  684.      */
  685.     TMR1H = 0xFB;           //
  686.     TMR1L = 0x1D;           //
  687.     T0IE = 1; //enable TMR0 overflow interrupts
  688.     GIE = 1; //enable Global interrupts
  689.     T0CONbits.TMR0ON = 1;   // timer ON
  690. // END - TMR0 setup
  691. } /* void initTmr(void) */
  692.  
  693. unsigned char a = 0;
  694.  
  695.  
  696. /*******************************************************************************
  697.  * Interrupt Service Routine (keyword "interrupt" tells the compiler it's an ISR)
  698.  */
  699. void interrupt ISR(void)
  700. {
  701. // TMR0 interrupt
  702.     if (T0IE && T0IF)
  703.     {
  704.         T0IF  = 0;              // clear interrupt flag
  705.         T0CONbits.TMR0ON = 0;   // Turn timer off to reset count register
  706.         TMR0H = 0xFB;           // Reset timer count - 1khz = 1ms
  707.         TMR0L = 0x1D-2;         //
  708.  
  709.         T0CONbits.TMR0ON = 1;   // Turn timer back on
  710.         tick++; // each 1ms
  711.  
  712.         if ((tick & 0b11) == 0b11) // each 4ms
  713.         {
  714.             cnt++;
  715.            
  716.             if (cnt == 25) // each 25*4ms = 100ms
  717.             //if (cnt == 250) // each 250*4ms = 1000ms
  718.             {
  719.                 ev = 1;
  720.                 cnt = 0;
  721.             }
  722.         }
  723.  
  724.         PORTJbits.RJ0 = tick&1;
  725.  
  726.         /* generate SW PWM for cool FAN */
  727.         if (fanSpeedCool > (tick & 0x07))
  728.             PIN_FAN_COOL = PIN_ON;
  729.         else
  730.             PIN_FAN_COOL = PIN_OFF;
  731.  
  732.         /* generate SW PWM for heat/vent FAN */
  733.         if (fanSpeedHeatVent > (tick & 0x07))
  734.             PIN_FAN_HEAT_VENT = PIN_ON;
  735.         else
  736.             PIN_FAN_HEAT_VENT = PIN_OFF;
  737.  
  738.         /* generated SW PWM for heat element */
  739.         if (levelHeat  > (tick & 0x07))
  740.             PIN_HEAT_ELEMENT = PIN_ON;
  741.         else
  742.             PIN_HEAT_ELEMENT = PIN_OFF;
  743.     }
  744.  
  745.     // process other interrupt sources here, if required
  746. }
  747.  
  748.  
  749.  
  750. /*******************************************************************************
  751.  * Init Function
  752.  */
  753. void init(void)
  754. {
  755.     TRISD=0;
  756.     PORTD=0;
  757.     MEMCONbits.EBDIS=1;
  758.  
  759.     /* init UART */
  760.     UART_Init();
  761.     UART_puts((char *)"\n\rInitializing...\n\r");
  762.  
  763.     /* init buttons */
  764.     initButtons();
  765.  
  766.     /* init ADC */
  767.     initAdc();
  768.  
  769.     /* init PWM */
  770.     initPwm();
  771.  
  772.     /* init TMR */
  773.     initTmr();
  774.  
  775.     /* init LCD */
  776.     LcdInit();
  777.  
  778. /* START - transition from "Power OFF" to "OFF"*/
  779.     DBG("-> T to OFF\n\r");
  780.     climaState = STATE_OFF;
  781.     setLcd(); /* LCD according to OFF state */
  782.  
  783.     /* Standby LED ON */
  784.     setStandbyLed(ON);
  785.     /* set Heating element OFF */
  786.     setHeatElement(OFF);
  787.  
  788.     /* cool FAN speed 0 = OFF */
  789.     setSpeedFanCool(0);
  790.     /* heat/vent FAN speed 0 = OFF */
  791.     setSpeedFanHeatVent(0);
  792.     /* Heat level 0 = OFF */
  793.     setHeatElement(0);
  794.  
  795. /* END - transition from "Power OFF" to "OFF"*/
  796. } /* void init(void) */
  797.  
  798.  
  799.  
  800. /*******************************************************************************
  801.  * Main Function
  802.  */
  803. void main(void)
  804. {
  805.     init();
  806.     char i = 0;
  807.  
  808.     PORTJbits.RJ0 = 0;
  809.     TRISJbits.TRISJ0 = 0;
  810.     PORTJbits.RJ1 = 0;
  811.     TRISJbits.TRISJ1 = 0;
  812.     PORTJbits.RJ2 = 0;
  813.     TRISJbits.TRISJ2 = 0;
  814.  
  815.     TRISD = 0;
  816.  
  817. /* START - endless loop */
  818.     while(1)
  819.     {
  820.         while (ev == 0);
  821.         ev = 0;
  822.  
  823.         i++;
  824.         PORTJbits.RJ2 = 1;
  825.  
  826.         checkInputs();
  827.         stateMachine();
  828.         updateOutputs();
  829.  
  830.         /* clear events */
  831.         leftButtonEv = 0; /* clear event from left button */
  832.  
  833.         PORTJbits.RJ2 = 0;
  834.     }
  835. /* END - endless loop */
  836.  
  837. } /* void main(void) */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top