当前位置:网站首页>Stm8 MCU ADC sampling function is triggered by timer
Stm8 MCU ADC sampling function is triggered by timer
2022-06-26 04:40:00 【Embedded @hxydj】
In the use of STM8 Single chip microcomputer ADC When the function , Read ADC There are generally two ways to generate data , One is to constantly read the sampling flag bit , To judge ADC Whether the sampling is over , One is to inform the system whether the sampling is finished by interrupting .
Sometimes sampling ADC Data time , At certain intervals , Fixed point desampling data . The general way to use it is to use a timer , Then read in the timer interrupt function ADC Sampled data . But the sampling time in this way is not fixed , For example, after entering the timer interrupt ,ADC Sampling has just ended , You can directly use the currently sampled data . But if you're unlucky , After entering the timed interrupt ,ADC Sampling has just begun , Then we need to wait until ADC After sampling , To use data . This will cause each read ADC There will be a random period of data waiting for ADC Data delay .
General reference STM8 SCM manual found ,ADC Sampling can be triggered by a timer .
It's triggered by a timer ADC When sampling , The timing time of the timer is fixed , The sampling time is also fixed , In this way, the interval of sampling data is fixed . This is triggered by a timer ADC The sampling time of , Each read can be completely guaranteed ADC The time interval of sampling data is the same , Thus, the error of data is avoided .
Data manual for open ADC The trigger function is described as follows :
For how to set... Through code ADC Trigger , There is no official explanation , There are no relevant routines found on the Internet . So I can only grope by myself , Fortunately, through my own exploration , Pass the timer successfully TRGO The event triggered ADC Start of .
About ADC Setting of related registers , Basically, it's what I said above 6 strip . Next, you need to set the relevant registers of the timer .
The timer only needs to be set CR2 In register MMS Just a bit .
Next, it is implemented through code .
#include "adc.h"
#include "main.h"
#include "led.h"
u16 DATAH = 0; //ADC High conversion value 8 position
u16 DATAL = 0; //ADC The conversion value is low 8 position
_Bool ADC_flag = 0; //ADC Conversion success flag
u16 adc_cnt = 0;
//AD Channel pin initialization
void ADC_GPIO_Init( void )
{
PD_DDR &= ~( 1 << 2 ); //PD2 Set to input
PD_CR1 &= ~( 1 << 2 ); //PD2 Set as dangling input
PD_DDR &= ~( 1 << 3 ); //PD3 Set to input
PD_CR1 &= ~( 1 << 3 ); //PD3 Set as dangling input
}
//ch For the MCU ADC passageway
//ADC Enter the channel initialization entry parameter to indicate the channel selection
void ADC_CH_Init( u8 ch )
{
char l = 0;
ADC_CR1 = 0x00; //fADC = fMASTER/2, 8Mhz Single conversion , Prohibit conversion
ADC_CR2 = 0x00; // Default left alignment When reading data, first read high and then low
ADC_CR2 |= ( 1 << 6 ); // External trigger enable
ADC_CSR |= ch; // Control status register Choose to AD Input channel Such as :PD2(AIN3)
ADC_TDRL = ( 1 << ch ); // Prohibit the corresponding channel Schmidt trigger function 1 Move left ch position
ADC_CR1 |= 0x01; // Can make ADC And start to switch
ADC_CSR |= ( 1 << 5 ); //EOCIE Enable conversion end interrupt EOC Interrupt enable
for( l = 0; l < 100; l++ ); // Time delay , Guarantee ADC The power on of the module is completed At least 7us
ADC_CR1 = ADC_CR1 | 0x01; // Once again CR1 The lowest position of the register 1 Can make ADC And start to switch
}
u16 value = 0;
//AD Interrupt service function Interrupt number 22
#pragma vector = 24 // IAR Interrupt number in , To be in STVD Add... To the interrupt number in 2
__interrupt void ADC_Handle( void )
{
ADC_CSR &= ~0x80; // The conversion end flag bit is cleared EOC
// Default left alignment When reading data, read high first 8 position Reread low 8 position
DATAH = ADC_DRH; // read out ADC The result is high 8 position
DATAL = ADC_DRL; // read out ADC The results are low 8 position
ADC_flag = 1; // ADC Interrupt flag Set up 1
value = ( DATAH << 2 ) + DATAL ; // Get ten bit accuracy data 0--1024
adc_cnt++;
LED = !LED;
}
stay ADC In the code , Compared with ordinary ADC Initialization mode , There is only one more sentence to add here ADC_CR2 Register settings .
ADC_CR2 |= ( 1 << 6 ); // External trigger enable

