ÔÚ¡¶stm8sÖÐÎIJο¼ÊÖ²á.pdf¡·µÚ24Õ ģÄâ/Êý×Öת»»Æ÷(ADC)Õ½ÚÖУ¬Ëµµ½ADC¾«¶È10bit£¬×ª»»Ä£Ê½ÓÐ5ÖÖ£ºµ¥´Îģʽ£¬µ¥´ÎÁ¬Ðø£¬µ¥´ÎÁ¬Ðø´ø»º´æģʽ£¬µ¥´ÎɨÃèģʽ£¬Á¬ÐøɨÃèģʽ£¬±¾È˸տªÊ¼Ê¹ÓÃʱҲ·Ö²»ÇåÕ⼸ÖÖµÄÇø±ð£¬¾¹ý¶à´ÎʵÑ飬ÏÖ¿ªÌû˵Ã÷Õâ4ÖÖģʽµÄʹÓùý³Ì£¬·½±ãºóÀ´ÕßÉÙ×ßÍä·£¬Ò²»¶ÓʹÓùýµÄ¸úÌû½»Á÷¡£
1. Ó¦Óõç·ͼʾ
µç·ÖÐMCU²ÉÓÃÄÚ²¿16MÕñµ´Ê±ÖÓ£¬Ä£ÄâµçѹÁ½Â··Ö±ð´ÓAIN3(PD2),AIN2(PC4)ÊäÈ룬²âÁ¿½á¹ûͨ¹ý´®¿Ú£¨PD5,PD6£©Êä³ö¡£
2. ¹¦ÄÜÐèÇó
²âÁ¿ AIN3(PD2), AIN2(PC4) µÄÄ£Äâµçѹת»»³ÉÊý×ÖÁ¿Í¨¹ý´®¿Ú1£¬·¢Ë͵½PC¶ËÉÏλ»ú¡£
3. ´úÂ루µÚ1ÖÖµ¥´Îת»»Ä£Ê½²âÁ¿£©
/********************************************************************************
³ÌÐòÃû: ADCµ¥´Îģʽ ¶àͨµÀ²âÁ¿ÑÝʾ v0.0
¿ª·¢»·¾³: IAR + ST-LINKV2
ÈÕÆÚ: 2017-6-30
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;
while (1)
{
//-------------------------------------------
//ÒÔϲâÁ¿AIN3ͨµÀµÄÊý×ÖÁ¿
ADC1->CSR = (0X3<<0); //ͨµÀÑ¡Ôñ AIN3(PD2),ͬʱÇåÁãEOCλ
ADC1->CR2 = (1<<3); //ÓÒ¶ÔÆë,·ÇɨÃèģʽ
ADC1->CR1 &= ~(1<<1); //·ÇÁ¬Ðøģʽ
if((ADC1->CR1&0x1) == 0) //ÅжÏģʽÊÇ·ñÒѾÉϵç
{ ADC1->CR1 |= (1<<0); } //ADCÄ£¿éÉϵç
ADC1->CR1 |= (1<<0); //¿ªÊ¼²âÁ¿
while((ADC1->CSR&(1<<7))==0); //µÈ´ýµ¥´ÎģʽÍê³É
adc1 = ADC1->DRL; //¶Á³öADCÊý×ÖÁ¿
adc1 |= ADC1->DRH<<8;
//-------------------------------------------
//ÒÔϲâÁ¿AIN2ͨµÀµÄÊý×ÖÁ¿
ADC1->CSR = (0X2<<0); //ͨµÀÑ¡Ôñ AIN2(PC4),ͬʱÇåÁãEOCλ
ADC1->CR2 = (1<<3); //ÓÒ¶ÔÆë,·ÇɨÃèģʽ
ADC1->CR1 &= ~(1<<1); //·ÇÁ¬Ðøģʽ
if((ADC1->CR1&0x1) == 0) //ÅжÏģʽÊÇ·ñÒѾÉϵç
{ ADC1->CR1 |= (1<<0); } //ADCÄ£¿éÉϵç
ADC1->CR1 |= (1<<0); //¿ªÊ¼²âÁ¿
while((ADC1->CSR&(1<<7))==0); //µÈ´ýµ¥´ÎģʽÍê³É
adc2 = ADC1->DRL; //¶Á³öADCÊý×ÖÁ¿
adc2 |= 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] = ',';
SendBuf[5] = adc2/1000%10 + '0';
SendBuf[6] = adc2/100%10 + '0';
SendBuf[7] = adc2/10%10 + '0';
SendBuf[8] = adc2%10 + '0';
SendBuf[9] = '\r';
SendBuf[10] = '\n';
SendBuf[11] = '\0';
SendStr(SendBuf);
delay(655350);
} //while(1)
} //main()
½ÓÊÕ¶ËЧ¹ûͼ:
4. ×ܽá
µ¥´Îת»»ÊÇÕë¶ÔÖ»²âÁ¿Ò»¸öͨµÀ¶øÉè¼ÆµÄ£¬Èç¹û·Öʱ²âÁ¿¶à¸öͨµÀ£¬ÔòÒªÉèÖüĴæÆ÷ ADC_CSR[3:0] µÄCH[3:0]λ¸Ä±äͨµÀÑ¡Ôñ¡£
¹ØÓÚ²âÁ¿Æô¶¯£¬ÒªÏÈÈ·±£ADC_CR1[0]µÄ ADCON λΪ1Ç°ÌáÏ£¬ÔÙÉèÖÃÒ»´ÎΪ1£¬¾ÍÊÇÆô¶¯²âÁ¿;Èç¹û´ËλΪ0£¬È»ºóÉèÖÃΪ1»á±»ÈÏΪÊÇ´ÓÐÝÃßÖл½ÐÑÉϵ磬²»»áÆô¶¯²âÁ¿¡£
5.ºóÐø
Ï´ÎÑÝʾ "ADC µ¥´ÎÁ¬Ðøģʽ ",¾´Çë¹Ø×¢!