打印
[PIC®/AVR®/dsPIC®产品]

PIC16F1829 数码管显示ADC采样

[复制链接]
1254|13
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
最近得了块PIC16F1829的开发板,还不错。麻雀虽小五脏俱全。今天我们就用它做下数码管显示温度传感器的ADC采样值。
我们一样用MCC配置:
先配置整个时钟:

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

配置如下:

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

配置ADC,通道8采样:

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

#ifndef SEG_H
#define        SEG_H

#ifdef        __cplusplus
extern "C" {
#endif

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "pic.h"
   
   
#define Smg_a    0x01
#define Smg_b    0x02
#define Smg_c    0x04
#define Smg_d    0x08
#define Smg_e    0x10
#define Smg_f    0x20
#define Smg_g    0x40
#define Smg_dp   0x80

#define Bmp0Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f
#define Bmp1Map          Smg_b | Smg_c
#define Bmp2Map          Smg_a | Smg_b | Smg_d | Smg_e | Smg_g
#define Bmp3Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_g
#define Bmp4Map          Smg_b | Smg_c | Smg_f | Smg_g
#define Bmp5Map          Smg_a | Smg_c | Smg_d | Smg_f | Smg_g
#define Bmp6Map          Smg_a | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define Bmp7Map          Smg_a | Smg_b | Smg_c
#define Bmp8Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define Bmp9Map          Smg_a | Smg_b | Smg_c | Smg_d | Smg_f | Smg_g
#define BmpAMap          Smg_a | Smg_b | Smg_c | Smg_e | Smg_f | Smg_g
#define BmpBMap          Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define BmpCMap          Smg_a | Smg_d | Smg_e | Smg_f
#define BmpDMap          Smg_b | Smg_c | Smg_d | Smg_e | Smg_g
#define BmpEMap          Smg_a | Smg_d | Smg_e | Smg_f | Smg_g
#define BmpFMap          Smg_a | Smg_e | Smg_f | Smg_g
#define BmpPMap          Smg_a | Smg_b | Smg_e | Smg_f | Smg_g
#define BmpEmtyMap       0x00

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

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


#ifdef        __cplusplus
}
#endif

#endif        /* SEG_H */

C文件:

#include "seg.h"

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

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


//-------------------------------------------------------------------------------
//        数码管显示驱动
//-------------------------------------------------------------------------------
void SegDisplay(void)
{
        static unsigned char i = 0;
        unsigned char j;
        unsigned char temp;
       
        temp = SegSCS[i];       //片选输出
        for(j=0;j<8;j++)        //8位
        {
                if(temp&0x80) SDAT = 1;
                else          SDAT = 0;
                SCLK = 1;
                SCLK = 0;
                temp = temp<< 1;
        }
        temp = SegBuf[i];       //数据输出
        for(j=0;j<8;j++)        //8位
        {
                if(temp&0x80) SDAT = 1;
                else          SDAT = 0;
                SCLK = 1;
                SCLK = 0;
                temp = temp<< 1;
        }       
        SRCK = 0;               //输出锁存
        SRCK = 1;        
       
        if(i < 5) i ++;         //扫描6位
        else      i = 0;
}       

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

  Company:
    Microchip Technology Inc.

  File Name:
    main.c

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

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

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

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

/*
                         Main application
*/
void main(void)
{
    unsigned int AvgADValue = 0;      // AD采样平均值
    // initialize the device
    SYSTEM_Initialize();

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

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
    INTERRUPT_PeripheralInterruptEnable();;                   //外设中断允许
        INTERRUPT_GlobalInterruptEnable();                        //总中断允许

    while (1)
    {
        // Add your application code
        AvgADValue=ADC_GetConversion(channel_AN8);
        LEDisplay(AvgADValue);  //刷新显示
        __delay_ms(240);                //延时

    }
}
/**
End of File
*/
下载,正常显示:


使用特权

评论回复
沙发
lcczg| | 2021-4-13 14:29 | 只看该作者
很详细,感谢分享

使用特权

评论回复
板凳
yzq13246068880| | 2021-5-4 10:02 | 只看该作者

使用特权

评论回复
地板
tfqi| | 2021-5-11 13:40 | 只看该作者
开发板大概多少钱入手啊

使用特权

评论回复
5
wiba| | 2021-5-11 13:44 | 只看该作者
采样结果稳定吗

使用特权

评论回复
6
zljiu| | 2021-5-11 13:45 | 只看该作者
外部可以有两个时钟吗

使用特权

评论回复
7
coshi| | 2021-5-11 13:57 | 只看该作者
代码风格非常好

使用特权

评论回复
8
aoyi| | 2021-5-11 13:58 | 只看该作者
这个可以外扩其他功能模块吗

使用特权

评论回复
9
qjp1988113|  楼主 | 2021-5-12 10:19 | 只看该作者
tfqi 发表于 2021-5-11 13:40
开发板大概多少钱入手啊

买的2手的啊画了80,  然后在店主那花了20买了份资料。这个买新的要180左右。

使用特权

评论回复
10
qjp1988113|  楼主 | 2021-5-12 10:20 | 只看该作者
wiba 发表于 2021-5-11 13:44
采样结果稳定吗

这个看你的采样信号怎么样了 不过10BIT的ADC不能太强求精度。

使用特权

评论回复
11
qjp1988113|  楼主 | 2021-5-12 10:22 | 只看该作者
aoyi 发表于 2021-5-11 13:58
这个可以外扩其他功能模块吗

可以 引脚都引出来了~

使用特权

评论回复
12
qjp1988113|  楼主 | 2021-5-12 10:24 | 只看该作者
zljiu 发表于 2021-5-11 13:45
外部可以有两个时钟吗

2个时钟?什么意思,没明白!你是指是不是可以同时有高速 和 低速时钟?
想多了,这个仅是8位单片机~外部只能配一个时钟。

使用特权

评论回复
13
单片小菜| | 2021-5-12 15:17 | 只看该作者
这个**不仅这个可以使用,别的也可以使用的。

使用特权

评论回复
14
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

粉丝