Advertisement
igendel

PIC12F675 Fireplace toy project

Aug 9th, 2014
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <xc.h>
  3.  
  4. #pragma config FOSC = INTRCIO   // INTOSC: I/O function on GP4/5
  5. #pragma config WDTE = OFF       // Watchdog Timer
  6. #pragma config PWRTE = OFF      // Power-Up Timer
  7. #pragma config MCLRE = ON       // MCLR external control
  8. #pragma config BOREN = OFF      // Brown-out Detect
  9. #pragma config CP = OFF         // Program memory code Protection
  10. #pragma config CPD = OFF        // Data memory code protection
  11.  
  12. #define PWM_STEPS 16
  13. #define STATIC_REPEATS 4
  14. #define LED_NUM 3
  15.  
  16. // Timeout to start dimming
  17. // Each 1K is ~7.5 seconds
  18. #define MAX_CYCLES 10000
  19. // Timeout for dim step
  20. #define DIM_COUNT 200
  21.  
  22. struct tLED {
  23.  
  24.     char brightness;
  25.     char pinBinary;
  26.    
  27. } LED[LED_NUM];
  28.  
  29. void initLEDs() {
  30.  
  31.     char j;
  32.     for (j = 0; j < LED_NUM; j++) {
  33.         LED[j].brightness = 0;
  34.     }
  35.  
  36.     LED[0].pinBinary = 4; // GP2
  37.     LED[1].pinBinary = 16; // GP4
  38.     LED[2].pinBinary = 32; // GP5
  39. }
  40.  
  41. void initRegisters() {
  42.  
  43.     ANSEL = 0; // All pins Digital
  44.     TRISIO = 0; // All pins output
  45.     CMCON = 7; // Comparator off
  46.     ADCON0 = 0; //A/D conversion off
  47.     GPIO = 0; // Outputs LOW
  48.  
  49. }
  50.  
  51. void main() {
  52.  
  53.     char j;
  54.     unsigned char r, pwmStep, pattern;
  55.     unsigned char randResult;
  56.    
  57.     unsigned int cycleCount = 0;
  58.     char highPWM = PWM_STEPS;
  59.  
  60.     initRegisters();
  61.     initLEDs();
  62.  
  63.     while (1) {
  64.  
  65.         randResult = rand();
  66.         for (j = 0; j < LED_NUM; j++) {
  67.  
  68.             switch (randResult & 3) {
  69.                 case 1: if (LED[j].brightness < highPWM)
  70.                           LED[j].brightness++;
  71.                         break;
  72.                 case 2: if (LED[j].brightness > 0)
  73.                           LED[j].brightness--;
  74.                         break;
  75.             } // switch
  76.             randResult >>= 2;
  77.  
  78.         } // for
  79.  
  80.         for (r = 0; r < STATIC_REPEATS; r++)
  81.             for (pwmStep = 0; pwmStep < PWM_STEPS; pwmStep++) {
  82.  
  83.                 pattern = 0;
  84.                 for (j = 0; j < LED_NUM; j++)
  85.                     if (LED[j].brightness > pwmStep) pattern |= LED[j].pinBinary;
  86.                 GPIO = pattern;
  87.  
  88.             } // for
  89.  
  90.         cycleCount++;
  91.  
  92.         // Are we in the dimming phase?
  93.         if (highPWM < PWM_STEPS) {
  94.  
  95.             if (cycleCount == DIM_COUNT) {
  96.               cycleCount = 0;
  97.               highPWM--;
  98.               if (!highPWM) break;
  99.             }
  100.  
  101.         } else if (cycleCount == MAX_CYCLES) {
  102.             // Switch to dimming mode
  103.             highPWM--;
  104.             cycleCount = 0;
  105.            }
  106.  
  107.     } // while
  108.  
  109.     asm("sleep");
  110.  
  111. } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement