Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tab space = 4
- /*********************************************************************
- * DISCLAIMER: *
- * The software supplied by Renesas Technology America Inc. is *
- * intended and supplied for use on Renesas Technology products. *
- * This software is owned by Renesas Technology America, Inc. or *
- * Renesas Technology Corporation and is protected under applicable *
- * copyright laws. All rights are reserved. *
- * *
- * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, *
- * IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO IMPLIED *
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
- * APPLY TO THIS SOFTWARE. RENESAS TECHNOLOGY AMERICA, INC. AND *
- * AND RENESAS TECHNOLOGY CORPORATION RESERVE THE RIGHT, WITHOUT *
- * NOTICE, TO MAKE CHANGES TO THIS SOFTWARE. NEITHER RENESAS *
- * TECHNOLOGY AMERICA, INC. NOR RENESAS TECHNOLOGY CORPORATION SHALL, *
- * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR *
- * CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER ARISING OUT OF THE *
- * USE OR APPLICATION OF THIS SOFTWARE. *
- *********************************************************************/
- /*-----------------------------------------------------------------------------
- FILE NAME: Demo1.c
- -----------
- DESCRIPTION: Demo program for R8C/17 SKP
- -----------
- DETAILS: This demo has two modes of operation: light and temperature.
- Switch S1 is used to determine which demo is running.
- i) Light: If S1's actuator is towards pushbutton S2, the light
- demo is running. The LEDs sequential blink speed varies depending
- on the light level detected using the CdS Cell (the brighter it
- is, the faster the blink speed becomes and vice versa). Pressing
- S2 changes the blinking sequence of the LEDs.
- ii) Temperature: If S1's actuator is away from S2, the temperature
- demo is running. Temperature is sampled and the LEDs are lit based
- on rising or falling temperature. When the temperature of the
- Thermistor stays constant the yellow LED is lit. When the
- temperature increases, the Red LED is lit, and when the temperature
- decreases, the Green LED is lit.
- ------------------
- Revision History
- ------------------
- 1.0 Nov 19, 2004
- Initial Version
- -----------------------------------------------------------------------------*/
- #include "skp8c17_bsp.h"
- /* Function Prototypes */
- void main(void);
- void mcu_init(void); /* in mcu_init.c */
- void tmrZ_isr(void);
- // bits 0:RED, 1:YLW, 2:GRN
- // values 1:OFF, 0 ON)
- const char led_pattern1[] = {6,4,0,1,3,7,3,1,0,4,6,7};
- const char led_pattern2[] = {6,5,3,5};
- /*****************************************************************************
- Name : main
- Parameters : none
- Returns : nothing
- Description: Main Program
- *****************************************************************************/
- void main(void)
- {
- mcu_init(); /* initialize MCU */
- ENABLE_LEDS /* LED initialization */
- /* Configure Timer X - for Timer Z underflow count source */
- txmr = 0x00; // timer mode
- prex = 0xFF; // X prescaler value (n)
- tx = 0x1F; // X register value (m)
- tcss = 0x21; // use f8 for timer X, underflow of timer X for Z
- /* Configure Timer Z - for ADC data acquisition */
- tzmr = 0x00; // timer mode
- prez = 0x01; // Z prescaler value (q)
- tzpr = 0x01; // Z primary register initial value (p)
- //tcss = 0x21; // use f8 for timer Z (already set above)
- // Timer mode output rate = ((n+1)(m+1)(q+1)(p+1))/(20Mhz/8)
- /* Configure ADC - AN10 (CdS cell) */
- adcon0 = 0x96; // AN10, one shot mode, fAD/2
- adcon1 = 0x20; // 8-bit mode, Vref connected.
- adcon2 = 0x01; // Sample and hold enabled
- DISABLE_IRQ // disable irqs before setting irq registers
- tzic = 2; // Set the timer Z's interrupt priority to level 2
- ENABLE_IRQ // enable interrupts
- txs = 1; // Start timer X
- tzs = 1; // Start timer Z
- while(1)
- {
- /* loop forever */
- /* All action takes place in Timer interrupt routine */
- }
- }
- /*****************************************************************************
- Name: tmrZ_isr
- Parameters:
- Returns:
- Description: Timer Z ISR sets AD channel based on slider sw. value,
- gets a new AD sample and preloads timer X.
- It also varies the LED's D1, D2, & D3 sequentially by controlling the LED
- control variable.
- *****************************************************************************/
- #pragma INTERRUPT tmrZ_isr
- void tmrZ_isr(void)
- {
- /* Static Variables (retains values between interrupts */
- static char pattern_index = 0; // Current index of LED pattern array
- static char last_temp_value = 128; // Last A/D temperature value taken
- /* Auto Variable (created on stack each time ) */
- unsigned char current_temp_value; // Used to hold the current A/D temperature value
- union byte_def led_value; // Next led value to display
- // NOTE: "union byte_def" is defined in
- // the sfr header file
- /* Read slider switch S1 to determine current mode */
- if( S1 == 0 ) // Switch S1 towards button
- {
- /*** MEASURE LIGHT USING CDS CELL ***/
- ch0 = 0; // measure light level
- adcon1 = 0x20; // 8-bit mode, Vref connected.
- adst = 1; // Start A2D conversion
- while(adst); // wait for A/D conversion start bit to return to 0
- tzpr = adl; // read AD value and preload Timer Z to vary the LED switching rate
- /* Advance to the next LED pattern and display */
- /* The current position of the button determines the pattern to use */
- if( S2 ) // Button UP?
- {
- if(++pattern_index >= 12)
- pattern_index = 0; // return to initial state
- led_value.byte = led_pattern1[pattern_index];
- }
- else // Button DOWN
- {
- if(++pattern_index >= 4)
- pattern_index = 0; // return to initial state
- led_value.byte = led_pattern2[pattern_index];
- }
- /* Set LEDs */
- RED_LED = led_value.bit.b0; // Bit 0 is RED LED value
- YLW_LED = led_value.bit.b1; // Bit 1 is YELLOW LED value
- GRN_LED = led_value.bit.b2; // Bit 2 is GREEN LED value
- }
- else
- {
- /*** MEASURE TEMPERATURE USING THERMISTER ***/
- tzpr = 0x40; /* Fixed sample rate */
- /* Read temperature */
- ch0 = 1; // measure temperature
- adcon1 = 0x28; // 10-bit mode, Vref connected, fAD/2
- adst = 1; // Start A2D conversion
- while(adst); // wait for A/D conversion start bit to return to 0
- /* Reduce 10-bit AD sample to 8-bits by removing the last 2 bits */
- current_temp_value = ad >> 2;
- /* Change LEDs if temperature has changed */
- RED_LED = YLW_LED = GRN_LED = LED_OFF; // All LEDs OFF
- if (last_temp_value == current_temp_value)
- YLW_LED = LED_ON; // yellow LED ON - temp stable
- else if (last_temp_value > current_temp_value)
- RED_LED = LED_ON; // red LED ON - temp rising
- else
- GRN_LED = LED_ON; // green LED ON - temp falling
- last_temp_value = current_temp_value; // prepare for next reading/comparison
- }
- }
Add Comment
Please, Sign In to add comment