[PIC®/AVR®/dsPIC®产品] PIC16F1829 数码管显示ADC采样

[复制链接]
1993|13
 楼主| qjp1988113 发表于 2021-4-12 12:56 | 显示全部楼层 |阅读模式
最近得了块PIC16F1829的开发板,还不错。麻雀虽小五脏俱全。今天我们就用它做下数码管显示温度传感器的ADC采样值。
我们一样用MCC配置:
先配置整个时钟:

配置引脚,这里通过HC595 IO扩展芯片来控制数码管:
电路图:

配置如下:

配置定时器,用来刷新数码管显示:

配置ADC,通道8采样:

生成代码:
编写数码管显示程序:
H文件:
  1. /*
  2. * File:   seg.h
  3. * Author: Administrator
  4. *
  5. * Created on April 12, 2021, 10:20 AM
  6. */

  7. #ifndef SEG_H
  8. #define        SEG_H

  9. #ifdef        __cplusplus
  10. extern "C" {
  11. #endif

  12. #include "mcc_generated_files/mcc.h"
  13. #include "mcc_generated_files/pin_manager.h"
  14. #include "pic.h"
  15.    
  16.    
  17. #define Smg_a    0x01
  18. #define Smg_b    0x02
  19. #define Smg_c    0x04
  20. #define Smg_d    0x08
  21. #define Smg_e    0x10
  22. #define Smg_f    0x20
  23. #define Smg_g    0x40
  24. #define Smg_dp   0x80

  25. #define Bmp0Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f
  26. #define Bmp1Map          Smg_b | Smg_c
  27. #define Bmp2Map          Smg_a | Smg_b | Smg_d | Smg_e | Smg_g
  28. #define Bmp3Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_g
  29. #define Bmp4Map          Smg_b | Smg_c | Smg_f | Smg_g
  30. #define Bmp5Map          Smg_a | Smg_c | Smg_d | Smg_f | Smg_g
  31. #define Bmp6Map          Smg_a | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
  32. #define Bmp7Map          Smg_a | Smg_b | Smg_c
  33. #define Bmp8Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
  34. #define Bmp9Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_f | Smg_g
  35. #define BmpAMap          Smg_a | Smg_b | Smg_c | Smg_e | Smg_f | Smg_g
  36. #define BmpBMap          Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
  37. #define BmpCMap          Smg_a | Smg_d | Smg_e | Smg_f
  38. #define BmpDMap          Smg_b | Smg_c | Smg_d | Smg_e | Smg_g
  39. #define BmpEMap          Smg_a | Smg_d | Smg_e | Smg_f | Smg_g
  40. #define BmpFMap          Smg_a | Smg_e | Smg_f | Smg_g
  41. #define BmpPMap          Smg_a | Smg_b | Smg_e | Smg_f | Smg_g
  42. #define BmpEmtyMap       0x00

  43. #define SCLK   HC549_SCLK_LAT       //74HC595时钟
  44. #define SDAT   HC549_SDAT_LAT       //74HC595数据
  45. #define SRCK   HC549_SRCK_LAT       //74HC595锁存

  46. void SegDisplay(void);          //数码管显示
  47. void LEDisplay(unsigned int ADValue);


  48. #ifdef        __cplusplus
  49. }
  50. #endif

  51. #endif        /* SEG_H */

