Guest User

Untitled

a guest
Jul 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. // tab space = 4
  2. /*********************************************************************
  3. * DISCLAIMER: *
  4. * The software supplied by Renesas Technology America Inc. is *
  5. * intended and supplied for use on Renesas Technology products. *
  6. * This software is owned by Renesas Technology America, Inc. or *
  7. * Renesas Technology Corporation and is protected under applicable *
  8. * copyright laws. All rights are reserved. *
  9. * *
  10. * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, *
  11. * IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO IMPLIED *
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
  13. * APPLY TO THIS SOFTWARE. RENESAS TECHNOLOGY AMERICA, INC. AND *
  14. * AND RENESAS TECHNOLOGY CORPORATION RESERVE THE RIGHT, WITHOUT *
  15. * NOTICE, TO MAKE CHANGES TO THIS SOFTWARE. NEITHER RENESAS *
  16. * TECHNOLOGY AMERICA, INC. NOR RENESAS TECHNOLOGY CORPORATION SHALL, *
  17. * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR *
  18. * CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER ARISING OUT OF THE *
  19. * USE OR APPLICATION OF THIS SOFTWARE. *
  20. *********************************************************************/
  21.  
  22. /*-----------------------------------------------------------------------------
  23. FILE NAME: Demo1.c
  24. -----------
  25. DESCRIPTION: Demo program for R8C/17 SKP
  26. -----------
  27. DETAILS: This demo has two modes of operation: light and temperature.
  28. Switch S1 is used to determine which demo is running.
  29.  
  30. i) Light: If S1's actuator is towards pushbutton S2, the light
  31. demo is running. The LEDs sequential blink speed varies depending
  32. on the light level detected using the CdS Cell (the brighter it
  33. is, the faster the blink speed becomes and vice versa). Pressing
  34. S2 changes the blinking sequence of the LEDs.
  35.  
  36. ii) Temperature: If S1's actuator is away from S2, the temperature
  37. demo is running. Temperature is sampled and the LEDs are lit based
  38. on rising or falling temperature. When the temperature of the
  39. Thermistor stays constant the yellow LED is lit. When the
  40. temperature increases, the Red LED is lit, and when the temperature
  41. decreases, the Green LED is lit.
  42.  
  43. ------------------
  44. Revision History
  45. ------------------
  46. 1.0 Nov 19, 2004
  47. Initial Version
  48. -----------------------------------------------------------------------------*/
  49.  
  50. #include "skp8c17_bsp.h"
  51.  
  52. /* Function Prototypes */
  53. void main(void);
  54. void mcu_init(void); /* in mcu_init.c */
  55. void tmrZ_isr(void);
  56.  
  57. // bits 0:RED, 1:YLW, 2:GRN
  58. // values 1:OFF, 0 ON)
  59. const char led_pattern1[] = {6,4,0,1,3,7,3,1,0,4,6,7};
  60. const char led_pattern2[] = {6,5,3,5};
  61.  
  62. /*****************************************************************************
  63. Name : main
  64. Parameters : none
  65. Returns : nothing
  66. Description: Main Program
  67. *****************************************************************************/
  68. void main(void)
  69. {
  70. mcu_init(); /* initialize MCU */
  71. ENABLE_LEDS /* LED initialization */
  72.  
  73. /* Configure Timer X - for Timer Z underflow count source */
  74. txmr = 0x00; // timer mode
  75. prex = 0xFF; // X prescaler value (n)
  76. tx = 0x1F; // X register value (m)
  77. tcss = 0x21; // use f8 for timer X, underflow of timer X for Z
  78.  
  79. /* Configure Timer Z - for ADC data acquisition */
  80. tzmr = 0x00; // timer mode
  81. prez = 0x01; // Z prescaler value (q)
  82. tzpr = 0x01; // Z primary register initial value (p)
  83. //tcss = 0x21; // use f8 for timer Z (already set above)
  84.  
  85. // Timer mode output rate = ((n+1)(m+1)(q+1)(p+1))/(20Mhz/8)
  86.  
  87. /* Configure ADC - AN10 (CdS cell) */
  88. adcon0 = 0x96; // AN10, one shot mode, fAD/2
  89. adcon1 = 0x20; // 8-bit mode, Vref connected.
  90. adcon2 = 0x01; // Sample and hold enabled
  91.  
  92. DISABLE_IRQ // disable irqs before setting irq registers
  93. tzic = 2; // Set the timer Z's interrupt priority to level 2
  94. ENABLE_IRQ // enable interrupts
  95.  
  96. txs = 1; // Start timer X
  97. tzs = 1; // Start timer Z
  98.  
  99. while(1)
  100. {
  101. /* loop forever */
  102. /* All action takes place in Timer interrupt routine */
  103. }
  104. }
  105.  
  106. /*****************************************************************************
  107. Name: tmrZ_isr
  108. Parameters:
  109. Returns:
  110. Description: Timer Z ISR sets AD channel based on slider sw. value,
  111. gets a new AD sample and preloads timer X.
  112. It also varies the LED's D1, D2, & D3 sequentially by controlling the LED
  113. control variable.
  114. *****************************************************************************/
  115. #pragma INTERRUPT tmrZ_isr
  116. void tmrZ_isr(void)
  117. {
  118. /* Static Variables (retains values between interrupts */
  119. static char pattern_index = 0; // Current index of LED pattern array
  120. static char last_temp_value = 128; // Last A/D temperature value taken
  121.  
  122. /* Auto Variable (created on stack each time ) */
  123. unsigned char current_temp_value; // Used to hold the current A/D temperature value
  124. union byte_def led_value; // Next led value to display
  125. // NOTE: "union byte_def" is defined in
  126. // the sfr header file
  127.  
  128. /* Read slider switch S1 to determine current mode */
  129. if( S1 == 0 ) // Switch S1 towards button
  130. {
  131. /*** MEASURE LIGHT USING CDS CELL ***/
  132.  
  133. ch0 = 0; // measure light level
  134. adcon1 = 0x20; // 8-bit mode, Vref connected.
  135. adst = 1; // Start A2D conversion
  136. while(adst); // wait for A/D conversion start bit to return to 0
  137. tzpr = adl; // read AD value and preload Timer Z to vary the LED switching rate
  138.  
  139. /* Advance to the next LED pattern and display */
  140. /* The current position of the button determines the pattern to use */
  141. if( S2 ) // Button UP?
  142. {
  143. if(++pattern_index >= 12)
  144. pattern_index = 0; // return to initial state
  145. led_value.byte = led_pattern1[pattern_index];
  146. }
  147. else // Button DOWN
  148. {
  149. if(++pattern_index >= 4)
  150. pattern_index = 0; // return to initial state
  151. led_value.byte = led_pattern2[pattern_index];
  152. }
  153.  
  154. /* Set LEDs */
  155. RED_LED = led_value.bit.b0; // Bit 0 is RED LED value
  156. YLW_LED = led_value.bit.b1; // Bit 1 is YELLOW LED value
  157. GRN_LED = led_value.bit.b2; // Bit 2 is GREEN LED value
  158. }
  159. else
  160. {
  161. /*** MEASURE TEMPERATURE USING THERMISTER ***/
  162.  
  163. tzpr = 0x40; /* Fixed sample rate */
  164.  
  165. /* Read temperature */
  166. ch0 = 1; // measure temperature
  167. adcon1 = 0x28; // 10-bit mode, Vref connected, fAD/2
  168. adst = 1; // Start A2D conversion
  169. while(adst); // wait for A/D conversion start bit to return to 0
  170.  
  171. /* Reduce 10-bit AD sample to 8-bits by removing the last 2 bits */
  172. current_temp_value = ad >> 2;
  173.  
  174. /* Change LEDs if temperature has changed */
  175. RED_LED = YLW_LED = GRN_LED = LED_OFF; // All LEDs OFF
  176. if (last_temp_value == current_temp_value)
  177. YLW_LED = LED_ON; // yellow LED ON - temp stable
  178. else if (last_temp_value > current_temp_value)
  179. RED_LED = LED_ON; // red LED ON - temp rising
  180. else
  181. GRN_LED = LED_ON; // green LED ON - temp falling
  182.  
  183. last_temp_value = current_temp_value; // prepare for next reading/comparison
  184. }
  185. }
Add Comment
Please, Sign In to add comment