****************************** Copyright(C) CaKe ************************************
===================================
===============**Copyright information: CaKen Studio ==============
===============**File name: main.c ==============
==============**Author : CaKen ==============
===============**Version number: V1.0
===============**Time : 2014.08.28 ===============
===============**Function description: Touch dimming desk lamp experimental test ==============================**Sina Weibo: CaKen ==============
***************************************************
#include"
STC12C5A60S2.H" //STC12Cx052 or STC12Cx052AD series MCU header file
sbit ON_OFF_Key = P2 ^ 0; //ON/OFF switch key
sbit Add_Key = P2 ^ 3; //Add brightness (+)
sbit Doc_Key = P2 ^ 7; //Reduce brightness (-)
//LED connected to P1.3 (PWM0)
unsigned char Bright = 0x88; //global variable, brightness value
bit POWER = 0; //LED light on/off status flag
******************************************************
Function name: PWM initialization function
Call: PWM_Init();
Parameters: None
Return value: None
Result: Initialize PCA to PWM mode with an initial duty cycle of 0
Note: If you need more PWM outputs, just plug them into CCAPnH and CCAPnL.
******************************************************/
void PWM_Init (void)
{
CMOD = 0x02; //Set PCA timer
CL = 0x00;
CH = 0x00;
CCAPM0 = 0x42; //PWM0 sets PCA working mode to PWM mode (0100 0010)
CCAP0L = 0x00; //Set the initial value of PWM0 to be the same as CCAP0H
CCAP0H = 0x00; //PWM0 is initially 0
CR = 1; //Start PCA timer
}
*************************************************************
Function name: PWM0 duty cycle setting function
Call: PWM0_Set();
Parameter: 0x00~0xFF (0~255 can also be used)
Return value: None
Result: Set the PWM mode duty cycle, all high level when it is 0, all low level when it is 1
Note: If you need the setting function of PWM1, just change the 0 in CCAP0L and CCAP0H to 1.
**********************************************************/
void PWM0_Set (unsigned char a)
{
CCAP0L= a; //Set value to be written directly into CCAP0L
CCAP0H= a; //Set value to be written directly into CCAP0H
}
*************************************************************
Function name: millisecond CPU delay function
Call: DELAY_MS (?);
Parameter: 1~65535 (parameter cannot be 0)
Return value: None
Result: The CPU mode delay is the same as the parameter value in milliseconds
Note: When applied to 1T MCU, i<600; when applied to 12T MCU, i<125
************************************************************/
void DELAY_MS (unsigned int a)
{
unsigned int i;
while( --a != 0)
{
for(i = 0; i < 600; i++);
}
}
********************************************************
Function name:
UART serial port initialization function
Call: UART_Init();
Parameters: None
Return value: None
Result: Start UART serial port receiving interrupt, allow serial port receiving, start T/C1 to generate baud rate (occupied)
Note: The oscillator crystal is 12MHz, and the PC serial port is set to [
void UART_Init (void)
{
EA = 1; //Enable general interrupts (if interrupts are not used, they can be //masked)
ES = 1; //Enable UART serial port interrupt
TMOD = 0x20; //Timer T/C1 working mode 2
SCON = 0x50; //Serial port working mode 1, serial port reception is allowed (serial port reception is prohibited when SCON = 0x40)
TH1 = 0xFD; //The initial value of the timer is set to 9600 for the high 8 bits
TL1 = 0xFD; //Set the low 8 bits of the timer initial value
PCON = 0x80; //Baud rate multiplier
TR1 = 1; //Timer starts
}
*******************************************************
Function name: UART serial port sending function
Call: UART_Send (?);
Parameter: Data to be sent by the UART serial port (8 bits/1 byte)
Return value: None
Result: Send the data in the parameter to the UART serial port, and exit after confirming that the sending is completed.
Remark:
***********************************************************/
void UART_Send (unsigned char UART_data)
{ //Define the serial port sending data variable
SBUF = UART_data; //Send the received data back
while(TI == 0); //Check the send interrupt flag
TI = 0; //Set the transmit interrupt flag to 0 (cleared by software)
}
***********************************************************
Function name: main function
Call: No
Parameters: None
Return value: None
Result: Infinite loop at the beginning of the program
Note: Dimming desk lamp, touch delay lamp
*************************************************************/
void main (void)
{
PWM_Init(); //PWM initialization
UART_Init(); //Serial port initialization
P2M0 = 0x00; //Set the P2 interface to high impedance input
P2M1 = 0xFF; //Touch button enabled
PWM0_Set(0);
UART_Send (Bright); //Send the brightness value to the PC
DELAY_MS(200); //Delay to wait for the I/O interface level to stabilize
while(1)
{
unsigned char a;
UART_Send (Bright); //Send the brightness value to the PC
if(ON_OFF_Key == 1) //The on/off key is pressed
{
if(POWER == 0) //If the current state is off, execute the light-on program
{
for(a=0;a<=Bright;a++)
{
PWM0_Set(a);
DELAY_MS (20); //Time interval for fading to dark
}
PWM0_Set(Bright); //Achieve the stored LED brightness
POWER = 1; //Turn the status flag to on
// DELAY_MS (10000); //After 10 seconds, the light will automatically turn off
// PWM0_Set(0);
}
else //If the current state is on, execute the light off procedure
{
for(a=Bright;a>0;a--) // loop gradually darkens
{
PWM0_Set(a);
DELAY_MS (20); //Time interval for fading to dark
}
PWM0_Set(0); //Turn off LED
POWER = 0; //Turn the status flag to off
}
while(ON_OFF_Key == 0); //Wait for the button to be released
}
if(Add_Key == 1 && POWER == 1) //The brightness key is pressed and the light is on
{
Bright++; //Brightness value plus 1
PWM0_Set(Bright); //Write the value into PWM to control the LED brightness
if(Bright >= 0xFD) //If the brightness value is greater than 0xFD, it will not increase.
{
Bright = 0xFD;
}
DELAY_MS (20); //Time interval for gradual brightness change
}
if(Doc_Key == 1 && POWER == 1) //The brightness reduction key is pressed while the light is on
{
Bright--; //Brightness value minus 1
PWM0_Set(Bright); //Write the value into PWM to control the LED brightness
if(Bright < 0x08) //If the brightness value is less than 0x08, it will not be reduced
{
Bright = 0x08;
}
DELAY_MS (100); //Time interval for gradual brightness change
}
}
}