forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

周巍
2024-04-11 91ef77c00ed797b1048c5187f416e351e646a009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
/******************************************************************************
 * Copyright (C) 2014-2015 HangZhou SiZhu Co.,LTD.
 *
 *-----------------------------------------------------------------------------
 * File:                 AD421.c
 * Description:        3.5-20mA current output code
 * Author:              Lishoujian (867693272@qq.com)
 * Date:                Jan 8, 2015
 *****************************************************************************/
 
/* ----------------------- Platform includes --------------------------------*/
#include "AD421.h"
#include "delay.h"
#include "mem_pro.h"
 
    u16     delay_times=1;
 
void AD421_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    //´ò¿ª¿ØÖƶ˿ÚʱÖÓ
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    //¿ØÖÆÒý½Å£¨Êä³ö£©³õʼ»¯
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;   
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                                  //B11 DATA ready
    GPIO_Init(GPIOD,&GPIO_InitStructure);    
 
//    AD421_EN = 1;
 
}
 
 
 
//¿ØÖÆAD421ʱÖÓÆµÂÊ
void AD421_delay(void)
{
    u8            count_j;
 
    for(count_j = 0;count_j < 4;count_j ++)
    {
        delay_us(1);
    }
}
 
/******************************************
 * func:    AD421_output
 * desc:    output current: from 3.5mA to 24mA by step of 0.000244mA
 * input:   
 *   ma - the data of current output 
 * output:  output current data equal to the given data "ma"
 * return:  none
 *****************************************/
 
void AD421_output(float ma)
     
   uint32 i,k;//
     uint32 AD421_in;//AD421_in used to store 16bit transformed binary data from "float ma"    
        
     if(ma < 3.5) ma = 3.5;
     if(ma > 24)     ma = 24;
        
        ma = ma * 1.0015 + 0.01932;  //µçÁ÷Æ«²îÐÞÕý¡£
        if(ma > 24)     ma = 24;  //ÐÞÕýÖ»ÓÐÒ²²»Äܳ¬¹ý24ma
        
     k        = (ma - 3.5) / 0.00024414;
     AD421_in = 0x00003800 + k;
    
//     AD421_latch = 1;//send 1 pulse for start
//        AD421_delay();//delay_us(1);
//     AD421_latch = 0;
//        AD421_delay();//delay_us(1);
    
   for(i = 0;i < 17;i ++)
   {   
         AD421_clk  = 0;
         AD421_data = (AD421_in >> (16 - i));
 
            AD421_delay();//delay_us(1);
         
         AD421_clk = 1; 
 
            AD421_delay();//delay_us(1);
         
    }
        
        AD421_clk = 0;
        AD421_data    = 0;    
            
        AD421_delay();//delay_us(1);//send 1 pulse for end
      AD421_latch = 1;
        AD421_delay();//delay_us(1);
      AD421_latch = 0;
            
}