pic16f877a的AD实验学习

一、主函数


  1. //采集AD值

  2. #include <pic.h>
  3. #include "ad.h"
  4. #include "usart.h"

  5. __CONFIG(0xFF32);

  6. const uchar disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
  7.                         0x07,0x7f,0x6f};

  8. //延时函数
  9. void delayms(uint x)
  10. {
  11.     uint j,k;
  12.     for(j=x;j>0;j--)
  13.      for(k=110;k>0;k--);
  14. }

  15. //显示4位,低位数码管显示不正常
  16. void display(uint x)
  17. {
  18.     PORTD= disp[x/1000]|0x80;
  19.     PORTA= 0x10;
  20.     delayms(2);
  21.    
  22.        PORTD= disp[x/100%10];
  23.       PORTA= 0x08;
  24.     delayms(4);

  25.        PORTD= disp[x/10%10];
  26.        PORTA= 0x04;
  27.     delayms(2);

  28.        PORTD= disp[x%10];
  29.        PORTA= 0x02;
  30.     delayms(2);
  31. }



  32. //初始化IO
  33. void init()
  34. {
  35.     TRISA = 0x01;            //RA0 input mode
  36.     TRISC = 0xff;                //RC output mode
  37.     TRISD = 0;
  38.     PORTC = 0x01;

  39.     ADCON0 = 0x41;            //ADCS1:ADCS0=01,AN0,,A/D On
  40.     ADCON1 = 0x8E;            //right justified,ADCS2=0,1110
  41.     delayms(10);
  42. }

  43. //获取AD值
  44. uint getad()
  45. {
  46.     uint adval;
  47.     float advalf;
  48.     GO = 1;
  49.     while(GO);
  50.     adval = ADRESH;
  51.     adval = adval<<8|ADRESL;
  52.     advalf = adval/1023.0*5.0;                //转换成电压
  53.     adval = (uint)(advalf*1000);            //转换成4位整型
  54.     return (adval);
  55. }

  56. void main()
  57. {
  58.     uint i = 0;
  59.     uint temp;
  60.     init();
  61.     usart_init();
  62.     while(1)
  63.     {
  64.         temp = getad();
  65.         display(temp);
  66.         if(i == 100)
  67.         {
  68.             usart_tx(temp>>8);
  69.             usart_tx(temp);
  70.             i=0;
  71.         }
  72.         delayms(5);
  73.         i++;
  74.     }
  75. }

二、usart函数


  1. #include <pic.h>
  2. #include "usart.h"

  3. //串口初始化
  4. void usart_init(void)
  5. {
  6.     SPBRG = 0xC;                //波特率19200
  7.     TXSTA = 0x24;                //开启发送,高速模式
  8.     RCSTA = 0X90;                //使能TXRX引脚,接收到CREN直到被清零
  9.     RCIE = 0X1;                    //使能接受中断
  10.     GIE = 0X1;                    //开启总中断
  11.     PEIE = 0X1;                    //开启串口中断
  12. }

  13. void interrupt usart(void)
  14. {
  15.     if(RCIE && RCIF)
  16.     {
  17.         TXREG = RCREG;            //返回接收的数据
  18.     }
  19. }

  20. void usart_tx(unsigned char date)
  21. {
  22.     TXREG = date;
  23.     while(TRMT!=1);
  24. }


阅读(0) | 评论(0) | 转发(0) |
0

上一篇:uORBMain.cpp学习

下一篇:pic16f877a的PWM实验学习

相关热门文章
  • SHTML是什么_SSI有什么用...
  • 查看linux中某个端口(port)...
  • 卡尔曼滤波的原理说明...
  • shell中字符串操作
  • 关于java中的“错误:找不到或...
给主人留下些什么吧!~~
评论热议

你可能感兴趣的:(嵌入式)