Advertisement
Mark2020H

UV Dental timer

Feb 27th, 2023 (edited)
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.99 KB | None | 0 0
  1. /*Using the Boost C Compiler for pic micro  MD Harrington purchased from Maplin electronics now closed
  2. 8 years ago before people changed for the worse I was doing this
  3. A dental  UV tooth whitener I built for someone in an urgent hurry  so they could hang on to a job
  4.  
  5. They might not remember I do though */
  6.  
  7.  
  8. /* You can find the full code  on my git hub account plus schematics parts list , PCB outlay and hex code
  9. https://github.com/markh2016/UV_Dental_Timer
  10.  
  11. https://www.facebook.com/mark.harrington.14289/
  12.  
  13.  
  14.  
  15. */
  16.  
  17.  
  18.  
  19. #include <system.h>
  20. // DATA CONFIGURATION FOR 12F675
  21. #ifndef _PIC12F675
  22. #error "This sample code was designed for PIC12F675"
  23. #pragma DATA_CONFIG, _CPD_OFF & _CP_OFF  & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF  & _MCLRE_OFF & _INTRC_OSC_NOCLKOUT
  24. #endif //_PIC12F675
  25. // CONFIG FOR OSCAL 344C
  26. // GLOBAL VARIABLES
  27. /* These are the Interrupts and flags for later use
  28. GIE Global interrupt enable
  29. PIE Peripheral interrupt enable
  30. INTF Interrupt Flag must be cleared after interrupt
  31. TOIE                Timer interrupt enable
  32. TIOF                Timer interrupt Overflow Flag
  33. GPIE                general purpose interrupt enable
  34. GPIF  general purpose interrupt flag
  35. */
  36. #define  CLEAR  0x00 ;
  37. #define  Pressed  0 ;      // this is bit 0 of our Flags register
  38. #define     State    1 ;   // this  is bit 1 of our Flags Register
  39. #define     TimerState 2 ;    // this is bit 2 of ourFlags register
  40. unsigned swVal = 0 ;
  41. unsigned char  curState ;
  42. unsigned char  timer    ;
  43. unsigned char seconds ;       // stores and increments seconds
  44. unsigned char minutes ;       // stores and increments minutes
  45. unsigned char milliseconds ;  // stores and  increments the milliseconds
  46. unsigned char TMRFlag  ;      /* stores the status of the timer
  47.    when timer is activated for light is Zero  
  48.    for final minute countdown this is 1 */
  49. unsigned char max_seconds ;   /* For storing the maximum seconds
  50.   when light is on this 9 when final minute
  51.   this is equals  1 */
  52. unsigned char tmrOffset ;
  53. unsigned char oldseconds = 0x00 ;
  54. // method forward declaration
  55. void startTimerLight(unsigned char maxMinutes);
  56. void init(void) ;
  57. void sound(unsigned char period , unsigned char cycles, unsigned char duration) ;
  58. unsigned char startTimer(unsigned char maxvalue) ;
  59. unsigned char  getKeyPress(void) ;
  60. void notifyFinish(unsigned char lastSecs , unsigned char secsInterrupt) ;
  61. /*
  62.  personal notes for PCB Layout this may differ
  63.  from time to time
  64.  Soundport gpio.2 ;
  65.     button    gpio.0 ;
  66.     Luxion    gpio.1 ;
  67. */
  68. void interrupt(void)
  69. {
  70.  // HANDLES  TIMER INTERRUPT
  71.  // disable GIE
  72. clear_bit(intcon, T0IE);            // Disable Timer 0 Interrupts
  73. clear_bit(intcon, GIE);             // clear Global interrupt
  74.  if(TMRFlag == 0x00)
  75.  {
  76.  startTimerLight(9) ;
  77.  }
  78. clear_bit(intcon, T0IF);
  79.   if((timer == 1)&&(TMRFlag == 0x00))
  80.  {
  81.   set_bit(intcon,GIE) ;
  82.   set_bit(intcon, T0IE);
  83.  }
  84.  if ((TMRFlag == 0x01)&&(timer ==0))
  85.  {
  86.   set_bit(intcon,GIE) ;
  87.   set_bit(intcon, T0IE);
  88.    // final minute call notify
  89.    notifyFinish(60,1);
  90.  }
  91. }
  92. // 12F675 register settings
  93.        /* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  94.           NU   NU  out in  nu  out out  in
  95.           0    0   0    1   0   0    0   1
  96.  =  0x11 hex
  97.   */
  98. void init(void)
  99. {
  100.     vrcon = CLEAR;                    // Turn off the voltage reference peripheral
  101.     cmcon = 0b00000111;              // Disble comparator module - make Port  digital I/O port
  102.     ansel = CLEAR;                    // For the 12F675 Only
  103.     gpio = CLEAR;                    // Clear the output ports
  104.  trisio = 0x11  ;                 // set up port direction
  105.     wpu = 0b00000000;                // En/disable Weak Pull Ups
  106.  /* T1CON — TIMER1 CONTROL REGISTER (ADDRESS: 10h)
  107.           7
  108.           6   Gate enable bit sued for external triggering
  109.        5   T1CKPS1: Next two bits are the pre scale bits
  110.           4   T1CKPS0
  111.    3   T1OSCEN: LP Oscillator Enable Control bit If INTOSC without CLKOUT oscillator is active:
  112.    2   T1SYNC: Timer1 External Clock Input Synchronization Control bit
  113.    1   TMR1CS: Timer1 Clock Source Select bit
  114.           0   TMR1ON: Timer1 On bit setting enables timer
  115.     */
  116.    // clear all registers    
  117.     seconds = CLEAR ;
  118.     minutes = CLEAR ;
  119.     milliseconds = CLEAR ;
  120.     TMRFlag = CLEAR ;
  121.     max_seconds = CLEAR ;
  122.     tmrOffset = CLEAR  ;
  123.     oldseconds = CLEAR ;
  124.     timer = 0x00 ;
  125.  // Enable/disbale interrupts
  126.     clear_bit(intcon, T0IE);            // Disable Timer 0 Interrupts
  127.     clear_bit(intcon, GIE);             // Disable Global Interrupts
  128.     clear_bit(intcon, T0IF);
  129.  delay_us(100) ; // allow time for cpu to settle
  130. }
  131. void sound(unsigned char period , unsigned char cycles, unsigned char duration)
  132. {
  133.  for(int j = 0 ; j < duration; j++ )
  134.  {
  135.  for(int i = 0 ; i < cycles; i ++)
  136.  {
  137.  delay_us(period);
  138.  {
  139.   toggle_bit(gpio,2) ;
  140.  }
  141.  }
  142.  }
  143.  gpio.2 = 0 ; // switch off luxian
  144. }
  145. /*Notes A simpler way to access a bit within a byte is to use a constant expression of the form
  146.  (1 << n)
  147. where n is the bit number and << is the left shift operator
  148. */
  149. // starts the TMR0 interrupt loads timer with value and
  150. // begins light time on sequence
  151. unsigned char startTimer(unsigned char maxvalue)
  152. {
  153.     tmr0 = CLEAR ;
  154.    // GPPU INTEDG T0CS T0SE PSA PS2 PS1 PS0
  155.  //   0    0     0     0   0   1    1  1
  156.     option_reg = 0x07 ; // use  256 pre scaling
  157.     // enable interrupts and timer interrupt
  158.     // clear overflow flag to start with
  159.     clear_bit(intcon, T0IF);
  160.     set_bit(intcon, GIE);             // enable Global Interrupts
  161.     tmr0 = maxvalue ;
  162.  set_bit(intcon, T0IE);            // enable Timer 0 Interrupts
  163.     return 0x01 ;
  164. }
  165. unsigned char getKeyPress(void)
  166. {    
  167.         unsigned char  value = 0x00 ;
  168.      while(value == 0x00)
  169.  {
  170.      value = gpio.0 ; // get the value
  171.  delay_us(255);
  172.  }
  173.  value = (0x11 & value) ;  // masking to ensure we have correct bits for input
  174.      return value  ;
  175. }
  176. void startTimerLight(unsigned char maxMinutes)
  177. {
  178. milliseconds += 1 ; // increment milliseconds by one
  179.             if(milliseconds == 60)
  180.  { milliseconds  = 0 ; // reset milliseconds
  181.  seconds +=1 ;  // increment seconds by one
  182.  if(seconds == 60)
  183.  {
  184.   seconds = 0 ;
  185.   minutes += 1 ;
  186.    if(minutes == maxMinutes)
  187.    {
  188.  minutes = 0 ;
  189.      TMRFlag = 0x01 ;
  190.  timer = 0 ;
  191.    }// end if minutes
  192.  }// end if seconds
  193.  }// end if milliseconds
  194.  tmr0 = CLEAR ;  
  195.  tmr0 = tmrOffset ;
  196. }
  197. void notifyFinish(unsigned char lastSecs , unsigned char secsInterrupt)
  198. {
  199.   milliseconds +=1 ;
  200.      if(milliseconds == 60)
  201.      {
  202.  milliseconds  = 0 ; // reset milliseconds
  203.  seconds +=1 ;  // increment seconds by one
  204.  oldseconds+=1 ;
  205.  if(oldseconds == secsInterrupt)
  206.  {
  207.    // sound alarm for finishing
  208.    sound(200,200,5);  // makes a beep noise for number of reps
  209.    oldseconds = 0 ;
  210.  }
  211.  if(seconds == lastSecs)
  212.  {
  213.  seconds = 0;
  214.                     timer = 0 ; // we have finshed back to key press
  215.  TMRFlag = 0x00 ; // reset this flag to notify back into timer state
  216.  gpio.1 = 0 ; // switch off led
  217.  gpio = CLEAR ;
  218.  // finally disable intrerrupts again
  219.   // disable intterupt
  220.      clear_bit(intcon, T0IE);            // Disable Timer 0 Interrupts
  221.      clear_bit(intcon, GIE);             // Disable Global Interrupts
  222.      clear_bit(intcon, T0IF);
  223.  }
  224.   }
  225.  tmr0 = CLEAR ;  
  226.  tmr0 = tmrOffset ;
  227. }
  228. void main()
  229. {
  230.    // sets up the calibration of Osscal
  231.  asm{
  232.  bsf _status,RP0
  233.         call 0x3ff
  234.         movwf _osccal
  235.         bcf _status,RP0
  236.  }// end asm
  237. // sound the buzzer once and light the led for a second
  238.    init() ;
  239.    gpio.1 = 1 ;         // turn on the led
  240.    sound(200,255,10);  // makes a beep noise for number of reps
  241.    gpio.1 = 0 ; // turn this off
  242.  //main program loop start here
  243.  while(1)
  244.   {
  245.         // check to see if timer is on Zero indicates off
  246.         if(timer == 0)
  247.         {
  248.          swVal= getKeyPress();
  249.  if(swVal==1)
  250.  { TMRFlag = 0x00 ;
  251.      tmrOffset  = 188 ;
  252.  timer =  startTimer(tmrOffset) ;
  253.  gpio.1 = 1;
  254.  }
  255.          }
  256.    }
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement