ÔÚ¡¶stm8sÖÐÎIJο¼ÊÖ²á.pdf¡·µÚ24Õ ģÄâ/Êý×Öת»»Æ÷(ADC)Õ½ÚÖУ¬Ëµµ½ADC¾«¶È10bit£¬×ª»»Ä£Ê½ÓÐ5ÖÖ£ºµ¥´Îģʽ£¬µ¥´ÎÁ¬Ðø£¬µ¥´ÎÁ¬Ðø´ø»º´æģʽ£¬µ¥´ÎɨÃèģʽ£¬Á¬ÐøɨÃèģʽ¡£
µ¥´ÎģʽµÄ´úÂëÒѾ·¢¹ýÌû£¬ÇëËÑË÷¡¶STM8SF003F3P6 Ä£Êýת»»ADCϵÁÐÖ®Ò»:µ¥´Îת»»Ä£Ê½¡·£¬Ï־͵¥´ÎÁ¬ÐøģʽµÄʹÓÃ×÷Ò»¸ö˵Ã÷£¬µ¥´ÎÁ¬ÐøģʽÊʺÏÓÚ¶Ôµ¥¸öͨµÀ¶à´Î²ÉÑùµÄ³¡ºÏ£¬Ö»ÒªÉèÖüĴæÆ÷ ADC_CR1[1]µÄCONTλΪ1,È»ºó¿ªÆôת»»,ÄÇô¾Íת»»²»Í£Ö¹ÁË£¬Çë¿´ÏÂÃæµÄ´úÂë¡£
1. ʾÀý´úÂë - µ¥Í¨µÀÁ¬Ðø²âÁ¿
/********************************************************************************
³ÌÐòÃû: ADCµ¥´ÎÁ¬Ðø²âÁ¿ÑÝʾ v0.0
¿ª·¢»·¾³: IAR + ST-LINKV2
ÈÕÆÚ: 2017-7-10
writeby: HZX
*******************************************************************************/
#include "stm8s.h"
#include "stm8s_clk.h"
#include "stm8s_gpio.h"
#define Blue_OFF GPIO_WriteHigh(GPIOC, GPIO_PIN_7)//blueÕýÈ·¸ßµçƽµ¼Í¨
#define Blue_ON GPIO_WriteLow(GPIOC, GPIO_PIN_7)
void uart1_Init(void)
{
UART1_Init(9600,UART1_WORDLENGTH_8D,
UART1_STOPBITS_1,UART1_PARITY_NO,
UART1_SYNCMODE_LASTBIT_DISABLE,UART1_MODE_TXRX_ENABLE);
UART1_Cmd(ENABLE);
}
void SendStr(char *p)
{
while(*p != 0)
{
UART1_SendData8(*p++);
while(UART1_GetFlagStatus(UART1_FLAG_TC)==RESET){;}
}
}
void delay(u32 t)
{
while(t--);
}
void main(void)
{
unsigned short adc1,adc2;
char SendBuf[50]={0};
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//ÅäÖÃϵͳʱÖÓΪ16M
CLK_HSECmd(DISABLE); //½ûÖ¹ÍⲿʱÖÓ
uart1_Init(); //´®¿Ú1³õʼ»¯Îª9600²¨ÌØÂÊ
GPIO_Init(GPIOC, GPIO_PIN_7, GPIO_MODE_OUT_PP_LOW_FAST); //ledµçÔ´½ÅÉèÖÃΪÊä³öģʽ
Blue_ON;
//-------------------------------------------
//ÒÔϲâÁ¿AIN3ͨµÀµÄÊý×ÖÁ¿
ADC1->CSR = (0X3<<0); //ͨµÀÑ¡Ôñ AIN3(PD2),ͬʱÇåÁãEOCλ
ADC1->CR2 = (1<<3); //ÓÒ¶ÔÆë,·ÇɨÃèģʽ
ADC1->CR1 |= (1<<1); //Á¬Ðøģʽ,CONT=1
if((ADC1->CR1&0x1) == 0) //ÅжÏģʽÊÇ·ñÒѾÉϵç
{ ADC1->CR1 |= (1<<0); } //ADCÄ£¿éÉϵç
ADC1->CR1 |= (1<<0); //¿ªÊ¼²âÁ¿
while (1)
{
if( (ADC1->CSR&(1<<7)) ) //ÅжϲâÁ¿ÊÇ·ñÍê³É
{
adc1 = ADC1->DRL; //¶Á³öADCÊý×ÖÁ¿
adc1 |= ADC1->DRH<<8;
//-------------------------------------------
//ÒÔÏÂÏòPC¶ËÉÏλ»ú·¢ËÍ
SendBuf[0] = adc1/1000%10 + '0';
SendBuf[1] = adc1/100%10 + '0';
SendBuf[2] = adc1/10%10 + '0';
SendBuf[3] = adc1%10 + '0';
SendBuf[4] = '\r';
SendBuf[5] = '\n';
SendBuf[6] = '\0';
SendStr(SendBuf);
}
delay(6553);
}
}
2. ˵Ã÷×ܽá
´ËģʽÓëµ¥´ÎģʽµÄÇø±ðÊÇ£¬Ã¿´Î¶Á³öÒ»´Î²âÁ¿Öµºó£¬ADCģʽ²¢²»Í£Ö¹£¬¶øÊÇÂíÉϽøÐÐÏÂÒ»´Î²âÁ¿£¬ÕâÑù¾ÍÉÙÁËÒ»¾äÔÙ´ÎÆô¶¯µÄ´úÂ룬Ìá¸ßÁËת»»Ð§ÂÊ¡£
3.ºóÐø
Ï´ÎÑÝʾ "ADC µ¥´ÎÁ¬Ðø´ø»º´æģʽ ",´Ëģʽ±ÈÉÏÃæÕâÖÖģʽ¸üÓÐЧÂÊ£¬¾´Çë¹Ø×¢!