|
|
@@ -5,7 +5,7 @@
|
|
|
#define CS1180_RDATAC 0X03
|
|
|
#define CS1180_STOPC 0x0f
|
|
|
#define CS1180_RREG 0x10
|
|
|
-#define CS1180_WREG 0x05
|
|
|
+#define CS1180_WREG 0x50
|
|
|
#define CS1180_CALSELF 0xf0
|
|
|
#define CS1180_OCALSELF 0xf1
|
|
|
#define CS1180_SLFGCAL 0xf2
|
|
|
@@ -16,42 +16,176 @@
|
|
|
#define CS1180_SLEEP 0xfd
|
|
|
#define CS1180_RESET 0xfe
|
|
|
|
|
|
+#define GAIN 0x07
|
|
|
|
|
|
-static void _spi_write_data(uint8_t data);
|
|
|
-static uint8_t _spi_read_data(uint8_t data);
|
|
|
+static void cs1180_write_one_data(uint8_t data);
|
|
|
+static uint8_t cs1180_read_one_data(uint8_t data);
|
|
|
|
|
|
+static void delay(void)
|
|
|
+{
|
|
|
+ uint32_t count = 3;
|
|
|
+ while(count--);
|
|
|
+}
|
|
|
+static void delay_50us(void)
|
|
|
+{
|
|
|
+ uint32_t count = 3*50;
|
|
|
+ while(count--);
|
|
|
+}
|
|
|
|
|
|
-void cs1180_init(void){
|
|
|
- spi1_init();
|
|
|
-
|
|
|
- _spi_read_data(0xFF);
|
|
|
+static void delay_1ms(int ms){
|
|
|
}
|
|
|
|
|
|
-static void _spi_write_data(uint8_t data){
|
|
|
- spi1_send_byte(data, NULL);
|
|
|
+static void spi_write_reg(uint8_t reg, uint8_t *data, uint8_t len){
|
|
|
+ cs1180_cs(0);
|
|
|
+ cs1180_write_one_data(CS1180_WREG|reg);
|
|
|
+ cs1180_write_one_data(len - 1);
|
|
|
+ while(len -- > 0){
|
|
|
+ cs1180_write_one_data(*data);
|
|
|
+ data++;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-static uint8_t _spi_read_data(uint8_t data){
|
|
|
- uint8_t r_data = 0xFF;
|
|
|
- spi1_send_byte(data, &r_data);
|
|
|
- return r_data;
|
|
|
+#if 0
|
|
|
+static void spi_read_reg(uint8_t reg, uint8_t *data, uint8_t len){
|
|
|
+ cs1180_cs(0);
|
|
|
+ cs1180_write_one_data(CS1180_RREG|reg);
|
|
|
+ cs1180_write_one_data(len - 1);
|
|
|
+ while(len -- > 0){
|
|
|
+ *data = cs1180_read_one_data(0xFF);
|
|
|
+ data++;
|
|
|
+ }
|
|
|
+}
|
|
|
+#endif
|
|
|
+static void cs1180_osalsys(void)
|
|
|
+{
|
|
|
+ cs1180_cs(0);
|
|
|
+ cs1180_write_one_data(CS1180_OCALSYS);
|
|
|
+ cs1180_cs(1);
|
|
|
}
|
|
|
|
|
|
-void cs1180_osalsys(void)
|
|
|
+static void cs1180_calibSelf(void)
|
|
|
{
|
|
|
- //------自校准
|
|
|
cs1180_cs(0);
|
|
|
- _spi_write_data(CS1180_OCALSYS);
|
|
|
+ cs1180_write_one_data(CS1180_CALSELF);
|
|
|
cs1180_cs(1);
|
|
|
}
|
|
|
|
|
|
-void cs1180_calibSelf(void)
|
|
|
+
|
|
|
+void cs1180_init(void){
|
|
|
+ spi1_init();
|
|
|
+ delay();
|
|
|
+ cs1180_cs(0);
|
|
|
+ cs1180_write_one_data(CS1180_RESET);
|
|
|
+ delay();
|
|
|
+ cs1180_cs(1);
|
|
|
+
|
|
|
+ cs1180_cs(0);
|
|
|
+ delay();
|
|
|
+ /* 0x07 pga =128 设置STEPUP寄存器
|
|
|
+ * 0x01 设置ad 通道
|
|
|
+ * 0x02 输出频率 15hz,参考电压2.5V 输出数据高位在前,输入缓冲器除能,采样频率 OSC/128,数据格式双极性
|
|
|
+ */
|
|
|
+ uint8_t data[] = {0xF&GAIN, 0x01, 0x00};
|
|
|
+ spi_write_reg(0x00, data, 3);
|
|
|
+ delay();
|
|
|
+ cs1180_cs(1);
|
|
|
+ delay_50us();
|
|
|
+ cs1180_calibSelf();
|
|
|
+ while (IS_CS1180_READY());
|
|
|
+ while (!IS_CS1180_READY());
|
|
|
+ delay_1ms(10);
|
|
|
+ while (IS_CS1180_READY()); //扔调一次数据
|
|
|
+
|
|
|
+ cs1180_calibSelf();
|
|
|
+
|
|
|
+//****************************************************
|
|
|
+ cs1180_osalsys();
|
|
|
+ while (IS_CS1180_READY());
|
|
|
+ while (!IS_CS1180_READY());
|
|
|
+ delay_1ms(10);
|
|
|
+ while (IS_CS1180_READY()); //扔调一次数据
|
|
|
+ cs1180_osalsys();
|
|
|
+//****************************************************
|
|
|
+ delay_1ms(500);
|
|
|
+ //**************************************************************************/
|
|
|
+ while (IS_CS1180_READY());
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//adc/2^19/128*1.25/0.001
|
|
|
+int32_t Cal_Current(uint32_t adc,uint8_t fh )
|
|
|
{
|
|
|
- //------自校准
|
|
|
+#if 0
|
|
|
+ const uint32_t jing_du = 0x0007FFFF;
|
|
|
+ uint8_t zeng_yi = 128;//(g_zeng_yi<<7);
|
|
|
+ double vrf_v = 123500000.0/RSENSE_VAL;// 1.25*1000,dianzu 1mo*100,*1000ma
|
|
|
+ int32_t cur = 0;
|
|
|
+
|
|
|
+ cur = (int32_t)((double)adc/jing_du/zeng_yi*vrf_v);
|
|
|
+
|
|
|
+ if(fh == 1)
|
|
|
+ cur = 0 - cur;
|
|
|
+
|
|
|
+ return cur;
|
|
|
+#else
|
|
|
+ return 0;
|
|
|
+#endif
|
|
|
+}
|
|
|
+/**************************************************************************************
|
|
|
+*函数原型: unsigned long ReadAdData(void)
|
|
|
+*功能: 从CS1160中读取ad转换结果,并将结果以长整型数据返回
|
|
|
+**************************************************************************************/
|
|
|
+int32_t ReadAdData(void)
|
|
|
+{
|
|
|
+ uint8_t i,fh = 0;
|
|
|
+ uint8_t Buf_rev[3];
|
|
|
+ uint32_t a;
|
|
|
+
|
|
|
+ while (IS_CS1180_READY()); //当drdy 为高时,不读取数据
|
|
|
+ a = 0;
|
|
|
+
|
|
|
cs1180_cs(0);
|
|
|
- _spi_write_data(CS1180_CALSELF);
|
|
|
+ cs1180_write_one_data(CS1180_RDATA); //0X01
|
|
|
+
|
|
|
+ //for (i=0;i<5;i++)
|
|
|
+ {
|
|
|
+ delay_50us();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for (i=0;i<3;i++)
|
|
|
+ {
|
|
|
+ Buf_rev[i] = cs1180_read_one_data(0xFF);
|
|
|
+ }
|
|
|
cs1180_cs(1);
|
|
|
+
|
|
|
+ a = Buf_rev[0];
|
|
|
+ a = a<<8;
|
|
|
+ a |= Buf_rev[1];
|
|
|
+ a = a<<8;
|
|
|
+ a |= Buf_rev[2];
|
|
|
+ a>>=4;
|
|
|
+
|
|
|
+ if(a >= 0x80000)
|
|
|
+ {
|
|
|
+ a = ~a;
|
|
|
+ a = a&0x7FFFF;
|
|
|
+ fh = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return Cal_Current(a,fh) - 5;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
+static void cs1180_write_one_data(uint8_t data){
|
|
|
+ spi1_send_byte(data, NULL);
|
|
|
+}
|
|
|
+
|
|
|
+static uint8_t cs1180_read_one_data(uint8_t data){
|
|
|
+ uint8_t r_data = 0xFF;
|
|
|
+ spi1_send_byte(data, &r_data);
|
|
|
+ return r_data;
|
|
|
+}
|
|
|
|