ST 3 in 1 development board stm8 learning led

Publisher:本人在Latest update time:2020-08-19 Source: eefocusKeywords:ST  stm8  led Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

ST 3-in-1 development board stm8 learning led
 
This example uses three peripherals: adc tim and gpio. The program function is to change
the flashing frequency and brightness of the led light according to the analog quantity input by adc.


Configuration process:

Adc related program:
     void ADC_Init(void)
{
    ADC_CR2 = 0x00; Configuration register 2 Configure whether the external trigger is enabled Select the external trigger mode Data alignment bit Scan mode is enabled bit Here it is configured to disable external trigger Conversion data left aligned Disable scan mode?
    ADC_CR1 = 0x00; Configuration register 1 Configure pre-scaling bit Conversion mode is word or continuous ad conversion switch. Here it is configured as fadc=fmaster/2, single conversion, disable adc
    ADC_CSR = 0x03; Control status register: configure various interrupt enable conversion completion flags and conversion channels. Here is the configuration to disable various interrupts Select channel 3 pb3
       ADC_TDRL = 0x20; The ADC_TDRH and ADC_TDRL registers can be used to disable the Schmitt trigger in the AIN analog input pin. Disabling the Schmitt trigger can reduce the power consumption of the I/O pin.


There are many more registers. For specific uses, please refer to the relevant instructions.
}

GPIO related  

 void GPIO_Init(void) 
{
    /* LED IO Configuration */
    /* LD3: PD3 */ Please refer to the GPIO explanation part
    /* LD2: PD1 */
    /* LD1: PD0 */
    PD_DDR |= 0x0D; /* before
    */ PD_CR1 |= 0x0D; /* already */
    PD_CR2 = 0x00; /* */ mentioned

    /* External interrupt pd7 */
    EXTI_CR1 = 0x00; Configuration Trigger mode Here it is configured as falling edge and low level trigger      
    EXTI_CR2 = 0x00; Interrupt trigger mode Falling edge and low level trigger
    PD_DDR &=~0x80; PD7 input mode
    PD_CR2 |= 0x80; Enable PD7 external interrupt
}

Tim related:

 void TIM_Init(void)
{
    /* TIM2 CC2 controls brightness */
    TIM2_CCMR2 |= 0x70; Configured as pwm2 mode, PWM mode 2 - When counting up, once TIMx_CNTTIMx_CCR2, channel 2 is at a valid level, otherwise it is at an invalid level.
     TIM2_CCER1 |= 0x30; /*Select channel 2 Output low level Valid */
    TIM2_ARRH = 0x00;
    TIM2_ARRL = 0xff; /*Configure reload value ARRH default value is ff */
    TIM2_CCR2 = 0x00; /*Output compare 2 value Used to compare with cnt Start
here is zero Output to the pin level is zero
(valid level) PWM2 mode */

    TIM2_PSCR = 0x00; /*Prescaler value Value + 1 */ 
    TIM2_CR1 |= 0x01; Enable count cr1 highest bit Enable preload function
Disable here
TIM2 channel 2 is configured as PWM2 mode above (When counting up, once TIMx_CNTTIMx_CCR2, channel 2 is at a valid level, otherwise it is at an invalid level.) 
  And configure the reload value and prescale value, and start counting. About the counting mode, there should be only the upward counting mode.

The following is the configuration of tim3 channel 1  
                    */
 /* TIM3 CC1 controls flashing */
    TIM3_CCMR1 |= 0x78; Configured as pwm2 mode Turn on the pre-load function
    TIM3_CCER1 |= 0x03; /* Select channel 1 */
    TIM3_ARRH = 0x03;
    TIM3_ARRL = 0xff;          
    TIM3_CCR1H = 0x02;              
TIM3_CCR1L = 0x00;

    TIM3_PSCR |= 0x0d;             
TIM3_CR1 |= 0x81;

}

The configuration of tim3 is the same as tim2. No more explanation.
The most commonly used timer is timing and generating different waveforms. For basic timing, we can directly set it to counting mode. Output comparison and pwm are as described above.

Turn on the output capture function and turn on the corresponding channel, and set the low level to the effective level.
void ADC_Init(void)
{
    ADC_CR2 = 0x00;
    ADC_CR1 = 0x00;
    ADC_CSR = 0x03; Select analog channel 3 input
    ADC_TDRL = 0x20; Turn off the corresponding Schmitt trigger
}
Set adc bit Single conversion Conversion data is left-aligned and stored. Select analog channel 3 input, turn off the corresponding Schmitt trigger (rectified wave),



