[STM8] STM8S105K4使用ADC1,转换结果异常,求助

[复制链接]
 楼主| dexis 发表于 2015-8-8 19:24 | 显示全部楼层 |阅读模式
本帖最后由 dexis 于 2015-8-8 21:58 编辑

单片机:STM8S105K4T6
编程环境:STVD+COSMIC
使用引脚:PB0
实际现象:外接0v,转换结果0x0000 (好像小于1.6v都是0);外接1.6V,转换结果0x03ff ;外接3.3v,转换结果0x037f (好像大于2v都是0x037f) ;
问题代码如下:
main.c文件
  1. /*----------------------------------------------------------------------------*/
  2. /*       name    :        main.c                                                   */
  3. /*       berif      :                                                                   */
  4. /*       Include:        stm8s105k.h                                           */
  5. /*                          stm8s105k_led.h                                                                                                                                                                                 */
  6. /*----------------------------------------------------------------------------*/
  7. //=========================================//
  8. /*        include-------------------------------------------------------------------*/
  9. #include "stm8s105k.h"
  10. #include "stm8s105k_led.h"
  11. /*        define--------------------------------------------------------------------*/
  12. /*        extern--------------------------------------------------------------------*/
  13. extern uint8_t ADC1_CONVER_FLAG;
  14. /*        main()--------------------------------------------------------------------*/
  15. void main()
  16. {
  17. /*    局部变量定义------------------------------------------------------------*/
  18.         uint8_t i;
  19.         //open global interrupt
  20.         enableInterrupts();
  21.         //direction runing is ok
  22.         LED_Init;LED_ON;
  23.         CLK_CKDIVR = (CLK_CKDIVR_HSIDIV_2 | CLK_CKDIVR_CPUDIV_1);
  24. /*      ADC1初始化部分----------------------------------------------------------*/
  25.         //1.引脚模式:浮空输入
  26.         PB_DDR &= (uint8_t)~(1<<0);
  27.         PB_CR1 &= (uint8_t)~(1<<0);
  28.         PB_CR2 &= (uint8_t)~(1<<0);
  29.         //2.打开ADC时钟
  30.         CLK_PCKENR2 |= (uint8_t)(1<<3);
  31.         //3.选择ADC转换通道
  32.         ADC_CSR &= 0xF0;//通道0
  33.         ADC_CSR |= 0x00;
  34.         //4.关闭使用通道的斯密特触发器
  35.         ADC_TDRL |= (uint8_t)(1<<0);
  36.         //5.设置转换模式:带缓存的连续模式
  37.         ADC_CR1 |= (uint8_t)(1<<1);//使能连续转换
  38.         //(缓存满时,置位EOC,然后自动开始下一轮转换,所以进中断的第一件事是清CONT)
  39.         ADC_CR3 |= (uint8_t)(1<<7);//使用缓存
  40.         //6.关闭使用通道的模拟看门狗功能
  41.         ADC_AWCRL &= (uint8_t)~(1<<0);
  42.         //7.禁止模拟看门狗中断
  43.         ADC_CSR &= (uint8_t)~(1<<4);//写“0”禁止
  44.         //8.清除模拟看门狗中断标志
  45.         ADC_CSR &= (uint8_t)~(1<<6);
  46.         //9.禁止外部信号触发转换
  47.         ADC_CR2 &= (uint8_t)~(1<<6);
  48.         //10.设置F_ADC频率:F_master/8
  49.         ADC_CR1 &= 0x8F;
  50.         ADC_CR1 |= (uint8_t)0x40;
  51.         //11.使能转换结束中断
  52.         ADC_CSR |= (uint8_t)(1<<5);
  53.         //12.清除转换结束标志
  54.         ADC_CSR &= (uint8_t)~(1<<7);
  55.         //13.设置转换结果数据对齐方式:右对齐
  56.         ADC_CR2 |= (1<<3);//先读低,再读高
  57.         //14.将ADC从低功耗模式中唤醒
  58.         ADC_CR1 |= (uint8_t)(1<<0);
  59.         //15.等待ADC供电稳定(最小7us)
  60.         for(i=0;i<10;i++);//大概20us
  61.         //16.开始转换
  62.         ADC_CR1 |= (uint8_t)(1<<0);
  63.         LED_OFF;
  64.         
  65.         while(1)
  66.         {
  67.                 if(ADC1_CONVER_FLAG==1)
  68.                         LED_ON;
  69.         }
  70. }