C文件:

  1. #include "seg.h"

  2. const unsigned char SegCode[] =     //图型表
  3. {
  4.     Bmp0Map,Bmp1Map,Bmp2Map,Bmp3Map,Bmp4Map,Bmp5Map,
  5.     Bmp6Map,Bmp7Map,Bmp8Map,Bmp9Map,BmpAMap,BmpBMap,
  6.     BmpCMap,BmpDMap,BmpEMap,BmpFMap
  7. };

  8. unsigned char SegSCS[6] = {0xFE,0xFD,0xFB,0xF7,0xEF,0xDF};                   //显示片选
  9. unsigned char SegBuf[6] = {BmpPMap,Bmp1Map,BmpCMap,Bmp1Map,Bmp6Map,BmpFMap}; //显示缓冲


  10. //-------------------------------------------------------------------------------
  11. //        数码管显示驱动
  12. //-------------------------------------------------------------------------------
  13. void SegDisplay(void)
  14. {
  15.         static unsigned char i = 0;
  16.         unsigned char j;
  17.         unsigned char temp;
  18.        
  19.         temp = SegSCS[i];       //片选输出
  20.         for(j=0;j<8;j++)        //8位
  21.         {
  22.                 if(temp&0x80) SDAT = 1;
  23.                 else          SDAT = 0;
  24.                 SCLK = 1;
  25.                 SCLK = 0;
  26.                 temp = temp<< 1;
  27.         }
  28.         temp = SegBuf[i];       //数据输出
  29.         for(j=0;j<8;j++)        //8位
  30.         {
  31.                 if(temp&0x80) SDAT = 1;
  32.                 else          SDAT = 0;
  33.                 SCLK = 1;
  34.                 SCLK = 0;
  35.                 temp = temp<< 1;
  36.         }       
  37.         SRCK = 0;               //输出锁存
  38.         SRCK = 1;        
  39.        
  40.         if(i < 5) i ++;         //扫描6位
  41.         else      i = 0;
  42. }       

  43. //-------------------------------------------------------------------------------
  44. //        数码管分段显示函数
  45. //-------------------------------------------------------------------------------
  46. void LEDisplay(unsigned int ADValue)
  47. {
  48.         SegBuf[0] = SegCode[10];              // "A"
  49.         SegBuf[1] = SegCode[13];              // "D"
  50.         SegBuf[2] = SegCode[ADValue/1000];    //千位
  51.         SegBuf[3] = SegCode[ADValue%1000/100];//百位
  52.         SegBuf[4] = SegCode[ADValue%100/10];  //十位
  53.         SegBuf[5] = SegCode[ADValue%10];      //个位
  54. }       
添加显示函数到TIMER1中断:
  1. void TMR1_CallBack(void)
  2. {
  3.     // Add your custom callback code here
  4.     SegDisplay();                      //动态扫描
  5.     if(TMR1_InterruptHandler)
  6.     {
  7.         TMR1_InterruptHandler();
  8.     }
  9. }
在main函数里面开启中断,并在WHILE里面进行ADC采样并显示:
  1. /**
  2.   Generated Main Source File

  3.   Company:
  4.     Microchip Technology Inc.

  5.   File Name:
  6.     main.c

  7.   Summary:
  8.     This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

  9.   Description:
  10.     This header file provides implementations for driver APIs for all modules selected in the GUI.
  11.     Generation Information :
  12.         Product Revision  :  PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.7
  13.         Device            :  PIC16F1829
  14.         Driver Version    :  2.00
  15. */

  16. /*
  17.     (c) 2018 Microchip Technology Inc. and its subsidiaries.
  18.    
  19.     Subject to your compliance with these terms, you may use Microchip software and any
  20.     derivatives exclusively with Microchip products. It is your responsibility to comply with third party
  21.     license terms applicable to your use of third party software (including open source software) that
  22.     may accompany Microchip software.
  23.    
  24.     THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
  25.     EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
  26.     IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
  27.     FOR A PARTICULAR PURPOSE.
  28.    
  29.     IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
  30.     INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
  31.     WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
  32.     HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
  33.     THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
  34.     CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
  35.     OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
  36.     SOFTWARE.
  37. */

  38. #include "mcc_generated_files/mcc.h"
  39. #include "seg.h"

  40. /*
  41.                          Main application
  42. */
  43. void main(void)
  44. {
  45.     unsigned int AvgADValue = 0;      // AD采样平均值
  46.     // initialize the device
  47.     SYSTEM_Initialize();

  48.     // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
  49.     // Use the following macros to:

  50.     // Enable the Global Interrupts
  51.     //INTERRUPT_GlobalInterruptEnable();

  52.     // Enable the Peripheral Interrupts
  53.     //INTERRUPT_PeripheralInterruptEnable();

  54.     // Disable the Global Interrupts
  55.     //INTERRUPT_GlobalInterruptDisable();

  56.     // Disable the Peripheral Interrupts
  57.     //INTERRUPT_PeripheralInterruptDisable();
  58.     INTERRUPT_PeripheralInterruptEnable();;                   //外设中断允许
  59.         INTERRUPT_GlobalInterruptEnable();                        //总中断允许

  60.     while (1)
  61.     {
  62.         // Add your application code
  63.         AvgADValue=ADC_GetConversion(channel_AN8);
  64.         LEDisplay(AvgADValue);  //刷新显示
  65.         __delay_ms(240);                //延时

  66.     }
  67. }
  68. /**
  69. End of File
  70. */
