STM8单片机ADCTimerUSART寄存器直接操作实用例程文档格式.docx
《STM8单片机ADCTimerUSART寄存器直接操作实用例程文档格式.docx》由会员分享,可在线阅读,更多相关《STM8单片机ADCTimerUSART寄存器直接操作实用例程文档格式.docx(8页珍藏版)》请在冰豆网上搜索。
#defineADC_RATIO((uint16_t)806)/*ADC_RATIO=(3.3*1000*1000)/4095*/
#defineSampleADC((uint8_t)0x0A)
/*Privatevariables---------------------------------------------------------*/
uint8_tBuffer[4]={0,0,0,0};
uint16_tADCdata=0;
uint16_tADCvalue=0;
unsignedcharLED=1;
unsignedcharc=8;
uint16_tacode=1234;
//43"
+"
0x2B;
voidDelay(__IOuint16_tnCount)
{
/*DecrementnCountvalue*/
while(nCount!
=0)
{
nCount--;
}
}
//intputchar(intc)
//{
//while((USART1_SR&
0x80)==0x00);
//UART2_sendchar((u8)c);
//return(c);
//}
staticvoidCLK_Config(void)
/*SelectHSIassystemclocksource*/
CLK_SYSCLKSourceSwitchCmd(ENABLE);
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
/*Highspeedexternalclockprescaler:
1*/
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
while(CLK_GetSYSCLKSource()!
=CLK_SYSCLKSource_HSI)
{}
/*EnableADC1clock*/
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1,ENABLE);
/*EnableDMA1clock*/
//CLK_PeripheralClockConfig(CLK_Peripheral_DMA1,ENABLE);
/*EnableTIM2clock*/
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2,ENABLE);
CLK_PeripheralClockConfig(CLK_Peripheral_USART1,ENABLE);
staticvoidGPIO_Config(void)
PC_DDR=0X20;
PC_CR1=0X20;
PA_DDR=0X0C;
PA_CR1=0X0C;
}
staticvoidUSART1_Config(void)
//ADCvalue=USART1_DR;
USART1_BRR2=0x03;
USART1_BRR1=0x68;
//16M/9600=683
USART1_CR2=0x0C;
//Transmitter&
receiverenable
staticvoidADC_Config(void)
ADC1_CR2=0x22;
//risignedge,softwaestart,samplingtime16ADCclock
ADC1_SQR3=0x80;
//ADC=15channel
ADC1_TRIGR1=0x10;
//useinternalref,samplingtime16ADCclockforRef
RI_ASCR1=0x00;
voidTIM2_Config(void)
TIM2_PSCR=0x07;
//应是16,但只能置三位,所以是111fCK_PSC/2(PSC[2:
0]).
//TIM2_PSCRL=0x3F;
//PSCR=0x1F3F,f=8M/(0x1F3F+1)=1000Hz,每个计数周期1ms
TIM2_ARRH=0x00;
//自动重载寄存器ARR=0x01F4=500
TIM2_ARRL=0x18;
//24X4us=96us每记数500次产生一次中断,即500ms
TIM2_IER=0x01;
//允许更新中断
TIM2_CR1=0x05;
//计数器使能,开始计数,只允许更新中断请求
voidDelay(uint16_tnCount);
/*Privatefunctions---------------------------------------------------------*/
//#pragmavector=ADC1_EOC_vector
//__interruptvoidADC1_EOC(void)
//{
//ADCdata=ADC_GetConversionValue(ADC1);
//}
#pragmavector=TIM2_OVR_UIF_vector
__interruptvoidTIM2_OVR_UIF(void)
asm("
sim"
);
staticuint8_tmeasurements=SampleADC;
staticuint32_taccumulator=0;
uint32_taverage=0;
uint16_tfactor10=1000;
int8_ti;
ADCdata=0;
TIM2_SR1=0x00;
//ClearUIF
ADC1_SR=0x00;
//ClearEOC
ADC1_CR1=0x03;
//EOCinterruptunable,softwaresetstart,ADCenable
//while(!
(ADC1_SR&
0x01));
/*最后一位不是1,结果就是全零,结果为False,!
则是True,循环下去;
若是1,则跳出。
*/
/*GetlastADCconverteddata.*/
Delay(0x000F);
ADCdata=(uint16_t)(ADC1_DRH);
ADCdata=(uint16_t)((uint16_t)((uint16_t)ADCdata<
<
8)|ADC1_DRL);
accumulator+=ADCdata;
if(measurements==0)
{
LED=!
LED;
measurements=SampleADC;
average=accumulator/SampleADC;
ADCvalue=(uint16_t)((uint32_t)((uint32_t)average*(uint32_t)ADC_RATIO)/(uint32_t)1000);
accumulator=0;
while(!
USART1_SR_TXE);
for(i=3;
i>
=0;
i--)
{
Buffer[i]=(ADCvalue/factor10)%10+48;
//提取十进制每一位,并转换成ASCII码
factor10=factor10/10;
while(!
USART1_DR=Buffer[i];
//发送每一位,先发高位
}
USART1_DR=32;
//发送空格
}
measurements--;
rim"
voidmain(void)
asm("
GPIO_Config();
GPIO_Init(LED_GPIO_PORT,LED_GPIO_PINS,GPIO_Mode_Out_PP_Low_Fast);
CLK_Config();
ADC_Config();
TIM2_Config();
USART1_Config();
//DMA_Config();
while
(1){
PA_ODR_ODR2=LED;
PA_ODR_ODR3=LED;