复制代码



stm8_interrupt_vector.c文件

  1. /*----------------------------------------------------------------------------*/
  2. /* [url=home.php?mod=space&uid=139335]@name[/url]  : stm8_interrupt_vector.c                     */
  3. /* [url=home.php?mod=space&uid=247401]@brief[/url] :                                 */
  4. /* @Include: stm8s105k.h                           */
  5. /*      stm8s105k_led.h                         */
  6. /*----------------------------------------------------------------------------*/
  7. //============================================================================//
  8. /* include-------------------------------------------------------------------*/
  9. #include "stm8s105k.h"
  10. #include "stm8s105k_led.h"
  11. /* $ 别名定义----------------------------------------------------------------*/
  12. typedef void [url=home.php?mod=space&uid=1095855]@far[/url] (*interrupt_handler_t)(void);
  13. /* $ 结构体定义--------------------------------------------------------------*/
  14. struct interrupt_vector {
  15. unsigned char interrupt_instruction;
  16. interrupt_handler_t interrupt_handler;
  17. };
  18. /* $ 全局变量----------------------------------------------------------------*/
  19. uint16_t adc1_valtage[10]={'\0'};
  20. /* $ 运行标志----------------------------------------------------------------*/
  21. uint8_t ADC1_CONVER_FLAG = 0;
  22. /* $ 中断函数----------------------------------------------------------------*/
  23. [url=home.php?mod=space&uid=1095855]@far[/url] [url=home.php?mod=space&uid=422518]@interrupt[/url] void NonHandledInterrupt (void)
  24. {
  25. /* in order to detect unexpected events during development,
  26.      it is recommended to set a breakpoint on the following instruction
  27. */
  28. return;
  29. }
  30. [url=home.php?mod=space&uid=1095855]@far[/url] [url=home.php?mod=space&uid=422518]@interrupt[/url] void ISP_ADC1 (void)
  31. {
  32. uint8_t i;
  33. uint8_t *pDBx = &ADC_DB0RH;
  34. uint8_t temp_low,temp_high;
  35. uint16_t temp;
  36. ADC_CR1 &= (uint8_t)~(1<<1);//关闭连续转换
  37. if(!(ADC_CR3 & (uint8_t)(1<<6)))//数据没有溢出,则将结果读出
  38. {//数据右对齐,先读低,后读高
  39.    for(i=0;i<10;i++)
  40.    {
  41.     temp_low = *(pDBx + 1) ;
  42.     temp_high = *pDBx ;
  43.     temp = ((uint16_t)temp_high<<8)+(uint16_t)temp_low;
  44.     adc1_valtage = temp;
  45.     pDBx += 2;
  46.    }
  47.    adc1_valtage[10] = '\0';
  48.    ADC1_CONVER_FLAG = 1;
  49. }
  50. else //数据溢出,则从新转换一次
  51. {
  52.    ADC_CR3 &= (uint8_t)~(1<<6);//清溢出标志
  53.    ADC_CR1 |= (uint8_t) (1<<1);//使能连续转换
  54.    ADC_CR1 |= (uint8_t) (1<<0);//开始转换
  55.    LED_OFF;
  56. }
  57. ADC_CSR &= (uint8_t)~(1<<7);//清转换结束标志
  58. return;
  59. }
  60. extern void _stext();     /* startup routine */
  61. struct interrupt_vector const _vectab[] = {
  62. {0x82, (interrupt_handler_t)_stext}, /* reset */
  63. {0x82, NonHandledInterrupt}, /* trap  */
  64. {0x82, NonHandledInterrupt}, /* irq0  */
  65. {0x82, NonHandledInterrupt}, /* irq1  */
  66. {0x82, NonHandledInterrupt}, /* irq2  */
  67. {0x82, NonHandledInterrupt}, /* irq3  */
  68. {0x82, NonHandledInterrupt}, /* irq4  */
  69. {0x82, NonHandledInterrupt}, /* irq5  */
  70. {0x82, NonHandledInterrupt}, /* irq6  */
  71. {0x82, NonHandledInterrupt}, /* irq7  */
  72. {0x82, NonHandledInterrupt}, /* irq8  */
  73. {0x82, NonHandledInterrupt}, /* irq9  */
  74. {0x82, NonHandledInterrupt}, /* irq10 */
  75. {0x82, NonHandledInterrupt}, /* irq11 */
  76. {0x82, NonHandledInterrupt}, /* irq12 */
  77. {0x82, NonHandledInterrupt}, /* irq13 */
  78. {0x82, NonHandledInterrupt}, /* irq14 */
  79. {0x82, NonHandledInterrupt}, /* irq15 */
  80. {0x82, NonHandledInterrupt}, /* irq16 */
  81. {0x82, NonHandledInterrupt}, /* irq17 */
  82. {0x82, NonHandledInterrupt}, /* irq18 */
  83. {0x82, NonHandledInterrupt}, /* irq19 */
  84. {0x82, NonHandledInterrupt}, /* irq20 */
  85. {0x82, NonHandledInterrupt}, /* irq21 */
  86. {0x82, ISP_ADC1}, /* irq22 */
  87. {0x82, NonHandledInterrupt}, /* irq23 */
  88. {0x82, NonHandledInterrupt}, /* irq24 */
  89. {0x82, NonHandledInterrupt}, /* irq25 */
  90. {0x82, NonHandledInterrupt}, /* irq26 */
  91. {0x82, NonHandledInterrupt}, /* irq27 */
  92. {0x82, NonHandledInterrupt}, /* irq28 */
  93. {0x82, NonHandledInterrupt}, /* irq29 */
  94. };