Ok The above configures the gpio port of the led and the port of the external trigger interrupt, the two comparison outputs of the timer. And adc single conversion input. Take a look at the main program:
 void LED_Control(unsigned char duration)
{
    int i = 0;
    unsigned char uc = 0;
    unsigned long Temp;

    ADC_CR1 |= 0x01;      
     i = 6;           
    while(i--); 
    ADC_CR1 |= 0x01;        
    while(!(ADC_CSR & 0x80));/* Waiting for AD convert finished (EOP=1). 
     
To turn on the adc, you need to assign values ​​to ADON twice. The first time is to turn on the power supply of the adc, and then assign values ​​again when the power supply is stable to start the conversion./*
    Store ADC value to AD_Value */
    AD_Value = ((((unsigned int)ADC_DRH)<<2)+ADC_DRL);

    /* The new duty cycle value is written in CCR. */
    TIM2_CCR2H=0x00;
    TIM2_CCR2L=(unsigned char)(AD_Value>>2);
 Change the value of the compare register according to the value of the adc, because tim2 Used to change the brightness, so the low level time should be short

    if (AD_Value>0x10)
    {
      Temp=(unsigned char)(AD_Value>>8);

      if ((TIM3_ARRH>Temp+0x05)||(TIM3_ARRH      {
        TIM3_ARRH = Temp;
        TIM3_ARRL = (unsigned char)(AD_Value&0xff);
        TIM3_CCR1H = (unsigned char)(AD_Value>>9);
        TIM3_CCR1L = (unsigned char)((AD_Value>>1)&0xff);
      }
}

When the conversion value is greater than 0x10, the light flashes and keeps changing.
    /* Delay time = duration * Y */
    while ( uc < duration ) /* The following loop is run "duration" times. */
    {
      while ( i < 1200 ) /* This loop "Y" waits approximately 4.3ms. */
      {
        i++;
      }
      i = 0;
      uc++;
    }
The above is the delay function
}

Ok So far for the LED example

Keywords:ST  stm8  led Reference address:ST 3 in 1 development board stm8 learning led

Previous article:3-in-1 kit stm8 study notes css
Next article:Description of assert_param() of stm8s

Recommended ReadingLatest update time:2024-11-24 11:38

STMicroelectronics' Q3 2024 financial report is released: STMicroelectronics continues to strengthen its layout in automotive and industrial electrification
STMicroelectronics (ST) recently announced its Q3 2024 financial report, with total revenue of US$3.25 billion, a year-on-year decrease of 26.6%. Net sales revenue from OEM and agency channels decreased by 17.5% and 45.4% year-on-year, respectively. Revenue increased by 0.6% month-on-month, which was in line with th
[Semiconductor design/manufacturing]
New AC LED Converter Topology Design
The traditional constant current control of LED lamps is through AC/DC, and then through the DC/DC converter for constant current control. In the AC/DC converter, a filter capacitor is usually used after the rectifier circuit to smooth the output voltage, but the presence of large capacitors causes the input current w
[Power Management]
New AC LED Converter Topology Design
STM8 ADC initialization + data collection
ADC initialization function: void Adc_Initialize(void) {     ADC2_Init(ADC2_CONVERSIONMODE_SINGLE,ADC2_CHANNEL_8,ADC2_PRESSEL_FCPU_D2,                         ADC2_EXTTRIG_TIM,DISABLE,ADC2_ALIGN_RIGHT,ADC2_SCHMITTTRIG_CHANNEL8,DISABLE);     ADC2_Cmd(ENABLE);      } in: ADC2_CONVERSIONMODE_SINGLE: ADC conversion
[Microcontroller]
STM8 ADC initialization + data collection
How to choose the right LED lamp? Lighting designers have a trick!
  The selection of lighting equipment is one of the important factors in the performance of lighting design results. With the innovation of lighting technology, LED lighting has developed rapidly. Some laboratories can update a new generation of products in one month. The main performance of LED in lighting is:
[Power Management]
Huawei and Alibaba participated in the survey! Leyard’s Micro LED orders exceeded 100 million yuan
On March 31, Leyard released its annual report stating that in 2020, the company achieved operating income of 6.634 billion yuan, a year-on-year decrease of 26.68%; the net profit attributable to the parent company's owners was a loss of 976 million yuan, compared with a net profit of 704 million yuan in the same peri
[Mobile phone portable]
Design of LED display screen based on ARM S3C44B0X
1 Introduction LED display screens are widely used and are an effective tool for information dissemination. In a certain underground mining equipment monitoring system, the ARMS3C44B0X32-bit single-chip microcomputer is selected as the CPU. According to the application requirements, the display part of the monitoring
[Microcontroller]
Design of LED display screen based on ARM S3C44B0X
Risks of LED lamps in abnormal use
As the country's strength increases, more and more preferential policies are being granted to rural areas, and the support for rural development is increasing. Therefore, the rural lighting market has huge potential to be tapped. Under the premise of the rapid development of LED technology, the cost of various compone
[Power Management]
Singapore Energy Group builds Singapore's largest industrial cooling system for STMicroelectronics
Singapore Energy Group builds Singapore's largest industrial cooling system for STMicroelectronics • Designed and installed by Singapore Power, the industrial district cooling system has a cooling capacity of 36,000 tonnes, making it the largest industrial district cooling system in Singapore. • This move will bring
[Internet of Things]
Singapore Energy Group builds Singapore's largest industrial cooling system for STMicroelectronics
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号