2710 views|1 replies

2

Posts

0

Resources
The OP
 

Microcontroller touch dimming desk lamp program [Copy link]

  • /************************************************************************************************
  • Program name: Touch dimming desk lamp (UART serial port debugging version)
  • Written by: Du Yang
  • Written on: July 25
  • Hardware support: STC12C2052 12MHz
  • Interface description: P3.7 (PWM0) uses PWM to control LED, P1.5~P1.7 connect 3-way touch keys
  • Modification log:
  • NO.1-20090725_0622 Complete the touch switch LED lamp part and PWM dimming part program.
  • NO.2-20090725_0638 Complete the gradual brightness when switching the lamp.
  • NO.3-20090725_1208 Add UART serial port debugging program.
  • /*************************************************************************************************
  • Description: When making the circuit, you need to put the touch key and the VCC line together, and then touch the key pin and the VCC line at the same time.
  • /*************************************************************************************************/
  • #include//STC12Cx052 or STC12Cx052AD series MCU header file
  • sbit ON_OFF_Key = P1 ^ 7; //ON/OFF switch key
  • sbit Add_Key = P1 ^ 6; //Add brightness (+)
  • sbit Doc_Key = P1 ^ 5; //Subtract brightness (-)
  • //LED is connected to P3.7 (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();
  • Parameter: None
  • Return value: None
  • Result: Initialize PCA to PWM mode with an initial duty cycle of 0
  • Remarks: If you need more PWM outputs, just insert them into CCAPnH and CCAPnL directly
  • /************************************************************************************/
  • 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 PWM0 initial value to 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. When it is 0, all high levels are set; when it is 1, all low levels are set
  • Remarks: 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; //The set value is written directly to CCAP0L
  • CCAP0H= a; //The set value is written directly to CCAP0H
  • }
  • /******************************************************************************************************/
  • /************************************************************************************************
  • Function name: Millisecond CPU delay function
  • Call: DELAY_MS (?);
  • Parameter: 1~65535 (parameter cannot be 0)
  • Return value: None
  • Result: Occupy CPU mode to delay the same millisecond time as the parameter value
  • Remarks: 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();
  • Parameter: None
  • Return value: None
  • Result: Start UART serial port receive interrupt, allow serial port reception, start T/C1 to generate baud rate (occupied)
  • Remarks: The oscillator crystal is 12MHz, and the PC serial port settings are [4800, 8, None, 1, None]
  • /**********************************************************************************************/
  • void UART_init (void){
  • EA = 1; //Enable total interrupt (if interrupt is not used, it can be //shielded)
  • 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 = 0xF3; //Set the high 8 bits of the initial value of the timer
  • TL1 = 0xF3; //Set the low 8 bits of the initial value of the timer
  • PCON = 0x80; //Baud rate multiplication (shield the baud rate of this sentence to 2400)
  • TR1 = 1; //Timer start
  • }
  • /**************************************************************************************************/
  • /*************************************************************************************************
  • Function name: UART serial port sending function
  • Call: UART_T (?);
  • 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
  • Remarks:
  • /**********************************************************************************************/
  • void UART_T (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 send interrupt flag to 0 (cleared by software)
  • }
  • /******************************************************************************************************/
  • /************************************************************************************************
  • Function name: Main function
  • Call: None
  • Parameter: None
  • Return value: None
  • Result: At the beginning of the program, infinite loop
  • Remarks:
  • /******************************************************************************************/
  • void main (void){
  • PWM_init(); //PWM initialization
  • UART_init(); //The serial secretary is initialized here^_^
  • P1M0 = 0xff; //Set the P1 interface to high-impedance input
  • P1M1 = 0x00; //Touch button enable
  • DELAY_MS (200); //Delay waiting for the I/O interface level state to stabilize
  • while (1){ //Loop program part
  • unsigned char a; //Temporary variable
  • UART_T (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 dimming
  • }
  • PWM0_set(Bright); //Reach the stored LED brightness
  • POWER = 1; //Turn the status flag to on
  • }else{ //If the current state is on, execute the light-off program
  • for(a=Bright;a>0;a--){ //Loop dimming
  • PWM0_set(a);
  • DELAY_MS (20);//Time interval for fading to dark
  • }
  • PWM0_set(0); //Turn off LED
  • POWER = 0; //Change the status flag to off
  • }
  • while(ON_OFF_Key == 1); //Wait for the button to be released
  • }
  • if(Add_Key == 1 && POWER == 1){ //The brightness increase key is pressed, and the light is on
  • Bright++; //Brightness value plus 1
  • PWM0_set(Bright); //Write the value into PWM to control LED brightness
  • if(Bright >= 0xFD){ //If the brightness value is greater than 0xFD, it will no longer increase
  • Bright = 0xFD;
  • }
  • DELAY_MS (20); //Time interval for fading brightness
  • }
  • if(Doc_Key == 1 && POWER == 1){ //The brightness decrease key is pressed, and the light is on
  • Bright--; //Brightness value minus 1
  • PWM0_set(Bright); //Write the value into PWM to control LED brightness
  • if(Bright < 0x08){ //If the brightness value is less than 0x08, it will no longer decrease
  • Bright = 0x08;
  • }
  • DELAY_MS (20); //Time interval for gradual brightness change
  • }
  • }
  • }
  • /**************************************************************************************************/

This post is from 51mcu
 

2

Posts

0

Resources
2
 
good......
This post is from 51mcu
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list