复制代码


stm8s105k_led.h文件
  1. /*----------------------------------------------------------------------------*/
  2. /* [url=home.php?mod=space&uid=139335]@name[/url]  : stm8s105k_led.h                         */
  3. /* [url=home.php?mod=space&uid=247401]@brief[/url] :                                 */
  4. /* @Include: stm8s105k.h                           */
  5. /*----------------------------------------------------------------------------*/
  6. //============================================================================//
  7. #ifndef __STM8S105K_LED_H
  8. #define __STM8S105K_LED_H
  9. /* include-------------------------------------------------------------------*/
  10. #include "stm8s105k.h"
  11. /* define--------------------------------------------------------------------*/
  12. #define LED_Init PE_DDR |= (uint8_t) (1<<GPIO_PIN_5);\
  13.           PE_CR1 &= (uint8_t)~(1<<GPIO_PIN_5);\
  14.           PE_CR2 &= (uint8_t)~(1<<GPIO_PIN_5);\
  15.           PE_ODR |= (uint8_t) (1<<GPIO_PIN_5)
  16. #define LED_ON  PE_ODR &= (uint8_t)~(1<<GPIO_PIN_5)
  17. #define LED_OFF  PE_ODR |= (uint8_t) (1<<GPIO_PIN_5)
  18. #define LED_Filp PE_ODR ^= (uint8_t) (1<<GPIO_PIN_5)
  19. #endif
复制代码



   

实际运行结果

实际运行结果
 楼主| dexis 发表于 2015-8-8 20:04 | 显示全部楼层
咋没人回我,呜呜呜~~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

24

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部