下载,正常显示:


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
lcczg 发表于 2021-4-13 14:29 | 显示全部楼层
很详细,感谢分享
yzq13246068880 发表于 2021-5-4 10:02 | 显示全部楼层
tfqi 发表于 2021-5-11 13:40 | 显示全部楼层
开发板大概多少钱入手啊
wiba 发表于 2021-5-11 13:44 | 显示全部楼层
采样结果稳定吗
zljiu 发表于 2021-5-11 13:45 | 显示全部楼层
外部可以有两个时钟吗
coshi 发表于 2021-5-11 13:57 | 显示全部楼层
代码风格非常好
aoyi 发表于 2021-5-11 13:58 | 显示全部楼层
这个可以外扩其他功能模块吗
 楼主| qjp1988113 发表于 2021-5-12 10:19 | 显示全部楼层
tfqi 发表于 2021-5-11 13:40
开发板大概多少钱入手啊

买的2手的啊画了80,  然后在店主那花了20买了份资料。这个买新的要180左右。
 楼主| qjp1988113 发表于 2021-5-12 10:20 | 显示全部楼层
wiba 发表于 2021-5-11 13:44
采样结果稳定吗

这个看你的采样信号怎么样了 不过10BIT的ADC不能太强求精度。
 楼主| qjp1988113 发表于 2021-5-12 10:22 | 显示全部楼层
aoyi 发表于 2021-5-11 13:58
这个可以外扩其他功能模块吗

可以 引脚都引出来了~
 楼主| qjp1988113 发表于 2021-5-12 10:24 | 显示全部楼层
zljiu 发表于 2021-5-11 13:45
外部可以有两个时钟吗

2个时钟?什么意思,没明白!你是指是不是可以同时有高速 和 低速时钟?
想多了,这个仅是8位单片机~外部只能配一个时钟。
单片小菜 发表于 2021-5-12 15:17 | 显示全部楼层
这个**不仅这个可以使用,别的也可以使用的。
nickwolfe 发表于 2021-5-12 22:50 | 显示全部楼层
我记得microchip官网资料里有一篇PIC12F1822的ADC--->DAC的文献,把10bitADC的结果输出到DAC 5bit的管脚上去。
详细程序都给出来了。

这个资料基本可以移植到PIC16F182X家族的吧。
https://ww1.microchip.com/downloads/en/DeviceDoc/12F_1822_DAC.zip


// Device: PIC12F1822
// Compiler: Microchip XC8 v1.11
// IDE: MPLAB X v1.5
// Created: January 2013
//
// This program shows how to use the ADC to capture an analog voltage from a
// potentiometer connected to pin RA1, and based on that voltage, change the
// output of the DAC on pin RA0.
//
// NOTE: If you want to do debugging with the PIC12F1822 using a debugger,
// you will need to obtain the AC244043 (or AC244044 for 'LF' device)
// "header board" which will allow you to use all the pins on the device while
// you are debugging.  
//
//**********************************************************************************
//          PIC12F1822 Pinout for this example
//          ----------
//      Vdd |1      8| GND
//      RA5 |2      7| RA0 -> DAC voltage output
//      RA4 |3      6| RA1 <- Analog input voltage from potentiometer (0-5V)
//      RA3 |4      5| RA2
//          ----------
//
//***********************************************************************************

#include <xc.h> // include standard header file
您需要登录后才可以回帖 登录 | 注册

本版积分规则

111

主题

627

帖子

2

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