最近得了块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
*/
下载,正常显示:
|