stay ADC_CR2 In the register Enable the external trigger conversion function , Set the external trigger event to Internal timer 1 TRG event .
here ADC The single trigger mode is used , Turn on the external trigger function , Turn on ADC Conversion interrupted , When ADC After the conversion is complete , It will enter into ADC In interruption , Read the sampled data in the interrupt , Then flip LED The state of , Observe with an oscilloscope LED Change of pin level , We can know ADC Interrupt the frequency of entry .
Next, write the timer initialization code .
unsigned long time_cnt = 0;
// Use Timer triggered ADC sampling
void tim1_init( void )
{
TIM1_ARRH = ( unsigned char )( 1000 >> 8 ); // timing 1ms
TIM1_ARRL = ( unsigned char )( 1000 );
TIM1_PSCRH = ( unsigned char )( 0x0F >> 8 ); // 16M / (1+15) =1M
TIM1_PSCRL = ( unsigned char )( 0x0F );
TIM1_RCR = 0x00; // Repeat counter value
TIM1_SR1 = ( ~0x01 ); // Clear update interrupt flag
TIM1_CR2 |= ( 2 << 4 ); // Enable signal , Used to trigger the output (TRGO)
TIM1_CR1 |= 0x01; // Enable counter
TIM1_IER |= 0x01; // Update interrupt enable
}
#pragma vector = 13 //IAR Interrupt number in , To be in STVD Add... To the interrupt number in 2
__interrupt void Timer1_Handle( void ) //1ms Timed interrupt
{
TIM1_SR1 = ( ~0x01 ); // Clear update interrupt flag
time_cnt++;
}
Timer initialization code , There is also one more line of initialization code than normal .
TIM1_CR2 |= ( 2 << 4 ); // Enable signal , Used to trigger the output (TRGO)
Used to turn on the timer TRG function .
After testing , Here's the timer CR2 Value in register Can only be set to 010 perhaps 011, When set to other values , Can't trigger ADC sampling . At the beginning of the test, follow the instructions on the chip data ,MMS The value of is set to 001,ADC It can't be triggered , I thought it was a question of method , It turned out to be MMS Value setting problem .
ADC And after the timer initialization code is set , Next, initialize these two functions in the main function , According to the information , First initialize ADC after , Then initialize the timer .
void main( void )
{
__asm( "sim" ); // No interruptions
SysClkInit();
delay_init( 16 );
LED_GPIO_Init();
ADC_GPIO_Init();
ADC_CH_Init(3);
tim1_init();
__asm( "rim" ); // Open interrupt
while( 1 )
{
}
}
Next, run the program .
Respectively in ADC Interrupt and timer interrupt use a variable to count the number of interrupt execution , You can see... Through the variable observation window ,ADC The number of interrupts is more than that of the timer 1 Time . This is because ADC At initialization time , It has been run once .
Then observe with an oscilloscope LED The level of the port .

The timing time of the timer is 1ms,LED The high and low level time is also 1ms, Description triggered by timer ADC The sampling function works normally .
To reduce the number of system entry interrupts , The interrupt function of the timer can be turned off . After the timer interrupt function is turned off ,ADC The trigger function of can still be used normally .
You only need to open one ADC interrupt , Plus the timer TRG After triggering the function , Can be realized ADC Timing sampling function .
边栏推荐
- 1.19 learning summary
- Condition query
- Hash problem
- MySQL index details
- mysql高级学习(跟着尚硅谷老师周阳学习)
- Redis cache message queue
- Use shell script to analyze system CPU, memory and network throughput
- Add, delete, modify and query curd in PHP native SQL
- Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start
- 小程序中实现视频通话及互动直播功能
猜你喜欢

Dameng database backup and restore

Install cenos in the virtual machine

Motivational skills for achieving goals

Microsoft prohibits Russian users from downloading and installing win10/11
![[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface](/img/37/84b7d59818e854dac71d6f06700cde.jpg)
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface

CDN with OSS acceleration

Install dbeaver and connect Clickhouse

#微信小程序# 在小程序里面退出退出小程序(navigator以及API--wx.exitMiniProgram)

1.21 learning summary
![[geek challenge 2019] rce me](/img/92/978c54fb42391198300c76ae92893d.jpg)
[geek challenge 2019] rce me
随机推荐
Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
08_ Spingboot integrated redis
0622-马棕榈跌9%
1.18 learning summary
Guide de la pompe de données Oracle
小程序中实现视频通话及互动直播功能
2021-01-31
1.20 learning summary
Jenkins introduces custom jars
#微信小程序# 在小程序里面退出退出小程序(navigator以及API--wx.exitMiniProgram)
08_SpingBoot 集成Redis
1.21 learning summary
[Qunhui] this suite requires you to start PgSQL adapter service
1.11 learning summary
Gateway can not connect to tcp://127.0.0.1: Connection refused
Tp6 multi table Association (table a is associated with table B, table B is associated with table C, and table d)
Notes on enterprise wechat development [original]
CTF serialization and deserialization
NPM installation tutorial
Physical design of database design (2)