粘上代碼“adc.h”文件
#ifndef _adc_H
#define _adc_H
#include "system.h"
void ADCx_Init(void);
u16 Get_ADC_Value(u8 ch,u8 times);
#endif
//adc.c 文件
#include "adc.h"
#include "SysTick.h"
/*******************************************************************************
* 函 數 名 : ADCx_Init
* 函數功能 : ADC初始化
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void ADCx_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定義結構體變量
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//設置ADC分頻因子6 72M/6=12,ADC最大時間不能超過14M
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//ADC
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模擬輸入
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//非掃描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//關閉連續轉換
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//禁止觸發檢測,使用軟件觸發
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右對齊
ADC_InitStructure.ADC_NbrOfChannel = 1;//1個轉換在規則序列中 也就是只轉換規則序列1
ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
ADC_Cmd(ADC1, ENABLE);//開啟AD轉換器
ADC_ResetCalibration(ADC1);//重置指定的ADC的校準寄存器
while(ADC_GetResetCalibrationStatus(ADC1));//獲取ADC重置校準寄存器的狀態
ADC_StartCalibration(ADC1);//開始指定ADC的校準狀態
while(ADC_GetCalibrationStatus(ADC1));//獲取指定ADC的校準程序
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能或者失能指定的ADC的軟件轉換啟動功能
}
/*******************************************************************************
* 函 數 名 : Get_ADC_Value
* 函數功能 : 獲取通道ch的轉換值,取times次,然后平均
* 輸 入 : ch:通道編號
times:獲取次數
* 輸 出 : 通道ch的times次轉換結果平均值
*******************************************************************************/
u16 Get_ADC_Value(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
//設置指定ADC的規則組通道,一個序列,采樣時間
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5); //ADC1,ADC通道,239.5個周期,提高采樣時間可以提高精確度
for(t=0;t<times;t++)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能指定的ADC1的軟件轉換啟動功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待轉換結束
temp_val+=ADC_GetConversionValue(ADC1);
delay_ms(5);
}
return temp_val/times;
}
//smg.h文件
#ifndef _SMG_H
#define _SMG_H
#include "SysTick.h"
#define SCL GPIO_Pin_10//時鐘脈沖信號——上升沿有效ST
#define RCL GPIO_Pin_11//打入信號————上升沿有效SH
#define DIO GPIO_Pin_9//串行數據入DS...............
#define SMG_PORT GPIOB
#define SCL_LOW GPIO_ResetBits(SMG_PORT,SCL)
#define SCL_HIGH GPIO_SetBits(SMG_PORT,SCL)
#define RCL_LOW GPIO_ResetBits(SMG_PORT,RCL)
#define RCL_HIGH GPIO_SetBits(SMG_PORT,RCL)
#define DIO_LOW GPIO_ResetBits(SMG_PORT,DIO)
#define DIO_HIGH GPIO_SetBits(SMG_PORT,DIO)
void Smg_Init(void);
void SMG_Write_Byte (u8 dat,u8 dat1);
void SMG_Number (u16 vo);
#endif
//smg.c文件
#include"smg.h"
#include"adc.h"
u16 smgduan[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共陽
//u16 smgduan[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共陰
u8 DisPlayData[8];
extern u16 count;
extern u16 sec,min,hour;
extern u8 i;
u16 value;
u8 vo;
float vol;
/**************************************************************************************
****
****
****
****
***************************************************************************************/
void Smg_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能,PORTB時鐘
GPIO_InitStructure.GPIO_Pin = DIO|RCL|SCL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(SMG_PORT,DIO|RCL|SCL);
}
/*************************************************************************************************
****函數名: SMG_Write_Bite (u8 dat,u8 dat1)
****功能 寫入數據
****
****
***************************************************************************************************/
void SMG_Write_Byte (u8 dat,u8 dat1)
{
u8 i;
for(i=0;i<8;i++)
{
((dat<<i)&0x80)? DIO_HIGH:DIO_LOW; // 三目運算
SCL_HIGH;
SCL_LOW;
}
for(i=0;i<8;i++)
{
((dat1<<i)&0x80)? DIO_HIGH:DIO_LOW; // 三目運算
SCL_HIGH;
SCL_LOW;
}
RCL_HIGH;
RCL_LOW;
}
void SMG_Number (u16 vo)
{
value=Get_ADC_Value(ADC_Channel_1,20);
vol=(float)value*(3.3/4096);
vo=vol*1000;
{
SMG_Write_Byte (smgduan[vo%1000],0x01);
delay_ms(10);
SMG_Write_Byte (smgduan[(vo%100)%10],0x02);
delay_ms(10);
SMG_Write_Byte (smgduan[(vo%100)/10],0x04);
delay_ms(10);
SMG_Write_Byte (smgduan[vo/1000],0x08);
delay_ms(10);
}
}
//main.c文件
#include "system.h"
#include "SysTick.h"
#include "smg.h"
#include "adc.h"
extern u16 vo;
/*******************************************************************************
* 函 數 名 : main
* 函數功能 : 主函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
int main()
{
SysTick_Init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
ADCx_Init();
while(1)
{
SMG_Number ( vo);
delay_ms(100);
}
}
編譯下載后,數碼管顯示亂碼閃爍,有知道的麻煩給出指正
|