📄 main.c
字号:
/*******************************************************************************
NEC Electronics 78K0S/KB1+
********************************************************************************
78K0S/KB1+ Sample program
********************************************************************************
8-bit timer H1 (PWM output)
********************************************************************************
<<History>>
2007.7.-- Release
********************************************************************************
<<Overview>>
This sample program presents an example of using the PWM output function of 8-
bit timer H1. The LED1 brightness is controlled through the PWM output duty
by connecting the timer output (TOH1 pin) of 8-bit timer H1 to LED1. The duty
is changed at a cycle of 500 ms by using timer H1 interrupts and LED2 output
is reversed simultaneously.
<Principal setting contents>
- Stop the watchdog timer operation
- Set the low-voltage detection voltage (VLVI) to 4.3 V +-0.2 V
- Generate an internal reset signal (low-voltage detector) when VDD < VLVI after VDD >= VLVI
- Set the CPU clock to 8 MHz
- Set the clock supplied to the peripheral hardware to 8 MHz
<8-bit timer H1 settings>
- Set to the PWM mode
- Enable timer output of the TOH1 pin
- Count clock = fxp/2^6 (125 kHz)
- Timer cycle = 2 ms (8[us/clk] x 250[count] = 2[ms])
<PWM output duty and LEDs>
- LED2 output is reversed simultaneously with the duty that is changed every 500 ms in the following order.
+-----------------+-----------------------------+
| PWM output duty | 10% | 30% | 50% | 70% | 90% | (Hereafter, repeated from 10%)
+-----------------+-----------------------------+
| LED1 brightness | 90% | 70% | 50% | 30% | 10% |
+-----------------+-----------------------------+
# PWM output is high active and LED1 is low active; therefore, the LED
brightness = 100 - duty factor.
<<I/O port settings>>
Input: -
Output: P00-P03, P20-P23, P30-P33, P40-P47, P120-P123, P130
# All unused ports are set as the output mode.
*******************************************************************************/
/*==============================================================================
Preprocessing directive (#pragma)
==============================================================================*/
#pragma SFR /* SFR names can be described at the C source level */
#pragma EI /* EI instructions can be described at the C source level */
#pragma NOP /* NOP instructions can be described at the C source level */
#pragma interrupt INTTMH1 fn_inttmH1 /* Interrupt function declaration:INTTMH1 */
/*==============================================================================
Define the global variables
==============================================================================*/
sreg unsigned char ucTMH1cnt = 250; /* 8-bit variable for counting the number of INTTMH1 interrupts */
/*******************************************************************************
Initialization after RESET
*******************************************************************************/
void hdwinit(void){
unsigned char ucCnt200us; /* 8-bit variable for 200 us wait */
/*------------------------------------------------------------------------------
Initialize the watchdog timer + detect low-voltage + set the clock
------------------------------------------------------------------------------*/
/* Initialize the watchdog timer */
WDTM = 0b01110111; /* Stop the watchdog timer operation */
/* Set the clock <1> */
PCC = 0b00000000; /* The clock supplied to the CPU (fcpu) = fxp (= fx/4 = 2 MHz) */
LSRCM = 0b00000001; /* Stop the oscillation of the low-speed internal oscillator */
/* Check the reset source */
if (!(RESF & 0b00000001)){ /* Omit subsequent LVI-related processing during LVI reset */
/* Set low-voltage detection */
LVIS = 0b00000000; /* Set the low-voltage detection level (VLVI) to 4.3 V +-0.2 V */
LVION = 1; /* Enable the low-voltage detector operation */
for (ucCnt200us = 0; ucCnt200us < 9; ucCnt200us++){ /* Wait of about 200 us */
NOP();
}
while (LVIF){ /* Wait for VDD >= VLVI */
NOP();
}
LVIMD = 1; /* Set so that an internal reset signal is generated when VDD < VLVI */
}
/* Set the clock <2> */
PPCC = 0b00000000; /* The clock supplied to the peripheral hardware (fxp) = fx (= 8 MHz)
-> The clock supplied to the CPU (fcpu) = fxp = 8 MHz */
/*------------------------------------------------------------------------------
Initialize the port 0
------------------------------------------------------------------------------*/
P0 = 0b00000000; /* Set output latches of P00-P03 as low */
PM0 = 0b11110000; /* Set P00-P03 as output mode */
/*------------------------------------------------------------------------------
Initialize the port 2
------------------------------------------------------------------------------*/
P2 = 0b00000000; /* Set output latches of P20-P23 as low (P21: turn on LED2) */
PM2 = 0b11110000; /* Set P20-P23 as output mode */
/*------------------------------------------------------------------------------
Initialize the port 3
------------------------------------------------------------------------------*/
P3 = 0b00000000; /* Set output latches of P30-P33 as low */
PM3 = 0b11110000; /* Set P30-P33 as output mode */
/*------------------------------------------------------------------------------
Initialize the port 4
------------------------------------------------------------------------------*/
P4 = 0b00000000; /* Set output latches of P40-P47 as low (P42: turn on LED1) */
PM4 = 0b00000000; /* Set P40-P47 as output mode */
/*------------------------------------------------------------------------------
Initialize the port 12
------------------------------------------------------------------------------*/
P12 = 0b00000000; /* Set output latches of P120-P123 as low */
PM12 = 0b11110000; /* Set P120-P123 as output mode */
/*------------------------------------------------------------------------------
Initialize the port 13
------------------------------------------------------------------------------*/
P13 = 0b00000001; /* Set output latch of P130 as high */
/*------------------------------------------------------------------------------
Set 8-bit timer H1
------------------------------------------------------------------------------*/
TMHMD1 = 0b00111001; /* Count clock = fxp/2^6 = 125 kHz, PWM mode, enable TOH1 output */
CMP01 = 250-1; /* Initialize CMP01 (cycle: 2 ms) */
CMP11 = 25-1; /* Initialize CMP11 (duty: 10%) */
TMHE1 = 1; /* Start the timer operation */
/*------------------------------------------------------------------------------
Set the interrupt
------------------------------------------------------------------------------*/
IF0 = 0x00; /* Clear invalid interrupt requests in advance */
TMMKH1 = 0; /* Unmask INTTMH1 interrupts */
return;
}
/*******************************************************************************
Main loop
*******************************************************************************/
void main(void){
EI(); /* Enable vector interrupt */
while (1){
NOP();
NOP();
}
}
/*******************************************************************************
Interrupt INTTMH1
*******************************************************************************/
__interrupt void fn_inttmH1(){
if (--ucTMH1cnt == 0){ /* Processing when the number of INTTMH1 interrupts is 250 */
ucTMH1cnt = 250; /* Initialize the number of interrupts */
P2 ^= 0b00000010; /* Reverse LED2 */
if (CMP11 >= 225-1){ /* Processing when the duty is at least 90% */
CMP11 = 25-1; /* Initialize the duty to 10% */
}
else {
CMP11 += 50; /* Increase the duty by 20% */
}
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -