資源描述:
《STM8單片機(jī)ADC、Timer、USART寄存器直接操作實(shí)用例程》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、STM8單片機(jī)ADC、Timer、USART實(shí)用例程這是一個(gè)我花了較長時(shí)間摸索出來的STM8L-051的例程,它控制LED燈,Timer2定時(shí)100us進(jìn)入中斷,軟件啟動ADC,采樣10次后取平均,將結(jié)果通過UASART發(fā)送至PC機(jī),在超級終端上顯示的實(shí)用程序,因其內(nèi)存極小,不能用printf等函數(shù),因此對于想用這款資源極少的MCU的開發(fā)者來說,讀這篇文章會大大節(jié)約你的研發(fā)時(shí)間。有不會的問題請發(fā)郵件johnwa@21cn.com。#include#include"stm8l15x.h"#include"iostm8l051f3.h"#de
2、fineLED_GPIO_PORTGPIOA#defineLED_GPIO_PINSGPIO_Pin_2
3、GPIO_Pin_3/*Privatefunctionprototypes-----------------------------------------------*/#defineADC1_DR_ADDRESS((uint16_t)0x5344)#defineBUFFER_SIZE((uint8_t)0x02)#defineBUFFER_ADDRESS((uint16_t)(&Buffer))#defineASCII_NUM_0((uint8_t
4、)48)#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
5、_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_SYSCLKSourceSwitchC
6、md(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_P
7、eripheralClockConfig(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(v
8、oid){//ADCvalue=USART1_DR;USART1_BRR2=0x03;USART1_BRR1=0x68;//16M/9600=683USART1_CR2=0x0C;//Transmitter&receiverenable}staticvoidADC_Config(void){ADC1_CR2=0x22;//risignedge,softwaestart,samplingtime16ADCclockADC1_SQR3=0x80;//ADC=15channelADC1_TRIGR1=0x10;//useinternalref,samplingt
9、ime16ADCclockforRefRI_ASCR1=0x00;