新手上路
- 积分
- 43
- 金钱
- 43
- 注册时间
- 2021-7-28
- 在线时间
- 9 小时
|
5金钱
#include "ad7606.h"
#include "exti.h"
#include "lcd.h"
u8 finish_flag=0;
u16 ad7606_num; //AD第几次数据
float ad7606_data[8][1024]; //存储AD读取数据
void AD7606_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure ;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG, ENABLE); //使能PB,PF,PG时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOF, &GPIO_InitStructure); //F口 低8位d0~d7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOG, &GPIO_InitStructure); //G口 高8位d8~d15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //normal GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStructure); //PB6 BUSY 状态线 读
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PB7 CS信号线 写
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PB8 RD信号线 写
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9 CONVSTAB信号线 写
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10 OS0信号线 写
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PB11 OS1信号线 写
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PB12 OS2信号线 写
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //PB13 RST信号线 写
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void AD7606_Init(void)
{
AD7606_GPIO_Init();
OS2 = 0;
OS1 = 0;
OS0 = 0; //OSC[2:0]=001为2倍过采样
COV = 1; //转换开始输入AB
CS = 1; //片选,输出总线失能
RST = 0; //初始复位低电平
RST = 0; //延时
RST = 1; //复位
RST = 0; //初始化
EXTIX_Init();
}
void AD7606_Read(void)
{
char i=0; //通道1 数组0
CS = 0; //数据帧传输使能
for(i = 0; i < 8; i++)
{
RD = 0; //下降沿
RD = 0; //延时
RD = 0; //延时
RD = 0; //延时
RD = 1; //上升沿
//读取数据
ad7606_data[ad7606_num] = (GPIOF->IDR&0x00ff) | ((GPIOG->IDR&0x00ff)<<8);
if(ad7606_data[ad7606_num]>32767)
{
ad7606_data[ad7606_num] = ad7606_data[ad7606_num] - 65536;
}
}
CS = 1;
}
void AD7606_Start(void)
{
COV = 0;
COV = 0; //延时一条指令的时间
COV = 1;
COV = 1;
}
//外部中断4服务程序
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line6) == SET)
{
//读取数据
AD7606_Read();
}
EXTI_ClearITPendingBit(EXTI_Line6);//清除LINE4上的中断标志位
}
//定时器3中断服务函数
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
{
AD7606_Start(); //启动AD
ad7606_num++;
if(ad7606_num==1024)
{
ad7606_num=0;
TIM_Cmd(TIM3,DISABLE); //关闭定时器3
finish_flag=1;
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
}
#ifndef __AD7606_H
#define __AD7606_H
#include "sys.h"
#define BUSY PBin(6) //AD转换完成
#define CS PBout(7) //片选
#define RD PBout(8) //读取数据
#define COV PBout(9) //启动AD
#define OS2 PBout(10) //配置过采样
#define OS1 PBout(11) //配置过采样
#define OS0 PBout(12) //配置过采样
#define RST PBout(13) //复位
extern u16 ad_num; //AD读取个数计数
extern u8 finish_flag;
extern float ad7606_data[8][1024]; //存储AD读取数据
void AD7606_GPIO_Init(void);
void AD7606_Init(void);
void AD7606_Read(void);
void AD7606_Start(void);
#endif
|
|